blob: e0cfc888b93f0de4dc8269a895610db55e2eca0a [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);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800541 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
542 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700543 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800544 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700545 }
546
547 mStacks.add(addIndex, this);
548
549 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
550 if (isOnHomeDisplay()) {
551 mStackSupervisor.setFocusStack(reason, this);
552 }
553 if (task != null) {
554 insertTaskAtTop(task, null);
555 } else {
556 task = topTask();
557 }
558 if (task != null) {
559 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800560 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800561 }
562
563 final boolean isAttached() {
564 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800565 }
566
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700567 /**
568 * Returns the top activity in any existing task matching the given
569 * Intent. Returns null if no such task is found.
570 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700571 ActivityRecord findTaskLocked(ActivityRecord target) {
572 Intent intent = target.intent;
573 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700574 ComponentName cls = intent.getComponent();
575 if (info.targetActivity != null) {
576 cls = new ComponentName(info.packageName, info.targetActivity);
577 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700578 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700579 boolean isDocument = intent != null & intent.isDocument();
580 // If documentData is non-null then it must match the existing task data.
581 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800582
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700583 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800584 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
585 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700586 if (task.voiceSession != null) {
587 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700588 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700589 continue;
590 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700591 if (task.userId != userId) {
592 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700593 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700594 continue;
595 }
Craig Mautner000f0022013-02-26 15:04:29 -0800596 final ActivityRecord r = task.getTopActivity();
597 if (r == null || r.finishing || r.userId != userId ||
598 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700599 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800600 continue;
601 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700602 if (r.mActivityType != target.mActivityType) {
603 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
604 continue;
605 }
Craig Mautner000f0022013-02-26 15:04:29 -0800606
Craig Mautnerd00f4742014-03-12 14:17:26 -0700607 final Intent taskIntent = task.intent;
608 final Intent affinityIntent = task.affinityIntent;
609 final boolean taskIsDocument;
610 final Uri taskDocumentData;
611 if (taskIntent != null && taskIntent.isDocument()) {
612 taskIsDocument = true;
613 taskDocumentData = taskIntent.getData();
614 } else if (affinityIntent != null && affinityIntent.isDocument()) {
615 taskIsDocument = true;
616 taskDocumentData = affinityIntent.getData();
617 } else {
618 taskIsDocument = false;
619 taskDocumentData = null;
620 }
621
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700622 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700623 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700624 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700625 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700626 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
627 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700628 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700629 return r;
630 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700631 } else if (taskIntent != null && taskIntent.getComponent() != null &&
632 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700633 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700634 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800635 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700636 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
637 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800638 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700639 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
640 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700641 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700642 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800643 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700644 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
645 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800646 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700647 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700648 }
649
650 return null;
651 }
652
653 /**
654 * Returns the first activity (starting from the top of the stack) that
655 * is the same as the given activity. Returns null if no such activity
656 * is found.
657 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700658 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700659 ComponentName cls = intent.getComponent();
660 if (info.targetActivity != null) {
661 cls = new ComponentName(info.packageName, info.targetActivity);
662 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700663 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700664
Craig Mautner000f0022013-02-26 15:04:29 -0800665 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700666 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700667 final boolean notCurrentUserTask =
668 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700669 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700670
Craig Mautner000f0022013-02-26 15:04:29 -0800671 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
672 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700673 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700674 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700675 }
Craig Mautner000f0022013-02-26 15:04:29 -0800676 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700677 return r;
678 }
679 }
680 }
681
682 return null;
683 }
684
Amith Yamasani742a6712011-05-04 14:49:28 -0700685 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700686 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700687 */
Craig Mautner93529a42013-10-04 15:03:13 -0700688 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800689 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700690 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800691 }
692 mCurrentUser = userId;
693
694 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800695 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800696 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700697 final TaskRecord task = mTaskHistory.get(i);
698
699 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
700 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700701 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700702 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700703 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700704 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800705 mTaskHistory.remove(i);
706 mTaskHistory.add(task);
707 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800708 // Use same value for i.
709 } else {
710 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800711 }
712 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700713 if (VALIDATE_TOKENS) {
714 validateAppTokensLocked();
715 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700716 }
717
Craig Mautner2420ead2013-04-01 17:13:20 -0700718 void minimalResumeActivityLocked(ActivityRecord r) {
719 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700720 if (DEBUG_STATES) Slog.v(TAG_STATES,
721 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700722 r.stopped = false;
723 mResumedActivity = r;
724 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800725 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700726 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700727 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700728 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700729 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
730 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700731 }
732
Chong Zhang45c25ce2015-08-10 22:18:26 -0700733 private void addRecentActivityLocked(ActivityRecord r) {
734 if (r != null) {
735 mRecentTasks.addLocked(r.task);
736 r.task.touchActiveTime();
737 }
738 }
739
Narayan Kamath7829c812015-06-08 17:39:43 +0100740 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700741 if (mFullyDrawnStartTime != 0) {
742 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
743 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100744 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700745 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
746 }
747
748 private void stopFullyDrawnTraceIfNeeded() {
749 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
750 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
751 mFullyDrawnStartTime = 0;
752 }
753 }
754
Craig Mautnere79d42682013-04-01 19:01:53 -0700755 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700756 if (r.displayStartTime == 0) {
757 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
758 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100759 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700760 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700761 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700762 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100763 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700764 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700765 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700766 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800767
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700768 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700769 // Make sure that there is no activity waiting for this to launch.
770 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
771 r.displayStartTime = r.fullyDrawnStartTime = 0;
772 } else {
773 mStackSupervisor.removeTimeoutsForActivityLocked(r);
774 mStackSupervisor.scheduleIdleTimeoutLocked(r);
775 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700776 }
777
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800778 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800779 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800780 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
781 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
782 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
783 activities.get(activityNdx).setSleeping(false);
784 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800785 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800786 if (mPausingActivity != null) {
787 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
788 activityPausedLocked(mPausingActivity.appToken, true);
789 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800790 }
791
Craig Mautner0eea92c2013-05-16 13:35:39 -0700792 /**
793 * @return true if something must be done before going to sleep.
794 */
795 boolean checkReadyForSleepLocked() {
796 if (mResumedActivity != null) {
797 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700798 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
799 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
800 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700801 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700802 return true;
803 }
804 if (mPausingActivity != null) {
805 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700806 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700807 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800808 }
809
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700810 if (hasVisibleBehindActivity()) {
811 // Stop visible behind activity before going to sleep.
812 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
813 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700814 if (DEBUG_STATES) Slog.v(TAG_STATES,
815 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700816 return true;
817 }
818
Craig Mautner0eea92c2013-05-16 13:35:39 -0700819 return false;
820 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800821
Craig Mautner0eea92c2013-05-16 13:35:39 -0700822 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700823 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800824
Craig Mautner0eea92c2013-05-16 13:35:39 -0700825 // Make sure any stopped but visible activities are now sleeping.
826 // This ensures that the activity's onStop() is called.
827 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
828 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
829 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
830 final ActivityRecord r = activities.get(activityNdx);
831 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
832 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800833 }
834 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800835 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 }
Craig Mautner59c00972012-07-30 12:10:24 -0700837
Winson8b1871d2015-11-20 09:56:20 -0800838 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
839 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800840 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700841 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800842 return null;
843 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800844
Winson Chung083baf92014-07-11 10:32:42 -0700845 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700846 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800847 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700848 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700849 return null;
850 }
851
Winson Chung48a10a52014-08-27 14:36:51 -0700852 int w = mService.mThumbnailWidth;
853 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800854 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800855 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700856 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800857
858 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800859 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
860 // use within SystemUI while keeping memory usage low.
Winson8b1871d2015-11-20 09:56:20 -0800861 if (mService.mTakeFullscreenScreenshots) {
Winson8b1871d2015-11-20 09:56:20 -0800862 w = h = -1;
Winson387aac62015-11-25 11:18:56 -0800863 scale = 0.5f;
Winson8b1871d2015-11-20 09:56:20 -0800864 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800865 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800866 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800867 }
Winson Chung376543b2014-05-21 17:43:28 -0700868 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800869 return null;
870 }
871
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700872 /**
873 * Start pausing the currently resumed activity. It is an error to call this if there
874 * is already an activity being paused or there is no resumed activity.
875 *
876 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
877 * @param uiSleeping True if this is happening with the user interface going to sleep (the
878 * screen turning off).
879 * @param resuming True if this is being called as part of resuming the top activity, so
880 * we shouldn't try to instigate a resume here.
881 * @param dontWait True if the caller does not want to wait for the pause to complete. If
882 * set to true, we will immediately complete the pause here before returning.
883 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
884 * it to tell us when it is done.
885 */
886 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
887 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800888 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800889 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
890 + " state=" + mPausingActivity.state);
891 if (!mService.isSleeping()) {
892 // Avoid recursion among check for sleep and complete pause during sleeping.
893 // Because activity will be paused immediately after resume, just let pause
894 // be completed by the order of activity paused from clients.
895 completePauseLocked(false);
896 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800897 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700898 ActivityRecord prev = mResumedActivity;
899 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700900 if (!resuming) {
901 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
902 mStackSupervisor.resumeTopActivitiesLocked();
903 }
904 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700905 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800906
907 if (mActivityContainer.mParentActivity == null) {
908 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700909 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800910 }
911
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700912 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700913 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700914 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800915 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700916 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700917 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
918 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700919 prev.state = ActivityState.PAUSING;
920 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700921 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700922 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700923 if (mService.mHasRecents
924 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson8b1871d2015-11-20 09:56:20 -0800925 prev.updateThumbnailLocked(screenshotActivitiesLocked(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700926 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700927 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700928
929 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800930
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700931 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700932 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700933 try {
934 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700935 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700936 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700937 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800938 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700939 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700940 } catch (Exception e) {
941 // Ignore exception, if process died other code will cleanup.
942 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800943 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700944 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700945 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700946 }
947 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800948 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700949 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700950 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700951 }
952
953 // If we are not going to sleep, we want to ensure the device is
954 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700955 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700956 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700957 }
958
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800959 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700960 // Have the window manager pause its key dispatching until the new
961 // activity has started. If we're pausing the activity just because
962 // the screen is being turned off and the UI is sleeping, don't interrupt
963 // key dispatch; the same activity will pick it up again on wakeup.
964 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800965 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700966 } else if (DEBUG_PAUSE) {
967 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700968 }
969
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700970 if (dontWait) {
971 // If the caller said they don't want to wait for the pause, then complete
972 // the pause now.
973 completePauseLocked(false);
974 return false;
975
976 } else {
977 // Schedule a pause timeout in case the app doesn't respond.
978 // We don't give it much time because this directly impacts the
979 // responsiveness seen by the user.
980 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
981 msg.obj = prev;
982 prev.pauseTime = SystemClock.uptimeMillis();
983 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700984 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700985 return true;
986 }
987
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700988 } else {
989 // This activity failed to schedule the
990 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700991 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700992 if (!resuming) {
993 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
994 }
995 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700996 }
997 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700998
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700999 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001000 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1001 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001002
Craig Mautnerd2328952013-03-05 12:46:26 -08001003 final ActivityRecord r = isInStackLocked(token);
1004 if (r != null) {
1005 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1006 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001007 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001008 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001009 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -08001010 } else {
1011 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1012 r.userId, System.identityHashCode(r), r.shortComponentName,
1013 mPausingActivity != null
1014 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001015 if (r.state == ActivityState.PAUSING) {
1016 r.state = ActivityState.PAUSED;
1017 if (r.finishing) {
1018 if (DEBUG_PAUSE) Slog.v(TAG,
1019 "Executing finish of failed to pause activity: " + r);
1020 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1021 }
louis_chang047dfd42015-04-08 16:35:55 +08001022 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001023 }
1024 }
1025 }
1026
Craig Mautnera0026042014-04-23 11:45:37 -07001027 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1028 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001029 if (r.state != ActivityState.STOPPING) {
1030 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1031 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1032 return;
1033 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001034 if (persistentState != null) {
1035 r.persistentState = persistentState;
1036 mService.notifyTaskPersisterLocked(r.task, false);
1037 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001038 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001039 if (icicle != null) {
1040 // If icicle is null, this is happening due to a timeout, so we
1041 // haven't really saved the state.
1042 r.icicle = icicle;
1043 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001044 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001045 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001046 }
1047 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001048 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001049 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1050 r.stopped = true;
1051 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -07001052 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
1053 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001054 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001055 if (r.finishing) {
1056 r.clearOptionsLocked();
1057 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001058 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001059 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -07001060 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001061 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001062 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001063 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001064 }
1065 }
1066 }
1067
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001068 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001069 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001070 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001071
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001072 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001073 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001074 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001075 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001076 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001077 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001078 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001079 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001080 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1081 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001082 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001083 if (prev.configDestroy) {
1084 // The previous is being paused because the configuration
1085 // is changing, which means it is actually stopping...
1086 // To juggle the fact that we are also starting a new
1087 // instance right now, we need to first completely stop
1088 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001089 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001090 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001091 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001092 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001093 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001094 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001095 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1096 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001097 // If we already have a few activities waiting to stop,
1098 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001099 // them out. Or if r is the last of activity of the last task the stack
1100 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001101 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001102 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001103 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001104 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001105 }
1106 }
1107 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001108 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001109 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001110 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001111 // It is possible the activity was freezing the screen before it was paused.
1112 // In that case go ahead and remove the freeze this activity has on the screen
1113 // since it is no longer visible.
1114 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001115 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001116 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001117
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001118 if (resumeNext) {
1119 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1120 if (!mService.isSleepingOrShuttingDown()) {
1121 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1122 } else {
1123 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001124 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001125 if (top == null || (prev != null && top != prev)) {
1126 // If there are no more activities available to run,
1127 // do resume anyway to start something. Also if the top
1128 // activity on the stack is not the just paused activity,
1129 // we need to go ahead and resume it to ensure we complete
1130 // an in-flight app switch.
1131 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1132 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001133 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001134 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001135
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001136 if (prev != null) {
1137 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001138
Craig Mautner525f3d92013-05-07 14:01:50 -07001139 if (prev.app != null && prev.cpuTimeAtResume > 0
1140 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001141 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1142 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001143 if (diff > 0) {
1144 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1145 synchronized (bsi) {
1146 BatteryStatsImpl.Uid.Proc ps =
1147 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001148 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001149 if (ps != null) {
1150 ps.addForegroundTimeLocked(diff);
1151 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001152 }
1153 }
1154 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001155 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001156 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001157
1158 // Notfiy when the task stack has changed
1159 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001160 }
1161
1162 /**
1163 * Once we know that we have asked an application to put an activity in
1164 * the resumed state (either by launching it or explicitly telling it),
1165 * this function updates the rest of our state to match that fact.
1166 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001167 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001168 next.idle = false;
1169 next.results = null;
1170 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001171
Chong Zhang85ee6542015-10-02 13:36:38 -07001172 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001173 ProcessRecord app = next.task.mActivities.get(0).app;
1174 if (app != null && app != mService.mHomeProcess) {
1175 mService.mHomeProcess = app;
1176 }
1177 }
1178
Craig Mautner07566322013-09-26 16:42:55 -07001179 if (next.nowVisible) {
1180 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001181 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001182 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001183
1184 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001185 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001186
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001187 mStackSupervisor.reportResumedActivityLocked(next);
1188
1189 next.resumeKeyDispatchingLocked();
1190 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001191
1192 // Mark the point when the activity is resuming
1193 // TODO: To be more accurate, the mark should be before the onCreate,
1194 // not after the onResume. But for subsequent starts, onResume is fine.
1195 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001196 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001197 } else {
1198 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1199 }
Winson Chung376543b2014-05-21 17:43:28 -07001200
George Mount6ba042b2014-07-28 11:12:28 -07001201 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001202
1203 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1204 // When resuming an activity, require it to call requestVisibleBehind() again.
1205 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1206 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001207 }
1208
Craig Mautner2568c3a2015-03-26 14:22:34 -07001209 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001210 r.visible = visible;
1211 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001212 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001213 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001214 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001215 container.setVisible(visible);
1216 }
1217 }
1218
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001219 // Find the first visible activity above the passed activity and if it is translucent return it
1220 // otherwise return null;
1221 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1222 TaskRecord task = r.task;
1223 if (task == null) {
1224 return null;
1225 }
1226
1227 ActivityStack stack = task.stack;
1228 if (stack == null) {
1229 return null;
1230 }
1231
1232 int stackNdx = mStacks.indexOf(stack);
1233
1234 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1235 int taskNdx = tasks.indexOf(task);
1236
1237 ArrayList<ActivityRecord> activities = task.mActivities;
1238 int activityNdx = activities.indexOf(r) + 1;
1239
1240 final int numStacks = mStacks.size();
1241 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001242 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001243 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001244 final int numTasks = tasks.size();
1245 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001246 final TaskRecord currentTask = tasks.get(taskNdx);
1247 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001248 final int numActivities = activities.size();
1249 while (activityNdx < numActivities) {
1250 final ActivityRecord activity = activities.get(activityNdx);
1251 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001252 return historyStack.mFullscreen
1253 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001254 }
1255 ++activityNdx;
1256 }
1257 activityNdx = 0;
1258 ++taskNdx;
1259 }
1260 taskNdx = 0;
1261 ++stackNdx;
1262 }
1263
1264 return null;
1265 }
1266
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001267 private ActivityStack getNextVisibleStackLocked() {
1268 ArrayList<ActivityStack> stacks = mStacks;
1269 final ActivityRecord parent = mActivityContainer.mParentActivity;
1270 if (parent != null) {
1271 stacks = parent.task.stack.mStacks;
1272 }
1273 if (stacks != null) {
1274 for (int i = stacks.size() - 1; i >= 0; --i) {
1275 ActivityStack stack = stacks.get(i);
1276 if (stack != this && stack.isStackVisibleLocked()) {
1277 return stack;
1278 }
1279 }
1280 }
1281 return null;
1282 }
1283
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001284 /** Returns true if the stack contains a fullscreen task. */
1285 private boolean hasFullscreenTask() {
1286 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1287 final TaskRecord task = mTaskHistory.get(i);
1288 if (task.mFullscreen) {
1289 return true;
1290 }
1291 }
1292 return false;
1293 }
1294
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001295 private boolean hasTranslucentActivity(ActivityStack stack) {
1296 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1297 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1298 final TaskRecord task = tasks.get(taskNdx);
1299 final ArrayList<ActivityRecord> activities = task.mActivities;
1300 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1301 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001302
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001303 // Conditions for an activity to obscure the stack we're
1304 // examining:
1305 // 1. Not Finishing AND Visible AND:
1306 // 2. Either:
1307 // - Full Screen Activity OR
1308 // - On top of Home and our stack is NOT home
1309 if (!r.finishing && r.visible && (r.fullscreen ||
1310 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1311 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001312 }
1313 }
1314 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001315 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001316 }
1317
1318 /** Returns true if the stack is considered visible. */
Todd Kennedyaab56db2015-01-30 09:39:53 -08001319 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001320 if (!isAttached()) {
1321 return false;
1322 }
1323
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001324 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001325 return true;
1326 }
1327
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001328 final int stackIndex = mStacks.indexOf(this);
1329
1330 if (stackIndex == mStacks.size() - 1) {
1331 Slog.wtf(TAG,
1332 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1333 return false;
1334 }
1335
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001336 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1337 final int focusedStackId = focusedStack.mStackId;
1338
1339 if (mStackId == DOCKED_STACK_ID) {
1340 // Docked stack is always visible, except in the case where the home activity
1341 // is the top running activity in the focused home stack.
1342 if (focusedStackId != HOME_STACK_ID) {
1343 return true;
1344 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001345 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked();
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001346 return topHomeActivity == null || !topHomeActivity.isHomeActivity();
1347 }
1348
Chong Zhangb16cf342015-11-12 17:22:40 -08001349 // Find the first stack below focused stack that actually got something visible.
1350 int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1351 while (belowFocusedIndex >= 0 &&
1352 mStacks.get(belowFocusedIndex).topRunningActivityLocked() == null) {
1353 belowFocusedIndex--;
1354 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001355 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1356 && stackIndex == belowFocusedIndex) {
1357 // Stacks directly behind the docked or pinned stack are always visible.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001358 return true;
1359 }
1360
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001361 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1362 && hasTranslucentActivity(focusedStack)) {
1363 // Stacks behind the fullscreen stack with a translucent activity are always
1364 // visible so they can act as a backdrop to the translucent activity.
1365 // For example, dialog activities
1366 if (stackIndex == belowFocusedIndex) {
1367 return true;
1368 }
1369 if (belowFocusedIndex >= 0) {
1370 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001371 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1372 && stackIndex == (belowFocusedIndex - 1)) {
1373 // The stack behind the docked or pinned stack is also visible so we can have a
1374 // complete backdrop to the translucent activity when the docked stack is up.
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001375 return true;
1376 }
1377 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001378 }
1379
Wale Ogunwale3797c222015-10-27 14:21:58 -07001380 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001381 // Visibility of any static stack should have been determined by the conditions above.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001382 return false;
1383 }
1384
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001385 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001386 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001387
1388 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1389 continue;
1390 }
1391
Wale Ogunwale3797c222015-10-27 14:21:58 -07001392 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001393 // These stacks can't have any dynamic stacks visible behind them.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001394 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001395 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001396
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001397 if (!hasTranslucentActivity(stack)) {
1398 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001399 }
1400 }
1401
1402 return true;
1403 }
1404
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001405 final int rankTaskLayers(int baseLayer) {
1406 int layer = 0;
1407 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1408 final TaskRecord task = mTaskHistory.get(taskNdx);
1409 ActivityRecord r = task.topRunningActivityLocked();
1410 if (r == null || r.finishing || !r.visible) {
1411 task.mLayerRank = -1;
1412 } else {
1413 task.mLayerRank = baseLayer + layer++;
1414 }
1415 }
1416 return layer;
1417 }
1418
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001419 /**
1420 * Make sure that all activities that need to be visible (that is, they
1421 * currently can be seen by the user) actually are.
1422 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001423 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1424 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001425 ActivityRecord top = topRunningActivityLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07001426 if (top == null) {
1427 return;
1428 }
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001429 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001430 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001431 checkTranslucentActivityWaiting(top);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001432
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001433 // If the top activity is not fullscreen, then we need to
1434 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001435 boolean aboveTop = true;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001436 final boolean stackInvisible = !isStackVisibleLocked();
1437 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001438 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001439
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001440 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001441 final TaskRecord task = mTaskHistory.get(taskNdx);
1442 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001443
Craig Mautnerd44711d2013-02-23 11:24:36 -08001444 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1445 final ActivityRecord r = activities.get(activityNdx);
1446 if (r.finishing) {
1447 continue;
1448 }
1449 if (aboveTop && r != top) {
1450 continue;
1451 }
1452 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001453 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1454 // but must be drawn initially for the animation as though they were visible.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001455 if (!behindFullscreenActivity || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001456 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1457 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001458 + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001459 // First: if this is not the current activity being started, make
1460 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001461 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001462 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001463 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001464
1465 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001466 if (makeVisibleAndRestartIfNeeded(starting, configChanges, top,
1467 noStackActivityResumed, r)) {
1468 if (activityNdx >= activities.size()) {
1469 // Record may be removed if its process needs to restart.
1470 activityNdx = activities.size() - 1;
1471 } else {
1472 noStackActivityResumed = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001473 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001474 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001475 } else if (r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001476 if (alreadyVisible(r)) {
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001477 noStackActivityResumed = false;
1478 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001479 } else {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001480 becomeVisible(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001481 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001482 // Aggregate current change flags.
1483 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001484 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1485 behindFullscreenActivity, task, r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001486 } else {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001487 becomeInvisible(stackInvisible, behindFullscreenActivity, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001488 }
1489 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001490 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1491 // The visibility of tasks and the activities they contain in freeform stack are
1492 // determined individually unlike other stacks where the visibility or fullscreen
1493 // status of an activity in a previous task affects other.
1494 behindFullscreenActivity = stackInvisible;
1495 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001496 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001497
1498 if (mTranslucentActivityWaiting != null &&
1499 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1500 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1501 notifyActivityDrawnLocked(null);
1502 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001503 }
Craig Mautner58547802013-03-05 08:23:53 -08001504
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001505 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1506 if (mTranslucentActivityWaiting != top) {
1507 mUndrawnActivitiesBelowTopTranslucent.clear();
1508 if (mTranslucentActivityWaiting != null) {
1509 // Call the callback with a timeout indication.
1510 notifyActivityDrawnLocked(null);
1511 mTranslucentActivityWaiting = null;
1512 }
1513 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1514 }
1515 }
1516
1517 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
1518 ActivityRecord top, boolean noStackActivityResumed, ActivityRecord r) {
1519 // We need to make sure the app is running if it's the top, or it is just made visible from
1520 // invisible. If the app is already visible, it must have died while it was visible. In this
1521 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1522 // thrashing.
1523 if (r == top || !r.visible) {
1524 // This activity needs to be visible, but isn't even running...
1525 // get it started and resume if no other stack in this stack is resumed.
1526 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1527 if (r != starting) {
1528 r.startFreezingScreenLocked(r.app, configChanges);
1529 }
1530 if (!r.visible || r.mLaunchTaskBehind) {
1531 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1532 setVisible(r, true);
1533 }
1534 if (r != starting) {
1535 mStackSupervisor.startSpecificActivityLocked(r, noStackActivityResumed, false);
1536 return true;
1537 }
1538 }
1539 return false;
1540 }
1541
1542 private void becomeInvisible(boolean stackInvisible, boolean behindFullscreenActivity,
1543 ActivityRecord r) {
1544 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r + " finishing="
1545 + r.finishing + " state=" + r.state + " stackInvisible=" + stackInvisible
1546 + " behindFullscreenActivity=" + behindFullscreenActivity);
1547 if (!r.visible) {
1548 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1549 return;
1550 }
1551 // Now for any activities that aren't visible to the user, make sure they no longer are
1552 // keeping the screen frozen.
1553 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
1554 try {
1555 setVisible(r, false);
1556 switch (r.state) {
1557 case STOPPING:
1558 case STOPPED:
1559 if (r.app != null && r.app.thread != null) {
1560 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1561 "Scheduling invisibility: " + r);
1562 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1563 }
1564 break;
1565
1566 case INITIALIZING:
1567 case RESUMED:
1568 case PAUSING:
1569 case PAUSED:
1570 // This case created for transitioning activities from
1571 // translucent to opaque {@link Activity#convertToOpaque}.
1572 if (getVisibleBehindActivity() == r) {
1573 releaseBackgroundResources(r);
1574 } else {
1575 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1576 mStackSupervisor.mStoppingActivities.add(r);
1577 }
1578 mStackSupervisor.scheduleIdleLocked();
1579 }
1580 break;
1581
1582 default:
1583 break;
1584 }
1585 } catch (Exception e) {
1586 // Just skip on any failure; we'll make it visible when it next restarts.
1587 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1588 }
1589 }
1590
1591 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1592 TaskRecord task, ActivityRecord r) {
1593 if (r.fullscreen) {
1594 // At this point, nothing else needs to be shown in this task.
1595 behindFullscreenActivity = true;
1596 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
1597 + " stackInvisible=" + stackInvisible
1598 + " behindFullscreenActivity=" + behindFullscreenActivity);
1599 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
1600 behindFullscreenActivity = true;
1601 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1602 + " stackInvisible=" + stackInvisible
1603 + " behindFullscreenActivity=" + behindFullscreenActivity);
1604 }
1605 return behindFullscreenActivity;
1606 }
1607
1608 private void becomeVisible(ActivityRecord starting, ActivityRecord r) {
1609 // This activity is not currently visible, but is running. Tell it to become visible.
1610 r.visible = true;
1611 if (r.state != ActivityState.RESUMED && r != starting) {
1612 // If this activity is paused, tell it to now show its window.
1613 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1614 "Making visible and scheduling visibility: " + r);
1615 try {
1616 if (mTranslucentActivityWaiting != null) {
1617 r.updateOptionsLocked(r.returningOptions);
1618 mUndrawnActivitiesBelowTopTranslucent.add(r);
1619 }
1620 setVisible(r, true);
1621 r.sleeping = false;
1622 r.app.pendingUiClean = true;
1623 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1624 r.stopFreezingScreenLocked(false);
1625 } catch (Exception e) {
1626 // Just skip on any failure; we'll make it
1627 // visible when it next restarts.
1628 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
1629 }
1630 }
1631 }
1632
1633 private boolean alreadyVisible(ActivityRecord r) {
1634 // If this activity is already visible, then there is nothing else to do here.
1635 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1636 r.stopFreezingScreenLocked(false);
1637 try {
1638 if (r.returningOptions != null) {
1639 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1640 }
1641 } catch(RemoteException e) {
1642 }
1643 return r.state == ActivityState.RESUMED;
1644 }
1645
Todd Kennedyaab56db2015-01-30 09:39:53 -08001646 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001647 mTranslucentActivityWaiting = r;
1648 mUndrawnActivitiesBelowTopTranslucent.clear();
1649 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1650 }
1651
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001652 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1653 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1654 final TaskRecord task = mTaskHistory.get(taskNdx);
1655 final ArrayList<ActivityRecord> activities = task.mActivities;
1656 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1657 final ActivityRecord r = activities.get(activityNdx);
1658 if ( r.appTimeTracker != except) {
1659 r.appTimeTracker = null;
1660 }
1661 }
1662 }
1663 }
1664
Craig Mautner5eda9b32013-07-02 11:58:16 -07001665 /**
1666 * Called as activities below the top translucent activity are redrawn. When the last one is
1667 * redrawn notify the top activity by calling
1668 * {@link Activity#onTranslucentConversionComplete}.
1669 *
1670 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1671 * occurred and the activity will be notified immediately.
1672 */
1673 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001674 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001675 if ((r == null)
1676 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1677 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1678 // The last undrawn activity below the top has just been drawn. If there is an
1679 // opaque activity at the top, notify it that it can become translucent safely now.
1680 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1681 mTranslucentActivityWaiting = null;
1682 mUndrawnActivitiesBelowTopTranslucent.clear();
1683 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1684
Craig Mautner71dd1b62014-02-18 15:48:52 -08001685 if (waitingActivity != null) {
1686 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1687 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1688 try {
1689 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1690 waitingActivity.appToken, r != null);
1691 } catch (RemoteException e) {
1692 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001693 }
1694 }
1695 }
1696 }
1697
Craig Mautnera61bc652013-10-28 15:43:18 -07001698 /** If any activities below the top running one are in the INITIALIZING state and they have a
1699 * starting window displayed then remove that starting window. It is possible that the activity
1700 * in this state will never resumed in which case that starting window will be orphaned. */
1701 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001702 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001703 boolean aboveTop = true;
1704 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1705 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1706 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1707 final ActivityRecord r = activities.get(activityNdx);
1708 if (aboveTop) {
1709 if (r == topActivity) {
1710 aboveTop = false;
1711 }
1712 continue;
1713 }
1714
1715 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001716 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1717 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001718 r.mStartingWindowShown = false;
1719 mWindowManager.removeAppStartingWindow(r.appToken);
1720 }
1721 }
1722 }
1723 }
1724
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001725 /**
1726 * Ensure that the top activity in the stack is resumed.
1727 *
1728 * @param prev The previously resumed activity, for when in the process
1729 * of pausing; can be null to call from elsewhere.
1730 *
1731 * @return Returns true if something is being resumed, or false if
1732 * nothing happened.
1733 */
1734 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001735 return resumeTopActivityLocked(prev, null);
1736 }
1737
Chong Zhang280d3322015-11-03 17:27:26 -08001738 final boolean resumeTopActivityLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001739 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001740 // Don't even start recursing.
1741 return false;
1742 }
1743
1744 boolean result = false;
1745 try {
1746 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001747 mStackSupervisor.inResumeTopActivity = true;
1748 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1749 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001750 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001751 }
Craig Mautner544efa72014-09-04 16:41:20 -07001752 result = resumeTopActivityInnerLocked(prev, options);
1753 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001754 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001755 }
1756 return result;
1757 }
1758
Chong Zhang280d3322015-11-03 17:27:26 -08001759 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001760 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001761
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001762 if (!mService.mBooting && !mService.mBooted) {
1763 // Not ready yet!
1764 return false;
1765 }
1766
Craig Mautnerdf88d732014-01-27 09:21:32 -08001767 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001768 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001769 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001770 // Do not resume this stack if its parent is not resumed.
1771 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1772 return false;
1773 }
1774
Craig Mautnera61bc652013-10-28 15:43:18 -07001775 cancelInitializingActivities();
1776
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001777 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001778 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001779
1780 // Remember how we'll process this pause/resume situation, and ensure
1781 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001782 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1783 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001784
Craig Mautner84984fa2014-06-19 11:19:20 -07001785 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001786 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001787 // There are no more activities!
1788 final String reason = "noMoreActivities";
1789 if (!mFullscreen) {
1790 // Try to move focus to the next visible stack with a running activity if this
1791 // stack is not covering the entire screen.
1792 final ActivityStack stack = getNextVisibleStackLocked();
1793 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1794 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1795 }
1796 }
1797 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001798 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001799 if (DEBUG_STATES) Slog.d(TAG_STATES,
1800 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001801 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001802 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001803 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1804 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1805 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001806 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001807 }
1808
1809 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001810
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001811 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001812 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1813 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001814 // Make sure we have executed any pending transitions, since there
1815 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001816 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001817 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001818 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001819 if (DEBUG_STATES) Slog.d(TAG_STATES,
1820 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001821 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001822 return false;
1823 }
1824
Craig Mautner525f3d92013-05-07 14:01:50 -07001825 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001826 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001827 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001828 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001829 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001830 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001831 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001832 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001833 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001834 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001835 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001836 } else if (!isOnHomeDisplay()) {
1837 return false;
1838 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001839 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001840 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001841 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1842 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1843 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001844 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001845 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001846 }
1847
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001848 // If we are sleeping, and there is no resumed activity, and the top
1849 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001850 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001851 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001852 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001853 // Make sure we have executed any pending transitions, since there
1854 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001855 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001856 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001857 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001858 if (DEBUG_STATES) Slog.d(TAG_STATES,
1859 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001860 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001861 return false;
1862 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001863
1864 // Make sure that the user who owns this activity is started. If not,
1865 // we will just leave it as is because someone should be bringing
1866 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001867 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001868 Slog.w(TAG, "Skipping resume of top activity " + next
1869 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001870 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001871 return false;
1872 }
1873
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001874 // The activity may be waiting for stop, but that is no longer
1875 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001876 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001877 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001878 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001879 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001880
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001881 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001882
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001883 // If we are currently pausing an activity, then don't do anything
1884 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001885 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001886 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001887 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001888 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001889 return false;
1890 }
1891
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001892 // Okay we are now going to start a switch, to 'next'. We may first
1893 // have to pause the current activity, but this is an important point
1894 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001895 // XXX "App Redirected" dialog is getting too many false positives
1896 // at this point, so turn off for now.
1897 if (false) {
1898 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1899 long now = SystemClock.uptimeMillis();
1900 final boolean inTime = mLastStartedActivity.startTime != 0
1901 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1902 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1903 final int nextUid = next.info.applicationInfo.uid;
1904 if (inTime && lastUid != nextUid
1905 && lastUid != next.launchedFromUid
1906 && mService.checkPermission(
1907 android.Manifest.permission.STOP_APP_SWITCHES,
1908 -1, next.launchedFromUid)
1909 != PackageManager.PERMISSION_GRANTED) {
1910 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1911 } else {
1912 next.startTime = now;
1913 mLastStartedActivity = next;
1914 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001915 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001916 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001917 mLastStartedActivity = next;
1918 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001919 }
Craig Mautner58547802013-03-05 08:23:53 -08001920
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001921 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1922
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001923 // We need to start pausing the current activity so the top one
1924 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001925 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1926 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001927 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001928 if (DEBUG_STATES) Slog.d(TAG_STATES,
1929 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001930 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001931 }
1932 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001933 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001934 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001935 // At this point we want to put the upcoming activity's process
1936 // at the top of the LRU list, since we know we will be needing it
1937 // very soon and it would be a waste to let it get killed if it
1938 // happens to be sitting towards the end.
1939 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001940 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001941 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001942 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001943 return true;
1944 }
1945
Christopher Tated3f175c2012-06-14 14:16:54 -07001946 // If the most recent activity was noHistory but was only stopped rather
1947 // than stopped+finished because the device went to sleep, we need to make
1948 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001949 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001950 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001951 if (DEBUG_STATES) Slog.d(TAG_STATES,
1952 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001953 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001954 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001955 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001956 }
1957
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001958 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001959 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1960 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001961 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001962 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1963 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001964 } else {
1965 // The next activity is already visible, so hide the previous
1966 // activity's windows right now so we can show the new one ASAP.
1967 // We only do this if the previous is finishing, which should mean
1968 // it is on top of the one being resumed so hiding it quickly
1969 // is good. Otherwise, we want to do the normal route of allowing
1970 // the resumed activity to be shown so we can decide if the
1971 // previous should actually be hidden depending on whether the
1972 // new one is found to be full-screen or not.
1973 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001974 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001975 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1976 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001977 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001978 + ", nowVisible=" + next.nowVisible);
1979 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001980 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001981 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001982 + ", waitingVisible="
1983 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1984 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001985 }
1986 }
1987 }
1988
Dianne Hackborne7f97212011-02-24 14:40:20 -08001989 // Launching this app's activity, make sure the app is no longer
1990 // considered stopped.
1991 try {
1992 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001993 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001994 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001995 } catch (IllegalArgumentException e) {
1996 Slog.w(TAG, "Failed trying to unstop package "
1997 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001998 }
1999
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002000 // We are starting up the next activity, so tell the window manager
2001 // that the previous one will be hidden soon. This way it can know
2002 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002003 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002004 if (prev != null) {
2005 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002006 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002007 "Prepare close transition: prev=" + prev);
2008 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002009 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002010 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002011 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002012 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002013 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2014 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002015 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002016 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002017 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002018 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2019 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002020 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002021 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002022 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002023 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002024 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002025 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002026 : next.mLaunchTaskBehind
2027 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2028 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002029 }
2030 }
Craig Mautner967212c2013-04-13 21:10:58 -07002031 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002032 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002033 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002034 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002035 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002036 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002037 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002038 }
2039 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002040
2041 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002042 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002043 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2044 if (opts != null) {
2045 resumeAnimOptions = opts.toBundle();
2046 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002047 next.applyOptionsLocked();
2048 } else {
2049 next.clearOptionsLocked();
2050 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002051
Craig Mautnercf910b02013-04-23 11:23:27 -07002052 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002053 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002054 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002055
2056 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002057 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002058
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002059 // schedule launch ticks to collect information about slow apps.
2060 next.startLaunchTickingLocked();
2061
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002062 ActivityRecord lastResumedActivity =
2063 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002064 ActivityState lastState = next.state;
2065
2066 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002067
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002068 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002069 next.state = ActivityState.RESUMED;
2070 mResumedActivity = next;
2071 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002072 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002073 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002074 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002075 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002076
2077 // Have the window manager re-evaluate the orientation of
2078 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002079 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002080 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002081 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002082 mService.mConfiguration,
2083 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2084 if (config != null) {
2085 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002086 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002087 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002088 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002089
Craig Mautner525f3d92013-05-07 14:01:50 -07002090 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002091 // The configuration update wasn't able to keep the existing
2092 // instance of the activity, and instead started a new one.
2093 // We should be all done, but let's just make sure our activity
2094 // is still at the top and schedule another run if something
2095 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002096 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002097 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002098 "Activity config changed during resume: " + next
2099 + ", new next: " + nextNext);
2100 if (nextNext != next) {
2101 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002102 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002103 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002104 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002105 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002106 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002107 return true;
2108 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002109 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002110 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002111 }
Craig Mautner58547802013-03-05 08:23:53 -08002112
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002113 try {
2114 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002115 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002116 if (a != null) {
2117 final int N = a.size();
2118 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002119 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2120 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002121 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002122 }
2123 }
2124
2125 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002126 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002127 }
2128
Craig Mautner299f9602015-01-26 09:47:33 -08002129 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2130 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002131
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002132 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002133 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002134 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002135 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002136 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002137 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002138 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002139
Craig Mautner0eea92c2013-05-16 13:35:39 -07002140 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002141
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002142 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002143 } catch (Exception e) {
2144 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002145 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002146 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002147 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002148 if (lastStack != null) {
2149 lastStack.mResumedActivity = lastResumedActivity;
2150 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002151 Slog.i(TAG, "Restarting because process died: " + next);
2152 if (!next.hasBeenLaunched) {
2153 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002154 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2155 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002156 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002157 next.appToken, next.packageName, next.theme,
2158 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002159 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2160 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002161 }
George Mount2c92c972014-03-20 09:38:23 -07002162 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002163 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002164 return true;
2165 }
2166
2167 // From this point on, if something goes wrong there is no way
2168 // to recover the activity.
2169 try {
2170 next.visible = true;
2171 completeResumeLocked(next);
2172 } catch (Exception e) {
2173 // If any exception gets thrown, toss away this
2174 // activity and try the next one.
2175 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002176 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002177 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002178 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002179 return true;
2180 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002181 next.stopped = false;
2182
2183 } else {
2184 // Whoops, need to restart this activity!
2185 if (!next.hasBeenLaunched) {
2186 next.hasBeenLaunched = true;
2187 } else {
2188 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002189 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002190 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002191 mService.compatibilityInfoForPackageLocked(
2192 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002193 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002194 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002195 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002196 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002197 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002198 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002199 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002200 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002201 }
2202
Craig Mautnercf910b02013-04-23 11:23:27 -07002203 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002204 return true;
2205 }
2206
riddle_hsuc215a4f2014-12-27 12:10:45 +08002207 private TaskRecord getNextTask(TaskRecord targetTask) {
2208 final int index = mTaskHistory.indexOf(targetTask);
2209 if (index >= 0) {
2210 final int numTasks = mTaskHistory.size();
2211 for (int i = index + 1; i < numTasks; ++i) {
2212 TaskRecord task = mTaskHistory.get(i);
2213 if (task.userId == targetTask.userId) {
2214 return task;
2215 }
2216 }
2217 }
2218 return null;
2219 }
2220
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002221 private void insertTaskAtPosition(TaskRecord task, int position) {
2222 if (position >= mTaskHistory.size()) {
2223 insertTaskAtTop(task, null);
2224 return;
2225 }
2226 // Calculate maximum possible position for this task.
2227 int maxPosition = mTaskHistory.size();
2228 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002229 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002230 // Put non-current user tasks below current user tasks.
2231 while (maxPosition > 0) {
2232 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2233 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002234 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002235 break;
2236 }
2237 maxPosition--;
2238 }
2239 }
2240 position = Math.min(position, maxPosition);
2241 mTaskHistory.remove(task);
2242 mTaskHistory.add(position, task);
2243 updateTaskMovement(task, true);
2244 }
2245
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002246 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002247 // If the moving task is over home stack, transfer its return type to next task
2248 if (task.isOverHomeStack()) {
2249 final TaskRecord nextTask = getNextTask(task);
2250 if (nextTask != null) {
2251 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2252 }
2253 }
2254
Craig Mautner9c85c202013-10-04 20:11:26 -07002255 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002256 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002257 if (isOnHomeDisplay()) {
2258 ActivityStack lastStack = mStackSupervisor.getLastStack();
2259 final boolean fromHome = lastStack.isHomeStack();
2260 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002261 task.setTaskToReturnTo(fromHome
2262 ? lastStack.topTask() == null
2263 ? HOME_ACTIVITY_TYPE
2264 : lastStack.topTask().taskType
2265 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002266 }
2267 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002268 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002269 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002270
Craig Mautnerac6f8432013-07-17 13:24:59 -07002271 mTaskHistory.remove(task);
2272 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002273 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002274 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002275 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002276 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002277 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002278 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002279 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002280 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002281 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002282 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002283 break;
2284 }
2285 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002286 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002287 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002288 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002289 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002290 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002291
Craig Mautner8849a5e2013-04-02 16:41:03 -07002292 final void startActivityLocked(ActivityRecord r, boolean newTask,
Chong Zhang280d3322015-11-03 17:27:26 -08002293 boolean doResume, boolean keepCurTransition, ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002294 TaskRecord rTask = r.task;
2295 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002296 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2297 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002298 // Last activity in task had been removed or ActivityManagerService is reusing task.
2299 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002300 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002301 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002302 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002303 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002304 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002305 if (!newTask) {
2306 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002307 boolean startIt = true;
2308 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2309 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002310 if (task.getTopActivity() == null) {
2311 // All activities in task are finishing.
2312 continue;
2313 }
Craig Mautner70a86932013-02-28 22:37:44 -08002314 if (task == r.task) {
2315 // Here it is! Now, if this is not yet visible to the
2316 // user, then just add it without starting; it will
2317 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002318 if (!startIt) {
2319 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2320 + task, new RuntimeException("here").fillInStackTrace());
2321 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002322 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002323 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002324 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002325 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002326 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002327 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002328 return;
2329 }
2330 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002331 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002332 startIt = false;
2333 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002334 }
2335 }
2336
2337 // Place a new activity at top of stack, so it is next to interact
2338 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002339
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002340 // If we are not placing the new activity frontmost, we do not want
2341 // to deliver the onUserLeaving callback to the actual frontmost
2342 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002343 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002344 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002345 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002346 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002347 }
Craig Mautner70a86932013-02-28 22:37:44 -08002348
2349 task = r.task;
2350
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002351 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002352 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002353 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002354 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002355 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002356
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002357 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002358 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002359 // We want to show the starting preview window if we are
2360 // switching to a new task, or the next activity's process is
2361 // not currently running.
2362 boolean showStartingIcon = newTask;
2363 ProcessRecord proc = r.app;
2364 if (proc == null) {
2365 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2366 }
2367 if (proc == null || proc.thread == null) {
2368 showStartingIcon = true;
2369 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002370 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002371 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002372 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002373 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002374 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002375 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002376 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002377 ? r.mLaunchTaskBehind
2378 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2379 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002380 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002381 mNoAnimActivities.remove(r);
2382 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002383 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002384 boolean doShow = true;
2385 if (newTask) {
2386 // Even though this activity is starting fresh, we still need
2387 // to reset it to make sure we apply affinities to move any
2388 // existing activities from other tasks in to it.
2389 // If the caller has requested that the target task be
2390 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002391 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002392 resetTaskIfNeededLocked(r, r);
2393 doShow = topRunningNonDelayedActivityLocked(null) == r;
2394 }
Chong Zhang280d3322015-11-03 17:27:26 -08002395 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002396 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2397 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002398 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002399 if (r.mLaunchTaskBehind) {
2400 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2401 // tell WindowManager that r is visible even though it is at the back of the stack.
2402 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002403 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002404 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002405 // Figure out if we are transitioning from another activity that is
2406 // "has the same starting icon" as the next one. This allows the
2407 // window manager to keep the previous window it had previously
2408 // created, if it still had one.
2409 ActivityRecord prev = mResumedActivity;
2410 if (prev != null) {
2411 // We don't want to reuse the previous starting preview if:
2412 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002413 if (prev.task != r.task) {
2414 prev = null;
2415 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002416 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002417 else if (prev.nowVisible) {
2418 prev = null;
2419 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002420 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002421 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002422 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002423 mService.compatibilityInfoForPackageLocked(
2424 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002425 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002426 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002427 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002428 }
2429 } else {
2430 // If this is the first activity, don't do any fancy animations,
2431 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002432 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002433 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002434 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002435 }
2436 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002437 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002438 }
2439
2440 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002441 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002442 } else {
2443 addRecentActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002444 }
2445 }
2446
Dianne Hackbornbe707852011-11-11 14:32:10 -08002447 final void validateAppTokensLocked() {
2448 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002449 mValidateAppTokens.ensureCapacity(numActivities());
2450 final int numTasks = mTaskHistory.size();
2451 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2452 TaskRecord task = mTaskHistory.get(taskNdx);
2453 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002454 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002455 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002456 }
Craig Mautner000f0022013-02-26 15:04:29 -08002457 TaskGroup group = new TaskGroup();
2458 group.taskId = task.taskId;
2459 mValidateAppTokens.add(group);
2460 final int numActivities = activities.size();
2461 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2462 final ActivityRecord r = activities.get(activityNdx);
2463 group.tokens.add(r.appToken);
2464 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002465 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002466 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002467 }
2468
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002469 /**
2470 * Perform a reset of the given task, if needed as part of launching it.
2471 * Returns the new HistoryRecord at the top of the task.
2472 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002473 /**
2474 * Helper method for #resetTaskIfNeededLocked.
2475 * We are inside of the task being reset... we'll either finish this activity, push it out
2476 * for another task, or leave it as-is.
2477 * @param task The task containing the Activity (taskTop) that might be reset.
2478 * @param forceReset
2479 * @return An ActivityOptions that needs to be processed.
2480 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002481 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002482 ActivityOptions topOptions = null;
2483
2484 int replyChainEnd = -1;
2485 boolean canMoveOptions = true;
2486
2487 // We only do this for activities that are not the root of the task (since if we finish
2488 // the root, we may no longer have the task!).
2489 final ArrayList<ActivityRecord> activities = task.mActivities;
2490 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002491 final int rootActivityNdx = task.findEffectiveRootIndex();
2492 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002493 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002494 if (target.frontOfTask)
2495 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002496
2497 final int flags = target.info.flags;
2498 final boolean finishOnTaskLaunch =
2499 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2500 final boolean allowTaskReparenting =
2501 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2502 final boolean clearWhenTaskReset =
2503 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2504
2505 if (!finishOnTaskLaunch
2506 && !clearWhenTaskReset
2507 && target.resultTo != null) {
2508 // If this activity is sending a reply to a previous
2509 // activity, we can't do anything with it now until
2510 // we reach the start of the reply chain.
2511 // XXX note that we are assuming the result is always
2512 // to the previous activity, which is almost always
2513 // the case but we really shouldn't count on.
2514 if (replyChainEnd < 0) {
2515 replyChainEnd = i;
2516 }
2517 } else if (!finishOnTaskLaunch
2518 && !clearWhenTaskReset
2519 && allowTaskReparenting
2520 && target.taskAffinity != null
2521 && !target.taskAffinity.equals(task.affinity)) {
2522 // If this activity has an affinity for another
2523 // task, then we need to move it out of here. We will
2524 // move it as far out of the way as possible, to the
2525 // bottom of the activity stack. This also keeps it
2526 // correctly ordered with any activities we previously
2527 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002528 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002529 final ActivityRecord bottom =
2530 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002531 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002532 if (bottom != null && target.taskAffinity != null
2533 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002534 // If the activity currently at the bottom has the
2535 // same task affinity as the one we are moving,
2536 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002537 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002538 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002539 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002540 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002541 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002542 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002543 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002544 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002545 + " out to new task " + target.task);
2546 }
2547
Wale Ogunwale706ed792015-08-02 10:29:44 -07002548 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002549
Craig Mautner525f3d92013-05-07 14:01:50 -07002550 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002551 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2552 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002553 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002554 if (p.finishing) {
2555 continue;
2556 }
2557
Craig Mautnere3a74d52013-02-22 14:14:58 -08002558 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002559 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002560 topOptions = p.takeOptionsLocked();
2561 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002562 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002563 }
2564 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002565 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2566 "Removing activity " + p + " from task=" + task + " adding to task="
2567 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002568 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2569 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002570 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002571 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002572
Wale Ogunwale706ed792015-08-02 10:29:44 -07002573 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002574 }
2575
Wale Ogunwale706ed792015-08-02 10:29:44 -07002576 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002577 if (VALIDATE_TOKENS) {
2578 validateAppTokensLocked();
2579 }
2580
2581 replyChainEnd = -1;
2582 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2583 // If the activity should just be removed -- either
2584 // because it asks for it, or the task should be
2585 // cleared -- then finish it and anything that is
2586 // part of its reply chain.
2587 int end;
2588 if (clearWhenTaskReset) {
2589 // In this case, we want to finish this activity
2590 // and everything above it, so be sneaky and pretend
2591 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002592 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002593 } else if (replyChainEnd < 0) {
2594 end = i;
2595 } else {
2596 end = replyChainEnd;
2597 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002598 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002599 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002600 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002601 if (p.finishing) {
2602 continue;
2603 }
2604 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002605 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002606 topOptions = p.takeOptionsLocked();
2607 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002608 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002609 }
2610 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002611 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002612 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002613 if (finishActivityLocked(
2614 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002615 end--;
2616 srcPos--;
2617 }
2618 }
2619 replyChainEnd = -1;
2620 } else {
2621 // If we were in the middle of a chain, well the
2622 // activity that started it all doesn't want anything
2623 // special, so leave it all as-is.
2624 replyChainEnd = -1;
2625 }
2626 }
2627
2628 return topOptions;
2629 }
2630
2631 /**
2632 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2633 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2634 * @param affinityTask The task we are looking for an affinity to.
2635 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2636 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2637 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2638 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002639 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002640 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002641 int replyChainEnd = -1;
2642 final int taskId = task.taskId;
2643 final String taskAffinity = task.affinity;
2644
2645 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2646 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002647 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2648
2649 // Do not operate on or below the effective root Activity.
2650 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002651 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002652 if (target.frontOfTask)
2653 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002654
2655 final int flags = target.info.flags;
2656 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2657 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2658
2659 if (target.resultTo != null) {
2660 // If this activity is sending a reply to a previous
2661 // activity, we can't do anything with it now until
2662 // we reach the start of the reply chain.
2663 // XXX note that we are assuming the result is always
2664 // to the previous activity, which is almost always
2665 // the case but we really shouldn't count on.
2666 if (replyChainEnd < 0) {
2667 replyChainEnd = i;
2668 }
2669 } else if (topTaskIsHigher
2670 && allowTaskReparenting
2671 && taskAffinity != null
2672 && taskAffinity.equals(target.taskAffinity)) {
2673 // This activity has an affinity for our task. Either remove it if we are
2674 // clearing or move it over to our task. Note that
2675 // we currently punt on the case where we are resetting a
2676 // task that is not at the top but who has activities above
2677 // with an affinity to it... this is really not a normal
2678 // case, and we will need to later pull that task to the front
2679 // and usually at that point we will do the reset and pick
2680 // up those remaining activities. (This only happens if
2681 // someone starts an activity in a new task from an activity
2682 // in a task that is not currently on top.)
2683 if (forceReset || finishOnTaskLaunch) {
2684 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002685 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2686 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002687 for (int srcPos = start; srcPos >= i; --srcPos) {
2688 final ActivityRecord p = activities.get(srcPos);
2689 if (p.finishing) {
2690 continue;
2691 }
Todd Kennedy539db512014-12-15 09:57:55 -08002692 finishActivityLocked(
2693 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002694 }
2695 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002696 if (taskInsertionPoint < 0) {
2697 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002698
Craig Mautner77878772013-03-04 19:46:24 -08002699 }
Craig Mautner77878772013-03-04 19:46:24 -08002700
2701 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002702 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2703 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2704 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002705 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002706 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002707 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002708 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002709
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002710 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2711 "Removing and adding activity " + p + " to stack at " + task
2712 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002713 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2714 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002715 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002716 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002717 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002718 if (VALIDATE_TOKENS) {
2719 validateAppTokensLocked();
2720 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002721
2722 // Now we've moved it in to place... but what if this is
2723 // a singleTop activity and we have put it on top of another
2724 // instance of the same activity? Then we drop the instance
2725 // below so it remains singleTop.
2726 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2727 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002728 int targetNdx = taskActivities.indexOf(target);
2729 if (targetNdx > 0) {
2730 ActivityRecord p = taskActivities.get(targetNdx - 1);
2731 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002732 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2733 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002734 }
2735 }
2736 }
2737 }
2738
2739 replyChainEnd = -1;
2740 }
2741 }
Craig Mautner77878772013-03-04 19:46:24 -08002742 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002743 }
2744
Craig Mautner8849a5e2013-04-02 16:41:03 -07002745 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002746 ActivityRecord newActivity) {
2747 boolean forceReset =
2748 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2749 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2750 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2751 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2752 forceReset = true;
2753 }
2754 }
2755
2756 final TaskRecord task = taskTop.task;
2757
2758 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2759 * for remaining tasks. Used for later tasks to reparent to task. */
2760 boolean taskFound = false;
2761
2762 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2763 ActivityOptions topOptions = null;
2764
Craig Mautner77878772013-03-04 19:46:24 -08002765 // Preserve the location for reparenting in the new task.
2766 int reparentInsertionPoint = -1;
2767
Craig Mautnere3a74d52013-02-22 14:14:58 -08002768 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2769 final TaskRecord targetTask = mTaskHistory.get(i);
2770
2771 if (targetTask == task) {
2772 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2773 taskFound = true;
2774 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002775 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2776 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002777 }
2778 }
2779
Craig Mautner70a86932013-02-28 22:37:44 -08002780 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002781 if (taskNdx >= 0) {
2782 do {
2783 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2784 } while (taskTop == null && taskNdx >= 0);
2785 }
Craig Mautner70a86932013-02-28 22:37:44 -08002786
Craig Mautnere3a74d52013-02-22 14:14:58 -08002787 if (topOptions != null) {
2788 // If we got some ActivityOptions from an activity on top that
2789 // was removed from the task, propagate them to the new real top.
2790 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002791 taskTop.updateOptionsLocked(topOptions);
2792 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002793 topOptions.abort();
2794 }
2795 }
2796
2797 return taskTop;
2798 }
2799
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002800 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2801 String resultWho, int requestCode, int resultCode, Intent data) {
2802
2803 if (callingUid > 0) {
2804 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002805 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002806 }
2807
2808 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2809 + " : who=" + resultWho + " req=" + requestCode
2810 + " res=" + resultCode + " data=" + data);
2811 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2812 try {
2813 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2814 list.add(new ResultInfo(resultWho, requestCode,
2815 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002816 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002817 return;
2818 } catch (Exception e) {
2819 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2820 }
2821 }
2822
2823 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2824 }
2825
Craig Mautner299f9602015-01-26 09:47:33 -08002826 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002827 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2828 return;
2829 }
2830
2831 final ActivityRecord next = topRunningActivityLocked();
2832 final String myReason = reason + " adjustFocus";
2833 if (next != r) {
2834 if (next != null && StackId.keepFocusInStackIfPossible(mStackId)) {
2835 // For freeform, docked, and pinned stacks we always keep the focus within the
2836 // stack as long as there is a running activity in the stack that we can adjust
2837 // focus to.
2838 mService.setFocusedActivityLocked(next, myReason);
2839 return;
2840 } else {
2841 final TaskRecord task = r.task;
2842 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2843 // For non-fullscreen stack, we want to move the focus to the next visible
2844 // stack to prevent the home screen from moving to the top and obscuring
2845 // other visible stacks.
2846 if (!mFullscreen
2847 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2848 return;
2849 }
2850 // Move the home stack to the top if this stack is fullscreen or there is no
2851 // other visible stack.
2852 if (mStackSupervisor.moveHomeStackTaskToTop(
2853 task.getTaskToReturnTo(), myReason)) {
2854 // Activity focus was already adjusted. Nothing else to do...
2855 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002856 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002857 }
2858 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08002859 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002860
Wale Ogunwaled045c822015-12-02 09:14:28 -08002861 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2862 if (top != null) {
2863 mService.setFocusedActivityLocked(top, myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002864 }
2865 }
2866
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002867 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2868 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2869 final String myReason = reason + " adjustFocusToNextVisibleStack";
2870 if (stack == null) {
2871 return false;
2872 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002873 final ActivityRecord top = stack.topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002874 if (top == null) {
2875 return false;
2876 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002877 mService.setFocusedActivityLocked(top, myReason);
2878 return true;
2879 }
2880
Craig Mautnerf3333272013-04-22 10:55:53 -07002881 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002882 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002883 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2884 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2885 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002886 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002887 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002888 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2889 "stop-no-history", false)) {
2890 // Activity was finished, no need to continue trying to schedule stop.
2891 adjustFocusedActivityLocked(r, "stopActivityFinished");
2892 r.resumeKeyDispatchingLocked();
2893 return;
2894 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002895 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002896 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002897 + " on stop because we're just sleeping");
2898 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002899 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002900 }
2901
2902 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002903 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002904 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002905 try {
2906 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002907 if (DEBUG_STATES) Slog.v(TAG_STATES,
2908 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002909 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002910 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2911 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002912 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002913 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002914 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002915 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002916 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002917 r.setSleeping(true);
2918 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002919 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002920 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002921 } catch (Exception e) {
2922 // Maybe just ignore exceptions here... if the process
2923 // has crashed, our death notification will clean things
2924 // up.
2925 Slog.w(TAG, "Exception thrown during pause", e);
2926 // Just in case, assume it to be stopped.
2927 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002928 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002929 r.state = ActivityState.STOPPED;
2930 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002931 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002932 }
2933 }
2934 }
2935 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002936
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002937 /**
2938 * @return Returns true if the activity is being finished, false if for
2939 * some reason it is being left as-is.
2940 */
2941 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002942 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002943 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002944 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2945 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002946 + ", result=" + resultCode + ", data=" + resultData
2947 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002948 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002949 return false;
2950 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002951
Craig Mautnerd44711d2013-02-23 11:24:36 -08002952 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002953 return true;
2954 }
2955
Craig Mautnerd2328952013-03-05 12:46:26 -08002956 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002957 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2958 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2959 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2960 ActivityRecord r = activities.get(activityNdx);
2961 if (r.resultTo == self && r.requestCode == requestCode) {
2962 if ((r.resultWho == null && resultWho == null) ||
2963 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2964 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2965 false);
2966 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002967 }
2968 }
2969 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002970 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002971 }
2972
Todd Kennedy539db512014-12-15 09:57:55 -08002973 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002974 ActivityRecord r = topRunningActivityLocked();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002975 if (r != null && r.app == app) {
2976 // If the top running activity is from this crashing
2977 // process, then terminate it to avoid getting in a loop.
2978 Slog.w(TAG, " Force finishing activity "
2979 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002980 int taskNdx = mTaskHistory.indexOf(r.task);
2981 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002982 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002983 // Also terminate any activities below it that aren't yet
2984 // stopped, to avoid a situation where one will get
2985 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002986 --activityNdx;
2987 if (activityNdx < 0) {
2988 do {
2989 --taskNdx;
2990 if (taskNdx < 0) {
2991 break;
2992 }
2993 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2994 } while (activityNdx < 0);
2995 }
2996 if (activityNdx >= 0) {
2997 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002998 if (r.state == ActivityState.RESUMED
2999 || r.state == ActivityState.PAUSING
3000 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07003001 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003002 Slog.w(TAG, " Force finishing activity "
3003 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08003004 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003005 }
3006 }
3007 }
3008 }
3009 }
3010
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003011 final void finishVoiceTask(IVoiceInteractionSession session) {
3012 IBinder sessionBinder = session.asBinder();
3013 boolean didOne = false;
3014 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3015 TaskRecord tr = mTaskHistory.get(taskNdx);
3016 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3017 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3018 ActivityRecord r = tr.mActivities.get(activityNdx);
3019 if (!r.finishing) {
3020 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3021 false);
3022 didOne = true;
3023 }
3024 }
3025 }
3026 }
3027 if (didOne) {
3028 mService.updateOomAdjLocked();
3029 }
3030 }
3031
Craig Mautnerd2328952013-03-05 12:46:26 -08003032 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003033 ArrayList<ActivityRecord> activities = r.task.mActivities;
3034 for (int index = activities.indexOf(r); index >= 0; --index) {
3035 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003036 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003037 break;
3038 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003039 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003040 }
3041 return true;
3042 }
3043
Dianne Hackborn5c607432012-02-28 14:44:19 -08003044 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3045 // send the result
3046 ActivityRecord resultTo = r.resultTo;
3047 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003048 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003049 + " who=" + r.resultWho + " req=" + r.requestCode
3050 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003051 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003052 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003053 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003054 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003055 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003056 if (r.info.applicationInfo.uid > 0) {
3057 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3058 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003059 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003060 }
3061 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3062 resultData);
3063 r.resultTo = null;
3064 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003065 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003066
3067 // Make sure this HistoryRecord is not holding on to other resources,
3068 // because clients have remote IPC references to this object so we
3069 // can't assume that will go away and want to avoid circular IPC refs.
3070 r.results = null;
3071 r.pendingResults = null;
3072 r.newIntents = null;
3073 r.icicle = null;
3074 }
3075
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003076 /**
3077 * @return Returns true if this activity has been removed from the history
3078 * list, or false if it is still in the list and will be removed later.
3079 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003080 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3081 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003082 if (r.finishing) {
3083 Slog.w(TAG, "Duplicate finish request for " + r);
3084 return false;
3085 }
3086
Wale Ogunwale7d701172015-03-11 15:36:30 -07003087 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003088 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003089 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003090 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003091 task.taskId, r.shortComponentName, reason);
3092 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003093 final int index = activities.indexOf(r);
3094 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003095 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003096 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003097 // If the caller asked that this activity (and all above it)
3098 // be cleared when the task is reset, don't lose that information,
3099 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003100 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003101 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003102 }
3103 }
3104
3105 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003106
Craig Mautner299f9602015-01-26 09:47:33 -08003107 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003108
Dianne Hackborn5c607432012-02-28 14:44:19 -08003109 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003110
Craig Mautnerde4ef022013-04-07 19:01:33 -07003111 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003112 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003113 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003114 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003115 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003116 ? AppTransition.TRANSIT_TASK_CLOSE
3117 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003118
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003119 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003120 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003121
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003122 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003123 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3124 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3125 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003126 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003127 }
3128
Craig Mautneraea74a52014-03-08 14:23:10 -08003129 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003130 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003131 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003132 } else if (r.state != ActivityState.PAUSING) {
3133 // If the activity is PAUSING, we will complete the finish once
3134 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003135 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003136 if (r.visible) {
3137 mWindowManager.setAppVisibility(r.appToken, false);
3138 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003139 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003140 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003141 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003142 }
3143
3144 return false;
3145 }
3146
Craig Mautnerf3333272013-04-22 10:55:53 -07003147 static final int FINISH_IMMEDIATELY = 0;
3148 static final int FINISH_AFTER_PAUSE = 1;
3149 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003150
Craig Mautnerf3333272013-04-22 10:55:53 -07003151 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003152 // First things first: if this activity is currently visible,
3153 // and the resumed activity is not yet visible, then hold off on
3154 // finishing until the resumed one becomes visible.
3155 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003156 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
3157 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07003158 if (mStackSupervisor.mStoppingActivities.size() > 3
3159 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003160 // If we already have a few activities waiting to stop,
3161 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07003162 // them out. Or if r is the last of activity of the last task the stack
3163 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07003164 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003165 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003166 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003167 }
3168 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003169 if (DEBUG_STATES) Slog.v(TAG_STATES,
3170 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003171 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003172 if (oomAdj) {
3173 mService.updateOomAdjLocked();
3174 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003175 return r;
3176 }
3177
3178 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003179 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003180 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003181 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003182 if (mResumedActivity == r) {
3183 mResumedActivity = null;
3184 }
3185 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003186 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003187 r.state = ActivityState.FINISHING;
3188
3189 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003190 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003191 || prevState == ActivityState.STOPPED
3192 || prevState == ActivityState.INITIALIZING) {
3193 // If this activity is already stopped, we can just finish
3194 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003195 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003196 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003197 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003198 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003199 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003200 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003201 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3202 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003203 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003204 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003205
3206 // Need to go through the full pause cycle to get this
3207 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003208 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003209 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003210 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003211 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003212 return r;
3213 }
3214
Craig Mautneree36c772014-07-16 14:56:05 -07003215 void finishAllActivitiesLocked(boolean immediately) {
3216 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003217 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3218 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3219 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3220 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003221 noActivitiesInStack = false;
3222 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003223 continue;
3224 }
Craig Mautneree36c772014-07-16 14:56:05 -07003225 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003226 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3227 }
3228 }
Craig Mautneree36c772014-07-16 14:56:05 -07003229 if (noActivitiesInStack) {
3230 mActivityContainer.onTaskListEmptyLocked();
3231 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003232 }
3233
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003234 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3235 // Basic case: for simple app-centric recents, we need to recreate
3236 // the task if the affinity has changed.
3237 if (srec == null || srec.task.affinity == null ||
3238 !srec.task.affinity.equals(destAffinity)) {
3239 return true;
3240 }
3241 // Document-centric case: an app may be split in to multiple documents;
3242 // they need to re-create their task if this current activity is the root
3243 // of a document, unless simply finishing it will return them to the the
3244 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003245 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3246 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003247 // Okay, this activity is at the root of its task. What to do, what to do...
3248 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3249 // Finishing won't return to an application, so we need to recreate.
3250 return true;
3251 }
3252 // We now need to get the task below it to determine what to do.
3253 int taskIdx = mTaskHistory.indexOf(srec.task);
3254 if (taskIdx <= 0) {
3255 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3256 return false;
3257 }
3258 if (taskIdx == 0) {
3259 // At the bottom of the stack, nothing to go back to.
3260 return true;
3261 }
3262 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3263 if (!srec.task.affinity.equals(prevTask.affinity)) {
3264 // These are different apps, so need to recreate.
3265 return true;
3266 }
3267 }
3268 return false;
3269 }
3270
Wale Ogunwale7d701172015-03-11 15:36:30 -07003271 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003272 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003273 final TaskRecord task = srec.task;
3274 final ArrayList<ActivityRecord> activities = task.mActivities;
3275 final int start = activities.indexOf(srec);
3276 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003277 return false;
3278 }
3279 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003280 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003281 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003282 final ComponentName dest = destIntent.getComponent();
3283 if (start > 0 && dest != null) {
3284 for (int i = finishTo; i >= 0; i--) {
3285 ActivityRecord r = activities.get(i);
3286 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003287 r.info.name.equals(dest.getClassName())) {
3288 finishTo = i;
3289 parent = r;
3290 foundParentInTask = true;
3291 break;
3292 }
3293 }
3294 }
3295
3296 IActivityController controller = mService.mController;
3297 if (controller != null) {
3298 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3299 if (next != null) {
3300 // ask watcher if this is allowed
3301 boolean resumeOK = true;
3302 try {
3303 resumeOK = controller.activityResuming(next.packageName);
3304 } catch (RemoteException e) {
3305 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003306 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003307 }
3308
3309 if (!resumeOK) {
3310 return false;
3311 }
3312 }
3313 }
3314 final long origId = Binder.clearCallingIdentity();
3315 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003316 ActivityRecord r = activities.get(i);
3317 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003318 // Only return the supplied result for the first activity finished
3319 resultCode = Activity.RESULT_CANCELED;
3320 resultData = null;
3321 }
3322
3323 if (parent != null && foundParentInTask) {
3324 final int parentLaunchMode = parent.info.launchMode;
3325 final int destIntentFlags = destIntent.getFlags();
3326 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3327 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3328 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3329 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003330 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3331 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003332 } else {
3333 try {
3334 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3335 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003336 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003337 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003338 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003339 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003340 foundParentInTask = res == ActivityManager.START_SUCCESS;
3341 } catch (RemoteException e) {
3342 foundParentInTask = false;
3343 }
3344 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003345 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003346 }
3347 }
3348 Binder.restoreCallingIdentity(origId);
3349 return foundParentInTask;
3350 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003351 /**
3352 * Perform the common clean-up of an activity record. This is called both
3353 * as part of destroyActivityLocked() (when destroying the client-side
3354 * representation) and cleaning things up as a result of its hosting
3355 * processing going away, in which case there is no remaining client-side
3356 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003357 *
3358 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003359 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003360 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3361 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003362 if (mResumedActivity == r) {
3363 mResumedActivity = null;
3364 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003365 if (mPausingActivity == r) {
3366 mPausingActivity = null;
3367 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003368 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003369
3370 r.configDestroy = false;
3371 r.frozenBeforeDestroy = false;
3372
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003373 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003374 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003375 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003376 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003377 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003378 }
3379
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003380 // Make sure this record is no longer in the pending finishes list.
3381 // This could happen, for example, if we are trimming activities
3382 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003383 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003384 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003385
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003386 // Remove any pending results.
3387 if (r.finishing && r.pendingResults != null) {
3388 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3389 PendingIntentRecord rec = apr.get();
3390 if (rec != null) {
3391 mService.cancelIntentSenderLocked(rec, false);
3392 }
3393 }
3394 r.pendingResults = null;
3395 }
3396
3397 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003398 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003399 }
3400
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003401 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003402 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003403 if (getVisibleBehindActivity() == r) {
3404 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003405 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003406 }
3407
3408 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003409 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003410 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003411 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003412 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003413 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003414 }
3415
Craig Mautner299f9602015-01-26 09:47:33 -08003416 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003417 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003418 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003419 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003420 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3421 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3422
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003423 r.takeFromHistory();
3424 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003425 if (DEBUG_STATES) Slog.v(TAG_STATES,
3426 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003427 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003428 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003429 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003430 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003431 if (VALIDATE_TOKENS) {
3432 validateAppTokensLocked();
3433 }
Craig Mautner312ba862014-02-10 17:55:01 -08003434 final TaskRecord task = r.task;
3435 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003436 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003437 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003438 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003439 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003440 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003441 }
Craig Mautner299f9602015-01-26 09:47:33 -08003442 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003443 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003444 cleanUpActivityServicesLocked(r);
3445 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003446 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003447
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003448 /**
3449 * Perform clean-up of service connections in an activity record.
3450 */
3451 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3452 // Throw away any services that have been bound by this activity.
3453 if (r.connections != null) {
3454 Iterator<ConnectionRecord> it = r.connections.iterator();
3455 while (it.hasNext()) {
3456 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003457 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003458 }
3459 r.connections = null;
3460 }
3461 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003462
Craig Mautneree2e45a2014-06-27 12:10:03 -07003463 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003464 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003465 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003466 mHandler.sendMessage(msg);
3467 }
3468
Craig Mautneree2e45a2014-06-27 12:10:03 -07003469 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003470 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003471 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003472 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3473 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3474 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3475 final ActivityRecord r = activities.get(activityNdx);
3476 if (r.finishing) {
3477 continue;
3478 }
3479 if (r.fullscreen) {
3480 lastIsOpaque = true;
3481 }
3482 if (owner != null && r.app != owner) {
3483 continue;
3484 }
3485 if (!lastIsOpaque) {
3486 continue;
3487 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003488 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003489 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003490 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003491 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003492 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003493 activityRemoved = true;
3494 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003495 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003496 }
3497 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003498 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003499 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003500 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003501 }
3502
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003503 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3504 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003505 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3506 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003507 + " pausing=" + mPausingActivity + " for reason " + reason);
3508 return destroyActivityLocked(r, true, reason);
3509 }
3510 return false;
3511 }
3512
3513 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3514 String reason) {
3515 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003516 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003517 int maxTasks = tasks.size() / 4;
3518 if (maxTasks < 1) {
3519 maxTasks = 1;
3520 }
3521 int numReleased = 0;
3522 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3523 final TaskRecord task = mTaskHistory.get(taskNdx);
3524 if (!tasks.contains(task)) {
3525 continue;
3526 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003527 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003528 int curNum = 0;
3529 final ArrayList<ActivityRecord> activities = task.mActivities;
3530 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3531 final ActivityRecord activity = activities.get(actNdx);
3532 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003533 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003534 + " in state " + activity.state + " resumed=" + mResumedActivity
3535 + " pausing=" + mPausingActivity + " for reason " + reason);
3536 destroyActivityLocked(activity, true, reason);
3537 if (activities.get(actNdx) != activity) {
3538 // Was removed from list, back up so we don't miss the next one.
3539 actNdx--;
3540 }
3541 curNum++;
3542 }
3543 }
3544 if (curNum > 0) {
3545 numReleased += curNum;
3546 maxTasks--;
3547 if (mTaskHistory.get(taskNdx) != task) {
3548 // The entire task got removed, back up so we don't miss the next one.
3549 taskNdx--;
3550 }
3551 }
3552 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003553 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3554 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003555 return numReleased;
3556 }
3557
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003558 /**
3559 * Destroy the current CLIENT SIDE instance of an activity. This may be
3560 * called both when actually finishing an activity, or when performing
3561 * a configuration switch where we destroy the current client-side object
3562 * but then create a new client-side object for this same HistoryRecord.
3563 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003564 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003565 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3566 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003567 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003568 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003569 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003570 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003571
3572 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003573
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003574 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003575
3576 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003577
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003578 if (hadApp) {
3579 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003580 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003581 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3582 mService.mHeavyWeightProcess = null;
3583 mService.mHandler.sendEmptyMessage(
3584 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3585 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003586 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003587 // Update any services we are bound to that might care about whether
3588 // their client may have activities.
3589 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003590 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003591 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003592 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003593 }
3594 }
3595
3596 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003597
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003598 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003599 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003600 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003601 r.configChangeFlags);
3602 } catch (Exception e) {
3603 // We can just ignore exceptions here... if the process
3604 // has crashed, our death notification will clean things
3605 // up.
3606 //Slog.w(TAG, "Exception thrown during finish", e);
3607 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003608 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003609 removedFromHistory = true;
3610 skipDestroy = true;
3611 }
3612 }
3613
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003614 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003615
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003616 // If the activity is finishing, we need to wait on removing it
3617 // from the list to give it a chance to do its cleanup. During
3618 // that time it may make calls back with its token so we need to
3619 // be able to find it on the list and so we don't want to remove
3620 // it from the list yet. Otherwise, we can just immediately put
3621 // it in the destroyed state since we are not removing it from the
3622 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003623 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003624 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003625 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003626 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003627 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003628 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3629 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003630 if (DEBUG_STATES) Slog.v(TAG_STATES,
3631 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003632 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003633 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003634 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003635 }
3636 } else {
3637 // remove this record from the history.
3638 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003639 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003640 removedFromHistory = true;
3641 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003642 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003643 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003644 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003645 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003646 }
3647 }
3648
3649 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003650
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003651 if (!mLRUActivities.remove(r) && hadApp) {
3652 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3653 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003654
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003655 return removedFromHistory;
3656 }
3657
Craig Mautner299f9602015-01-26 09:47:33 -08003658 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003659 final long origId = Binder.clearCallingIdentity();
3660 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003661 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003662 if (r != null) {
3663 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003664 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003665 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003666
Wale Ogunwale60454db2015-01-23 16:05:07 -08003667 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003668 if (r.state == ActivityState.DESTROYING) {
3669 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003670 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003671 }
3672 }
Craig Mautner05d29032013-05-03 13:40:13 -07003673 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003674 } finally {
3675 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003676 }
3677 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003678
Todd Kennedyaab56db2015-01-30 09:39:53 -08003679 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003680 if (hasVisibleBehindActivity() &&
3681 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003682 if (r == topRunningActivityLocked()) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003683 // Don't release the top activity if it has requested to run behind the next
3684 // activity.
3685 return;
3686 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003687 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003688 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003689 " thread=" + r.app.thread);
3690 if (r != null && r.app != null && r.app.thread != null) {
3691 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003692 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003693 } catch (RemoteException e) {
3694 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003695 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003696 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003697 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003698 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003699 }
3700 }
3701 }
3702
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003703 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003704 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3705 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003706 if (r != null) {
3707 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003708 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003709 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003710 }
3711 mStackSupervisor.resumeTopActivitiesLocked();
3712 }
3713
Jose Lima4b6c6692014-08-12 17:41:12 -07003714 boolean hasVisibleBehindActivity() {
3715 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003716 }
3717
Jose Lima4b6c6692014-08-12 17:41:12 -07003718 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003719 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003720 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003721 }
3722 }
3723
Jose Lima4b6c6692014-08-12 17:41:12 -07003724 ActivityRecord getVisibleBehindActivity() {
3725 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003726 }
3727
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003728 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3729 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003730 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003731 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3732 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003733 while (i > 0) {
3734 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003735 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003736 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003737 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003738 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003739 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003740 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003741 }
3742 }
3743 }
3744
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003745 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3746 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003747 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3748 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003749 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3750 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003751 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003752 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003753 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3754 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003755
3756 boolean hasVisibleActivities = false;
3757
3758 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003759 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003760 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3761 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003762 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3763 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3764 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3765 final ActivityRecord r = activities.get(activityNdx);
3766 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003767 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3768 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003769 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003770 if (r.visible) {
3771 hasVisibleActivities = true;
3772 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003773 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003774 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3775 // Don't currently have state for the activity, or
3776 // it is finishing -- always remove it.
3777 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003778 } else if (!r.visible && r.launchCount > 2 &&
3779 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003780 // We have launched this activity too many times since it was
3781 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003782 // (Note if the activity is visible, we don't remove the record.
3783 // We leave the dead window on the screen but the process will
3784 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003785 remove = true;
3786 } else {
3787 // The process may be gone, but the activity lives on!
3788 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003789 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003790 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003791 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3792 "Removing activity " + r + " from stack at " + i
3793 + ": haveState=" + r.haveState
3794 + " stateNotNeeded=" + r.stateNotNeeded
3795 + " finishing=" + r.finishing
3796 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003797 if (!r.finishing) {
3798 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3799 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3800 r.userId, System.identityHashCode(r),
3801 r.task.taskId, r.shortComponentName,
3802 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003803 if (r.state == ActivityState.RESUMED) {
3804 mService.updateUsageStats(r, false);
3805 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003806 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003807 } else {
3808 // We have the current state for this activity, so
3809 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003810 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3811 if (DEBUG_APP) Slog.v(TAG_APP,
3812 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003813 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003814 // Set nowVisible to previous visible state. If the app was visible while
3815 // it died, we leave the dead window on screen so it's basically visible.
3816 // This is needed when user later tap on the dead window, we need to stop
3817 // other apps when user transfers focus to the restarted activity.
3818 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003819 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003820 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003821 "App died, clearing saved state of " + r);
3822 r.icicle = null;
3823 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003824 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003825 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003826 if (remove) {
3827 removeActivityFromHistoryLocked(r, "appDied");
3828 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003829 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003830 }
3831 }
3832
3833 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003834 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003835
Chong Zhang280d3322015-11-03 17:27:26 -08003836 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003837 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003838 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003839 if (r != null && r.state != ActivityState.RESUMED) {
3840 r.updateOptionsLocked(options);
3841 } else {
3842 ActivityOptions.abort(options);
3843 }
3844 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003845 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003846 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003847
Craig Mautner21d24a22014-04-23 11:45:37 -07003848 void updateTaskMovement(TaskRecord task, boolean toFront) {
3849 if (task.isPersistable) {
3850 task.mLastTimeMoved = System.currentTimeMillis();
3851 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3852 // recently will be most negative, tasks sent to the bottom before that will be less
3853 // negative. Similarly for recent tasks moved to the top which will be most positive.
3854 if (!toFront) {
3855 task.mLastTimeMoved *= -1;
3856 }
3857 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003858 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003859 }
3860
Craig Mautner84984fa2014-06-19 11:19:20 -07003861 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003862 final int top = mTaskHistory.size() - 1;
3863 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3864 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003865 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003866 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3867 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003868 mTaskHistory.remove(taskNdx);
3869 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003870 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003871 return;
3872 }
3873 }
3874 }
3875
Chong Zhang280d3322015-11-03 17:27:26 -08003876 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003877 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003878 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003879
Craig Mautner11bf9a52013-02-19 14:08:51 -08003880 final int numTasks = mTaskHistory.size();
3881 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003882 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003883 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003884 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003885 ActivityOptions.abort(options);
3886 } else {
3887 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3888 }
3889 return;
3890 }
3891
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003892 if (timeTracker != null) {
3893 // The caller wants a time tracker associated with this task.
3894 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3895 tr.mActivities.get(i).appTimeTracker = timeTracker;
3896 }
3897 }
3898
Craig Mautner11bf9a52013-02-19 14:08:51 -08003899 // Shift all activities with this task up to the top
3900 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003901 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003902
Chong Zhang45c25ce2015-08-10 22:18:26 -07003903 // Don't refocus if invisible to current user
3904 ActivityRecord top = tr.getTopActivity();
3905 if (!okToShowLocked(top)) {
3906 addRecentActivityLocked(top);
3907 ActivityOptions.abort(options);
3908 return;
3909 }
3910
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003911 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003912 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003913 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003914
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003915 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003916 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003917 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003918 if (r != null) {
3919 mNoAnimActivities.add(r);
3920 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003921 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003922 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003923 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003924 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003925
Craig Mautner05d29032013-05-03 13:40:13 -07003926 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003927 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003928
3929 if (VALIDATE_TOKENS) {
3930 validateAppTokensLocked();
3931 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003932 }
3933
3934 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003935 * Worker method for rearranging history stack. Implements the function of moving all
3936 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003937 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003938 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003939 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3940 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003941 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003942 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003943 * @return Returns true if the move completed, false if not.
3944 */
Craig Mautner299f9602015-01-26 09:47:33 -08003945 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003946 final TaskRecord tr = taskForIdLocked(taskId);
3947 if (tr == null) {
3948 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3949 return false;
3950 }
3951
3952 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003953 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003954
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003955 // If we have a watcher, preflight the move before committing to it. First check
3956 // for *other* available tasks, but if none are available, then try again allowing the
3957 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003958 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003959 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003960 if (next == null) {
3961 next = topRunningActivityLocked(null, 0);
3962 }
3963 if (next != null) {
3964 // ask watcher if this is allowed
3965 boolean moveOK = true;
3966 try {
3967 moveOK = mService.mController.activityResuming(next.packageName);
3968 } catch (RemoteException e) {
3969 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003970 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003971 }
3972 if (!moveOK) {
3973 return false;
3974 }
3975 }
3976 }
3977
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003978 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003979
riddle_hsuc215a4f2014-12-27 12:10:45 +08003980 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07003981
3982 // If true, we should resume the home activity next if the task we are moving to the
3983 // back is over the home stack. We force to false if the task we are moving to back
3984 // is the home task and we don't want it resumed after moving to the back.
3985 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
3986 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08003987 final TaskRecord nextTask = getNextTask(tr);
3988 if (nextTask != null) {
3989 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3990 } else {
3991 prevIsHome = true;
3992 }
3993 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003994 mTaskHistory.remove(tr);
3995 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003996 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003997
Craig Mautnerc8143c62013-09-03 12:15:57 -07003998 // There is an assumption that moving a task to the back moves it behind the home activity.
3999 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004000 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004001 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4002 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004003 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004004 break;
4005 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004006 if (taskNdx == 1) {
4007 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004008 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004009 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004010 }
4011
Craig Mautner299f9602015-01-26 09:47:33 -08004012 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004013 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004014
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004015 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004016 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004017 }
4018
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004019 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004020 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004021 if (!mService.mBooting && !mService.mBooted) {
4022 // Not ready yet!
4023 return false;
4024 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004025 final int taskToReturnTo = tr.getTaskToReturnTo();
4026 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004027 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004028 }
4029
Craig Mautner05d29032013-05-03 13:40:13 -07004030 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004031 return true;
4032 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004033
Craig Mautner8849a5e2013-04-02 16:41:03 -07004034 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004035 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004036 final Uri data = r.intent.getData();
4037 final String strData = data != null ? data.toSafeString() : null;
4038
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004039 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004040 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004041 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004042 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004043 }
4044
4045 /**
4046 * Make sure the given activity matches the current configuration. Returns
4047 * false if the activity had to be destroyed. Returns true if the
4048 * configuration is the same, or the activity will remain running as-is
4049 * for whatever reason. Ensures the HistoryRecord is updated with the
4050 * correct configuration and all other bookkeeping is handled.
4051 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004052 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4053 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004054 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004055 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004056 "Skipping config check (will change): " + r);
4057 return true;
4058 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004059
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004060 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004061 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004062
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004063 // Short circuit: if the two configurations are the exact same
4064 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004065 final Configuration newConfig = mService.mConfiguration;
4066 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004067 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004068 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004069 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004070 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004071 "Configuration unchanged in " + r);
4072 return true;
4073 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004074
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004075 // We don't worry about activities that are finishing.
4076 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004077 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004078 "Configuration doesn't matter in finishing " + r);
4079 r.stopFreezingScreenLocked(false);
4080 return true;
4081 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004082
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004083 // Okay we now are going to make this activity have the new config.
4084 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004085 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004086 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004087 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004088 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004089
Filip Gruszczynski23493322015-07-29 17:02:59 -07004090 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004091 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004092 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004093 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004094 "Configuration no differences in " + r);
4095 return true;
4096 }
4097
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004098 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4099 "Configuration changes for " + r + " ; taskChanges="
4100 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4101 + Configuration.configurationDiffToString(changes));
4102
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004103 // If the activity isn't currently running, just leave the new
4104 // configuration and it will pick that up next time it starts.
4105 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004106 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004107 "Configuration doesn't matter not running " + r);
4108 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004109 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004110 return true;
4111 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004112
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004113 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004114 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4115 "Checking to restart " + r.info.name + ": changed=0x"
4116 + Integer.toHexString(changes) + ", handles=0x"
4117 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
4118
Dianne Hackborne6676352011-06-01 16:51:20 -07004119 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004120 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4121 r.configChangeFlags |= changes;
4122 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004123 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004124 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004125 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004126 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004127 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004128 } else if (r.state == ActivityState.PAUSING) {
4129 // A little annoying: we are waiting for this activity to
4130 // finish pausing. Let's not do anything now, but just
4131 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004132 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004133 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004134 r.configDestroy = true;
4135 return true;
4136 } else if (r.state == ActivityState.RESUMED) {
4137 // Try to optimize this case: the configuration is changing
4138 // and we need to restart the top, resumed activity.
4139 // Instead of doing the normal handshaking, just say
4140 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004141 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004142 "Config is relaunching resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004143 relaunchActivityLocked(r, r.configChangeFlags, true,
4144 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004145 r.configChangeFlags = 0;
4146 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004147 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004148 "Config is relaunching non-resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004149 relaunchActivityLocked(r, r.configChangeFlags, false,
4150 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004151 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004152 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004153
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004154 // All done... tell the caller we weren't able to keep this
4155 // activity around.
4156 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004157 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004158
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004159 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004160 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004161 // changes is always sent to all processes when they happen so it can just use whatever
4162 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004163 if (r.app != null && r.app.thread != null) {
4164 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004165 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004166 r.app.thread.scheduleActivityConfigurationChanged(
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004167 r.appToken, new Configuration(taskConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004168 } catch (RemoteException e) {
4169 // If process died, whatever.
4170 }
4171 }
4172 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004173
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004174 return true;
4175 }
4176
Filip Gruszczynski23493322015-07-29 17:02:59 -07004177 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4178 Configuration oldTaskOverride) {
4179 // Determine what has changed. May be nothing, if this is a config
4180 // that has come back from the app after going idle. In that case
4181 // we just want to leave the official config object now in the
4182 // activity and do nothing else.
4183 int taskChanges = oldTaskOverride.diff(taskConfig);
4184 // We don't want to use size changes if they don't cross boundaries that are important to
4185 // the app.
4186 if ((taskChanges & ActivityInfo.CONFIG_SCREEN_SIZE) != 0) {
4187 final boolean crosses = record.crossesHorizontalSizeThreshold(
4188 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4189 || record.crossesVerticalSizeThreshold(
4190 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4191 if (!crosses) {
4192 taskChanges &= ~ActivityInfo.CONFIG_SCREEN_SIZE;
4193 }
4194 }
4195 if ((taskChanges & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4196 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4197 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004198 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004199 taskChanges &= ~ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4200 }
4201 }
4202 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4203 }
4204
4205 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4206 Configuration oldTaskOverride, int taskChanges) {
4207 if (taskChanges == 0) {
4208 // {@link Configuration#diff} doesn't catch changes from unset values.
4209 // Check for changes we care about.
4210 if (oldTaskOverride.orientation != taskConfig.orientation) {
4211 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
4212 }
4213 // We want to explicitly track situations where the size configuration goes from
4214 // undefined to defined. We don't care about crossing the threshold in that case,
4215 // because there is no threshold.
4216 final int oldHeight = oldTaskOverride.screenHeightDp;
4217 final int newHeight = taskConfig.screenHeightDp;
4218 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4219 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4220 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4221 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4222 }
4223 final int oldWidth = oldTaskOverride.screenWidthDp;
4224 final int newWidth = taskConfig.screenWidthDp;
4225 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4226 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4227 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4228 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4229 }
4230 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4231 final int newSmallest = taskConfig.smallestScreenWidthDp;
4232 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4233 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4234 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4235 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4236 }
4237 }
4238 return taskChanges;
4239 }
4240
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004241 private static boolean isResizeOnlyChange(int change) {
4242 return (change & ~(ActivityInfo.CONFIG_SCREEN_SIZE
4243 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_ORIENTATION)) == 0;
4244 }
4245
Wale Ogunwale83301a92015-09-24 15:54:08 -07004246 private void relaunchActivityLocked(
4247 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4248 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
4249 return;
4250 }
4251
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004252 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004253 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004254 if (andResume) {
4255 results = r.results;
4256 newIntents = r.newIntents;
4257 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004258 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4259 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004260 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004261 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004262 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004263 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004264
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004265 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004266
Craig Mautner34b73df2014-01-12 21:11:08 -08004267 mStackSupervisor.removeChildActivityContainers(r);
4268
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004269 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004270 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4271 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004272 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004273 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4274 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004275 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004276 // Note: don't need to call pauseIfSleepingLocked() here, because
4277 // the caller will only pass in 'andResume' if this activity is
4278 // currently resumed, which implies we aren't sleeping.
4279 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004280 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004281 }
4282
4283 if (andResume) {
4284 r.results = null;
4285 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004286 r.state = ActivityState.RESUMED;
4287 } else {
4288 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4289 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004290 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004291 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004292
4293 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004294 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4295 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4296 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4297 final ActivityRecord r = activities.get(activityNdx);
4298 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004299 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004300 }
4301 if (r.fullscreen && !r.finishing) {
4302 return false;
4303 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004304 }
4305 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004306 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004307 if (r == null) {
4308 return false;
4309 }
4310 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4311 + " would have returned true for r=" + r);
4312 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004313 }
4314
4315 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004316 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4317 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4318 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4319 final ActivityRecord r = activities.get(activityNdx);
4320 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004321 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004322 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004323 }
4324 }
4325 }
4326
Wale Ogunwale540e1232015-05-01 15:35:39 -07004327 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4328 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004329 boolean didSomething = false;
4330 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004331 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004332 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4333 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4334 int numActivities = activities.size();
4335 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4336 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004337 final boolean sameComponent =
4338 (r.packageName.equals(packageName) && (filterByClasses == null
4339 || filterByClasses.contains(r.realActivity.getClassName())))
4340 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004341 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004342 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004343 && (r.app == null || evenPersistent || !r.app.persistent)) {
4344 if (!doit) {
4345 if (r.finishing) {
4346 // If this activity is just finishing, then it is not
4347 // interesting as far as something to stop.
4348 continue;
4349 }
4350 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004351 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004352 if (r.isHomeActivity()) {
4353 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4354 Slog.i(TAG, "Skip force-stop again " + r);
4355 continue;
4356 } else {
4357 homeActivity = r.realActivity;
4358 }
4359 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004360 didSomething = true;
4361 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004362 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004363 if (r.app != null) {
4364 r.app.removed = true;
4365 }
4366 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004367 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004368 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004369 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4370 true)) {
4371 // r has been deleted from mActivities, accommodate.
4372 --numActivities;
4373 --activityNdx;
4374 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004375 }
4376 }
4377 }
4378 return didSomething;
4379 }
4380
Dianne Hackborn09233282014-04-30 11:33:59 -07004381 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004382 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4383 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004384 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004385 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004386 if (task.getTopActivity() == null) {
4387 continue;
4388 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004389 ActivityRecord r = null;
4390 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004391 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004392 int numActivities = 0;
4393 int numRunning = 0;
4394 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004395 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004396 continue;
4397 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004398 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004399 tmp = activities.get(activityNdx);
4400 if (tmp.finishing) {
4401 continue;
4402 }
4403 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004404
Craig Mautneraab647e2013-02-28 16:31:36 -08004405 // Initialize state for next task if needed.
4406 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4407 top = r;
4408 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004409 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004410
4411 // Add 'r' into the current task.
4412 numActivities++;
4413 if (r.app != null && r.app.thread != null) {
4414 numRunning++;
4415 }
4416
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004417 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004418 TAG, r.intent.getComponent().flattenToShortString()
4419 + ": task=" + r.task);
4420 }
4421
4422 RunningTaskInfo ci = new RunningTaskInfo();
4423 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004424 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004425 ci.baseActivity = r.intent.getComponent();
4426 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004427 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004428 if (focusedStack && topTask) {
4429 // Give the latest time to ensure foreground task can be sorted
4430 // at the first, because lastActiveTime of creating task is 0.
4431 ci.lastActiveTime = System.currentTimeMillis();
4432 topTask = false;
4433 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004434
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004435 if (top.task != null) {
4436 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004437 }
4438 ci.numActivities = numActivities;
4439 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004440 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004441 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004442 }
4443
4444 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004445 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004446 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004447 if (top >= 0) {
4448 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4449 int activityTop = activities.size() - 1;
4450 if (activityTop > 0) {
4451 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4452 "unhandled-back", true);
4453 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004454 }
4455 }
4456
Craig Mautner6b74cb52013-09-27 17:02:21 -07004457 /**
4458 * Reset local parameters because an app's activity died.
4459 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004460 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004461 */
4462 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004463 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004464 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004465 "App died while pausing: " + mPausingActivity);
4466 mPausingActivity = null;
4467 }
4468 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4469 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004470 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004471 }
4472
Craig Mautner19091252013-10-05 00:03:53 -07004473 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004474 }
4475
Craig Mautnercae015f2013-02-08 14:31:27 -08004476 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004477 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4478 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4479 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4480 final ActivityRecord r = activities.get(activityNdx);
4481 if (r.app == app) {
4482 Slog.w(TAG, " Force finishing activity "
4483 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004484 // Force the destroy to skip right to removal.
4485 r.app = null;
4486 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004487 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004488 }
4489 }
4490 }
4491
Dianne Hackborn390517b2013-05-30 15:03:32 -07004492 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004493 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004494 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004495 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4496 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004497 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4498 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004499 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004500 " Task id #" + task.taskId + "\n" +
4501 " mFullscreen=" + task.mFullscreen + "\n" +
4502 " mBounds=" + task.mBounds + "\n" +
4503 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004504 if (printed) {
4505 header = null;
4506 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004507 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004508 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004509 }
4510
4511 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4512 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4513
4514 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004515 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4516 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004517 }
4518 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004519 final int top = mTaskHistory.size() - 1;
4520 if (top >= 0) {
4521 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4522 int listTop = list.size() - 1;
4523 if (listTop >= 0) {
4524 activities.add(list.get(listTop));
4525 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004526 }
4527 } else {
4528 ItemMatcher matcher = new ItemMatcher();
4529 matcher.build(name);
4530
Craig Mautneraab647e2013-02-28 16:31:36 -08004531 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4532 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4533 if (matcher.match(r1, r1.intent.getComponent())) {
4534 activities.add(r1);
4535 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004536 }
4537 }
4538 }
4539
4540 return activities;
4541 }
4542
4543 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004544 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004545
4546 // All activities that came from the package must be
4547 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004548 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4549 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4550 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4551 final ActivityRecord a = activities.get(activityNdx);
4552 if (a.info.packageName.equals(packageName)) {
4553 a.forceNewConfig = true;
4554 if (starting != null && a == starting && a.visible) {
4555 a.startFreezingScreenLocked(starting.app,
4556 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4557 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004558 }
4559 }
4560 }
4561
4562 return starting;
4563 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004564
Craig Mautner299f9602015-01-26 09:47:33 -08004565 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004566 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004567 }
4568
Wale Ogunwale000957c2015-04-03 08:19:12 -07004569 /**
4570 * Removes the input task from this stack.
4571 * @param task to remove.
4572 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004573 * @param moving task to another stack. In the case we are moving we don't want to perform
4574 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004575 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004576 void removeTask(TaskRecord task, String reason, boolean moving) {
4577 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004578 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004579 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004580 if (!StackId.persistTaskBounds(mStackId)) {
4581 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4582 // default configuration the next time it launches.
4583 task.updateOverrideConfiguration(null);
4584 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004585 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004586
Craig Mautner04a0ea62014-01-13 12:51:26 -08004587 final ActivityRecord r = mResumedActivity;
4588 if (r != null && r.task == task) {
4589 mResumedActivity = null;
4590 }
4591
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004592 final int taskNdx = mTaskHistory.indexOf(task);
4593 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004594 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4595 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4596 if (!nextTask.isOverHomeStack()) {
4597 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4598 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004599 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004600 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004601 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004602
Wale Ogunwale040b4702015-08-06 18:10:50 -07004603 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004604 final boolean isVoiceSession = task.voiceSession != null;
4605 if (isVoiceSession) {
4606 try {
4607 task.voiceSession.taskFinished(task.intent, task.taskId);
4608 } catch (RemoteException e) {
4609 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004610 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004611 if (task.autoRemoveFromRecents() || isVoiceSession) {
4612 // Task creator asked to remove this when done, or this task was a voice
4613 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004614 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004615 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004616 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004617 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004618
4619 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004620 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004621 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004622 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004623 String myReason = reason + " leftTaskHistoryEmpty";
4624 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004625 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004626 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004627 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004628 if (mStacks != null) {
4629 mStacks.remove(this);
4630 mStacks.add(0, this);
4631 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004632 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004633 mActivityContainer.onTaskListEmptyLocked();
4634 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004635 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004636
4637 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004638 }
4639
Dianne Hackborn91097de2014-04-04 18:02:06 -07004640 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4641 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4642 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004643 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4644 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004645 // add the task to stack first, mTaskPositioner might need the stack association
4646 addTask(task, toTop, false);
Wale Ogunwale935e5022015-11-10 12:36:10 -08004647 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.mResizeable) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004648 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004649 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004650 return task;
4651 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004652
Wale Ogunwale935e5022015-11-10 12:36:10 -08004653 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4654 if (mTaskPositioner == null) {
4655 return false;
4656 }
4657 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4658 return true;
4659 }
4660
Craig Mautnerc00204b2013-03-05 15:02:14 -08004661 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004662 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004663 }
4664
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004665 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004666 task.stack = this;
4667 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004668 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004669 } else {
4670 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004671 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004672 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004673 if (!moving && task.voiceSession != null) {
4674 try {
4675 task.voiceSession.taskStarted(task.intent, task.taskId);
4676 } catch (RemoteException e) {
4677 }
4678 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004679 }
4680
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004681 void positionTask(final TaskRecord task, int position, boolean moving) {
4682 task.stack = this;
4683 insertTaskAtPosition(task, position);
4684 if (!moving && task.voiceSession != null) {
4685 try {
4686 task.voiceSession.taskStarted(task.intent, task.taskId);
4687 } catch (RemoteException e) {
4688 }
4689 }
4690 }
4691
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004692 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07004693 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004694 task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004695 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4696 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
4697 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
4698 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
Wale Ogunwale61b009e2015-09-16 15:43:05 -07004699 bounds, task.mOverrideConfig, !r.isHomeActivity());
Chong Zhangb15758a2015-11-17 12:12:03 -08004700 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004701 r.taskConfigOverride = task.mOverrideConfig;
4702 }
4703
Wale Ogunwale079a0042015-10-24 11:44:07 -07004704 void setFocusAndResumeStateIfNeeded(
4705 ActivityRecord r, boolean setFocus, boolean setResume, String reason) {
4706 // If the activity had focus before move focus to this stack.
4707 if (setFocus) {
4708 // If the activity owns the last resumed activity, transfer that together,
4709 // so that we don't resume the same activity again in the new stack.
4710 // Apps may depend on onResume()/onPause() being called in pairs.
4711 if (setResume) {
4712 mResumedActivity = r;
4713 // Move the stack in which we are placing the activity to the front. We don't use
4714 // ActivityManagerService.setFocusedActivityLocked, because if the activity is
4715 // already focused, the call will short-circuit and do nothing.
4716 moveToFront(reason);
4717 } else {
4718 // We need to not only move the stack to the front, but also have the activity
4719 // focused. This will achieve both goals.
4720 mService.setFocusedActivityLocked(r, reason);
4721 }
4722 }
4723 }
4724
4725 /**
4726 * Moves the input activity from its current stack to this one.
4727 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4728 * created on this stack which the activity is added to.
4729 * */
4730 void moveActivityToStack(ActivityRecord r) {
4731 final ActivityStack prevStack = r.task.stack;
4732 if (prevStack.mStackId == mStackId) {
4733 // You are already in the right stack silly...
4734 return;
4735 }
4736
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004737 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004738 && (mStackSupervisor.topRunningActivityLocked() == r);
4739 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4740
4741 final TaskRecord task = createTaskRecord(
4742 mStackSupervisor.getNextTaskId(), r.info, r.intent, null, null, true);
4743 r.setTask(task, null);
4744 task.addActivityToTop(r);
4745 setAppTask(r, task);
4746 setFocusAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4747 }
4748
Wale Ogunwale706ed792015-08-02 10:29:44 -07004749 private void setAppTask(ActivityRecord r, TaskRecord task) {
4750 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004751 task.updateOverrideConfiguration(bounds);
4752 mWindowManager.setAppTask(
Wale Ogunwale2998eef2015-12-02 19:46:29 -08004753 r.appToken, task.taskId, mStackId, task.getLaunchBounds(), task.mOverrideConfig);
Chong Zhangb15758a2015-11-17 12:12:03 -08004754 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004755 r.taskConfigOverride = task.mOverrideConfig;
4756 }
4757
Craig Mautnerc00204b2013-03-05 15:02:14 -08004758 public int getStackId() {
4759 return mStackId;
4760 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004761
4762 @Override
4763 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004764 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4765 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004766 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004767
Craig Mautner15df08a2015-04-01 12:17:18 -07004768 void onLockTaskPackagesUpdatedLocked() {
4769 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4770 mTaskHistory.get(taskNdx).setLockTaskAuth();
4771 }
4772 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004773}