blob: 64699c9d2e9f7bebd118b91d7ba171004f3f8f1e [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
23import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070024import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070025
Wale Ogunwalee23149f2015-03-06 15:39:44 -080026import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner0eea92c2013-05-16 13:35:39 -070027
Craig Mautner84984fa2014-06-19 11:19:20 -070028import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
29import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070030
Wale Ogunwale040b4702015-08-06 18:10:50 -070031import static com.android.server.am.ActivityStackSupervisor.MOVING;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070032import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwale040b4702015-08-06 18:10:50 -070033
Winsonc809cbb2015-11-02 12:06:15 -080034import android.graphics.Point;
Wale Ogunwale706ed792015-08-02 10:29:44 -070035import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070036import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070037
Dianne Hackborn91097de2014-04-04 18:02:06 -070038import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080039import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070040import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070041import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080042import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080043import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080044import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080045import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070046import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070047
48import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070049import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070050import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070051import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080053import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080055import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080061import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080062import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070064import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080065import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Handler;
67import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090068import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070070import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.RemoteException;
72import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080073import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070074import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070075import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070078import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079
Craig Mautnercae015f2013-02-08 14:31:27 -080080import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080081import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082import java.lang.ref.WeakReference;
83import java.util.ArrayList;
84import java.util.Iterator;
85import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080086import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070087import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070088
89/**
90 * State and management of a single stack of activities.
91 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070092final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080093
Wale Ogunwalee23149f2015-03-06 15:39:44 -080094 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070095 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
96 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080097 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070098 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070099 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700100 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700101 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700102 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700103 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
104 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700105 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700106 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700107 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
108 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
109 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
110 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
111 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700112
113 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800114
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700115 // Ticks during which we check progress while waiting for an app to launch.
116 static final int LAUNCH_TICK = 500;
117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700118 // How long we wait until giving up on the last activity to pause. This
119 // is short because it directly impacts the responsiveness of starting the
120 // next activity.
121 static final int PAUSE_TIMEOUT = 500;
122
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700123 // How long we wait for the activity to tell us it has stopped before
124 // giving up. This is a good amount of time because we really need this
125 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700126 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700127
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700128 // How long we wait until giving up on an activity telling us it has
129 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700130 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800131
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700132 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800133 // disabled.
134 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800135
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700136 // How long between activity launches that we consider safe to not warn
137 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700138 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800139
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700140 // Set to false to disable the preview that is shown while a new activity
141 // is being started.
142 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800143
Craig Mautner5eda9b32013-07-02 11:58:16 -0700144 // How long to wait for all background Activities to redraw following a call to
145 // convertToTranslucent().
146 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
147
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700148 enum ActivityState {
149 INITIALIZING,
150 RESUMED,
151 PAUSING,
152 PAUSED,
153 STOPPING,
154 STOPPED,
155 FINISHING,
156 DESTROYING,
157 DESTROYED
158 }
159
160 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700161 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800162 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800163
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700164 /**
165 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800166 * running) activities. It contains #TaskRecord objects.
167 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800168 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800169
170 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800171 * Used for validating app tokens with window manager.
172 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800173 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800174
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700175 /**
176 * List of running activities, sorted by recent usage.
177 * The first entry in the list is the least recently used.
178 * It contains HistoryRecord objects.
179 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800180 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700181
182 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700183 * Animations that for the current transition have requested not to
184 * be considered for the transition animation.
185 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800186 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700187
188 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700189 * When we are in the process of pausing an activity, before starting the
190 * next one, this variable holds the activity that is currently being paused.
191 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800192 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700193
194 /**
195 * This is the last activity that we put into the paused state. This is
196 * used to determine if we need to do an activity transition while sleeping,
197 * when we normally hold the top activity paused.
198 */
199 ActivityRecord mLastPausedActivity = null;
200
201 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700202 * Activities that specify No History must be removed once the user navigates away from them.
203 * If the device goes to sleep with such an activity in the paused state then we save it here
204 * and finish it later if another activity replaces it on wakeup.
205 */
206 ActivityRecord mLastNoHistoryActivity = null;
207
208 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700209 * Current activity that is resumed, or null if there is none.
210 */
211 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800212
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700213 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700214 * This is the last activity that has been started. It is only used to
215 * identify when multiple activities are started at once so that the user
216 * can be warned they may not be in the activity they think they are.
217 */
218 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800219
Craig Mautner5eda9b32013-07-02 11:58:16 -0700220 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
221 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
222 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
223 // Activity in mTranslucentActivityWaiting is notified via
224 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
225 // background activity being drawn then the same call will be made with a true value.
226 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700227 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700228
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700229 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700230 * Set when we know we are going to be calling updateConfiguration()
231 * soon, so want to skip intermediate config checks.
232 */
233 boolean mConfigWillChange;
234
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700235 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800236 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700237 // Current bounds of the stack or null if fullscreen.
238 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800239
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700240 long mLaunchStartTime = 0;
241 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800242
Craig Mautner858d8a62013-04-23 17:08:34 -0700243 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700244
Craig Mautnerc00204b2013-03-05 15:02:14 -0800245 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800246 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800247 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
248 ArrayList<ActivityStack> mStacks;
249 /** The attached Display's unique identifier, or -1 if detached */
250 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800251
Craig Mautner27084302013-03-25 08:05:25 -0700252 /** Run all ActivityStacks through this */
253 final ActivityStackSupervisor mStackSupervisor;
254
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700255 private final LaunchingTaskPositioner mTaskPositioner;
256
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700257 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700258 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
259 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
260 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
261 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700262 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700263 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700264 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700265
266 static class ScheduleDestroyArgs {
267 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700268 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700269 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700270 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700271 mReason = reason;
272 }
273 }
274
Zoran Marcetaf958b322012-08-09 20:27:12 +0900275 final Handler mHandler;
276
277 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800278
Craig Mautnerc8143c62013-09-03 12:15:57 -0700279 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900280 super(looper);
281 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700282
Zoran Marcetaf958b322012-08-09 20:27:12 +0900283 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700284 public void handleMessage(Message msg) {
285 switch (msg.what) {
286 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800287 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 // We don't at this point know if the activity is fullscreen,
289 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800290 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700291 synchronized (mService) {
292 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700293 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700294 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700295 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800296 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700297 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700298 case LAUNCH_TICK_MSG: {
299 ActivityRecord r = (ActivityRecord)msg.obj;
300 synchronized (mService) {
301 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700302 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700303 }
304 }
305 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700306 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800307 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700308 // We don't at this point know if the activity is fullscreen,
309 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800310 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800311 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800312 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800313 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700314 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700315 case STOP_TIMEOUT_MSG: {
316 ActivityRecord r = (ActivityRecord)msg.obj;
317 // We don't at this point know if the activity is fullscreen,
318 // so we need to be conservative and assume it isn't.
319 Slog.w(TAG, "Activity stop timeout for " + r);
320 synchronized (mService) {
321 if (r.isInHistory()) {
322 activityStoppedLocked(r, null, null, null);
323 }
324 }
325 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700326 case DESTROY_ACTIVITIES_MSG: {
327 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
328 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700329 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700330 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700331 } break;
332 case TRANSLUCENT_TIMEOUT_MSG: {
333 synchronized (mService) {
334 notifyActivityDrawnLocked(null);
335 }
336 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700337 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700338 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700339 final ActivityRecord r = getVisibleBehindActivity();
340 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700341 if (r != null) {
342 mService.killAppAtUsersRequest(r.app, null);
343 }
344 }
345 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700346 }
347 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800348 }
349
Craig Mautner34b73df2014-01-12 21:11:08 -0800350 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800351 int count = 0;
352 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
353 count += mTaskHistory.get(taskNdx).mActivities.size();
354 }
355 return count;
356 }
357
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800358 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
359 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800360 mActivityContainer = activityContainer;
361 mStackSupervisor = activityContainer.getOuter();
362 mService = mStackSupervisor.mService;
363 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
364 mWindowManager = mService.mWindowManager;
365 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700366 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800367 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700368 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
369 ? new LaunchingTaskPositioner() : null;
370 }
371
372 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
373 mDisplayId = activityDisplay.mDisplayId;
374 mStacks = activityDisplay.mStacks;
375 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
376 mFullscreen = mBounds == null;
377 if (mTaskPositioner != null) {
378 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
379 mTaskPositioner.configure(mBounds);
380 }
381 }
382
383 void detachDisplay() {
384 mDisplayId = Display.INVALID_DISPLAY;
385 mStacks = null;
386 if (mTaskPositioner != null) {
387 mTaskPositioner.reset();
388 }
389 mWindowManager.detachStack(mStackId);
390 }
391
Winsonc809cbb2015-11-02 12:06:15 -0800392 public void getDisplaySize(Point out) {
393 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
394 }
395
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700396 void setBounds(Rect bounds) {
397 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700398 if (mTaskPositioner != null) {
399 mTaskPositioner.configure(bounds);
400 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700401 }
Craig Mautner5962b122012-10-05 14:45:52 -0700402
Amith Yamasani734983f2014-03-04 16:48:05 -0800403 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700404 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700405 }
406
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700407 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800408 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700409 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700410 if (r != null) {
411 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800412 }
413 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700414 return null;
415 }
416
417 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800418 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
419 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800420 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800421 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
422 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800423 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800424 return r;
425 }
426 }
427 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700428 return null;
429 }
430
431 /**
432 * This is a simplified version of topRunningActivityLocked that provides a number of
433 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800434 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700435 * @param token If non-null, any history records matching this token will be skipped.
436 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800437 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700438 * @return Returns the HistoryRecord of the next activity on the stack.
439 */
440 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800441 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
442 TaskRecord task = mTaskHistory.get(taskNdx);
443 if (task.taskId == taskId) {
444 continue;
445 }
446 ArrayList<ActivityRecord> activities = task.mActivities;
447 for (int i = activities.size() - 1; i >= 0; --i) {
448 final ActivityRecord r = activities.get(i);
449 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800450 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800451 return r;
452 }
453 }
454 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700455 return null;
456 }
457
Craig Mautner8849a5e2013-04-02 16:41:03 -0700458 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700459 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
460 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700461 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
462 final ActivityRecord r = activities.get(activityNdx);
463 if (!r.finishing) {
464 return r;
465 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700466 }
467 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700468 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700469 }
470
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700471 final TaskRecord topTask() {
472 final int size = mTaskHistory.size();
473 if (size > 0) {
474 return mTaskHistory.get(size - 1);
475 }
476 return null;
477 }
478
Craig Mautnerd2328952013-03-05 12:46:26 -0800479 TaskRecord taskForIdLocked(int id) {
480 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
481 final TaskRecord task = mTaskHistory.get(taskNdx);
482 if (task.taskId == id) {
483 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800484 }
485 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700486 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700487 }
488
Craig Mautnerd2328952013-03-05 12:46:26 -0800489 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700490 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800491 return isInStackLocked(r);
492 }
493
494 ActivityRecord isInStackLocked(ActivityRecord r) {
495 if (r == null) {
496 return null;
497 }
498 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700499 if (task != null && task.stack != null
500 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800501 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800502 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800503 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800504 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800505 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800506 }
507
Craig Mautner2420ead2013-04-01 17:13:20 -0700508 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700509 final boolean hadit = mLRUActivities.remove(r);
510 mLRUActivities.add(r);
511 return hadit;
512 }
513
Craig Mautnerde4ef022013-04-07 19:01:33 -0700514 final boolean isHomeStack() {
515 return mStackId == HOME_STACK_ID;
516 }
517
Craig Mautnere0a38842013-12-16 16:14:02 -0800518 final boolean isOnHomeDisplay() {
519 return isAttached() &&
520 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
521 }
522
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700523 void moveToFront(String reason) {
524 moveToFront(reason, null);
525 }
526
527 /**
528 * @param reason The reason for moving the stack to the front.
529 * @param task If non-null, the task will be moved to the top of the stack.
530 * */
531 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700532 if (!isAttached()) {
533 return;
534 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700535
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700536 mStacks.remove(this);
537 int addIndex = mStacks.size();
538
539 if (addIndex > 0) {
540 final ActivityStack topStack = mStacks.get(addIndex - 1);
541 if (topStack.mStackId == PINNED_STACK_ID && topStack != this) {
542 // The pinned stack is always the top most stack (always-on-top).
543 // So, stack is moved just below the pinned stack.
544 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800545 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700546 }
547
548 mStacks.add(addIndex, this);
549
550 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
551 if (isOnHomeDisplay()) {
552 mStackSupervisor.setFocusStack(reason, this);
553 }
554 if (task != null) {
555 insertTaskAtTop(task, null);
556 } else {
557 task = topTask();
558 }
559 if (task != null) {
560 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800561 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800562 }
563
564 final boolean isAttached() {
565 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800566 }
567
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700568 /**
569 * Returns the top activity in any existing task matching the given
570 * Intent. Returns null if no such task is found.
571 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700572 ActivityRecord findTaskLocked(ActivityRecord target) {
573 Intent intent = target.intent;
574 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700575 ComponentName cls = intent.getComponent();
576 if (info.targetActivity != null) {
577 cls = new ComponentName(info.packageName, info.targetActivity);
578 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700579 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700580 boolean isDocument = intent != null & intent.isDocument();
581 // If documentData is non-null then it must match the existing task data.
582 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800583
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700584 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800585 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
586 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700587 if (task.voiceSession != null) {
588 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700589 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700590 continue;
591 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700592 if (task.userId != userId) {
593 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700594 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700595 continue;
596 }
Craig Mautner000f0022013-02-26 15:04:29 -0800597 final ActivityRecord r = task.getTopActivity();
598 if (r == null || r.finishing || r.userId != userId ||
599 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700600 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800601 continue;
602 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700603 if (r.mActivityType != target.mActivityType) {
604 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
605 continue;
606 }
Craig Mautner000f0022013-02-26 15:04:29 -0800607
Craig Mautnerd00f4742014-03-12 14:17:26 -0700608 final Intent taskIntent = task.intent;
609 final Intent affinityIntent = task.affinityIntent;
610 final boolean taskIsDocument;
611 final Uri taskDocumentData;
612 if (taskIntent != null && taskIntent.isDocument()) {
613 taskIsDocument = true;
614 taskDocumentData = taskIntent.getData();
615 } else if (affinityIntent != null && affinityIntent.isDocument()) {
616 taskIsDocument = true;
617 taskDocumentData = affinityIntent.getData();
618 } else {
619 taskIsDocument = false;
620 taskDocumentData = null;
621 }
622
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700623 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700624 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700625 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700626 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700627 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
628 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700629 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700630 return r;
631 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700632 } else if (taskIntent != null && taskIntent.getComponent() != null &&
633 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700634 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700635 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800636 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700637 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
638 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800639 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700640 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
641 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700642 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700643 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800644 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700645 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
646 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800647 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700648 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700649 }
650
651 return null;
652 }
653
654 /**
655 * Returns the first activity (starting from the top of the stack) that
656 * is the same as the given activity. Returns null if no such activity
657 * is found.
658 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700659 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700660 ComponentName cls = intent.getComponent();
661 if (info.targetActivity != null) {
662 cls = new ComponentName(info.packageName, info.targetActivity);
663 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700664 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700665
Craig Mautner000f0022013-02-26 15:04:29 -0800666 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700667 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700668 final boolean notCurrentUserTask =
669 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700670 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700671
Craig Mautner000f0022013-02-26 15:04:29 -0800672 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
673 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700674 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700675 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700676 }
Craig Mautner000f0022013-02-26 15:04:29 -0800677 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700678 return r;
679 }
680 }
681 }
682
683 return null;
684 }
685
Amith Yamasani742a6712011-05-04 14:49:28 -0700686 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700687 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700688 */
Craig Mautner93529a42013-10-04 15:03:13 -0700689 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800690 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700691 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800692 }
693 mCurrentUser = userId;
694
695 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800696 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800697 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700698 final TaskRecord task = mTaskHistory.get(i);
699
700 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
701 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700702 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700703 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700704 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700705 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800706 mTaskHistory.remove(i);
707 mTaskHistory.add(task);
708 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800709 // Use same value for i.
710 } else {
711 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800712 }
713 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700714 if (VALIDATE_TOKENS) {
715 validateAppTokensLocked();
716 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700717 }
718
Craig Mautner2420ead2013-04-01 17:13:20 -0700719 void minimalResumeActivityLocked(ActivityRecord r) {
720 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700721 if (DEBUG_STATES) Slog.v(TAG_STATES,
722 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700723 r.stopped = false;
724 mResumedActivity = r;
725 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800726 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700727 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700728 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700729 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700730 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
731 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700732 }
733
Chong Zhang45c25ce2015-08-10 22:18:26 -0700734 private void addRecentActivityLocked(ActivityRecord r) {
735 if (r != null) {
736 mRecentTasks.addLocked(r.task);
737 r.task.touchActiveTime();
738 }
739 }
740
Narayan Kamath7829c812015-06-08 17:39:43 +0100741 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700742 if (mFullyDrawnStartTime != 0) {
743 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
744 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100745 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700746 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
747 }
748
749 private void stopFullyDrawnTraceIfNeeded() {
750 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
751 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
752 mFullyDrawnStartTime = 0;
753 }
754 }
755
Craig Mautnere79d42682013-04-01 19:01:53 -0700756 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700757 if (r.displayStartTime == 0) {
758 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
759 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100760 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700761 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700762 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700763 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100764 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700765 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700766 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700767 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800768
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700769 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700770 // Make sure that there is no activity waiting for this to launch.
771 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
772 r.displayStartTime = r.fullyDrawnStartTime = 0;
773 } else {
774 mStackSupervisor.removeTimeoutsForActivityLocked(r);
775 mStackSupervisor.scheduleIdleTimeoutLocked(r);
776 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700777 }
778
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800779 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800780 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800781 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
782 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
783 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
784 activities.get(activityNdx).setSleeping(false);
785 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800786 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800787 if (mPausingActivity != null) {
788 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
789 activityPausedLocked(mPausingActivity.appToken, true);
790 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800791 }
792
Craig Mautner0eea92c2013-05-16 13:35:39 -0700793 /**
794 * @return true if something must be done before going to sleep.
795 */
796 boolean checkReadyForSleepLocked() {
797 if (mResumedActivity != null) {
798 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700799 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
800 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
801 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700802 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700803 return true;
804 }
805 if (mPausingActivity != null) {
806 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700807 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700808 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800809 }
810
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700811 if (hasVisibleBehindActivity()) {
812 // Stop visible behind activity before going to sleep.
813 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
814 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700815 if (DEBUG_STATES) Slog.v(TAG_STATES,
816 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700817 return true;
818 }
819
Craig Mautner0eea92c2013-05-16 13:35:39 -0700820 return false;
821 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800822
Craig Mautner0eea92c2013-05-16 13:35:39 -0700823 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700824 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800825
Craig Mautner0eea92c2013-05-16 13:35:39 -0700826 // Make sure any stopped but visible activities are now sleeping.
827 // This ensures that the activity's onStop() is called.
828 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
829 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
830 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
831 final ActivityRecord r = activities.get(activityNdx);
832 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
833 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800834 }
835 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800836 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700837 }
Craig Mautner59c00972012-07-30 12:10:24 -0700838
Dianne Hackbornd2835932010-12-13 16:28:46 -0800839 public final Bitmap screenshotActivities(ActivityRecord who) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700840 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800841 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700842 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800843 return null;
844 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800845
Winson Chung083baf92014-07-11 10:32:42 -0700846 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700847 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800848 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700849 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700850 return null;
851 }
852
Winson Chung48a10a52014-08-27 14:36:51 -0700853 int w = mService.mThumbnailWidth;
854 int h = mService.mThumbnailHeight;
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");
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800857 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Dianne Hackborn27eac1d2015-03-16 17:15:53 -0700858 w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800859 }
Winson Chung376543b2014-05-21 17:43:28 -0700860 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800861 return null;
862 }
863
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700864 /**
865 * Start pausing the currently resumed activity. It is an error to call this if there
866 * is already an activity being paused or there is no resumed activity.
867 *
868 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
869 * @param uiSleeping True if this is happening with the user interface going to sleep (the
870 * screen turning off).
871 * @param resuming True if this is being called as part of resuming the top activity, so
872 * we shouldn't try to instigate a resume here.
873 * @param dontWait True if the caller does not want to wait for the pause to complete. If
874 * set to true, we will immediately complete the pause here before returning.
875 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
876 * it to tell us when it is done.
877 */
878 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
879 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800880 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800881 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
882 + " state=" + mPausingActivity.state);
883 if (!mService.isSleeping()) {
884 // Avoid recursion among check for sleep and complete pause during sleeping.
885 // Because activity will be paused immediately after resume, just let pause
886 // be completed by the order of activity paused from clients.
887 completePauseLocked(false);
888 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800889 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700890 ActivityRecord prev = mResumedActivity;
891 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700892 if (!resuming) {
893 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
894 mStackSupervisor.resumeTopActivitiesLocked();
895 }
896 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700897 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800898
899 if (mActivityContainer.mParentActivity == null) {
900 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700901 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800902 }
903
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700904 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700905 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700906 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800907 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700908 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700909 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
910 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700911 prev.state = ActivityState.PAUSING;
912 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700913 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700914 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700915 if (mService.mHasRecents
916 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800917 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700918 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700919 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700920
921 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800922
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700923 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700924 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700925 try {
926 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700927 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700928 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700929 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800930 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700931 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932 } catch (Exception e) {
933 // Ignore exception, if process died other code will cleanup.
934 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800935 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700936 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700937 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700938 }
939 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800940 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700941 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700942 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700943 }
944
945 // If we are not going to sleep, we want to ensure the device is
946 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700947 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700948 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700949 }
950
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800951 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700952 // Have the window manager pause its key dispatching until the new
953 // activity has started. If we're pausing the activity just because
954 // the screen is being turned off and the UI is sleeping, don't interrupt
955 // key dispatch; the same activity will pick it up again on wakeup.
956 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800957 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700958 } else if (DEBUG_PAUSE) {
959 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700960 }
961
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700962 if (dontWait) {
963 // If the caller said they don't want to wait for the pause, then complete
964 // the pause now.
965 completePauseLocked(false);
966 return false;
967
968 } else {
969 // Schedule a pause timeout in case the app doesn't respond.
970 // We don't give it much time because this directly impacts the
971 // responsiveness seen by the user.
972 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
973 msg.obj = prev;
974 prev.pauseTime = SystemClock.uptimeMillis();
975 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700976 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700977 return true;
978 }
979
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700980 } else {
981 // This activity failed to schedule the
982 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700983 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700984 if (!resuming) {
985 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
986 }
987 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700988 }
989 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700990
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700991 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700992 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
993 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700994
Craig Mautnerd2328952013-03-05 12:46:26 -0800995 final ActivityRecord r = isInStackLocked(token);
996 if (r != null) {
997 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
998 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700999 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001000 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001001 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -08001002 } else {
1003 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1004 r.userId, System.identityHashCode(r), r.shortComponentName,
1005 mPausingActivity != null
1006 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001007 if (r.state == ActivityState.PAUSING) {
1008 r.state = ActivityState.PAUSED;
1009 if (r.finishing) {
1010 if (DEBUG_PAUSE) Slog.v(TAG,
1011 "Executing finish of failed to pause activity: " + r);
1012 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1013 }
louis_chang047dfd42015-04-08 16:35:55 +08001014 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001015 }
1016 }
1017 }
1018
Craig Mautnera0026042014-04-23 11:45:37 -07001019 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1020 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001021 if (r.state != ActivityState.STOPPING) {
1022 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1023 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1024 return;
1025 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001026 if (persistentState != null) {
1027 r.persistentState = persistentState;
1028 mService.notifyTaskPersisterLocked(r.task, false);
1029 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001030 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001031 if (icicle != null) {
1032 // If icicle is null, this is happening due to a timeout, so we
1033 // haven't really saved the state.
1034 r.icicle = icicle;
1035 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001036 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001037 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001038 }
1039 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001040 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001041 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1042 r.stopped = true;
1043 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -07001044 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
1045 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001046 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001047 if (r.finishing) {
1048 r.clearOptionsLocked();
1049 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001050 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001051 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -07001052 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001053 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001054 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001055 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001056 }
1057 }
1058 }
1059
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001060 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001061 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001062 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001063
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001064 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001065 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001066 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001067 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001068 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001069 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001070 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001071 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001072 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1073 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001074 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001075 if (prev.configDestroy) {
1076 // The previous is being paused because the configuration
1077 // is changing, which means it is actually stopping...
1078 // To juggle the fact that we are also starting a new
1079 // instance right now, we need to first completely stop
1080 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001081 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001082 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001083 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001084 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001085 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001086 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001087 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1088 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001089 // If we already have a few activities waiting to stop,
1090 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001091 // them out. Or if r is the last of activity of the last task the stack
1092 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001093 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001094 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001095 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001096 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001097 }
1098 }
1099 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001100 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001101 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001102 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001103 // It is possible the activity was freezing the screen before it was paused.
1104 // In that case go ahead and remove the freeze this activity has on the screen
1105 // since it is no longer visible.
1106 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001107 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001108 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001109
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001110 if (resumeNext) {
1111 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1112 if (!mService.isSleepingOrShuttingDown()) {
1113 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1114 } else {
1115 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001116 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001117 if (top == null || (prev != null && top != prev)) {
1118 // If there are no more activities available to run,
1119 // do resume anyway to start something. Also if the top
1120 // activity on the stack is not the just paused activity,
1121 // we need to go ahead and resume it to ensure we complete
1122 // an in-flight app switch.
1123 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1124 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001125 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001126 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001127
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001128 if (prev != null) {
1129 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001130
Craig Mautner525f3d92013-05-07 14:01:50 -07001131 if (prev.app != null && prev.cpuTimeAtResume > 0
1132 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001133 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1134 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001135 if (diff > 0) {
1136 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1137 synchronized (bsi) {
1138 BatteryStatsImpl.Uid.Proc ps =
1139 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001140 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001141 if (ps != null) {
1142 ps.addForegroundTimeLocked(diff);
1143 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001144 }
1145 }
1146 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001147 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001148 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001149
1150 // Notfiy when the task stack has changed
1151 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001152 }
1153
1154 /**
1155 * Once we know that we have asked an application to put an activity in
1156 * the resumed state (either by launching it or explicitly telling it),
1157 * this function updates the rest of our state to match that fact.
1158 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001159 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001160 next.idle = false;
1161 next.results = null;
1162 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001163
Chong Zhang85ee6542015-10-02 13:36:38 -07001164 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001165 ProcessRecord app = next.task.mActivities.get(0).app;
1166 if (app != null && app != mService.mHomeProcess) {
1167 mService.mHomeProcess = app;
1168 }
1169 }
1170
Craig Mautner07566322013-09-26 16:42:55 -07001171 if (next.nowVisible) {
1172 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001173 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001174 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001175
1176 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001177 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001178
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001179 mStackSupervisor.reportResumedActivityLocked(next);
1180
1181 next.resumeKeyDispatchingLocked();
1182 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001183
1184 // Mark the point when the activity is resuming
1185 // TODO: To be more accurate, the mark should be before the onCreate,
1186 // not after the onResume. But for subsequent starts, onResume is fine.
1187 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001188 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001189 } else {
1190 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1191 }
Winson Chung376543b2014-05-21 17:43:28 -07001192
George Mount6ba042b2014-07-28 11:12:28 -07001193 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001194
1195 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1196 // When resuming an activity, require it to call requestVisibleBehind() again.
1197 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1198 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001199 }
1200
Craig Mautner2568c3a2015-03-26 14:22:34 -07001201 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001202 r.visible = visible;
1203 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001204 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001205 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001206 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001207 container.setVisible(visible);
1208 }
1209 }
1210
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001211 // Find the first visible activity above the passed activity and if it is translucent return it
1212 // otherwise return null;
1213 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1214 TaskRecord task = r.task;
1215 if (task == null) {
1216 return null;
1217 }
1218
1219 ActivityStack stack = task.stack;
1220 if (stack == null) {
1221 return null;
1222 }
1223
1224 int stackNdx = mStacks.indexOf(stack);
1225
1226 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1227 int taskNdx = tasks.indexOf(task);
1228
1229 ArrayList<ActivityRecord> activities = task.mActivities;
1230 int activityNdx = activities.indexOf(r) + 1;
1231
1232 final int numStacks = mStacks.size();
1233 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001234 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001235 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001236 final int numTasks = tasks.size();
1237 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001238 final TaskRecord currentTask = tasks.get(taskNdx);
1239 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001240 final int numActivities = activities.size();
1241 while (activityNdx < numActivities) {
1242 final ActivityRecord activity = activities.get(activityNdx);
1243 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001244 return historyStack.mFullscreen
1245 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001246 }
1247 ++activityNdx;
1248 }
1249 activityNdx = 0;
1250 ++taskNdx;
1251 }
1252 taskNdx = 0;
1253 ++stackNdx;
1254 }
1255
1256 return null;
1257 }
1258
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001259 private ActivityStack getNextVisibleStackLocked() {
1260 ArrayList<ActivityStack> stacks = mStacks;
1261 final ActivityRecord parent = mActivityContainer.mParentActivity;
1262 if (parent != null) {
1263 stacks = parent.task.stack.mStacks;
1264 }
1265 if (stacks != null) {
1266 for (int i = stacks.size() - 1; i >= 0; --i) {
1267 ActivityStack stack = stacks.get(i);
1268 if (stack != this && stack.isStackVisibleLocked()) {
1269 return stack;
1270 }
1271 }
1272 }
1273 return null;
1274 }
1275
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001276 /** Returns true if the stack contains a fullscreen task. */
1277 private boolean hasFullscreenTask() {
1278 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1279 final TaskRecord task = mTaskHistory.get(i);
1280 if (task.mFullscreen) {
1281 return true;
1282 }
1283 }
1284 return false;
1285 }
1286
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001287 private boolean hasTranslucentActivity(ActivityStack stack) {
1288 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1289 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1290 final TaskRecord task = tasks.get(taskNdx);
1291 final ArrayList<ActivityRecord> activities = task.mActivities;
1292 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1293 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001294
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001295 // Conditions for an activity to obscure the stack we're
1296 // examining:
1297 // 1. Not Finishing AND Visible AND:
1298 // 2. Either:
1299 // - Full Screen Activity OR
1300 // - On top of Home and our stack is NOT home
1301 if (!r.finishing && r.visible && (r.fullscreen ||
1302 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1303 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001304 }
1305 }
1306 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001307 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001308 }
1309
1310 /** Returns true if the stack is considered visible. */
Todd Kennedyaab56db2015-01-30 09:39:53 -08001311 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001312 if (!isAttached()) {
1313 return false;
1314 }
1315
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001316 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001317 return true;
1318 }
1319
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001320 final int stackIndex = mStacks.indexOf(this);
1321
1322 if (stackIndex == mStacks.size() - 1) {
1323 Slog.wtf(TAG,
1324 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1325 return false;
1326 }
1327
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001328 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1329 final int focusedStackId = focusedStack.mStackId;
1330
1331 if (mStackId == DOCKED_STACK_ID) {
1332 // Docked stack is always visible, except in the case where the home activity
1333 // is the top running activity in the focused home stack.
1334 if (focusedStackId != HOME_STACK_ID) {
1335 return true;
1336 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001337 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked();
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001338 return topHomeActivity == null || !topHomeActivity.isHomeActivity();
1339 }
1340
Chong Zhangb16cf342015-11-12 17:22:40 -08001341 // Find the first stack below focused stack that actually got something visible.
1342 int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1343 while (belowFocusedIndex >= 0 &&
1344 mStacks.get(belowFocusedIndex).topRunningActivityLocked() == null) {
1345 belowFocusedIndex--;
1346 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001347 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1348 && stackIndex == belowFocusedIndex) {
1349 // Stacks directly behind the docked or pinned stack are always visible.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001350 return true;
1351 }
1352
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001353 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1354 && hasTranslucentActivity(focusedStack)) {
1355 // Stacks behind the fullscreen stack with a translucent activity are always
1356 // visible so they can act as a backdrop to the translucent activity.
1357 // For example, dialog activities
1358 if (stackIndex == belowFocusedIndex) {
1359 return true;
1360 }
1361 if (belowFocusedIndex >= 0) {
1362 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001363 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1364 && stackIndex == (belowFocusedIndex - 1)) {
1365 // The stack behind the docked or pinned stack is also visible so we can have a
1366 // complete backdrop to the translucent activity when the docked stack is up.
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001367 return true;
1368 }
1369 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001370 }
1371
Wale Ogunwale3797c222015-10-27 14:21:58 -07001372 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001373 // Visibility of any static stack should have been determined by the conditions above.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001374 return false;
1375 }
1376
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001377 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001378 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001379
1380 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1381 continue;
1382 }
1383
Wale Ogunwale3797c222015-10-27 14:21:58 -07001384 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001385 // These stacks can't have any dynamic stacks visible behind them.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001386 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001387 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001388
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001389 if (!hasTranslucentActivity(stack)) {
1390 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001391 }
1392 }
1393
1394 return true;
1395 }
1396
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001397 final int rankTaskLayers(int baseLayer) {
1398 int layer = 0;
1399 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1400 final TaskRecord task = mTaskHistory.get(taskNdx);
1401 ActivityRecord r = task.topRunningActivityLocked();
1402 if (r == null || r.finishing || !r.visible) {
1403 task.mLayerRank = -1;
1404 } else {
1405 task.mLayerRank = baseLayer + layer++;
1406 }
1407 }
1408 return layer;
1409 }
1410
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001411 /**
1412 * Make sure that all activities that need to be visible (that is, they
1413 * currently can be seen by the user) actually are.
1414 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001415 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1416 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001417 ActivityRecord top = topRunningActivityLocked();
Craig Mautnerbb742462014-07-07 15:28:55 -07001418 if (top == null) {
1419 return;
1420 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001421 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1422 "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001423 + " configChanges=0x" + Integer.toHexString(configChanges));
1424
Craig Mautner5eda9b32013-07-02 11:58:16 -07001425 if (mTranslucentActivityWaiting != top) {
1426 mUndrawnActivitiesBelowTopTranslucent.clear();
1427 if (mTranslucentActivityWaiting != null) {
1428 // Call the callback with a timeout indication.
1429 notifyActivityDrawnLocked(null);
1430 mTranslucentActivityWaiting = null;
1431 }
1432 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1433 }
1434
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001435 // If the top activity is not fullscreen, then we need to
1436 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001437 boolean aboveTop = true;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001438 final boolean stackInvisible = !isStackVisibleLocked();
1439 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001440 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001441
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001442 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001443 final TaskRecord task = mTaskHistory.get(taskNdx);
1444 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001445
Craig Mautnerd44711d2013-02-23 11:24:36 -08001446 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1447 final ActivityRecord r = activities.get(activityNdx);
1448 if (r.finishing) {
1449 continue;
1450 }
1451 if (aboveTop && r != top) {
1452 continue;
1453 }
1454 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001455 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1456 // but must be drawn initially for the animation as though they were visible.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001457 if (!behindFullscreenActivity || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001458 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1459 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001460 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001461
Craig Mautnerd44711d2013-02-23 11:24:36 -08001462 // First: if this is not the current activity being started, make
1463 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001464 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001465 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001466 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001467
1468 if (r.app == null || r.app.thread == null) {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001469 // We need to make sure the app is running if it's the top, or it is
1470 // just made visible from invisible.
1471 // If the app is already visible, it must have died while it was visible.
1472 // In this case, we'll show the dead window but will not restart the app.
1473 // Otherwise we could end up thrashing.
1474 if (r == top || !r.visible) {
1475 // This activity needs to be visible, but isn't even running...
1476 // get it started and resume if no other stack in this stack is resumed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001477 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
Chong Zhang112eb8c2015-11-02 11:17:00 -08001478 "Start and freeze screen for " + r);
1479 if (r != starting) {
1480 r.startFreezingScreenLocked(r.app, configChanges);
1481 }
1482 if (!r.visible || r.mLaunchTaskBehind) {
1483 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1484 "Starting and making visible: " + r);
1485 setVisible(r, true);
1486 }
1487 if (r != starting) {
1488 mStackSupervisor.startSpecificActivityLocked(
1489 r, noStackActivityResumed, false);
1490 if (activityNdx >= activities.size()) {
1491 // Record may be removed if its process needs to restart.
1492 activityNdx = activities.size() - 1;
1493 } else {
1494 noStackActivityResumed = false;
1495 }
riddle_hsu36ee73d2015-06-05 16:38:38 +08001496 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001497 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001498 } else if (r.visible) {
1499 // If this activity is already visible, then there is nothing
1500 // else to do here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001501 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1502 "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001503 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001504 try {
George Mount6ba042b2014-07-28 11:12:28 -07001505 if (r.returningOptions != null) {
1506 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1507 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001508 }
1509 } catch(RemoteException e) {
1510 }
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001511 if (r.state == ActivityState.RESUMED) {
1512 noStackActivityResumed = false;
1513 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001514 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001515 // This activity is not currently visible, but is running.
1516 // Tell it to become visible.
1517 r.visible = true;
1518 if (r.state != ActivityState.RESUMED && r != starting) {
1519 // If this activity is paused, tell it
1520 // to now show its window.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001521 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1522 "Making visible and scheduling visibility: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001523 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001524 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001525 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001526 mUndrawnActivitiesBelowTopTranslucent.add(r);
1527 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001528 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001529 r.sleeping = false;
1530 r.app.pendingUiClean = true;
1531 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1532 r.stopFreezingScreenLocked(false);
1533 } catch (Exception e) {
1534 // Just skip on any failure; we'll make it
1535 // visible when it next restarts.
1536 Slog.w(TAG, "Exception thrown making visibile: "
1537 + r.intent.getComponent(), e);
1538 }
1539 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001540 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001541
Craig Mautnerd44711d2013-02-23 11:24:36 -08001542 // Aggregate current change flags.
1543 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001544
Craig Mautnerd44711d2013-02-23 11:24:36 -08001545 if (r.fullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001546 // At this point, nothing else needs to be shown in this task.
1547 behindFullscreenActivity = true;
1548 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001549 + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001550 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautner84984fa2014-06-19 11:19:20 -07001551 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001552 behindFullscreenActivity = true;
1553 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001554 + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001555 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001556 }
1557 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001558 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001559 "Make invisible? " + r + " finishing=" + r.finishing
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001560 + " state=" + r.state + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001561 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001562 // Now for any activities that aren't visible to the user, make
1563 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001564 if (r.visible) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001565 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001566 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001567 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001568 switch (r.state) {
1569 case STOPPING:
1570 case STOPPED:
1571 if (r.app != null && r.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001572 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1573 "Scheduling invisibility: " + r);
Craig Mautner4addfc52013-06-25 08:05:45 -07001574 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1575 }
1576 break;
1577
1578 case INITIALIZING:
1579 case RESUMED:
1580 case PAUSING:
1581 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001582 // This case created for transitioning activities from
1583 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001584 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001585 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001586 } else {
1587 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1588 mStackSupervisor.mStoppingActivities.add(r);
1589 }
1590 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001591 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001592 break;
1593
1594 default:
1595 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001596 }
1597 } catch (Exception e) {
1598 // Just skip on any failure; we'll make it
1599 // visible when it next restarts.
1600 Slog.w(TAG, "Exception thrown making hidden: "
1601 + r.intent.getComponent(), e);
1602 }
1603 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001604 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001605 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001606 }
1607 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001608 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1609 // The visibility of tasks and the activities they contain in freeform stack are
1610 // determined individually unlike other stacks where the visibility or fullscreen
1611 // status of an activity in a previous task affects other.
1612 behindFullscreenActivity = stackInvisible;
1613 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001614 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001615
1616 if (mTranslucentActivityWaiting != null &&
1617 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1618 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1619 notifyActivityDrawnLocked(null);
1620 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001621 }
Craig Mautner58547802013-03-05 08:23:53 -08001622
Todd Kennedyaab56db2015-01-30 09:39:53 -08001623 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001624 mTranslucentActivityWaiting = r;
1625 mUndrawnActivitiesBelowTopTranslucent.clear();
1626 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1627 }
1628
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001629 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1630 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1631 final TaskRecord task = mTaskHistory.get(taskNdx);
1632 final ArrayList<ActivityRecord> activities = task.mActivities;
1633 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1634 final ActivityRecord r = activities.get(activityNdx);
1635 if ( r.appTimeTracker != except) {
1636 r.appTimeTracker = null;
1637 }
1638 }
1639 }
1640 }
1641
Craig Mautner5eda9b32013-07-02 11:58:16 -07001642 /**
1643 * Called as activities below the top translucent activity are redrawn. When the last one is
1644 * redrawn notify the top activity by calling
1645 * {@link Activity#onTranslucentConversionComplete}.
1646 *
1647 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1648 * occurred and the activity will be notified immediately.
1649 */
1650 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001651 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001652 if ((r == null)
1653 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1654 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1655 // The last undrawn activity below the top has just been drawn. If there is an
1656 // opaque activity at the top, notify it that it can become translucent safely now.
1657 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1658 mTranslucentActivityWaiting = null;
1659 mUndrawnActivitiesBelowTopTranslucent.clear();
1660 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1661
Craig Mautner71dd1b62014-02-18 15:48:52 -08001662 if (waitingActivity != null) {
1663 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1664 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1665 try {
1666 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1667 waitingActivity.appToken, r != null);
1668 } catch (RemoteException e) {
1669 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001670 }
1671 }
1672 }
1673 }
1674
Craig Mautnera61bc652013-10-28 15:43:18 -07001675 /** If any activities below the top running one are in the INITIALIZING state and they have a
1676 * starting window displayed then remove that starting window. It is possible that the activity
1677 * in this state will never resumed in which case that starting window will be orphaned. */
1678 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001679 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001680 boolean aboveTop = true;
1681 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1682 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1683 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1684 final ActivityRecord r = activities.get(activityNdx);
1685 if (aboveTop) {
1686 if (r == topActivity) {
1687 aboveTop = false;
1688 }
1689 continue;
1690 }
1691
1692 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001693 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1694 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001695 r.mStartingWindowShown = false;
1696 mWindowManager.removeAppStartingWindow(r.appToken);
1697 }
1698 }
1699 }
1700 }
1701
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001702 /**
1703 * Ensure that the top activity in the stack is resumed.
1704 *
1705 * @param prev The previously resumed activity, for when in the process
1706 * of pausing; can be null to call from elsewhere.
1707 *
1708 * @return Returns true if something is being resumed, or false if
1709 * nothing happened.
1710 */
1711 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001712 return resumeTopActivityLocked(prev, null);
1713 }
1714
Chong Zhang280d3322015-11-03 17:27:26 -08001715 final boolean resumeTopActivityLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001716 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001717 // Don't even start recursing.
1718 return false;
1719 }
1720
1721 boolean result = false;
1722 try {
1723 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001724 mStackSupervisor.inResumeTopActivity = true;
1725 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1726 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001727 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001728 }
Craig Mautner544efa72014-09-04 16:41:20 -07001729 result = resumeTopActivityInnerLocked(prev, options);
1730 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001731 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001732 }
1733 return result;
1734 }
1735
Chong Zhang280d3322015-11-03 17:27:26 -08001736 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001737 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001738
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001739 if (!mService.mBooting && !mService.mBooted) {
1740 // Not ready yet!
1741 return false;
1742 }
1743
Craig Mautnerdf88d732014-01-27 09:21:32 -08001744 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001745 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001746 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001747 // Do not resume this stack if its parent is not resumed.
1748 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1749 return false;
1750 }
1751
Craig Mautnera61bc652013-10-28 15:43:18 -07001752 cancelInitializingActivities();
1753
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001754 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001755 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001756
1757 // Remember how we'll process this pause/resume situation, and ensure
1758 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001759 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1760 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001761
Craig Mautner84984fa2014-06-19 11:19:20 -07001762 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001763 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001764 // There are no more activities!
1765 final String reason = "noMoreActivities";
1766 if (!mFullscreen) {
1767 // Try to move focus to the next visible stack with a running activity if this
1768 // stack is not covering the entire screen.
1769 final ActivityStack stack = getNextVisibleStackLocked();
1770 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1771 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1772 }
1773 }
1774 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001775 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001776 if (DEBUG_STATES) Slog.d(TAG_STATES,
1777 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001778 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001779 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001780 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1781 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1782 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001783 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001784 }
1785
1786 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001787
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001788 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001789 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1790 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001791 // Make sure we have executed any pending transitions, since there
1792 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001793 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001794 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001795 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001796 if (DEBUG_STATES) Slog.d(TAG_STATES,
1797 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001798 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001799 return false;
1800 }
1801
Craig Mautner525f3d92013-05-07 14:01:50 -07001802 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001803 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001804 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001805 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001806 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001807 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001808 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001809 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001810 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001811 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001812 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001813 } else if (!isOnHomeDisplay()) {
1814 return false;
1815 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001816 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001817 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001818 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1819 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1820 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001821 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001822 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001823 }
1824
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001825 // If we are sleeping, and there is no resumed activity, and the top
1826 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001827 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001828 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001829 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001830 // Make sure we have executed any pending transitions, since there
1831 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001832 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001833 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001834 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001835 if (DEBUG_STATES) Slog.d(TAG_STATES,
1836 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001837 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001838 return false;
1839 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001840
1841 // Make sure that the user who owns this activity is started. If not,
1842 // we will just leave it as is because someone should be bringing
1843 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001844 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001845 Slog.w(TAG, "Skipping resume of top activity " + next
1846 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001847 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001848 return false;
1849 }
1850
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001851 // The activity may be waiting for stop, but that is no longer
1852 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001853 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001854 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001855 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001856 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001857
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001858 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001859
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001860 // If we are currently pausing an activity, then don't do anything
1861 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001862 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001863 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001864 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001865 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001866 return false;
1867 }
1868
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001869 // Okay we are now going to start a switch, to 'next'. We may first
1870 // have to pause the current activity, but this is an important point
1871 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001872 // XXX "App Redirected" dialog is getting too many false positives
1873 // at this point, so turn off for now.
1874 if (false) {
1875 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1876 long now = SystemClock.uptimeMillis();
1877 final boolean inTime = mLastStartedActivity.startTime != 0
1878 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1879 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1880 final int nextUid = next.info.applicationInfo.uid;
1881 if (inTime && lastUid != nextUid
1882 && lastUid != next.launchedFromUid
1883 && mService.checkPermission(
1884 android.Manifest.permission.STOP_APP_SWITCHES,
1885 -1, next.launchedFromUid)
1886 != PackageManager.PERMISSION_GRANTED) {
1887 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1888 } else {
1889 next.startTime = now;
1890 mLastStartedActivity = next;
1891 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001892 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001893 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001894 mLastStartedActivity = next;
1895 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001896 }
Craig Mautner58547802013-03-05 08:23:53 -08001897
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001898 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1899
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001900 // We need to start pausing the current activity so the top one
1901 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001902 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1903 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001904 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001905 if (DEBUG_STATES) Slog.d(TAG_STATES,
1906 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001907 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001908 }
1909 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001910 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001911 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001912 // At this point we want to put the upcoming activity's process
1913 // at the top of the LRU list, since we know we will be needing it
1914 // very soon and it would be a waste to let it get killed if it
1915 // happens to be sitting towards the end.
1916 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001917 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001918 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001919 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001920 return true;
1921 }
1922
Christopher Tated3f175c2012-06-14 14:16:54 -07001923 // If the most recent activity was noHistory but was only stopped rather
1924 // than stopped+finished because the device went to sleep, we need to make
1925 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001926 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001927 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001928 if (DEBUG_STATES) Slog.d(TAG_STATES,
1929 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001930 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001931 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001932 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001933 }
1934
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001935 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001936 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1937 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001938 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001939 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1940 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001941 } else {
1942 // The next activity is already visible, so hide the previous
1943 // activity's windows right now so we can show the new one ASAP.
1944 // We only do this if the previous is finishing, which should mean
1945 // it is on top of the one being resumed so hiding it quickly
1946 // is good. Otherwise, we want to do the normal route of allowing
1947 // the resumed activity to be shown so we can decide if the
1948 // previous should actually be hidden depending on whether the
1949 // new one is found to be full-screen or not.
1950 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001951 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001952 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1953 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001954 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001955 + ", nowVisible=" + next.nowVisible);
1956 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001957 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001958 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001959 + ", waitingVisible="
1960 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1961 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001962 }
1963 }
1964 }
1965
Dianne Hackborne7f97212011-02-24 14:40:20 -08001966 // Launching this app's activity, make sure the app is no longer
1967 // considered stopped.
1968 try {
1969 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001970 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001971 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001972 } catch (IllegalArgumentException e) {
1973 Slog.w(TAG, "Failed trying to unstop package "
1974 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001975 }
1976
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001977 // We are starting up the next activity, so tell the window manager
1978 // that the previous one will be hidden soon. This way it can know
1979 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001980 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001981 if (prev != null) {
1982 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001983 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001984 "Prepare close transition: prev=" + prev);
1985 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001986 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001987 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001988 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001989 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001990 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1991 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001992 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001993 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001994 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001995 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
1996 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001997 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001998 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001999 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002000 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002001 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002002 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002003 : next.mLaunchTaskBehind
2004 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2005 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002006 }
2007 }
Craig Mautner967212c2013-04-13 21:10:58 -07002008 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002009 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002010 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002011 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002012 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002013 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002014 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002015 }
2016 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002017
2018 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002019 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002020 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2021 if (opts != null) {
2022 resumeAnimOptions = opts.toBundle();
2023 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002024 next.applyOptionsLocked();
2025 } else {
2026 next.clearOptionsLocked();
2027 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002028
Craig Mautnercf910b02013-04-23 11:23:27 -07002029 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002030 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002031 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002032
2033 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002034 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002035
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002036 // schedule launch ticks to collect information about slow apps.
2037 next.startLaunchTickingLocked();
2038
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002039 ActivityRecord lastResumedActivity =
2040 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002041 ActivityState lastState = next.state;
2042
2043 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002044
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002045 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002046 next.state = ActivityState.RESUMED;
2047 mResumedActivity = next;
2048 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002049 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002050 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002051 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002052 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002053
2054 // Have the window manager re-evaluate the orientation of
2055 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002056 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002057 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002058 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002059 mService.mConfiguration,
2060 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2061 if (config != null) {
2062 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002063 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002064 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002065 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002066
Craig Mautner525f3d92013-05-07 14:01:50 -07002067 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002068 // The configuration update wasn't able to keep the existing
2069 // instance of the activity, and instead started a new one.
2070 // We should be all done, but let's just make sure our activity
2071 // is still at the top and schedule another run if something
2072 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002073 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002074 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002075 "Activity config changed during resume: " + next
2076 + ", new next: " + nextNext);
2077 if (nextNext != next) {
2078 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002079 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002080 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002081 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002082 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002083 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002084 return true;
2085 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002086 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002087 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002088 }
Craig Mautner58547802013-03-05 08:23:53 -08002089
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002090 try {
2091 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002092 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002093 if (a != null) {
2094 final int N = a.size();
2095 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002096 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2097 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002098 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002099 }
2100 }
2101
2102 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002103 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 }
2105
Craig Mautner299f9602015-01-26 09:47:33 -08002106 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2107 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002108
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002109 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002110 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002111 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002112 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002113 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002114 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002115 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002116
Craig Mautner0eea92c2013-05-16 13:35:39 -07002117 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002118
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002119 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002120 } catch (Exception e) {
2121 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002122 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002123 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002124 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002125 if (lastStack != null) {
2126 lastStack.mResumedActivity = lastResumedActivity;
2127 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002128 Slog.i(TAG, "Restarting because process died: " + next);
2129 if (!next.hasBeenLaunched) {
2130 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002131 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2132 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002133 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002134 next.appToken, next.packageName, next.theme,
2135 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002136 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2137 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002138 }
George Mount2c92c972014-03-20 09:38:23 -07002139 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002140 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002141 return true;
2142 }
2143
2144 // From this point on, if something goes wrong there is no way
2145 // to recover the activity.
2146 try {
2147 next.visible = true;
2148 completeResumeLocked(next);
2149 } catch (Exception e) {
2150 // If any exception gets thrown, toss away this
2151 // activity and try the next one.
2152 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002153 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002154 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002155 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002156 return true;
2157 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002158 next.stopped = false;
2159
2160 } else {
2161 // Whoops, need to restart this activity!
2162 if (!next.hasBeenLaunched) {
2163 next.hasBeenLaunched = true;
2164 } else {
2165 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002166 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002167 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002168 mService.compatibilityInfoForPackageLocked(
2169 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002170 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002171 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002172 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002173 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002174 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002175 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002176 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002177 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002178 }
2179
Craig Mautnercf910b02013-04-23 11:23:27 -07002180 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002181 return true;
2182 }
2183
riddle_hsuc215a4f2014-12-27 12:10:45 +08002184 private TaskRecord getNextTask(TaskRecord targetTask) {
2185 final int index = mTaskHistory.indexOf(targetTask);
2186 if (index >= 0) {
2187 final int numTasks = mTaskHistory.size();
2188 for (int i = index + 1; i < numTasks; ++i) {
2189 TaskRecord task = mTaskHistory.get(i);
2190 if (task.userId == targetTask.userId) {
2191 return task;
2192 }
2193 }
2194 }
2195 return null;
2196 }
2197
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002198 private void insertTaskAtPosition(TaskRecord task, int position) {
2199 if (position >= mTaskHistory.size()) {
2200 insertTaskAtTop(task, null);
2201 return;
2202 }
2203 // Calculate maximum possible position for this task.
2204 int maxPosition = mTaskHistory.size();
2205 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002206 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002207 // Put non-current user tasks below current user tasks.
2208 while (maxPosition > 0) {
2209 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2210 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002211 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002212 break;
2213 }
2214 maxPosition--;
2215 }
2216 }
2217 position = Math.min(position, maxPosition);
2218 mTaskHistory.remove(task);
2219 mTaskHistory.add(position, task);
2220 updateTaskMovement(task, true);
2221 }
2222
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002223 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002224 // If the moving task is over home stack, transfer its return type to next task
2225 if (task.isOverHomeStack()) {
2226 final TaskRecord nextTask = getNextTask(task);
2227 if (nextTask != null) {
2228 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2229 }
2230 }
2231
Craig Mautner9c85c202013-10-04 20:11:26 -07002232 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002233 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002234 if (isOnHomeDisplay()) {
2235 ActivityStack lastStack = mStackSupervisor.getLastStack();
2236 final boolean fromHome = lastStack.isHomeStack();
2237 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002238 task.setTaskToReturnTo(fromHome
2239 ? lastStack.topTask() == null
2240 ? HOME_ACTIVITY_TYPE
2241 : lastStack.topTask().taskType
2242 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002243 }
2244 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002245 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002246 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002247
Craig Mautnerac6f8432013-07-17 13:24:59 -07002248 mTaskHistory.remove(task);
2249 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002250 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002251 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002252 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002253 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002254 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002255 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002256 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002257 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002258 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002259 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002260 break;
2261 }
2262 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002263 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002264 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002265 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002266 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002267 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002268
Craig Mautner8849a5e2013-04-02 16:41:03 -07002269 final void startActivityLocked(ActivityRecord r, boolean newTask,
Chong Zhang280d3322015-11-03 17:27:26 -08002270 boolean doResume, boolean keepCurTransition, ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002271 TaskRecord rTask = r.task;
2272 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002273 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2274 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002275 // Last activity in task had been removed or ActivityManagerService is reusing task.
2276 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002277 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002278 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002279 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002280 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002281 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002282 if (!newTask) {
2283 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002284 boolean startIt = true;
2285 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2286 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002287 if (task.getTopActivity() == null) {
2288 // All activities in task are finishing.
2289 continue;
2290 }
Craig Mautner70a86932013-02-28 22:37:44 -08002291 if (task == r.task) {
2292 // Here it is! Now, if this is not yet visible to the
2293 // user, then just add it without starting; it will
2294 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002295 if (!startIt) {
2296 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2297 + task, new RuntimeException("here").fillInStackTrace());
2298 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002299 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002300 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002301 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002302 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002303 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002304 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002305 return;
2306 }
2307 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002308 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002309 startIt = false;
2310 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002311 }
2312 }
2313
2314 // Place a new activity at top of stack, so it is next to interact
2315 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002316
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002317 // If we are not placing the new activity frontmost, we do not want
2318 // to deliver the onUserLeaving callback to the actual frontmost
2319 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002320 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002321 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002322 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002323 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002324 }
Craig Mautner70a86932013-02-28 22:37:44 -08002325
2326 task = r.task;
2327
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002328 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002329 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002330 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002331 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002332 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002333
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002334 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002335 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002336 // We want to show the starting preview window if we are
2337 // switching to a new task, or the next activity's process is
2338 // not currently running.
2339 boolean showStartingIcon = newTask;
2340 ProcessRecord proc = r.app;
2341 if (proc == null) {
2342 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2343 }
2344 if (proc == null || proc.thread == null) {
2345 showStartingIcon = true;
2346 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002347 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002349 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002350 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002351 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002352 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002353 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002354 ? r.mLaunchTaskBehind
2355 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2356 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002357 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002358 mNoAnimActivities.remove(r);
2359 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002360 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002361 boolean doShow = true;
2362 if (newTask) {
2363 // Even though this activity is starting fresh, we still need
2364 // to reset it to make sure we apply affinities to move any
2365 // existing activities from other tasks in to it.
2366 // If the caller has requested that the target task be
2367 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002368 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002369 resetTaskIfNeededLocked(r, r);
2370 doShow = topRunningNonDelayedActivityLocked(null) == r;
2371 }
Chong Zhang280d3322015-11-03 17:27:26 -08002372 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002373 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2374 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002375 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002376 if (r.mLaunchTaskBehind) {
2377 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2378 // tell WindowManager that r is visible even though it is at the back of the stack.
2379 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002380 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002381 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002382 // Figure out if we are transitioning from another activity that is
2383 // "has the same starting icon" as the next one. This allows the
2384 // window manager to keep the previous window it had previously
2385 // created, if it still had one.
2386 ActivityRecord prev = mResumedActivity;
2387 if (prev != null) {
2388 // We don't want to reuse the previous starting preview if:
2389 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002390 if (prev.task != r.task) {
2391 prev = null;
2392 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002393 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002394 else if (prev.nowVisible) {
2395 prev = null;
2396 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002397 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002398 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002399 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002400 mService.compatibilityInfoForPackageLocked(
2401 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002402 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002403 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002404 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002405 }
2406 } else {
2407 // If this is the first activity, don't do any fancy animations,
2408 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002409 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002410 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002411 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002412 }
2413 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002414 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002415 }
2416
2417 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002418 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002419 } else {
2420 addRecentActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002421 }
2422 }
2423
Dianne Hackbornbe707852011-11-11 14:32:10 -08002424 final void validateAppTokensLocked() {
2425 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002426 mValidateAppTokens.ensureCapacity(numActivities());
2427 final int numTasks = mTaskHistory.size();
2428 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2429 TaskRecord task = mTaskHistory.get(taskNdx);
2430 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002431 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002432 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002433 }
Craig Mautner000f0022013-02-26 15:04:29 -08002434 TaskGroup group = new TaskGroup();
2435 group.taskId = task.taskId;
2436 mValidateAppTokens.add(group);
2437 final int numActivities = activities.size();
2438 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2439 final ActivityRecord r = activities.get(activityNdx);
2440 group.tokens.add(r.appToken);
2441 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002442 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002443 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002444 }
2445
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002446 /**
2447 * Perform a reset of the given task, if needed as part of launching it.
2448 * Returns the new HistoryRecord at the top of the task.
2449 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002450 /**
2451 * Helper method for #resetTaskIfNeededLocked.
2452 * We are inside of the task being reset... we'll either finish this activity, push it out
2453 * for another task, or leave it as-is.
2454 * @param task The task containing the Activity (taskTop) that might be reset.
2455 * @param forceReset
2456 * @return An ActivityOptions that needs to be processed.
2457 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002458 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002459 ActivityOptions topOptions = null;
2460
2461 int replyChainEnd = -1;
2462 boolean canMoveOptions = true;
2463
2464 // We only do this for activities that are not the root of the task (since if we finish
2465 // the root, we may no longer have the task!).
2466 final ArrayList<ActivityRecord> activities = task.mActivities;
2467 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002468 final int rootActivityNdx = task.findEffectiveRootIndex();
2469 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002470 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002471 if (target.frontOfTask)
2472 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002473
2474 final int flags = target.info.flags;
2475 final boolean finishOnTaskLaunch =
2476 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2477 final boolean allowTaskReparenting =
2478 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2479 final boolean clearWhenTaskReset =
2480 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2481
2482 if (!finishOnTaskLaunch
2483 && !clearWhenTaskReset
2484 && target.resultTo != null) {
2485 // If this activity is sending a reply to a previous
2486 // activity, we can't do anything with it now until
2487 // we reach the start of the reply chain.
2488 // XXX note that we are assuming the result is always
2489 // to the previous activity, which is almost always
2490 // the case but we really shouldn't count on.
2491 if (replyChainEnd < 0) {
2492 replyChainEnd = i;
2493 }
2494 } else if (!finishOnTaskLaunch
2495 && !clearWhenTaskReset
2496 && allowTaskReparenting
2497 && target.taskAffinity != null
2498 && !target.taskAffinity.equals(task.affinity)) {
2499 // If this activity has an affinity for another
2500 // task, then we need to move it out of here. We will
2501 // move it as far out of the way as possible, to the
2502 // bottom of the activity stack. This also keeps it
2503 // correctly ordered with any activities we previously
2504 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002505 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002506 final ActivityRecord bottom =
2507 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002508 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002509 if (bottom != null && target.taskAffinity != null
2510 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002511 // If the activity currently at the bottom has the
2512 // same task affinity as the one we are moving,
2513 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002514 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002515 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002516 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002517 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002518 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002519 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002520 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002521 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002522 + " out to new task " + target.task);
2523 }
2524
Wale Ogunwale706ed792015-08-02 10:29:44 -07002525 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002526
Craig Mautner525f3d92013-05-07 14:01:50 -07002527 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002528 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2529 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002530 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002531 if (p.finishing) {
2532 continue;
2533 }
2534
Craig Mautnere3a74d52013-02-22 14:14:58 -08002535 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002536 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002537 topOptions = p.takeOptionsLocked();
2538 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002539 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002540 }
2541 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002542 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2543 "Removing activity " + p + " from task=" + task + " adding to task="
2544 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002545 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2546 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002547 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002548 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002549
Wale Ogunwale706ed792015-08-02 10:29:44 -07002550 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002551 }
2552
Wale Ogunwale706ed792015-08-02 10:29:44 -07002553 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002554 if (VALIDATE_TOKENS) {
2555 validateAppTokensLocked();
2556 }
2557
2558 replyChainEnd = -1;
2559 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2560 // If the activity should just be removed -- either
2561 // because it asks for it, or the task should be
2562 // cleared -- then finish it and anything that is
2563 // part of its reply chain.
2564 int end;
2565 if (clearWhenTaskReset) {
2566 // In this case, we want to finish this activity
2567 // and everything above it, so be sneaky and pretend
2568 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002569 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002570 } else if (replyChainEnd < 0) {
2571 end = i;
2572 } else {
2573 end = replyChainEnd;
2574 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002575 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002576 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002577 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002578 if (p.finishing) {
2579 continue;
2580 }
2581 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002582 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002583 topOptions = p.takeOptionsLocked();
2584 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002585 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002586 }
2587 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002588 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002589 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002590 if (finishActivityLocked(
2591 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002592 end--;
2593 srcPos--;
2594 }
2595 }
2596 replyChainEnd = -1;
2597 } else {
2598 // If we were in the middle of a chain, well the
2599 // activity that started it all doesn't want anything
2600 // special, so leave it all as-is.
2601 replyChainEnd = -1;
2602 }
2603 }
2604
2605 return topOptions;
2606 }
2607
2608 /**
2609 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2610 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2611 * @param affinityTask The task we are looking for an affinity to.
2612 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2613 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2614 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2615 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002616 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002617 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002618 int replyChainEnd = -1;
2619 final int taskId = task.taskId;
2620 final String taskAffinity = task.affinity;
2621
2622 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2623 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002624 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2625
2626 // Do not operate on or below the effective root Activity.
2627 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002628 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002629 if (target.frontOfTask)
2630 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002631
2632 final int flags = target.info.flags;
2633 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2634 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2635
2636 if (target.resultTo != null) {
2637 // If this activity is sending a reply to a previous
2638 // activity, we can't do anything with it now until
2639 // we reach the start of the reply chain.
2640 // XXX note that we are assuming the result is always
2641 // to the previous activity, which is almost always
2642 // the case but we really shouldn't count on.
2643 if (replyChainEnd < 0) {
2644 replyChainEnd = i;
2645 }
2646 } else if (topTaskIsHigher
2647 && allowTaskReparenting
2648 && taskAffinity != null
2649 && taskAffinity.equals(target.taskAffinity)) {
2650 // This activity has an affinity for our task. Either remove it if we are
2651 // clearing or move it over to our task. Note that
2652 // we currently punt on the case where we are resetting a
2653 // task that is not at the top but who has activities above
2654 // with an affinity to it... this is really not a normal
2655 // case, and we will need to later pull that task to the front
2656 // and usually at that point we will do the reset and pick
2657 // up those remaining activities. (This only happens if
2658 // someone starts an activity in a new task from an activity
2659 // in a task that is not currently on top.)
2660 if (forceReset || finishOnTaskLaunch) {
2661 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002662 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2663 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002664 for (int srcPos = start; srcPos >= i; --srcPos) {
2665 final ActivityRecord p = activities.get(srcPos);
2666 if (p.finishing) {
2667 continue;
2668 }
Todd Kennedy539db512014-12-15 09:57:55 -08002669 finishActivityLocked(
2670 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002671 }
2672 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002673 if (taskInsertionPoint < 0) {
2674 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002675
Craig Mautner77878772013-03-04 19:46:24 -08002676 }
Craig Mautner77878772013-03-04 19:46:24 -08002677
2678 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002679 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2680 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2681 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002682 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002683 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002684 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002685 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002686
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002687 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2688 "Removing and adding activity " + p + " to stack at " + task
2689 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002690 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2691 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002692 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002693 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002694 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002695 if (VALIDATE_TOKENS) {
2696 validateAppTokensLocked();
2697 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002698
2699 // Now we've moved it in to place... but what if this is
2700 // a singleTop activity and we have put it on top of another
2701 // instance of the same activity? Then we drop the instance
2702 // below so it remains singleTop.
2703 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2704 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002705 int targetNdx = taskActivities.indexOf(target);
2706 if (targetNdx > 0) {
2707 ActivityRecord p = taskActivities.get(targetNdx - 1);
2708 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002709 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2710 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002711 }
2712 }
2713 }
2714 }
2715
2716 replyChainEnd = -1;
2717 }
2718 }
Craig Mautner77878772013-03-04 19:46:24 -08002719 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002720 }
2721
Craig Mautner8849a5e2013-04-02 16:41:03 -07002722 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002723 ActivityRecord newActivity) {
2724 boolean forceReset =
2725 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2726 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2727 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2728 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2729 forceReset = true;
2730 }
2731 }
2732
2733 final TaskRecord task = taskTop.task;
2734
2735 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2736 * for remaining tasks. Used for later tasks to reparent to task. */
2737 boolean taskFound = false;
2738
2739 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2740 ActivityOptions topOptions = null;
2741
Craig Mautner77878772013-03-04 19:46:24 -08002742 // Preserve the location for reparenting in the new task.
2743 int reparentInsertionPoint = -1;
2744
Craig Mautnere3a74d52013-02-22 14:14:58 -08002745 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2746 final TaskRecord targetTask = mTaskHistory.get(i);
2747
2748 if (targetTask == task) {
2749 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2750 taskFound = true;
2751 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002752 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2753 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002754 }
2755 }
2756
Craig Mautner70a86932013-02-28 22:37:44 -08002757 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002758 if (taskNdx >= 0) {
2759 do {
2760 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2761 } while (taskTop == null && taskNdx >= 0);
2762 }
Craig Mautner70a86932013-02-28 22:37:44 -08002763
Craig Mautnere3a74d52013-02-22 14:14:58 -08002764 if (topOptions != null) {
2765 // If we got some ActivityOptions from an activity on top that
2766 // was removed from the task, propagate them to the new real top.
2767 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002768 taskTop.updateOptionsLocked(topOptions);
2769 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002770 topOptions.abort();
2771 }
2772 }
2773
2774 return taskTop;
2775 }
2776
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002777 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2778 String resultWho, int requestCode, int resultCode, Intent data) {
2779
2780 if (callingUid > 0) {
2781 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002782 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002783 }
2784
2785 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2786 + " : who=" + resultWho + " req=" + requestCode
2787 + " res=" + resultCode + " data=" + data);
2788 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2789 try {
2790 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2791 list.add(new ResultInfo(resultWho, requestCode,
2792 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002793 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002794 return;
2795 } catch (Exception e) {
2796 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2797 }
2798 }
2799
2800 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2801 }
2802
Craig Mautner299f9602015-01-26 09:47:33 -08002803 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002804 if (mStackSupervisor.isFocusedStack(this) && mService.mFocusedActivity == r) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002805 ActivityRecord next = topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002806 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002807 if (next != r) {
Wale Ogunwale3797c222015-10-27 14:21:58 -07002808 if (next != null && StackId.keepFocusInStackIfPossible(mStackId)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07002809 // For freeform, docked, and pinned stacks we always keep the focus within the
2810 // stack as long as there is a running activity in the stack that we can adjust
2811 // focus to.
Wale Ogunwale61803ee2015-08-07 19:59:47 -07002812 mService.setFocusedActivityLocked(next, myReason);
2813 return;
2814 } else {
2815 final TaskRecord task = r.task;
2816 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2817 // For non-fullscreen stack, we want to move the focus to the next visible
2818 // stack to prevent the home screen from moving to the top and obscuring
2819 // other visible stacks.
2820 if (!mFullscreen
2821 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2822 return;
2823 }
2824 // Move the home stack to the top if this stack is fullscreen or there is no
2825 // other visible stack.
2826 if (mStackSupervisor.moveHomeStackTaskToTop(
2827 task.getTaskToReturnTo(), myReason)) {
2828 // Activity focus was already adjusted. Nothing else to do...
2829 return;
2830 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002831 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002832 }
2833 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002834
2835 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002836 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002837 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002838 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002839 }
2840 }
2841
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002842 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2843 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2844 final String myReason = reason + " adjustFocusToNextVisibleStack";
2845 if (stack == null) {
2846 return false;
2847 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002848 final ActivityRecord top = stack.topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002849 if (top == null) {
2850 return false;
2851 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002852 mService.setFocusedActivityLocked(top, myReason);
2853 return true;
2854 }
2855
Craig Mautnerf3333272013-04-22 10:55:53 -07002856 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002857 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002858 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2859 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2860 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002861 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002862 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002863 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2864 "stop-no-history", false)) {
2865 // Activity was finished, no need to continue trying to schedule stop.
2866 adjustFocusedActivityLocked(r, "stopActivityFinished");
2867 r.resumeKeyDispatchingLocked();
2868 return;
2869 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002870 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002871 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002872 + " on stop because we're just sleeping");
2873 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002874 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002875 }
2876
2877 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002878 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002879 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002880 try {
2881 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002882 if (DEBUG_STATES) Slog.v(TAG_STATES,
2883 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002884 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002885 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2886 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002887 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002888 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002889 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002890 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002891 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002892 r.setSleeping(true);
2893 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002894 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002895 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002896 } catch (Exception e) {
2897 // Maybe just ignore exceptions here... if the process
2898 // has crashed, our death notification will clean things
2899 // up.
2900 Slog.w(TAG, "Exception thrown during pause", e);
2901 // Just in case, assume it to be stopped.
2902 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002903 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002904 r.state = ActivityState.STOPPED;
2905 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002906 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002907 }
2908 }
2909 }
2910 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002911
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002912 /**
2913 * @return Returns true if the activity is being finished, false if for
2914 * some reason it is being left as-is.
2915 */
2916 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002917 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002918 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002919 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2920 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002921 + ", result=" + resultCode + ", data=" + resultData
2922 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002923 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002924 return false;
2925 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002926
Craig Mautnerd44711d2013-02-23 11:24:36 -08002927 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002928 return true;
2929 }
2930
Craig Mautnerd2328952013-03-05 12:46:26 -08002931 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002932 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2933 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2934 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2935 ActivityRecord r = activities.get(activityNdx);
2936 if (r.resultTo == self && r.requestCode == requestCode) {
2937 if ((r.resultWho == null && resultWho == null) ||
2938 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2939 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2940 false);
2941 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002942 }
2943 }
2944 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002945 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002946 }
2947
Todd Kennedy539db512014-12-15 09:57:55 -08002948 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002949 ActivityRecord r = topRunningActivityLocked();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002950 if (r != null && r.app == app) {
2951 // If the top running activity is from this crashing
2952 // process, then terminate it to avoid getting in a loop.
2953 Slog.w(TAG, " Force finishing activity "
2954 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002955 int taskNdx = mTaskHistory.indexOf(r.task);
2956 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002957 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002958 // Also terminate any activities below it that aren't yet
2959 // stopped, to avoid a situation where one will get
2960 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002961 --activityNdx;
2962 if (activityNdx < 0) {
2963 do {
2964 --taskNdx;
2965 if (taskNdx < 0) {
2966 break;
2967 }
2968 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2969 } while (activityNdx < 0);
2970 }
2971 if (activityNdx >= 0) {
2972 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002973 if (r.state == ActivityState.RESUMED
2974 || r.state == ActivityState.PAUSING
2975 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002976 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002977 Slog.w(TAG, " Force finishing activity "
2978 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002979 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002980 }
2981 }
2982 }
2983 }
2984 }
2985
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002986 final void finishVoiceTask(IVoiceInteractionSession session) {
2987 IBinder sessionBinder = session.asBinder();
2988 boolean didOne = false;
2989 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2990 TaskRecord tr = mTaskHistory.get(taskNdx);
2991 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2992 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2993 ActivityRecord r = tr.mActivities.get(activityNdx);
2994 if (!r.finishing) {
2995 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2996 false);
2997 didOne = true;
2998 }
2999 }
3000 }
3001 }
3002 if (didOne) {
3003 mService.updateOomAdjLocked();
3004 }
3005 }
3006
Craig Mautnerd2328952013-03-05 12:46:26 -08003007 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003008 ArrayList<ActivityRecord> activities = r.task.mActivities;
3009 for (int index = activities.indexOf(r); index >= 0; --index) {
3010 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003011 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003012 break;
3013 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003014 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003015 }
3016 return true;
3017 }
3018
Dianne Hackborn5c607432012-02-28 14:44:19 -08003019 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3020 // send the result
3021 ActivityRecord resultTo = r.resultTo;
3022 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003023 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003024 + " who=" + r.resultWho + " req=" + r.requestCode
3025 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003026 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003027 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003028 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003029 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003030 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003031 if (r.info.applicationInfo.uid > 0) {
3032 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3033 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003034 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003035 }
3036 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3037 resultData);
3038 r.resultTo = null;
3039 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003040 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003041
3042 // Make sure this HistoryRecord is not holding on to other resources,
3043 // because clients have remote IPC references to this object so we
3044 // can't assume that will go away and want to avoid circular IPC refs.
3045 r.results = null;
3046 r.pendingResults = null;
3047 r.newIntents = null;
3048 r.icicle = null;
3049 }
3050
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003051 /**
3052 * @return Returns true if this activity has been removed from the history
3053 * list, or false if it is still in the list and will be removed later.
3054 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003055 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3056 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003057 if (r.finishing) {
3058 Slog.w(TAG, "Duplicate finish request for " + r);
3059 return false;
3060 }
3061
Wale Ogunwale7d701172015-03-11 15:36:30 -07003062 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003063 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003064 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003065 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003066 task.taskId, r.shortComponentName, reason);
3067 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003068 final int index = activities.indexOf(r);
3069 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003070 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003071 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003072 // If the caller asked that this activity (and all above it)
3073 // be cleared when the task is reset, don't lose that information,
3074 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003075 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003076 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003077 }
3078 }
3079
3080 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003081
Craig Mautner299f9602015-01-26 09:47:33 -08003082 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003083
Dianne Hackborn5c607432012-02-28 14:44:19 -08003084 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003085
Craig Mautnerde4ef022013-04-07 19:01:33 -07003086 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003087 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003088 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003089 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003090 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003091 ? AppTransition.TRANSIT_TASK_CLOSE
3092 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003093
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003094 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003095 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003096
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003097 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003098 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3099 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3100 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003101 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003102 }
3103
Craig Mautneraea74a52014-03-08 14:23:10 -08003104 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003105 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003106 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003107 } else if (r.state != ActivityState.PAUSING) {
3108 // If the activity is PAUSING, we will complete the finish once
3109 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003110 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003111 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003112 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003113 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003114 }
3115
3116 return false;
3117 }
3118
Craig Mautnerf3333272013-04-22 10:55:53 -07003119 static final int FINISH_IMMEDIATELY = 0;
3120 static final int FINISH_AFTER_PAUSE = 1;
3121 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003122
Craig Mautnerf3333272013-04-22 10:55:53 -07003123 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003124 // First things first: if this activity is currently visible,
3125 // and the resumed activity is not yet visible, then hold off on
3126 // finishing until the resumed one becomes visible.
3127 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003128 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
3129 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07003130 if (mStackSupervisor.mStoppingActivities.size() > 3
3131 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003132 // If we already have a few activities waiting to stop,
3133 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07003134 // them out. Or if r is the last of activity of the last task the stack
3135 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07003136 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003137 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003138 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003139 }
3140 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003141 if (DEBUG_STATES) Slog.v(TAG_STATES,
3142 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003143 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003144 if (oomAdj) {
3145 mService.updateOomAdjLocked();
3146 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003147 return r;
3148 }
3149
3150 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003151 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003152 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003153 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003154 if (mResumedActivity == r) {
3155 mResumedActivity = null;
3156 }
3157 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003158 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003159 r.state = ActivityState.FINISHING;
3160
3161 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003162 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003163 || prevState == ActivityState.STOPPED
3164 || prevState == ActivityState.INITIALIZING) {
3165 // If this activity is already stopped, we can just finish
3166 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003167 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003168 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003169 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003170 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003171 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003172 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003173 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3174 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003175 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003176 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003177
3178 // Need to go through the full pause cycle to get this
3179 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003180 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003181 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003182 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003183 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003184 return r;
3185 }
3186
Craig Mautneree36c772014-07-16 14:56:05 -07003187 void finishAllActivitiesLocked(boolean immediately) {
3188 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003189 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3190 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3191 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3192 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003193 noActivitiesInStack = false;
3194 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003195 continue;
3196 }
Craig Mautneree36c772014-07-16 14:56:05 -07003197 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003198 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3199 }
3200 }
Craig Mautneree36c772014-07-16 14:56:05 -07003201 if (noActivitiesInStack) {
3202 mActivityContainer.onTaskListEmptyLocked();
3203 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003204 }
3205
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003206 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3207 // Basic case: for simple app-centric recents, we need to recreate
3208 // the task if the affinity has changed.
3209 if (srec == null || srec.task.affinity == null ||
3210 !srec.task.affinity.equals(destAffinity)) {
3211 return true;
3212 }
3213 // Document-centric case: an app may be split in to multiple documents;
3214 // they need to re-create their task if this current activity is the root
3215 // of a document, unless simply finishing it will return them to the the
3216 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003217 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3218 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003219 // Okay, this activity is at the root of its task. What to do, what to do...
3220 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3221 // Finishing won't return to an application, so we need to recreate.
3222 return true;
3223 }
3224 // We now need to get the task below it to determine what to do.
3225 int taskIdx = mTaskHistory.indexOf(srec.task);
3226 if (taskIdx <= 0) {
3227 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3228 return false;
3229 }
3230 if (taskIdx == 0) {
3231 // At the bottom of the stack, nothing to go back to.
3232 return true;
3233 }
3234 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3235 if (!srec.task.affinity.equals(prevTask.affinity)) {
3236 // These are different apps, so need to recreate.
3237 return true;
3238 }
3239 }
3240 return false;
3241 }
3242
Wale Ogunwale7d701172015-03-11 15:36:30 -07003243 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003244 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003245 final TaskRecord task = srec.task;
3246 final ArrayList<ActivityRecord> activities = task.mActivities;
3247 final int start = activities.indexOf(srec);
3248 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003249 return false;
3250 }
3251 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003252 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003253 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003254 final ComponentName dest = destIntent.getComponent();
3255 if (start > 0 && dest != null) {
3256 for (int i = finishTo; i >= 0; i--) {
3257 ActivityRecord r = activities.get(i);
3258 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003259 r.info.name.equals(dest.getClassName())) {
3260 finishTo = i;
3261 parent = r;
3262 foundParentInTask = true;
3263 break;
3264 }
3265 }
3266 }
3267
3268 IActivityController controller = mService.mController;
3269 if (controller != null) {
3270 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3271 if (next != null) {
3272 // ask watcher if this is allowed
3273 boolean resumeOK = true;
3274 try {
3275 resumeOK = controller.activityResuming(next.packageName);
3276 } catch (RemoteException e) {
3277 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003278 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003279 }
3280
3281 if (!resumeOK) {
3282 return false;
3283 }
3284 }
3285 }
3286 final long origId = Binder.clearCallingIdentity();
3287 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003288 ActivityRecord r = activities.get(i);
3289 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003290 // Only return the supplied result for the first activity finished
3291 resultCode = Activity.RESULT_CANCELED;
3292 resultData = null;
3293 }
3294
3295 if (parent != null && foundParentInTask) {
3296 final int parentLaunchMode = parent.info.launchMode;
3297 final int destIntentFlags = destIntent.getFlags();
3298 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3299 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3300 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3301 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003302 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3303 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003304 } else {
3305 try {
3306 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3307 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003308 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003309 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003310 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003311 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003312 foundParentInTask = res == ActivityManager.START_SUCCESS;
3313 } catch (RemoteException e) {
3314 foundParentInTask = false;
3315 }
3316 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003317 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003318 }
3319 }
3320 Binder.restoreCallingIdentity(origId);
3321 return foundParentInTask;
3322 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003323 /**
3324 * Perform the common clean-up of an activity record. This is called both
3325 * as part of destroyActivityLocked() (when destroying the client-side
3326 * representation) and cleaning things up as a result of its hosting
3327 * processing going away, in which case there is no remaining client-side
3328 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003329 *
3330 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003331 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003332 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3333 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003334 if (mResumedActivity == r) {
3335 mResumedActivity = null;
3336 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003337 if (mPausingActivity == r) {
3338 mPausingActivity = null;
3339 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003340 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003341
3342 r.configDestroy = false;
3343 r.frozenBeforeDestroy = false;
3344
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003345 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003346 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003347 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003348 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003349 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003350 }
3351
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003352 // Make sure this record is no longer in the pending finishes list.
3353 // This could happen, for example, if we are trimming activities
3354 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003355 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003356 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003357
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003358 // Remove any pending results.
3359 if (r.finishing && r.pendingResults != null) {
3360 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3361 PendingIntentRecord rec = apr.get();
3362 if (rec != null) {
3363 mService.cancelIntentSenderLocked(rec, false);
3364 }
3365 }
3366 r.pendingResults = null;
3367 }
3368
3369 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003370 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003371 }
3372
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003373 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003374 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003375 if (getVisibleBehindActivity() == r) {
3376 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003377 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003378 }
3379
3380 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003381 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003382 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003383 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003384 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003385 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003386 }
3387
Craig Mautner299f9602015-01-26 09:47:33 -08003388 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003389 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003390 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003391 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003392 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3393 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3394
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003395 r.takeFromHistory();
3396 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003397 if (DEBUG_STATES) Slog.v(TAG_STATES,
3398 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003399 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003400 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003401 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003402 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003403 if (VALIDATE_TOKENS) {
3404 validateAppTokensLocked();
3405 }
Craig Mautner312ba862014-02-10 17:55:01 -08003406 final TaskRecord task = r.task;
3407 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003408 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003409 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003410 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003411 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003412 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003413 }
Craig Mautner299f9602015-01-26 09:47:33 -08003414 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003415 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003416 cleanUpActivityServicesLocked(r);
3417 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003418 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003419
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003420 /**
3421 * Perform clean-up of service connections in an activity record.
3422 */
3423 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3424 // Throw away any services that have been bound by this activity.
3425 if (r.connections != null) {
3426 Iterator<ConnectionRecord> it = r.connections.iterator();
3427 while (it.hasNext()) {
3428 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003429 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003430 }
3431 r.connections = null;
3432 }
3433 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003434
Craig Mautneree2e45a2014-06-27 12:10:03 -07003435 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003436 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003437 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003438 mHandler.sendMessage(msg);
3439 }
3440
Craig Mautneree2e45a2014-06-27 12:10:03 -07003441 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003442 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003443 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003444 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3445 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3446 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3447 final ActivityRecord r = activities.get(activityNdx);
3448 if (r.finishing) {
3449 continue;
3450 }
3451 if (r.fullscreen) {
3452 lastIsOpaque = true;
3453 }
3454 if (owner != null && r.app != owner) {
3455 continue;
3456 }
3457 if (!lastIsOpaque) {
3458 continue;
3459 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003460 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003461 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003462 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003463 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003464 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003465 activityRemoved = true;
3466 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003467 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003468 }
3469 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003470 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003471 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003472 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003473 }
3474
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003475 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3476 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003477 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3478 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003479 + " pausing=" + mPausingActivity + " for reason " + reason);
3480 return destroyActivityLocked(r, true, reason);
3481 }
3482 return false;
3483 }
3484
3485 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3486 String reason) {
3487 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003488 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003489 int maxTasks = tasks.size() / 4;
3490 if (maxTasks < 1) {
3491 maxTasks = 1;
3492 }
3493 int numReleased = 0;
3494 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3495 final TaskRecord task = mTaskHistory.get(taskNdx);
3496 if (!tasks.contains(task)) {
3497 continue;
3498 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003499 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003500 int curNum = 0;
3501 final ArrayList<ActivityRecord> activities = task.mActivities;
3502 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3503 final ActivityRecord activity = activities.get(actNdx);
3504 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003505 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003506 + " in state " + activity.state + " resumed=" + mResumedActivity
3507 + " pausing=" + mPausingActivity + " for reason " + reason);
3508 destroyActivityLocked(activity, true, reason);
3509 if (activities.get(actNdx) != activity) {
3510 // Was removed from list, back up so we don't miss the next one.
3511 actNdx--;
3512 }
3513 curNum++;
3514 }
3515 }
3516 if (curNum > 0) {
3517 numReleased += curNum;
3518 maxTasks--;
3519 if (mTaskHistory.get(taskNdx) != task) {
3520 // The entire task got removed, back up so we don't miss the next one.
3521 taskNdx--;
3522 }
3523 }
3524 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003525 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3526 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003527 return numReleased;
3528 }
3529
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003530 /**
3531 * Destroy the current CLIENT SIDE instance of an activity. This may be
3532 * called both when actually finishing an activity, or when performing
3533 * a configuration switch where we destroy the current client-side object
3534 * but then create a new client-side object for this same HistoryRecord.
3535 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003536 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003537 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3538 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003539 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003540 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003541 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003542 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003543
3544 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003545
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003546 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003547
3548 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003549
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003550 if (hadApp) {
3551 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003552 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003553 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3554 mService.mHeavyWeightProcess = null;
3555 mService.mHandler.sendEmptyMessage(
3556 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3557 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003558 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003559 // Update any services we are bound to that might care about whether
3560 // their client may have activities.
3561 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003562 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003563 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003564 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003565 }
3566 }
3567
3568 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003569
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003570 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003571 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003572 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003573 r.configChangeFlags);
3574 } catch (Exception e) {
3575 // We can just ignore exceptions here... if the process
3576 // has crashed, our death notification will clean things
3577 // up.
3578 //Slog.w(TAG, "Exception thrown during finish", e);
3579 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003580 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003581 removedFromHistory = true;
3582 skipDestroy = true;
3583 }
3584 }
3585
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003586 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003587
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003588 // If the activity is finishing, we need to wait on removing it
3589 // from the list to give it a chance to do its cleanup. During
3590 // that time it may make calls back with its token so we need to
3591 // be able to find it on the list and so we don't want to remove
3592 // it from the list yet. Otherwise, we can just immediately put
3593 // it in the destroyed state since we are not removing it from the
3594 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003595 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003596 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003597 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003598 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003599 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003600 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3601 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003602 if (DEBUG_STATES) Slog.v(TAG_STATES,
3603 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003604 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003605 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003606 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003607 }
3608 } else {
3609 // remove this record from the history.
3610 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003611 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003612 removedFromHistory = true;
3613 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003614 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003615 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003616 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003617 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003618 }
3619 }
3620
3621 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003622
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003623 if (!mLRUActivities.remove(r) && hadApp) {
3624 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3625 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003626
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003627 return removedFromHistory;
3628 }
3629
Craig Mautner299f9602015-01-26 09:47:33 -08003630 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003631 final long origId = Binder.clearCallingIdentity();
3632 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003633 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003634 if (r != null) {
3635 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003636 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003637 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003638
Wale Ogunwale60454db2015-01-23 16:05:07 -08003639 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003640 if (r.state == ActivityState.DESTROYING) {
3641 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003642 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003643 }
3644 }
Craig Mautner05d29032013-05-03 13:40:13 -07003645 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003646 } finally {
3647 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003648 }
3649 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003650
Todd Kennedyaab56db2015-01-30 09:39:53 -08003651 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003652 if (hasVisibleBehindActivity() &&
3653 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003654 if (r == topRunningActivityLocked()) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003655 // Don't release the top activity if it has requested to run behind the next
3656 // activity.
3657 return;
3658 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003659 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003660 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003661 " thread=" + r.app.thread);
3662 if (r != null && r.app != null && r.app.thread != null) {
3663 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003664 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003665 } catch (RemoteException e) {
3666 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003667 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003668 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003669 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003670 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003671 }
3672 }
3673 }
3674
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003675 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003676 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3677 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003678 if (r != null) {
3679 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003680 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003681 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003682 }
3683 mStackSupervisor.resumeTopActivitiesLocked();
3684 }
3685
Jose Lima4b6c6692014-08-12 17:41:12 -07003686 boolean hasVisibleBehindActivity() {
3687 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003688 }
3689
Jose Lima4b6c6692014-08-12 17:41:12 -07003690 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003691 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003692 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003693 }
3694 }
3695
Jose Lima4b6c6692014-08-12 17:41:12 -07003696 ActivityRecord getVisibleBehindActivity() {
3697 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003698 }
3699
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003700 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3701 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003702 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003703 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3704 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003705 while (i > 0) {
3706 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003707 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003708 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003709 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003710 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003711 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003712 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003713 }
3714 }
3715 }
3716
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003717 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3718 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003719 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3720 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003721 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3722 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003723 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003724 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003725 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3726 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003727
3728 boolean hasVisibleActivities = false;
3729
3730 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003731 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003732 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3733 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003734 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3735 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3736 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3737 final ActivityRecord r = activities.get(activityNdx);
3738 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003739 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3740 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003741 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003742 if (r.visible) {
3743 hasVisibleActivities = true;
3744 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003745 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003746 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3747 // Don't currently have state for the activity, or
3748 // it is finishing -- always remove it.
3749 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003750 } else if (!r.visible && r.launchCount > 2 &&
3751 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003752 // We have launched this activity too many times since it was
3753 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003754 // (Note if the activity is visible, we don't remove the record.
3755 // We leave the dead window on the screen but the process will
3756 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003757 remove = true;
3758 } else {
3759 // The process may be gone, but the activity lives on!
3760 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003761 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003762 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003763 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3764 "Removing activity " + r + " from stack at " + i
3765 + ": haveState=" + r.haveState
3766 + " stateNotNeeded=" + r.stateNotNeeded
3767 + " finishing=" + r.finishing
3768 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003769 if (!r.finishing) {
3770 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3771 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3772 r.userId, System.identityHashCode(r),
3773 r.task.taskId, r.shortComponentName,
3774 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003775 if (r.state == ActivityState.RESUMED) {
3776 mService.updateUsageStats(r, false);
3777 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003778 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003779 } else {
3780 // We have the current state for this activity, so
3781 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003782 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3783 if (DEBUG_APP) Slog.v(TAG_APP,
3784 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003785 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003786 // Set nowVisible to previous visible state. If the app was visible while
3787 // it died, we leave the dead window on screen so it's basically visible.
3788 // This is needed when user later tap on the dead window, we need to stop
3789 // other apps when user transfers focus to the restarted activity.
3790 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003791 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003792 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003793 "App died, clearing saved state of " + r);
3794 r.icicle = null;
3795 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003796 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003797 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003798 if (remove) {
3799 removeActivityFromHistoryLocked(r, "appDied");
3800 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003801 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003802 }
3803 }
3804
3805 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003806 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003807
Chong Zhang280d3322015-11-03 17:27:26 -08003808 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003809 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003810 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003811 if (r != null && r.state != ActivityState.RESUMED) {
3812 r.updateOptionsLocked(options);
3813 } else {
3814 ActivityOptions.abort(options);
3815 }
3816 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003817 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003818 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003819
Craig Mautner21d24a22014-04-23 11:45:37 -07003820 void updateTaskMovement(TaskRecord task, boolean toFront) {
3821 if (task.isPersistable) {
3822 task.mLastTimeMoved = System.currentTimeMillis();
3823 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3824 // recently will be most negative, tasks sent to the bottom before that will be less
3825 // negative. Similarly for recent tasks moved to the top which will be most positive.
3826 if (!toFront) {
3827 task.mLastTimeMoved *= -1;
3828 }
3829 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003830 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003831 }
3832
Craig Mautner84984fa2014-06-19 11:19:20 -07003833 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003834 final int top = mTaskHistory.size() - 1;
3835 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3836 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003837 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003838 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3839 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003840 mTaskHistory.remove(taskNdx);
3841 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003842 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003843 return;
3844 }
3845 }
3846 }
3847
Chong Zhang280d3322015-11-03 17:27:26 -08003848 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003849 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003850 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003851
Craig Mautner11bf9a52013-02-19 14:08:51 -08003852 final int numTasks = mTaskHistory.size();
3853 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003854 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003855 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003856 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003857 ActivityOptions.abort(options);
3858 } else {
3859 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3860 }
3861 return;
3862 }
3863
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003864 if (timeTracker != null) {
3865 // The caller wants a time tracker associated with this task.
3866 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3867 tr.mActivities.get(i).appTimeTracker = timeTracker;
3868 }
3869 }
3870
Craig Mautner11bf9a52013-02-19 14:08:51 -08003871 // Shift all activities with this task up to the top
3872 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003873 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003874
Chong Zhang45c25ce2015-08-10 22:18:26 -07003875 // Don't refocus if invisible to current user
3876 ActivityRecord top = tr.getTopActivity();
3877 if (!okToShowLocked(top)) {
3878 addRecentActivityLocked(top);
3879 ActivityOptions.abort(options);
3880 return;
3881 }
3882
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003883 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003884 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003885 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003886
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003887 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003888 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003889 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003890 if (r != null) {
3891 mNoAnimActivities.add(r);
3892 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003893 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003894 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003895 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003896 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003897
Craig Mautner05d29032013-05-03 13:40:13 -07003898 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003899 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003900
3901 if (VALIDATE_TOKENS) {
3902 validateAppTokensLocked();
3903 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003904 }
3905
3906 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003907 * Worker method for rearranging history stack. Implements the function of moving all
3908 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003909 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003910 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003911 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3912 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003913 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003914 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003915 * @return Returns true if the move completed, false if not.
3916 */
Craig Mautner299f9602015-01-26 09:47:33 -08003917 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003918 final TaskRecord tr = taskForIdLocked(taskId);
3919 if (tr == null) {
3920 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3921 return false;
3922 }
3923
3924 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003925 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003926
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003927 // If we have a watcher, preflight the move before committing to it. First check
3928 // for *other* available tasks, but if none are available, then try again allowing the
3929 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003930 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003931 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003932 if (next == null) {
3933 next = topRunningActivityLocked(null, 0);
3934 }
3935 if (next != null) {
3936 // ask watcher if this is allowed
3937 boolean moveOK = true;
3938 try {
3939 moveOK = mService.mController.activityResuming(next.packageName);
3940 } catch (RemoteException e) {
3941 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003942 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003943 }
3944 if (!moveOK) {
3945 return false;
3946 }
3947 }
3948 }
3949
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003950 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003951
riddle_hsuc215a4f2014-12-27 12:10:45 +08003952 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07003953
3954 // If true, we should resume the home activity next if the task we are moving to the
3955 // back is over the home stack. We force to false if the task we are moving to back
3956 // is the home task and we don't want it resumed after moving to the back.
3957 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
3958 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08003959 final TaskRecord nextTask = getNextTask(tr);
3960 if (nextTask != null) {
3961 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3962 } else {
3963 prevIsHome = true;
3964 }
3965 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003966 mTaskHistory.remove(tr);
3967 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003968 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003969
Craig Mautnerc8143c62013-09-03 12:15:57 -07003970 // There is an assumption that moving a task to the back moves it behind the home activity.
3971 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003972 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003973 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3974 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003975 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003976 break;
3977 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003978 if (taskNdx == 1) {
3979 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003980 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003981 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003982 }
3983
Craig Mautner299f9602015-01-26 09:47:33 -08003984 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003985 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003986
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003987 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003988 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003989 }
3990
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003991 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07003992 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003993 if (!mService.mBooting && !mService.mBooted) {
3994 // Not ready yet!
3995 return false;
3996 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003997 final int taskToReturnTo = tr.getTaskToReturnTo();
3998 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003999 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004000 }
4001
Craig Mautner05d29032013-05-03 13:40:13 -07004002 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004003 return true;
4004 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004005
Craig Mautner8849a5e2013-04-02 16:41:03 -07004006 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004007 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004008 final Uri data = r.intent.getData();
4009 final String strData = data != null ? data.toSafeString() : null;
4010
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004011 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004012 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004013 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004014 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004015 }
4016
4017 /**
4018 * Make sure the given activity matches the current configuration. Returns
4019 * false if the activity had to be destroyed. Returns true if the
4020 * configuration is the same, or the activity will remain running as-is
4021 * for whatever reason. Ensures the HistoryRecord is updated with the
4022 * correct configuration and all other bookkeeping is handled.
4023 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004024 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4025 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004026 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004027 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004028 "Skipping config check (will change): " + r);
4029 return true;
4030 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004031
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004032 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004033 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004034
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004035 // Short circuit: if the two configurations are the exact same
4036 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004037 final Configuration newConfig = mService.mConfiguration;
4038 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004039 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004040 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004041 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004042 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004043 "Configuration unchanged in " + r);
4044 return true;
4045 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004046
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004047 // We don't worry about activities that are finishing.
4048 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004049 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004050 "Configuration doesn't matter in finishing " + r);
4051 r.stopFreezingScreenLocked(false);
4052 return true;
4053 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004054
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004055 // Okay we now are going to make this activity have the new config.
4056 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004057 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004058 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004059 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004060 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004061
Filip Gruszczynski23493322015-07-29 17:02:59 -07004062 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004063 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004064 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004065 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004066 "Configuration no differences in " + r);
4067 return true;
4068 }
4069
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004070 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4071 "Configuration changes for " + r + " ; taskChanges="
4072 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4073 + Configuration.configurationDiffToString(changes));
4074
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004075 // If the activity isn't currently running, just leave the new
4076 // configuration and it will pick that up next time it starts.
4077 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004078 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004079 "Configuration doesn't matter not running " + r);
4080 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004081 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004082 return true;
4083 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004084
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004085 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004086 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4087 "Checking to restart " + r.info.name + ": changed=0x"
4088 + Integer.toHexString(changes) + ", handles=0x"
4089 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
4090
Dianne Hackborne6676352011-06-01 16:51:20 -07004091 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004092 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4093 r.configChangeFlags |= changes;
4094 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004095 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004096 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004097 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004098 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004099 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004100 } else if (r.state == ActivityState.PAUSING) {
4101 // A little annoying: we are waiting for this activity to
4102 // finish pausing. Let's not do anything now, but just
4103 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004104 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004105 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004106 r.configDestroy = true;
4107 return true;
4108 } else if (r.state == ActivityState.RESUMED) {
4109 // Try to optimize this case: the configuration is changing
4110 // and we need to restart the top, resumed activity.
4111 // Instead of doing the normal handshaking, just say
4112 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004113 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004114 "Config is relaunching resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004115 relaunchActivityLocked(r, r.configChangeFlags, true,
4116 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004117 r.configChangeFlags = 0;
4118 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004119 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004120 "Config is relaunching non-resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004121 relaunchActivityLocked(r, r.configChangeFlags, false,
4122 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004123 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004124 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004125
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004126 // All done... tell the caller we weren't able to keep this
4127 // activity around.
4128 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004129 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004130
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004131 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004132 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004133 // changes is always sent to all processes when they happen so it can just use whatever
4134 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004135 if (r.app != null && r.app.thread != null) {
4136 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004137 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004138 r.app.thread.scheduleActivityConfigurationChanged(
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004139 r.appToken, new Configuration(taskConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004140 } catch (RemoteException e) {
4141 // If process died, whatever.
4142 }
4143 }
4144 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004145
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004146 return true;
4147 }
4148
Filip Gruszczynski23493322015-07-29 17:02:59 -07004149 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4150 Configuration oldTaskOverride) {
4151 // Determine what has changed. May be nothing, if this is a config
4152 // that has come back from the app after going idle. In that case
4153 // we just want to leave the official config object now in the
4154 // activity and do nothing else.
4155 int taskChanges = oldTaskOverride.diff(taskConfig);
4156 // We don't want to use size changes if they don't cross boundaries that are important to
4157 // the app.
4158 if ((taskChanges & ActivityInfo.CONFIG_SCREEN_SIZE) != 0) {
4159 final boolean crosses = record.crossesHorizontalSizeThreshold(
4160 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4161 || record.crossesVerticalSizeThreshold(
4162 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4163 if (!crosses) {
4164 taskChanges &= ~ActivityInfo.CONFIG_SCREEN_SIZE;
4165 }
4166 }
4167 if ((taskChanges & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4168 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4169 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004170 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004171 taskChanges &= ~ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4172 }
4173 }
4174 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4175 }
4176
4177 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4178 Configuration oldTaskOverride, int taskChanges) {
4179 if (taskChanges == 0) {
4180 // {@link Configuration#diff} doesn't catch changes from unset values.
4181 // Check for changes we care about.
4182 if (oldTaskOverride.orientation != taskConfig.orientation) {
4183 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
4184 }
4185 // We want to explicitly track situations where the size configuration goes from
4186 // undefined to defined. We don't care about crossing the threshold in that case,
4187 // because there is no threshold.
4188 final int oldHeight = oldTaskOverride.screenHeightDp;
4189 final int newHeight = taskConfig.screenHeightDp;
4190 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4191 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4192 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4193 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4194 }
4195 final int oldWidth = oldTaskOverride.screenWidthDp;
4196 final int newWidth = taskConfig.screenWidthDp;
4197 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4198 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4199 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4200 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4201 }
4202 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4203 final int newSmallest = taskConfig.smallestScreenWidthDp;
4204 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4205 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4206 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4207 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4208 }
4209 }
4210 return taskChanges;
4211 }
4212
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004213 private static boolean isResizeOnlyChange(int change) {
4214 return (change & ~(ActivityInfo.CONFIG_SCREEN_SIZE
4215 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_ORIENTATION)) == 0;
4216 }
4217
Wale Ogunwale83301a92015-09-24 15:54:08 -07004218 private void relaunchActivityLocked(
4219 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4220 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
4221 return;
4222 }
4223
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004224 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004225 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004226 if (andResume) {
4227 results = r.results;
4228 newIntents = r.newIntents;
4229 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004230 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4231 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004232 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004233 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004234 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004235 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004236
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004237 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004238
Craig Mautner34b73df2014-01-12 21:11:08 -08004239 mStackSupervisor.removeChildActivityContainers(r);
4240
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004241 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004242 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4243 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004244 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004245 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4246 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004247 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004248 // Note: don't need to call pauseIfSleepingLocked() here, because
4249 // the caller will only pass in 'andResume' if this activity is
4250 // currently resumed, which implies we aren't sleeping.
4251 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004252 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004253 }
4254
4255 if (andResume) {
4256 r.results = null;
4257 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004258 r.state = ActivityState.RESUMED;
4259 } else {
4260 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4261 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004262 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004263 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004264
4265 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004266 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4267 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4268 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4269 final ActivityRecord r = activities.get(activityNdx);
4270 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004271 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004272 }
4273 if (r.fullscreen && !r.finishing) {
4274 return false;
4275 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004276 }
4277 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004278 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004279 if (r == null) {
4280 return false;
4281 }
4282 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4283 + " would have returned true for r=" + r);
4284 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004285 }
4286
4287 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004288 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4289 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4290 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4291 final ActivityRecord r = activities.get(activityNdx);
4292 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004293 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004294 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004295 }
4296 }
4297 }
4298
Wale Ogunwale540e1232015-05-01 15:35:39 -07004299 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4300 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004301 boolean didSomething = false;
4302 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004303 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004304 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4305 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4306 int numActivities = activities.size();
4307 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4308 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004309 final boolean sameComponent =
4310 (r.packageName.equals(packageName) && (filterByClasses == null
4311 || filterByClasses.contains(r.realActivity.getClassName())))
4312 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004313 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004314 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004315 && (r.app == null || evenPersistent || !r.app.persistent)) {
4316 if (!doit) {
4317 if (r.finishing) {
4318 // If this activity is just finishing, then it is not
4319 // interesting as far as something to stop.
4320 continue;
4321 }
4322 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004323 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004324 if (r.isHomeActivity()) {
4325 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4326 Slog.i(TAG, "Skip force-stop again " + r);
4327 continue;
4328 } else {
4329 homeActivity = r.realActivity;
4330 }
4331 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004332 didSomething = true;
4333 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004334 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004335 if (r.app != null) {
4336 r.app.removed = true;
4337 }
4338 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004339 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004340 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004341 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4342 true)) {
4343 // r has been deleted from mActivities, accommodate.
4344 --numActivities;
4345 --activityNdx;
4346 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004347 }
4348 }
4349 }
4350 return didSomething;
4351 }
4352
Dianne Hackborn09233282014-04-30 11:33:59 -07004353 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004354 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4355 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004356 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004357 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004358 if (task.getTopActivity() == null) {
4359 continue;
4360 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004361 ActivityRecord r = null;
4362 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004363 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004364 int numActivities = 0;
4365 int numRunning = 0;
4366 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004367 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004368 continue;
4369 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004370 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004371 tmp = activities.get(activityNdx);
4372 if (tmp.finishing) {
4373 continue;
4374 }
4375 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004376
Craig Mautneraab647e2013-02-28 16:31:36 -08004377 // Initialize state for next task if needed.
4378 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4379 top = r;
4380 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004381 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004382
4383 // Add 'r' into the current task.
4384 numActivities++;
4385 if (r.app != null && r.app.thread != null) {
4386 numRunning++;
4387 }
4388
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004389 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004390 TAG, r.intent.getComponent().flattenToShortString()
4391 + ": task=" + r.task);
4392 }
4393
4394 RunningTaskInfo ci = new RunningTaskInfo();
4395 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004396 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004397 ci.baseActivity = r.intent.getComponent();
4398 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004399 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004400 if (focusedStack && topTask) {
4401 // Give the latest time to ensure foreground task can be sorted
4402 // at the first, because lastActiveTime of creating task is 0.
4403 ci.lastActiveTime = System.currentTimeMillis();
4404 topTask = false;
4405 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004406
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004407 if (top.task != null) {
4408 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004409 }
4410 ci.numActivities = numActivities;
4411 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004412 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004413 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004414 }
4415
4416 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004417 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004418 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004419 if (top >= 0) {
4420 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4421 int activityTop = activities.size() - 1;
4422 if (activityTop > 0) {
4423 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4424 "unhandled-back", true);
4425 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004426 }
4427 }
4428
Craig Mautner6b74cb52013-09-27 17:02:21 -07004429 /**
4430 * Reset local parameters because an app's activity died.
4431 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004432 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004433 */
4434 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004435 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004436 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004437 "App died while pausing: " + mPausingActivity);
4438 mPausingActivity = null;
4439 }
4440 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4441 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004442 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004443 }
4444
Craig Mautner19091252013-10-05 00:03:53 -07004445 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004446 }
4447
Craig Mautnercae015f2013-02-08 14:31:27 -08004448 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004449 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4450 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4451 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4452 final ActivityRecord r = activities.get(activityNdx);
4453 if (r.app == app) {
4454 Slog.w(TAG, " Force finishing activity "
4455 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004456 // Force the destroy to skip right to removal.
4457 r.app = null;
4458 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004459 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004460 }
4461 }
4462 }
4463
Dianne Hackborn390517b2013-05-30 15:03:32 -07004464 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004465 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004466 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004467 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4468 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004469 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4470 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004471 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004472 " Task id #" + task.taskId + "\n" +
4473 " mFullscreen=" + task.mFullscreen + "\n" +
4474 " mBounds=" + task.mBounds + "\n" +
4475 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004476 if (printed) {
4477 header = null;
4478 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004479 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004480 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004481 }
4482
4483 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4484 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4485
4486 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004487 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4488 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004489 }
4490 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004491 final int top = mTaskHistory.size() - 1;
4492 if (top >= 0) {
4493 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4494 int listTop = list.size() - 1;
4495 if (listTop >= 0) {
4496 activities.add(list.get(listTop));
4497 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004498 }
4499 } else {
4500 ItemMatcher matcher = new ItemMatcher();
4501 matcher.build(name);
4502
Craig Mautneraab647e2013-02-28 16:31:36 -08004503 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4504 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4505 if (matcher.match(r1, r1.intent.getComponent())) {
4506 activities.add(r1);
4507 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004508 }
4509 }
4510 }
4511
4512 return activities;
4513 }
4514
4515 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004516 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004517
4518 // All activities that came from the package must be
4519 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004520 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4521 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4522 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4523 final ActivityRecord a = activities.get(activityNdx);
4524 if (a.info.packageName.equals(packageName)) {
4525 a.forceNewConfig = true;
4526 if (starting != null && a == starting && a.visible) {
4527 a.startFreezingScreenLocked(starting.app,
4528 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4529 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004530 }
4531 }
4532 }
4533
4534 return starting;
4535 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004536
Craig Mautner299f9602015-01-26 09:47:33 -08004537 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004538 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004539 }
4540
Wale Ogunwale000957c2015-04-03 08:19:12 -07004541 /**
4542 * Removes the input task from this stack.
4543 * @param task to remove.
4544 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004545 * @param moving task to another stack. In the case we are moving we don't want to perform
4546 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004547 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004548 void removeTask(TaskRecord task, String reason, boolean moving) {
4549 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004550 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004551 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004552 if (!StackId.persistTaskBounds(mStackId)) {
4553 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4554 // default configuration the next time it launches.
4555 task.updateOverrideConfiguration(null);
4556 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004557 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004558
Craig Mautner04a0ea62014-01-13 12:51:26 -08004559 final ActivityRecord r = mResumedActivity;
4560 if (r != null && r.task == task) {
4561 mResumedActivity = null;
4562 }
4563
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004564 final int taskNdx = mTaskHistory.indexOf(task);
4565 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004566 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4567 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4568 if (!nextTask.isOverHomeStack()) {
4569 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4570 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004571 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004572 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004573 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004574
Wale Ogunwale040b4702015-08-06 18:10:50 -07004575 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004576 final boolean isVoiceSession = task.voiceSession != null;
4577 if (isVoiceSession) {
4578 try {
4579 task.voiceSession.taskFinished(task.intent, task.taskId);
4580 } catch (RemoteException e) {
4581 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004582 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004583 if (task.autoRemoveFromRecents() || isVoiceSession) {
4584 // Task creator asked to remove this when done, or this task was a voice
4585 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004586 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004587 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004588 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004589 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004590
4591 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004592 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004593 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004594 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004595 String myReason = reason + " leftTaskHistoryEmpty";
4596 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004597 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004598 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004599 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004600 if (mStacks != null) {
4601 mStacks.remove(this);
4602 mStacks.add(0, this);
4603 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004604 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004605 mActivityContainer.onTaskListEmptyLocked();
4606 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004607 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004608
4609 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004610 }
4611
Dianne Hackborn91097de2014-04-04 18:02:06 -07004612 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4613 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4614 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004615 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4616 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004617 // add the task to stack first, mTaskPositioner might need the stack association
4618 addTask(task, toTop, false);
Wale Ogunwale935e5022015-11-10 12:36:10 -08004619 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.mResizeable) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004620 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004621 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004622 return task;
4623 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004624
Wale Ogunwale935e5022015-11-10 12:36:10 -08004625 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4626 if (mTaskPositioner == null) {
4627 return false;
4628 }
4629 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4630 return true;
4631 }
4632
Craig Mautnerc00204b2013-03-05 15:02:14 -08004633 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004634 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004635 }
4636
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004637 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004638 task.stack = this;
4639 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004640 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004641 } else {
4642 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004643 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004644 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004645 if (!moving && task.voiceSession != null) {
4646 try {
4647 task.voiceSession.taskStarted(task.intent, task.taskId);
4648 } catch (RemoteException e) {
4649 }
4650 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004651 }
4652
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004653 void positionTask(final TaskRecord task, int position, boolean moving) {
4654 task.stack = this;
4655 insertTaskAtPosition(task, position);
4656 if (!moving && task.voiceSession != null) {
4657 try {
4658 task.voiceSession.taskStarted(task.intent, task.taskId);
4659 } catch (RemoteException e) {
4660 }
4661 }
4662 }
4663
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004664 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07004665 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004666 task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004667 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4668 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
4669 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
4670 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
Wale Ogunwale61b009e2015-09-16 15:43:05 -07004671 bounds, task.mOverrideConfig, !r.isHomeActivity());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004672 r.taskConfigOverride = task.mOverrideConfig;
4673 }
4674
Wale Ogunwale079a0042015-10-24 11:44:07 -07004675 void setFocusAndResumeStateIfNeeded(
4676 ActivityRecord r, boolean setFocus, boolean setResume, String reason) {
4677 // If the activity had focus before move focus to this stack.
4678 if (setFocus) {
4679 // If the activity owns the last resumed activity, transfer that together,
4680 // so that we don't resume the same activity again in the new stack.
4681 // Apps may depend on onResume()/onPause() being called in pairs.
4682 if (setResume) {
4683 mResumedActivity = r;
4684 // Move the stack in which we are placing the activity to the front. We don't use
4685 // ActivityManagerService.setFocusedActivityLocked, because if the activity is
4686 // already focused, the call will short-circuit and do nothing.
4687 moveToFront(reason);
4688 } else {
4689 // We need to not only move the stack to the front, but also have the activity
4690 // focused. This will achieve both goals.
4691 mService.setFocusedActivityLocked(r, reason);
4692 }
4693 }
4694 }
4695
4696 /**
4697 * Moves the input activity from its current stack to this one.
4698 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4699 * created on this stack which the activity is added to.
4700 * */
4701 void moveActivityToStack(ActivityRecord r) {
4702 final ActivityStack prevStack = r.task.stack;
4703 if (prevStack.mStackId == mStackId) {
4704 // You are already in the right stack silly...
4705 return;
4706 }
4707
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004708 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004709 && (mStackSupervisor.topRunningActivityLocked() == r);
4710 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4711
4712 final TaskRecord task = createTaskRecord(
4713 mStackSupervisor.getNextTaskId(), r.info, r.intent, null, null, true);
4714 r.setTask(task, null);
4715 task.addActivityToTop(r);
4716 setAppTask(r, task);
4717 setFocusAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4718 }
4719
Wale Ogunwale706ed792015-08-02 10:29:44 -07004720 private void setAppTask(ActivityRecord r, TaskRecord task) {
4721 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004722 task.updateOverrideConfiguration(bounds);
4723 mWindowManager.setAppTask(
4724 r.appToken, task.taskId, task.getLaunchBounds(), task.mOverrideConfig);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004725 r.taskConfigOverride = task.mOverrideConfig;
4726 }
4727
Craig Mautnerc00204b2013-03-05 15:02:14 -08004728 public int getStackId() {
4729 return mStackId;
4730 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004731
4732 @Override
4733 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004734 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4735 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004736 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004737
Craig Mautner15df08a2015-04-01 12:17:18 -07004738 void onLockTaskPackagesUpdatedLocked() {
4739 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4740 mTaskHistory.get(taskNdx).setLockTaskAuth();
4741 }
4742 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004743}