blob: 1f06b454a5a6a6f188c76b978df13893a9de8c96 [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.*;
Chong Zhang75b37202015-12-04 14:16:36 -080027import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
Craig Mautner84984fa2014-06-19 11:19:20 -070028import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
29import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070030
Wale Ogunwale040b4702015-08-06 18:10:50 -070031import static com.android.server.am.ActivityStackSupervisor.MOVING;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070032import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwale040b4702015-08-06 18:10:50 -070033
Winsonc809cbb2015-11-02 12:06:15 -080034import android.graphics.Point;
Wale Ogunwale706ed792015-08-02 10:29:44 -070035import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070036import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070037
Dianne Hackborn91097de2014-04-04 18:02:06 -070038import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080039import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070040import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070041import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080042import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080043import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080044import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080045import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070046import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070047
48import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070049import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070050import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070051import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080053import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080055import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080061import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080062import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070064import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080065import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Handler;
67import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090068import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070070import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.RemoteException;
72import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080073import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070074import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070075import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070078import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079
Craig Mautnercae015f2013-02-08 14:31:27 -080080import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080081import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082import java.lang.ref.WeakReference;
83import java.util.ArrayList;
84import java.util.Iterator;
85import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080086import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070087import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070088
89/**
90 * State and management of a single stack of activities.
91 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070092final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080093
Wale Ogunwalee23149f2015-03-06 15:39:44 -080094 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070095 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
96 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080097 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070098 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070099 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700100 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700101 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700102 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700103 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
104 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700105 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700106 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700107 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
108 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
109 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
110 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
111 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700112
113 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800114
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700115 // Ticks during which we check progress while waiting for an app to launch.
116 static final int LAUNCH_TICK = 500;
117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700118 // How long we wait until giving up on the last activity to pause. This
119 // is short because it directly impacts the responsiveness of starting the
120 // next activity.
121 static final int PAUSE_TIMEOUT = 500;
122
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700123 // How long we wait for the activity to tell us it has stopped before
124 // giving up. This is a good amount of time because we really need this
125 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700126 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700127
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700128 // How long we wait until giving up on an activity telling us it has
129 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700130 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800131
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700132 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800133 // disabled.
134 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800135
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700136 // How long between activity launches that we consider safe to not warn
137 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700138 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800139
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700140 // Set to false to disable the preview that is shown while a new activity
141 // is being started.
142 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800143
Craig Mautner5eda9b32013-07-02 11:58:16 -0700144 // How long to wait for all background Activities to redraw following a call to
145 // convertToTranslucent().
146 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
147
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700148 enum ActivityState {
149 INITIALIZING,
150 RESUMED,
151 PAUSING,
152 PAUSED,
153 STOPPING,
154 STOPPED,
155 FINISHING,
156 DESTROYING,
157 DESTROYED
158 }
159
160 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700161 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800162 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800163
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700164 /**
165 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800166 * running) activities. It contains #TaskRecord objects.
167 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800168 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800169
170 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800171 * Used for validating app tokens with window manager.
172 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800173 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800174
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700175 /**
176 * List of running activities, sorted by recent usage.
177 * The first entry in the list is the least recently used.
178 * It contains HistoryRecord objects.
179 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800180 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700181
182 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700183 * Animations that for the current transition have requested not to
184 * be considered for the transition animation.
185 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800186 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700187
188 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700189 * When we are in the process of pausing an activity, before starting the
190 * next one, this variable holds the activity that is currently being paused.
191 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800192 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700193
194 /**
195 * This is the last activity that we put into the paused state. This is
196 * used to determine if we need to do an activity transition while sleeping,
197 * when we normally hold the top activity paused.
198 */
199 ActivityRecord mLastPausedActivity = null;
200
201 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700202 * Activities that specify No History must be removed once the user navigates away from them.
203 * If the device goes to sleep with such an activity in the paused state then we save it here
204 * and finish it later if another activity replaces it on wakeup.
205 */
206 ActivityRecord mLastNoHistoryActivity = null;
207
208 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700209 * Current activity that is resumed, or null if there is none.
210 */
211 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800212
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700213 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700214 * This is the last activity that has been started. It is only used to
215 * identify when multiple activities are started at once so that the user
216 * can be warned they may not be in the activity they think they are.
217 */
218 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800219
Craig Mautner5eda9b32013-07-02 11:58:16 -0700220 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
221 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
222 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
223 // Activity in mTranslucentActivityWaiting is notified via
224 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
225 // background activity being drawn then the same call will be made with a true value.
226 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700227 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700228
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700229 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700230 * Set when we know we are going to be calling updateConfiguration()
231 * soon, so want to skip intermediate config checks.
232 */
233 boolean mConfigWillChange;
234
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700235 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800236 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700237 // Current bounds of the stack or null if fullscreen.
238 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800239
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700240 long mLaunchStartTime = 0;
241 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800242
Craig Mautner858d8a62013-04-23 17:08:34 -0700243 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700244
Craig Mautnerc00204b2013-03-05 15:02:14 -0800245 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800246 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800247 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
248 ArrayList<ActivityStack> mStacks;
249 /** The attached Display's unique identifier, or -1 if detached */
250 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800251
Craig Mautner27084302013-03-25 08:05:25 -0700252 /** Run all ActivityStacks through this */
253 final ActivityStackSupervisor mStackSupervisor;
254
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700255 private final LaunchingTaskPositioner mTaskPositioner;
256
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700257 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700258 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
259 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
260 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
261 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700262 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700263 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700264 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700265
266 static class ScheduleDestroyArgs {
267 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700268 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700269 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700270 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700271 mReason = reason;
272 }
273 }
274
Zoran Marcetaf958b322012-08-09 20:27:12 +0900275 final Handler mHandler;
276
277 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800278
Craig Mautnerc8143c62013-09-03 12:15:57 -0700279 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900280 super(looper);
281 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700282
Zoran Marcetaf958b322012-08-09 20:27:12 +0900283 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700284 public void handleMessage(Message msg) {
285 switch (msg.what) {
286 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800287 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 // We don't at this point know if the activity is fullscreen,
289 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800290 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700291 synchronized (mService) {
292 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700293 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700294 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700295 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800296 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700297 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700298 case LAUNCH_TICK_MSG: {
299 ActivityRecord r = (ActivityRecord)msg.obj;
300 synchronized (mService) {
301 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700302 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700303 }
304 }
305 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700306 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800307 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700308 // We don't at this point know if the activity is fullscreen,
309 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800310 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800311 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800312 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800313 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700314 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700315 case STOP_TIMEOUT_MSG: {
316 ActivityRecord r = (ActivityRecord)msg.obj;
317 // We don't at this point know if the activity is fullscreen,
318 // so we need to be conservative and assume it isn't.
319 Slog.w(TAG, "Activity stop timeout for " + r);
320 synchronized (mService) {
321 if (r.isInHistory()) {
322 activityStoppedLocked(r, null, null, null);
323 }
324 }
325 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700326 case DESTROY_ACTIVITIES_MSG: {
327 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
328 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700329 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700330 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700331 } break;
332 case TRANSLUCENT_TIMEOUT_MSG: {
333 synchronized (mService) {
334 notifyActivityDrawnLocked(null);
335 }
336 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700337 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700338 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700339 final ActivityRecord r = getVisibleBehindActivity();
340 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700341 if (r != null) {
342 mService.killAppAtUsersRequest(r.app, null);
343 }
344 }
345 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700346 }
347 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800348 }
349
Craig Mautner34b73df2014-01-12 21:11:08 -0800350 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800351 int count = 0;
352 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
353 count += mTaskHistory.get(taskNdx).mActivities.size();
354 }
355 return count;
356 }
357
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800358 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
359 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800360 mActivityContainer = activityContainer;
361 mStackSupervisor = activityContainer.getOuter();
362 mService = mStackSupervisor.mService;
363 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
364 mWindowManager = mService.mWindowManager;
365 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700366 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800367 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700368 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
369 ? new LaunchingTaskPositioner() : null;
370 }
371
372 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
373 mDisplayId = activityDisplay.mDisplayId;
374 mStacks = activityDisplay.mStacks;
375 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
376 mFullscreen = mBounds == null;
377 if (mTaskPositioner != null) {
378 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
379 mTaskPositioner.configure(mBounds);
380 }
381 }
382
383 void detachDisplay() {
384 mDisplayId = Display.INVALID_DISPLAY;
385 mStacks = null;
386 if (mTaskPositioner != null) {
387 mTaskPositioner.reset();
388 }
389 mWindowManager.detachStack(mStackId);
390 }
391
Winsonc809cbb2015-11-02 12:06:15 -0800392 public void getDisplaySize(Point out) {
393 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
394 }
395
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700396 void setBounds(Rect bounds) {
397 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700398 if (mTaskPositioner != null) {
399 mTaskPositioner.configure(bounds);
400 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700401 }
Craig Mautner5962b122012-10-05 14:45:52 -0700402
Amith Yamasani734983f2014-03-04 16:48:05 -0800403 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700404 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700405 }
406
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700407 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800408 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700409 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700410 if (r != null) {
411 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800412 }
413 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700414 return null;
415 }
416
417 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800418 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
419 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800420 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800421 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
422 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800423 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800424 return r;
425 }
426 }
427 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700428 return null;
429 }
430
431 /**
432 * This is a simplified version of topRunningActivityLocked that provides a number of
433 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800434 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700435 * @param token If non-null, any history records matching this token will be skipped.
436 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800437 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700438 * @return Returns the HistoryRecord of the next activity on the stack.
439 */
440 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800441 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
442 TaskRecord task = mTaskHistory.get(taskNdx);
443 if (task.taskId == taskId) {
444 continue;
445 }
446 ArrayList<ActivityRecord> activities = task.mActivities;
447 for (int i = activities.size() - 1; i >= 0; --i) {
448 final ActivityRecord r = activities.get(i);
449 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800450 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800451 return r;
452 }
453 }
454 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700455 return null;
456 }
457
Craig Mautner8849a5e2013-04-02 16:41:03 -0700458 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700459 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
460 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700461 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
462 final ActivityRecord r = activities.get(activityNdx);
463 if (!r.finishing) {
464 return r;
465 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700466 }
467 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700468 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700469 }
470
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700471 final TaskRecord topTask() {
472 final int size = mTaskHistory.size();
473 if (size > 0) {
474 return mTaskHistory.get(size - 1);
475 }
476 return null;
477 }
478
Craig Mautnerd2328952013-03-05 12:46:26 -0800479 TaskRecord taskForIdLocked(int id) {
480 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
481 final TaskRecord task = mTaskHistory.get(taskNdx);
482 if (task.taskId == id) {
483 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800484 }
485 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700486 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700487 }
488
Craig Mautnerd2328952013-03-05 12:46:26 -0800489 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700490 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800491 return isInStackLocked(r);
492 }
493
494 ActivityRecord isInStackLocked(ActivityRecord r) {
495 if (r == null) {
496 return null;
497 }
498 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700499 if (task != null && task.stack != null
500 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800501 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800502 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800503 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800504 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800505 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800506 }
507
Craig Mautner2420ead2013-04-01 17:13:20 -0700508 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700509 final boolean hadit = mLRUActivities.remove(r);
510 mLRUActivities.add(r);
511 return hadit;
512 }
513
Craig Mautnerde4ef022013-04-07 19:01:33 -0700514 final boolean isHomeStack() {
515 return mStackId == HOME_STACK_ID;
516 }
517
Craig Mautnere0a38842013-12-16 16:14:02 -0800518 final boolean isOnHomeDisplay() {
519 return isAttached() &&
520 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
521 }
522
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700523 void moveToFront(String reason) {
524 moveToFront(reason, null);
525 }
526
527 /**
528 * @param reason The reason for moving the stack to the front.
529 * @param task If non-null, the task will be moved to the top of the stack.
530 * */
531 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700532 if (!isAttached()) {
533 return;
534 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700535
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700536 mStacks.remove(this);
537 int addIndex = mStacks.size();
538
539 if (addIndex > 0) {
540 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800541 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
542 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700543 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800544 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700545 }
546
547 mStacks.add(addIndex, this);
548
549 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
550 if (isOnHomeDisplay()) {
551 mStackSupervisor.setFocusStack(reason, this);
552 }
553 if (task != null) {
554 insertTaskAtTop(task, null);
555 } else {
556 task = topTask();
557 }
558 if (task != null) {
559 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800560 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800561 }
562
563 final boolean isAttached() {
564 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800565 }
566
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700567 /**
568 * Returns the top activity in any existing task matching the given
569 * Intent. Returns null if no such task is found.
570 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700571 ActivityRecord findTaskLocked(ActivityRecord target) {
572 Intent intent = target.intent;
573 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700574 ComponentName cls = intent.getComponent();
575 if (info.targetActivity != null) {
576 cls = new ComponentName(info.packageName, info.targetActivity);
577 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700578 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700579 boolean isDocument = intent != null & intent.isDocument();
580 // If documentData is non-null then it must match the existing task data.
581 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800582
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700583 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800584 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
585 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700586 if (task.voiceSession != null) {
587 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700588 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700589 continue;
590 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700591 if (task.userId != userId) {
592 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700593 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700594 continue;
595 }
Craig Mautner000f0022013-02-26 15:04:29 -0800596 final ActivityRecord r = task.getTopActivity();
597 if (r == null || r.finishing || r.userId != userId ||
598 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700599 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800600 continue;
601 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700602 if (r.mActivityType != target.mActivityType) {
603 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
604 continue;
605 }
Craig Mautner000f0022013-02-26 15:04:29 -0800606
Craig Mautnerd00f4742014-03-12 14:17:26 -0700607 final Intent taskIntent = task.intent;
608 final Intent affinityIntent = task.affinityIntent;
609 final boolean taskIsDocument;
610 final Uri taskDocumentData;
611 if (taskIntent != null && taskIntent.isDocument()) {
612 taskIsDocument = true;
613 taskDocumentData = taskIntent.getData();
614 } else if (affinityIntent != null && affinityIntent.isDocument()) {
615 taskIsDocument = true;
616 taskDocumentData = affinityIntent.getData();
617 } else {
618 taskIsDocument = false;
619 taskDocumentData = null;
620 }
621
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700622 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700623 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700624 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700625 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700626 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
627 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700628 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700629 return r;
630 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700631 } else if (taskIntent != null && taskIntent.getComponent() != null &&
632 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700633 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700634 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800635 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700636 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
637 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800638 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700639 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
640 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700641 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700642 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800643 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700644 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
645 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800646 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700647 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700648 }
649
650 return null;
651 }
652
653 /**
654 * Returns the first activity (starting from the top of the stack) that
655 * is the same as the given activity. Returns null if no such activity
656 * is found.
657 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700658 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700659 ComponentName cls = intent.getComponent();
660 if (info.targetActivity != null) {
661 cls = new ComponentName(info.packageName, info.targetActivity);
662 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700663 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700664
Craig Mautner000f0022013-02-26 15:04:29 -0800665 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700666 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700667 final boolean notCurrentUserTask =
668 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700669 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700670
Craig Mautner000f0022013-02-26 15:04:29 -0800671 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
672 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700673 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700674 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700675 }
Craig Mautner000f0022013-02-26 15:04:29 -0800676 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700677 return r;
678 }
679 }
680 }
681
682 return null;
683 }
684
Amith Yamasani742a6712011-05-04 14:49:28 -0700685 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700686 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700687 */
Craig Mautner93529a42013-10-04 15:03:13 -0700688 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800689 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700690 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800691 }
692 mCurrentUser = userId;
693
694 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800695 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800696 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700697 final TaskRecord task = mTaskHistory.get(i);
698
699 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
700 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700701 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700702 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700703 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700704 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800705 mTaskHistory.remove(i);
706 mTaskHistory.add(task);
707 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800708 // Use same value for i.
709 } else {
710 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800711 }
712 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700713 if (VALIDATE_TOKENS) {
714 validateAppTokensLocked();
715 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700716 }
717
Craig Mautner2420ead2013-04-01 17:13:20 -0700718 void minimalResumeActivityLocked(ActivityRecord r) {
719 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700720 if (DEBUG_STATES) Slog.v(TAG_STATES,
721 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700722 r.stopped = false;
723 mResumedActivity = r;
724 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800725 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700726 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700727 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700728 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700729 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
730 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700731 }
732
Chong Zhang45c25ce2015-08-10 22:18:26 -0700733 private void addRecentActivityLocked(ActivityRecord r) {
734 if (r != null) {
735 mRecentTasks.addLocked(r.task);
736 r.task.touchActiveTime();
737 }
738 }
739
Narayan Kamath7829c812015-06-08 17:39:43 +0100740 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700741 if (mFullyDrawnStartTime != 0) {
742 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
743 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100744 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700745 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
746 }
747
748 private void stopFullyDrawnTraceIfNeeded() {
749 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
750 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
751 mFullyDrawnStartTime = 0;
752 }
753 }
754
Craig Mautnere79d42682013-04-01 19:01:53 -0700755 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700756 if (r.displayStartTime == 0) {
757 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
758 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100759 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700760 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700761 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700762 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100763 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700764 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700765 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700766 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800767
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700768 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700769 // Make sure that there is no activity waiting for this to launch.
770 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
771 r.displayStartTime = r.fullyDrawnStartTime = 0;
772 } else {
773 mStackSupervisor.removeTimeoutsForActivityLocked(r);
774 mStackSupervisor.scheduleIdleTimeoutLocked(r);
775 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700776 }
777
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800778 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800779 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800780 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
781 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
782 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
783 activities.get(activityNdx).setSleeping(false);
784 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800785 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800786 if (mPausingActivity != null) {
787 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
788 activityPausedLocked(mPausingActivity.appToken, true);
789 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800790 }
791
Craig Mautner0eea92c2013-05-16 13:35:39 -0700792 /**
793 * @return true if something must be done before going to sleep.
794 */
795 boolean checkReadyForSleepLocked() {
796 if (mResumedActivity != null) {
797 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700798 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
799 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
800 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700801 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700802 return true;
803 }
804 if (mPausingActivity != null) {
805 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700806 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700807 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800808 }
809
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700810 if (hasVisibleBehindActivity()) {
811 // Stop visible behind activity before going to sleep.
812 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
813 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700814 if (DEBUG_STATES) Slog.v(TAG_STATES,
815 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700816 return true;
817 }
818
Craig Mautner0eea92c2013-05-16 13:35:39 -0700819 return false;
820 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800821
Craig Mautner0eea92c2013-05-16 13:35:39 -0700822 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700823 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800824
Craig Mautner0eea92c2013-05-16 13:35:39 -0700825 // Make sure any stopped but visible activities are now sleeping.
826 // This ensures that the activity's onStop() is called.
827 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
828 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
829 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
830 final ActivityRecord r = activities.get(activityNdx);
831 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
832 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800833 }
834 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800835 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 }
Craig Mautner59c00972012-07-30 12:10:24 -0700837
Winson8b1871d2015-11-20 09:56:20 -0800838 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
839 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800840 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700841 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800842 return null;
843 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800844
Winson Chung083baf92014-07-11 10:32:42 -0700845 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700846 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800847 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700848 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700849 return null;
850 }
851
Winson Chung48a10a52014-08-27 14:36:51 -0700852 int w = mService.mThumbnailWidth;
853 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800854 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800855 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700856 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800857
858 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800859 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
860 // use within SystemUI while keeping memory usage low.
Winson8b1871d2015-11-20 09:56:20 -0800861 if (mService.mTakeFullscreenScreenshots) {
Winson8b1871d2015-11-20 09:56:20 -0800862 w = h = -1;
Winson387aac62015-11-25 11:18:56 -0800863 scale = 0.5f;
Winson8b1871d2015-11-20 09:56:20 -0800864 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800865 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800866 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800867 }
Winson Chung376543b2014-05-21 17:43:28 -0700868 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800869 return null;
870 }
871
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700872 /**
873 * Start pausing the currently resumed activity. It is an error to call this if there
874 * is already an activity being paused or there is no resumed activity.
875 *
876 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
877 * @param uiSleeping True if this is happening with the user interface going to sleep (the
878 * screen turning off).
879 * @param resuming True if this is being called as part of resuming the top activity, so
880 * we shouldn't try to instigate a resume here.
881 * @param dontWait True if the caller does not want to wait for the pause to complete. If
882 * set to true, we will immediately complete the pause here before returning.
883 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
884 * it to tell us when it is done.
885 */
886 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
887 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800888 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800889 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
890 + " state=" + mPausingActivity.state);
891 if (!mService.isSleeping()) {
892 // Avoid recursion among check for sleep and complete pause during sleeping.
893 // Because activity will be paused immediately after resume, just let pause
894 // be completed by the order of activity paused from clients.
895 completePauseLocked(false);
896 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800897 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700898 ActivityRecord prev = mResumedActivity;
899 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700900 if (!resuming) {
901 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
902 mStackSupervisor.resumeTopActivitiesLocked();
903 }
904 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700905 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800906
907 if (mActivityContainer.mParentActivity == null) {
908 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700909 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800910 }
911
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700912 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700913 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700914 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800915 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700916 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700917 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
918 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700919 prev.state = ActivityState.PAUSING;
920 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700921 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700922 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700923 if (mService.mHasRecents
924 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson8b1871d2015-11-20 09:56:20 -0800925 prev.updateThumbnailLocked(screenshotActivitiesLocked(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700926 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700927 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700928
929 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800930
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700931 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700932 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700933 try {
934 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700935 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700936 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700937 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800938 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700939 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700940 } catch (Exception e) {
941 // Ignore exception, if process died other code will cleanup.
942 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800943 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700944 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700945 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700946 }
947 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800948 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700949 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700950 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700951 }
952
953 // If we are not going to sleep, we want to ensure the device is
954 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700955 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700956 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700957 }
958
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800959 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700960 // Have the window manager pause its key dispatching until the new
961 // activity has started. If we're pausing the activity just because
962 // the screen is being turned off and the UI is sleeping, don't interrupt
963 // key dispatch; the same activity will pick it up again on wakeup.
964 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800965 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700966 } else if (DEBUG_PAUSE) {
967 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700968 }
969
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700970 if (dontWait) {
971 // If the caller said they don't want to wait for the pause, then complete
972 // the pause now.
973 completePauseLocked(false);
974 return false;
975
976 } else {
977 // Schedule a pause timeout in case the app doesn't respond.
978 // We don't give it much time because this directly impacts the
979 // responsiveness seen by the user.
980 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
981 msg.obj = prev;
982 prev.pauseTime = SystemClock.uptimeMillis();
983 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700984 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700985 return true;
986 }
987
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700988 } else {
989 // This activity failed to schedule the
990 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700991 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700992 if (!resuming) {
993 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
994 }
995 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700996 }
997 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700998
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700999 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001000 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1001 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001002
Craig Mautnerd2328952013-03-05 12:46:26 -08001003 final ActivityRecord r = isInStackLocked(token);
1004 if (r != null) {
1005 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1006 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001007 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001008 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001009 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -08001010 } else {
1011 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1012 r.userId, System.identityHashCode(r), r.shortComponentName,
1013 mPausingActivity != null
1014 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001015 if (r.state == ActivityState.PAUSING) {
1016 r.state = ActivityState.PAUSED;
1017 if (r.finishing) {
1018 if (DEBUG_PAUSE) Slog.v(TAG,
1019 "Executing finish of failed to pause activity: " + r);
1020 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1021 }
louis_chang047dfd42015-04-08 16:35:55 +08001022 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001023 }
1024 }
1025 }
1026
Craig Mautnera0026042014-04-23 11:45:37 -07001027 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1028 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001029 if (r.state != ActivityState.STOPPING) {
1030 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1031 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1032 return;
1033 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001034 if (persistentState != null) {
1035 r.persistentState = persistentState;
1036 mService.notifyTaskPersisterLocked(r.task, false);
1037 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001038 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001039 if (icicle != null) {
1040 // If icicle is null, this is happening due to a timeout, so we
1041 // haven't really saved the state.
1042 r.icicle = icicle;
1043 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001044 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001045 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001046 }
1047 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001048 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001049 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1050 r.stopped = true;
1051 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -07001052 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
1053 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001054 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001055 if (r.finishing) {
1056 r.clearOptionsLocked();
1057 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001058 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001059 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -07001060 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001061 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001062 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001063 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001064 }
1065 }
1066 }
1067
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001068 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001069 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001070 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001071
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001072 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001073 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001074 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001075 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001076 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001077 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001078 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001079 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001080 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1081 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001082 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001083 if (prev.configDestroy) {
1084 // The previous is being paused because the configuration
1085 // is changing, which means it is actually stopping...
1086 // To juggle the fact that we are also starting a new
1087 // instance right now, we need to first completely stop
1088 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001089 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001090 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001091 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001092 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001093 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001094 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001095 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1096 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001097 // If we already have a few activities waiting to stop,
1098 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001099 // them out. Or if r is the last of activity of the last task the stack
1100 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001101 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001102 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001103 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001104 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001105 }
1106 }
1107 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001108 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001109 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001110 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001111 // It is possible the activity was freezing the screen before it was paused.
1112 // In that case go ahead and remove the freeze this activity has on the screen
1113 // since it is no longer visible.
1114 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001115 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001116 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001117
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001118 if (resumeNext) {
1119 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1120 if (!mService.isSleepingOrShuttingDown()) {
1121 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1122 } else {
1123 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001124 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001125 if (top == null || (prev != null && top != prev)) {
1126 // If there are no more activities available to run,
1127 // do resume anyway to start something. Also if the top
1128 // activity on the stack is not the just paused activity,
1129 // we need to go ahead and resume it to ensure we complete
1130 // an in-flight app switch.
1131 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1132 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001133 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001134 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001135
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001136 if (prev != null) {
1137 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001138
Craig Mautner525f3d92013-05-07 14:01:50 -07001139 if (prev.app != null && prev.cpuTimeAtResume > 0
1140 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001141 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1142 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001143 if (diff > 0) {
1144 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1145 synchronized (bsi) {
1146 BatteryStatsImpl.Uid.Proc ps =
1147 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001148 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001149 if (ps != null) {
1150 ps.addForegroundTimeLocked(diff);
1151 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001152 }
1153 }
1154 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001155 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001156 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001157
1158 // Notfiy when the task stack has changed
1159 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001160 }
1161
1162 /**
1163 * Once we know that we have asked an application to put an activity in
1164 * the resumed state (either by launching it or explicitly telling it),
1165 * this function updates the rest of our state to match that fact.
1166 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001167 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001168 next.idle = false;
1169 next.results = null;
1170 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001171
Chong Zhang85ee6542015-10-02 13:36:38 -07001172 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001173 ProcessRecord app = next.task.mActivities.get(0).app;
1174 if (app != null && app != mService.mHomeProcess) {
1175 mService.mHomeProcess = app;
1176 }
1177 }
1178
Craig Mautner07566322013-09-26 16:42:55 -07001179 if (next.nowVisible) {
1180 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001181 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001182 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001183
1184 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001185 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001186
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001187 mStackSupervisor.reportResumedActivityLocked(next);
1188
1189 next.resumeKeyDispatchingLocked();
1190 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001191
1192 // Mark the point when the activity is resuming
1193 // TODO: To be more accurate, the mark should be before the onCreate,
1194 // not after the onResume. But for subsequent starts, onResume is fine.
1195 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001196 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001197 } else {
1198 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1199 }
Winson Chung376543b2014-05-21 17:43:28 -07001200
George Mount6ba042b2014-07-28 11:12:28 -07001201 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001202
1203 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1204 // When resuming an activity, require it to call requestVisibleBehind() again.
1205 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1206 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001207 }
1208
Craig Mautner2568c3a2015-03-26 14:22:34 -07001209 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001210 r.visible = visible;
1211 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001212 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001213 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001214 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001215 container.setVisible(visible);
1216 }
1217 }
1218
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001219 // Find the first visible activity above the passed activity and if it is translucent return it
1220 // otherwise return null;
1221 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1222 TaskRecord task = r.task;
1223 if (task == null) {
1224 return null;
1225 }
1226
1227 ActivityStack stack = task.stack;
1228 if (stack == null) {
1229 return null;
1230 }
1231
1232 int stackNdx = mStacks.indexOf(stack);
1233
1234 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1235 int taskNdx = tasks.indexOf(task);
1236
1237 ArrayList<ActivityRecord> activities = task.mActivities;
1238 int activityNdx = activities.indexOf(r) + 1;
1239
1240 final int numStacks = mStacks.size();
1241 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001242 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001243 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001244 final int numTasks = tasks.size();
1245 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001246 final TaskRecord currentTask = tasks.get(taskNdx);
1247 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001248 final int numActivities = activities.size();
1249 while (activityNdx < numActivities) {
1250 final ActivityRecord activity = activities.get(activityNdx);
1251 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001252 return historyStack.mFullscreen
1253 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001254 }
1255 ++activityNdx;
1256 }
1257 activityNdx = 0;
1258 ++taskNdx;
1259 }
1260 taskNdx = 0;
1261 ++stackNdx;
1262 }
1263
1264 return null;
1265 }
1266
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001267 private ActivityStack getNextVisibleStackLocked() {
1268 ArrayList<ActivityStack> stacks = mStacks;
1269 final ActivityRecord parent = mActivityContainer.mParentActivity;
1270 if (parent != null) {
1271 stacks = parent.task.stack.mStacks;
1272 }
1273 if (stacks != null) {
1274 for (int i = stacks.size() - 1; i >= 0; --i) {
1275 ActivityStack stack = stacks.get(i);
1276 if (stack != this && stack.isStackVisibleLocked()) {
1277 return stack;
1278 }
1279 }
1280 }
1281 return null;
1282 }
1283
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001284 /** Returns true if the stack contains a fullscreen task. */
1285 private boolean hasFullscreenTask() {
1286 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1287 final TaskRecord task = mTaskHistory.get(i);
1288 if (task.mFullscreen) {
1289 return true;
1290 }
1291 }
1292 return false;
1293 }
1294
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001295 private boolean hasTranslucentActivity(ActivityStack stack) {
1296 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1297 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1298 final TaskRecord task = tasks.get(taskNdx);
1299 final ArrayList<ActivityRecord> activities = task.mActivities;
1300 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1301 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001302
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001303 // Conditions for an activity to obscure the stack we're
1304 // examining:
1305 // 1. Not Finishing AND Visible AND:
1306 // 2. Either:
1307 // - Full Screen Activity OR
1308 // - On top of Home and our stack is NOT home
1309 if (!r.finishing && r.visible && (r.fullscreen ||
1310 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1311 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001312 }
1313 }
1314 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001315 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001316 }
1317
1318 /** Returns true if the stack is considered visible. */
Todd Kennedyaab56db2015-01-30 09:39:53 -08001319 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001320 if (!isAttached()) {
1321 return false;
1322 }
1323
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001324 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001325 return true;
1326 }
1327
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001328 final int stackIndex = mStacks.indexOf(this);
1329
1330 if (stackIndex == mStacks.size() - 1) {
1331 Slog.wtf(TAG,
1332 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1333 return false;
1334 }
1335
Chong Zhang75b37202015-12-04 14:16:36 -08001336 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1337 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
1338 return false;
1339 }
1340
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001341 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1342 final int focusedStackId = focusedStack.mStackId;
1343
1344 if (mStackId == DOCKED_STACK_ID) {
1345 // Docked stack is always visible, except in the case where the home activity
1346 // is the top running activity in the focused home stack.
1347 if (focusedStackId != HOME_STACK_ID) {
1348 return true;
1349 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001350 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked();
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001351 return topHomeActivity == null || !topHomeActivity.isHomeActivity();
1352 }
1353
Chong Zhangb16cf342015-11-12 17:22:40 -08001354 // Find the first stack below focused stack that actually got something visible.
1355 int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1356 while (belowFocusedIndex >= 0 &&
1357 mStacks.get(belowFocusedIndex).topRunningActivityLocked() == null) {
1358 belowFocusedIndex--;
1359 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001360 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1361 && stackIndex == belowFocusedIndex) {
1362 // Stacks directly behind the docked or pinned stack are always visible.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001363 return true;
1364 }
1365
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001366 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1367 && hasTranslucentActivity(focusedStack)) {
1368 // Stacks behind the fullscreen stack with a translucent activity are always
1369 // visible so they can act as a backdrop to the translucent activity.
1370 // For example, dialog activities
1371 if (stackIndex == belowFocusedIndex) {
1372 return true;
1373 }
1374 if (belowFocusedIndex >= 0) {
1375 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001376 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1377 && stackIndex == (belowFocusedIndex - 1)) {
1378 // The stack behind the docked or pinned stack is also visible so we can have a
1379 // complete backdrop to the translucent activity when the docked stack is up.
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001380 return true;
1381 }
1382 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001383 }
1384
Wale Ogunwale3797c222015-10-27 14:21:58 -07001385 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001386 // Visibility of any static stack should have been determined by the conditions above.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001387 return false;
1388 }
1389
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001390 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001391 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001392
1393 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1394 continue;
1395 }
1396
Wale Ogunwale3797c222015-10-27 14:21:58 -07001397 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001398 // These stacks can't have any dynamic stacks visible behind them.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001399 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001400 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001401
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001402 if (!hasTranslucentActivity(stack)) {
1403 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001404 }
1405 }
1406
1407 return true;
1408 }
1409
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001410 final int rankTaskLayers(int baseLayer) {
1411 int layer = 0;
1412 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1413 final TaskRecord task = mTaskHistory.get(taskNdx);
1414 ActivityRecord r = task.topRunningActivityLocked();
1415 if (r == null || r.finishing || !r.visible) {
1416 task.mLayerRank = -1;
1417 } else {
1418 task.mLayerRank = baseLayer + layer++;
1419 }
1420 }
1421 return layer;
1422 }
1423
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001424 /**
1425 * Make sure that all activities that need to be visible (that is, they
1426 * currently can be seen by the user) actually are.
1427 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001428 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1429 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001430 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001431 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001432 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001433 if (top != null) {
1434 checkTranslucentActivityWaiting(top);
1435 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001436
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001437 // If the top activity is not fullscreen, then we need to
1438 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001439 boolean aboveTop = top != null;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001440 final boolean stackInvisible = !isStackVisibleLocked();
1441 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001442 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001443
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001444 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001445 final TaskRecord task = mTaskHistory.get(taskNdx);
1446 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001447
Craig Mautnerd44711d2013-02-23 11:24:36 -08001448 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1449 final ActivityRecord r = activities.get(activityNdx);
1450 if (r.finishing) {
1451 continue;
1452 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001453 final boolean isTop = r == top;
1454 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001455 continue;
1456 }
1457 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001458 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1459 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001460 if ((!behindFullscreenActivity || r.mLaunchTaskBehind)
1461 && okToShowLocked(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001462 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1463 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001464 + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001465 // First: if this is not the current activity being started, make
1466 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001467 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001468 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001469 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001470
1471 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001472 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001473 noStackActivityResumed, r)) {
1474 if (activityNdx >= activities.size()) {
1475 // Record may be removed if its process needs to restart.
1476 activityNdx = activities.size() - 1;
1477 } else {
1478 noStackActivityResumed = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001479 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001480 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001481 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001482 // If this activity is already visible, then there is nothing to do here.
1483 if (handleAlreadyVisible(r)) {
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001484 noStackActivityResumed = false;
1485 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001486 } else {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001487 makeVisible(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001488 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001489 // Aggregate current change flags.
1490 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001491 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1492 behindFullscreenActivity, task, r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001493 } else {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001494 makeInvisible(stackInvisible, behindFullscreenActivity, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001495 }
1496 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001497 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1498 // The visibility of tasks and the activities they contain in freeform stack are
1499 // determined individually unlike other stacks where the visibility or fullscreen
1500 // status of an activity in a previous task affects other.
1501 behindFullscreenActivity = stackInvisible;
1502 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001503 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001504
1505 if (mTranslucentActivityWaiting != null &&
1506 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1507 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1508 notifyActivityDrawnLocked(null);
1509 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001510 }
Craig Mautner58547802013-03-05 08:23:53 -08001511
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001512 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1513 if (mTranslucentActivityWaiting != top) {
1514 mUndrawnActivitiesBelowTopTranslucent.clear();
1515 if (mTranslucentActivityWaiting != null) {
1516 // Call the callback with a timeout indication.
1517 notifyActivityDrawnLocked(null);
1518 mTranslucentActivityWaiting = null;
1519 }
1520 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1521 }
1522 }
1523
1524 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001525 boolean isTop, boolean noStackActivityResumed, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001526 // We need to make sure the app is running if it's the top, or it is just made visible from
1527 // invisible. If the app is already visible, it must have died while it was visible. In this
1528 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1529 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001530 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001531 // This activity needs to be visible, but isn't even running...
1532 // get it started and resume if no other stack in this stack is resumed.
1533 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1534 if (r != starting) {
1535 r.startFreezingScreenLocked(r.app, configChanges);
1536 }
1537 if (!r.visible || r.mLaunchTaskBehind) {
1538 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1539 setVisible(r, true);
1540 }
1541 if (r != starting) {
1542 mStackSupervisor.startSpecificActivityLocked(r, noStackActivityResumed, false);
1543 return true;
1544 }
1545 }
1546 return false;
1547 }
1548
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001549 private void makeInvisible(boolean stackInvisible, boolean behindFullscreenActivity,
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001550 ActivityRecord r) {
1551 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r + " finishing="
1552 + r.finishing + " state=" + r.state + " stackInvisible=" + stackInvisible
1553 + " behindFullscreenActivity=" + behindFullscreenActivity);
1554 if (!r.visible) {
1555 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1556 return;
1557 }
1558 // Now for any activities that aren't visible to the user, make sure they no longer are
1559 // keeping the screen frozen.
1560 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
1561 try {
1562 setVisible(r, false);
1563 switch (r.state) {
1564 case STOPPING:
1565 case STOPPED:
1566 if (r.app != null && r.app.thread != null) {
1567 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1568 "Scheduling invisibility: " + r);
1569 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1570 }
1571 break;
1572
1573 case INITIALIZING:
1574 case RESUMED:
1575 case PAUSING:
1576 case PAUSED:
1577 // This case created for transitioning activities from
1578 // translucent to opaque {@link Activity#convertToOpaque}.
1579 if (getVisibleBehindActivity() == r) {
1580 releaseBackgroundResources(r);
1581 } else {
1582 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1583 mStackSupervisor.mStoppingActivities.add(r);
1584 }
1585 mStackSupervisor.scheduleIdleLocked();
1586 }
1587 break;
1588
1589 default:
1590 break;
1591 }
1592 } catch (Exception e) {
1593 // Just skip on any failure; we'll make it visible when it next restarts.
1594 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1595 }
1596 }
1597
1598 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1599 TaskRecord task, ActivityRecord r) {
1600 if (r.fullscreen) {
1601 // At this point, nothing else needs to be shown in this task.
1602 behindFullscreenActivity = true;
1603 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
1604 + " stackInvisible=" + stackInvisible
1605 + " behindFullscreenActivity=" + behindFullscreenActivity);
1606 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
1607 behindFullscreenActivity = true;
1608 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1609 + " stackInvisible=" + stackInvisible
1610 + " behindFullscreenActivity=" + behindFullscreenActivity);
1611 }
1612 return behindFullscreenActivity;
1613 }
1614
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001615 private void makeVisible(ActivityRecord starting, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001616 // This activity is not currently visible, but is running. Tell it to become visible.
1617 r.visible = true;
1618 if (r.state != ActivityState.RESUMED && r != starting) {
1619 // If this activity is paused, tell it to now show its window.
1620 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1621 "Making visible and scheduling visibility: " + r);
1622 try {
1623 if (mTranslucentActivityWaiting != null) {
1624 r.updateOptionsLocked(r.returningOptions);
1625 mUndrawnActivitiesBelowTopTranslucent.add(r);
1626 }
1627 setVisible(r, true);
1628 r.sleeping = false;
1629 r.app.pendingUiClean = true;
1630 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1631 r.stopFreezingScreenLocked(false);
1632 } catch (Exception e) {
1633 // Just skip on any failure; we'll make it
1634 // visible when it next restarts.
1635 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
1636 }
1637 }
1638 }
1639
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001640 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001641 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1642 r.stopFreezingScreenLocked(false);
1643 try {
1644 if (r.returningOptions != null) {
1645 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1646 }
1647 } catch(RemoteException e) {
1648 }
1649 return r.state == ActivityState.RESUMED;
1650 }
1651
Todd Kennedyaab56db2015-01-30 09:39:53 -08001652 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001653 mTranslucentActivityWaiting = r;
1654 mUndrawnActivitiesBelowTopTranslucent.clear();
1655 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1656 }
1657
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001658 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1659 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1660 final TaskRecord task = mTaskHistory.get(taskNdx);
1661 final ArrayList<ActivityRecord> activities = task.mActivities;
1662 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1663 final ActivityRecord r = activities.get(activityNdx);
1664 if ( r.appTimeTracker != except) {
1665 r.appTimeTracker = null;
1666 }
1667 }
1668 }
1669 }
1670
Craig Mautner5eda9b32013-07-02 11:58:16 -07001671 /**
1672 * Called as activities below the top translucent activity are redrawn. When the last one is
1673 * redrawn notify the top activity by calling
1674 * {@link Activity#onTranslucentConversionComplete}.
1675 *
1676 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1677 * occurred and the activity will be notified immediately.
1678 */
1679 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001680 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001681 if ((r == null)
1682 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1683 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1684 // The last undrawn activity below the top has just been drawn. If there is an
1685 // opaque activity at the top, notify it that it can become translucent safely now.
1686 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1687 mTranslucentActivityWaiting = null;
1688 mUndrawnActivitiesBelowTopTranslucent.clear();
1689 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1690
Craig Mautner71dd1b62014-02-18 15:48:52 -08001691 if (waitingActivity != null) {
1692 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1693 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1694 try {
1695 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1696 waitingActivity.appToken, r != null);
1697 } catch (RemoteException e) {
1698 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001699 }
1700 }
1701 }
1702 }
1703
Craig Mautnera61bc652013-10-28 15:43:18 -07001704 /** If any activities below the top running one are in the INITIALIZING state and they have a
1705 * starting window displayed then remove that starting window. It is possible that the activity
1706 * in this state will never resumed in which case that starting window will be orphaned. */
1707 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001708 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001709 boolean aboveTop = true;
1710 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1711 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1712 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1713 final ActivityRecord r = activities.get(activityNdx);
1714 if (aboveTop) {
1715 if (r == topActivity) {
1716 aboveTop = false;
1717 }
1718 continue;
1719 }
1720
1721 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001722 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1723 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001724 r.mStartingWindowShown = false;
1725 mWindowManager.removeAppStartingWindow(r.appToken);
1726 }
1727 }
1728 }
1729 }
1730
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001731 /**
1732 * Ensure that the top activity in the stack is resumed.
1733 *
1734 * @param prev The previously resumed activity, for when in the process
1735 * of pausing; can be null to call from elsewhere.
1736 *
1737 * @return Returns true if something is being resumed, or false if
1738 * nothing happened.
1739 */
1740 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001741 return resumeTopActivityLocked(prev, null);
1742 }
1743
Chong Zhang280d3322015-11-03 17:27:26 -08001744 final boolean resumeTopActivityLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001745 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001746 // Don't even start recursing.
1747 return false;
1748 }
1749
1750 boolean result = false;
1751 try {
1752 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001753 mStackSupervisor.inResumeTopActivity = true;
1754 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1755 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001756 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001757 }
Craig Mautner544efa72014-09-04 16:41:20 -07001758 result = resumeTopActivityInnerLocked(prev, options);
1759 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001760 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001761 }
1762 return result;
1763 }
1764
Chong Zhang280d3322015-11-03 17:27:26 -08001765 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001766 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001767
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001768 if (!mService.mBooting && !mService.mBooted) {
1769 // Not ready yet!
1770 return false;
1771 }
1772
Craig Mautnerdf88d732014-01-27 09:21:32 -08001773 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001774 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001775 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001776 // Do not resume this stack if its parent is not resumed.
1777 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1778 return false;
1779 }
1780
Craig Mautnera61bc652013-10-28 15:43:18 -07001781 cancelInitializingActivities();
1782
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001783 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001784 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001785
1786 // Remember how we'll process this pause/resume situation, and ensure
1787 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001788 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1789 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001790
Craig Mautner84984fa2014-06-19 11:19:20 -07001791 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001792 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001793 // There are no more activities!
1794 final String reason = "noMoreActivities";
1795 if (!mFullscreen) {
1796 // Try to move focus to the next visible stack with a running activity if this
1797 // stack is not covering the entire screen.
1798 final ActivityStack stack = getNextVisibleStackLocked();
1799 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1800 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1801 }
1802 }
1803 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001804 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001805 if (DEBUG_STATES) Slog.d(TAG_STATES,
1806 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001807 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001808 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001809 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1810 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1811 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001812 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001813 }
1814
1815 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001816
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001817 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001818 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1819 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001820 // Make sure we have executed any pending transitions, since there
1821 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001822 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001823 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001824 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001825 if (DEBUG_STATES) Slog.d(TAG_STATES,
1826 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001827 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001828 return false;
1829 }
1830
Craig Mautner525f3d92013-05-07 14:01:50 -07001831 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001832 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001833 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001834 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001835 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001836 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001837 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001838 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001839 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001840 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001841 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001842 } else if (!isOnHomeDisplay()) {
1843 return false;
1844 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001845 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001846 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001847 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1848 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1849 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001850 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001851 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001852 }
1853
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001854 // If we are sleeping, and there is no resumed activity, and the top
1855 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001856 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001857 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001858 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001859 // Make sure we have executed any pending transitions, since there
1860 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001861 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001862 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001863 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001864 if (DEBUG_STATES) Slog.d(TAG_STATES,
1865 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001866 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001867 return false;
1868 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001869
1870 // Make sure that the user who owns this activity is started. If not,
1871 // we will just leave it as is because someone should be bringing
1872 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001873 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001874 Slog.w(TAG, "Skipping resume of top activity " + next
1875 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001876 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001877 return false;
1878 }
1879
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001880 // The activity may be waiting for stop, but that is no longer
1881 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001882 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001883 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001884 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001885 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001886
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001887 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001888
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001889 // If we are currently pausing an activity, then don't do anything
1890 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001891 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001892 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001893 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001894 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001895 return false;
1896 }
1897
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001898 // Okay we are now going to start a switch, to 'next'. We may first
1899 // have to pause the current activity, but this is an important point
1900 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001901 // XXX "App Redirected" dialog is getting too many false positives
1902 // at this point, so turn off for now.
1903 if (false) {
1904 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1905 long now = SystemClock.uptimeMillis();
1906 final boolean inTime = mLastStartedActivity.startTime != 0
1907 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1908 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1909 final int nextUid = next.info.applicationInfo.uid;
1910 if (inTime && lastUid != nextUid
1911 && lastUid != next.launchedFromUid
1912 && mService.checkPermission(
1913 android.Manifest.permission.STOP_APP_SWITCHES,
1914 -1, next.launchedFromUid)
1915 != PackageManager.PERMISSION_GRANTED) {
1916 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1917 } else {
1918 next.startTime = now;
1919 mLastStartedActivity = next;
1920 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001921 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001922 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001923 mLastStartedActivity = next;
1924 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001925 }
Craig Mautner58547802013-03-05 08:23:53 -08001926
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001927 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1928
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001929 // We need to start pausing the current activity so the top one
1930 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001931 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1932 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001933 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001934 if (DEBUG_STATES) Slog.d(TAG_STATES,
1935 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001936 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001937 }
1938 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001939 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001940 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001941 // At this point we want to put the upcoming activity's process
1942 // at the top of the LRU list, since we know we will be needing it
1943 // very soon and it would be a waste to let it get killed if it
1944 // happens to be sitting towards the end.
1945 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001946 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001947 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001948 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001949 return true;
1950 }
1951
Christopher Tated3f175c2012-06-14 14:16:54 -07001952 // If the most recent activity was noHistory but was only stopped rather
1953 // than stopped+finished because the device went to sleep, we need to make
1954 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001955 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001956 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001957 if (DEBUG_STATES) Slog.d(TAG_STATES,
1958 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001959 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001960 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001961 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001962 }
1963
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001964 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001965 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1966 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001967 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001968 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1969 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001970 } else {
1971 // The next activity is already visible, so hide the previous
1972 // activity's windows right now so we can show the new one ASAP.
1973 // We only do this if the previous is finishing, which should mean
1974 // it is on top of the one being resumed so hiding it quickly
1975 // is good. Otherwise, we want to do the normal route of allowing
1976 // the resumed activity to be shown so we can decide if the
1977 // previous should actually be hidden depending on whether the
1978 // new one is found to be full-screen or not.
1979 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001980 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001981 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1982 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001983 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001984 + ", nowVisible=" + next.nowVisible);
1985 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001986 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001987 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001988 + ", waitingVisible="
1989 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1990 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001991 }
1992 }
1993 }
1994
Dianne Hackborne7f97212011-02-24 14:40:20 -08001995 // Launching this app's activity, make sure the app is no longer
1996 // considered stopped.
1997 try {
1998 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001999 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002000 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002001 } catch (IllegalArgumentException e) {
2002 Slog.w(TAG, "Failed trying to unstop package "
2003 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002004 }
2005
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002006 // We are starting up the next activity, so tell the window manager
2007 // that the previous one will be hidden soon. This way it can know
2008 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002009 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002010 if (prev != null) {
2011 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002012 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002013 "Prepare close transition: prev=" + prev);
2014 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002015 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002016 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002017 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002018 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002019 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2020 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002021 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002022 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002023 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002024 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2025 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002026 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002027 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002028 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002029 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002030 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002031 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002032 : next.mLaunchTaskBehind
2033 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2034 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002035 }
2036 }
Craig Mautner967212c2013-04-13 21:10:58 -07002037 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002038 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002039 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002040 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002041 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002042 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002043 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002044 }
2045 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002046
2047 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002048 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002049 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2050 if (opts != null) {
2051 resumeAnimOptions = opts.toBundle();
2052 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002053 next.applyOptionsLocked();
2054 } else {
2055 next.clearOptionsLocked();
2056 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002057
Craig Mautnercf910b02013-04-23 11:23:27 -07002058 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002059 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002060 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002061
2062 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002063 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002064
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002065 // schedule launch ticks to collect information about slow apps.
2066 next.startLaunchTickingLocked();
2067
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002068 ActivityRecord lastResumedActivity =
2069 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002070 ActivityState lastState = next.state;
2071
2072 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002073
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002074 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002075 next.state = ActivityState.RESUMED;
2076 mResumedActivity = next;
2077 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002078 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002079 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002080 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002081 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002082
2083 // Have the window manager re-evaluate the orientation of
2084 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002085 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002086 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002087 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002088 mService.mConfiguration,
2089 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2090 if (config != null) {
2091 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002092 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002093 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002094 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002095
Craig Mautner525f3d92013-05-07 14:01:50 -07002096 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002097 // The configuration update wasn't able to keep the existing
2098 // instance of the activity, and instead started a new one.
2099 // We should be all done, but let's just make sure our activity
2100 // is still at the top and schedule another run if something
2101 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002102 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002103 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 "Activity config changed during resume: " + next
2105 + ", new next: " + nextNext);
2106 if (nextNext != next) {
2107 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002108 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002109 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002110 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002111 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002112 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002113 return true;
2114 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002115 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002116 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002117 }
Craig Mautner58547802013-03-05 08:23:53 -08002118
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002119 try {
2120 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002121 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002122 if (a != null) {
2123 final int N = a.size();
2124 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002125 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2126 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002127 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002128 }
2129 }
2130
2131 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002132 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002133 }
2134
Craig Mautner299f9602015-01-26 09:47:33 -08002135 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2136 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002137
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002138 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002139 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002140 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002141 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002142 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002143 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002144 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002145
Craig Mautner0eea92c2013-05-16 13:35:39 -07002146 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002147
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002148 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002149 } catch (Exception e) {
2150 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002151 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002152 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002153 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002154 if (lastStack != null) {
2155 lastStack.mResumedActivity = lastResumedActivity;
2156 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002157 Slog.i(TAG, "Restarting because process died: " + next);
2158 if (!next.hasBeenLaunched) {
2159 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002160 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2161 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002162 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002163 next.appToken, next.packageName, next.theme,
2164 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002165 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2166 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002167 }
George Mount2c92c972014-03-20 09:38:23 -07002168 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002169 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002170 return true;
2171 }
2172
2173 // From this point on, if something goes wrong there is no way
2174 // to recover the activity.
2175 try {
2176 next.visible = true;
2177 completeResumeLocked(next);
2178 } catch (Exception e) {
2179 // If any exception gets thrown, toss away this
2180 // activity and try the next one.
2181 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002182 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002183 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002184 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002185 return true;
2186 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002187 next.stopped = false;
2188
2189 } else {
2190 // Whoops, need to restart this activity!
2191 if (!next.hasBeenLaunched) {
2192 next.hasBeenLaunched = true;
2193 } else {
2194 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002195 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002196 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002197 mService.compatibilityInfoForPackageLocked(
2198 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002199 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002200 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002201 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002202 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002203 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002204 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002205 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002206 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002207 }
2208
Craig Mautnercf910b02013-04-23 11:23:27 -07002209 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002210 return true;
2211 }
2212
riddle_hsuc215a4f2014-12-27 12:10:45 +08002213 private TaskRecord getNextTask(TaskRecord targetTask) {
2214 final int index = mTaskHistory.indexOf(targetTask);
2215 if (index >= 0) {
2216 final int numTasks = mTaskHistory.size();
2217 for (int i = index + 1; i < numTasks; ++i) {
2218 TaskRecord task = mTaskHistory.get(i);
2219 if (task.userId == targetTask.userId) {
2220 return task;
2221 }
2222 }
2223 }
2224 return null;
2225 }
2226
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002227 private void insertTaskAtPosition(TaskRecord task, int position) {
2228 if (position >= mTaskHistory.size()) {
2229 insertTaskAtTop(task, null);
2230 return;
2231 }
2232 // Calculate maximum possible position for this task.
2233 int maxPosition = mTaskHistory.size();
2234 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002235 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002236 // Put non-current user tasks below current user tasks.
2237 while (maxPosition > 0) {
2238 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2239 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002240 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002241 break;
2242 }
2243 maxPosition--;
2244 }
2245 }
2246 position = Math.min(position, maxPosition);
2247 mTaskHistory.remove(task);
2248 mTaskHistory.add(position, task);
2249 updateTaskMovement(task, true);
2250 }
2251
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002252 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002253 // If the moving task is over home stack, transfer its return type to next task
2254 if (task.isOverHomeStack()) {
2255 final TaskRecord nextTask = getNextTask(task);
2256 if (nextTask != null) {
2257 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2258 }
2259 }
2260
Craig Mautner9c85c202013-10-04 20:11:26 -07002261 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002262 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002263 if (isOnHomeDisplay()) {
2264 ActivityStack lastStack = mStackSupervisor.getLastStack();
2265 final boolean fromHome = lastStack.isHomeStack();
2266 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002267 task.setTaskToReturnTo(fromHome
2268 ? lastStack.topTask() == null
2269 ? HOME_ACTIVITY_TYPE
2270 : lastStack.topTask().taskType
2271 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002272 }
2273 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002274 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002275 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002276
Craig Mautnerac6f8432013-07-17 13:24:59 -07002277 mTaskHistory.remove(task);
2278 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002279 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002280 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002281 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002282 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002283 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002284 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002285 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002286 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002287 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002288 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002289 break;
2290 }
2291 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002292 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002293 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002294 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002295 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002296 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002297
Craig Mautner8849a5e2013-04-02 16:41:03 -07002298 final void startActivityLocked(ActivityRecord r, boolean newTask,
Chong Zhang280d3322015-11-03 17:27:26 -08002299 boolean doResume, boolean keepCurTransition, ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002300 TaskRecord rTask = r.task;
2301 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002302 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2303 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002304 // Last activity in task had been removed or ActivityManagerService is reusing task.
2305 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002306 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002307 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002308 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002309 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002310 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002311 if (!newTask) {
2312 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002313 boolean startIt = true;
2314 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2315 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002316 if (task.getTopActivity() == null) {
2317 // All activities in task are finishing.
2318 continue;
2319 }
Craig Mautner70a86932013-02-28 22:37:44 -08002320 if (task == r.task) {
2321 // Here it is! Now, if this is not yet visible to the
2322 // user, then just add it without starting; it will
2323 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002324 if (!startIt) {
2325 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2326 + task, new RuntimeException("here").fillInStackTrace());
2327 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002328 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002329 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002330 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002331 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002332 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002333 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002334 return;
2335 }
2336 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002337 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002338 startIt = false;
2339 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002340 }
2341 }
2342
2343 // Place a new activity at top of stack, so it is next to interact
2344 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002345
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002346 // If we are not placing the new activity frontmost, we do not want
2347 // to deliver the onUserLeaving callback to the actual frontmost
2348 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002349 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002350 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002351 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002352 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002353 }
Craig Mautner70a86932013-02-28 22:37:44 -08002354
2355 task = r.task;
2356
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002357 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002358 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002359 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002360 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002361 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002362
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002363 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002364 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002365 // We want to show the starting preview window if we are
2366 // switching to a new task, or the next activity's process is
2367 // not currently running.
2368 boolean showStartingIcon = newTask;
2369 ProcessRecord proc = r.app;
2370 if (proc == null) {
2371 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2372 }
2373 if (proc == null || proc.thread == null) {
2374 showStartingIcon = true;
2375 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002376 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002377 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002378 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002379 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002380 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002381 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002382 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002383 ? r.mLaunchTaskBehind
2384 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2385 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002386 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002387 mNoAnimActivities.remove(r);
2388 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002389 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002390 boolean doShow = true;
2391 if (newTask) {
2392 // Even though this activity is starting fresh, we still need
2393 // to reset it to make sure we apply affinities to move any
2394 // existing activities from other tasks in to it.
2395 // If the caller has requested that the target task be
2396 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002397 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002398 resetTaskIfNeededLocked(r, r);
2399 doShow = topRunningNonDelayedActivityLocked(null) == r;
2400 }
Chong Zhang280d3322015-11-03 17:27:26 -08002401 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002402 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2403 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002404 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002405 if (r.mLaunchTaskBehind) {
2406 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2407 // tell WindowManager that r is visible even though it is at the back of the stack.
2408 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002409 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002410 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002411 // Figure out if we are transitioning from another activity that is
2412 // "has the same starting icon" as the next one. This allows the
2413 // window manager to keep the previous window it had previously
2414 // created, if it still had one.
2415 ActivityRecord prev = mResumedActivity;
2416 if (prev != null) {
2417 // We don't want to reuse the previous starting preview if:
2418 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002419 if (prev.task != r.task) {
2420 prev = null;
2421 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002422 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002423 else if (prev.nowVisible) {
2424 prev = null;
2425 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002426 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002427 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002428 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002429 mService.compatibilityInfoForPackageLocked(
2430 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002431 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002432 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002433 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002434 }
2435 } else {
2436 // If this is the first activity, don't do any fancy animations,
2437 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002438 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002439 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002440 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002441 }
2442 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002443 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002444 }
2445
2446 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002447 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002448 } else {
2449 addRecentActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002450 }
2451 }
2452
Dianne Hackbornbe707852011-11-11 14:32:10 -08002453 final void validateAppTokensLocked() {
2454 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002455 mValidateAppTokens.ensureCapacity(numActivities());
2456 final int numTasks = mTaskHistory.size();
2457 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2458 TaskRecord task = mTaskHistory.get(taskNdx);
2459 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002460 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002461 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002462 }
Craig Mautner000f0022013-02-26 15:04:29 -08002463 TaskGroup group = new TaskGroup();
2464 group.taskId = task.taskId;
2465 mValidateAppTokens.add(group);
2466 final int numActivities = activities.size();
2467 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2468 final ActivityRecord r = activities.get(activityNdx);
2469 group.tokens.add(r.appToken);
2470 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002471 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002472 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002473 }
2474
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002475 /**
2476 * Perform a reset of the given task, if needed as part of launching it.
2477 * Returns the new HistoryRecord at the top of the task.
2478 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002479 /**
2480 * Helper method for #resetTaskIfNeededLocked.
2481 * We are inside of the task being reset... we'll either finish this activity, push it out
2482 * for another task, or leave it as-is.
2483 * @param task The task containing the Activity (taskTop) that might be reset.
2484 * @param forceReset
2485 * @return An ActivityOptions that needs to be processed.
2486 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002487 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002488 ActivityOptions topOptions = null;
2489
2490 int replyChainEnd = -1;
2491 boolean canMoveOptions = true;
2492
2493 // We only do this for activities that are not the root of the task (since if we finish
2494 // the root, we may no longer have the task!).
2495 final ArrayList<ActivityRecord> activities = task.mActivities;
2496 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002497 final int rootActivityNdx = task.findEffectiveRootIndex();
2498 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002499 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002500 if (target.frontOfTask)
2501 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002502
2503 final int flags = target.info.flags;
2504 final boolean finishOnTaskLaunch =
2505 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2506 final boolean allowTaskReparenting =
2507 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2508 final boolean clearWhenTaskReset =
2509 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2510
2511 if (!finishOnTaskLaunch
2512 && !clearWhenTaskReset
2513 && target.resultTo != null) {
2514 // If this activity is sending a reply to a previous
2515 // activity, we can't do anything with it now until
2516 // we reach the start of the reply chain.
2517 // XXX note that we are assuming the result is always
2518 // to the previous activity, which is almost always
2519 // the case but we really shouldn't count on.
2520 if (replyChainEnd < 0) {
2521 replyChainEnd = i;
2522 }
2523 } else if (!finishOnTaskLaunch
2524 && !clearWhenTaskReset
2525 && allowTaskReparenting
2526 && target.taskAffinity != null
2527 && !target.taskAffinity.equals(task.affinity)) {
2528 // If this activity has an affinity for another
2529 // task, then we need to move it out of here. We will
2530 // move it as far out of the way as possible, to the
2531 // bottom of the activity stack. This also keeps it
2532 // correctly ordered with any activities we previously
2533 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002534 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002535 final ActivityRecord bottom =
2536 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002537 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002538 if (bottom != null && target.taskAffinity != null
2539 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002540 // If the activity currently at the bottom has the
2541 // same task affinity as the one we are moving,
2542 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002543 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002544 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002545 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002546 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002547 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002548 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002549 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002550 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002551 + " out to new task " + target.task);
2552 }
2553
Wale Ogunwale706ed792015-08-02 10:29:44 -07002554 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002555
Craig Mautner525f3d92013-05-07 14:01:50 -07002556 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002557 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2558 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002559 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002560 if (p.finishing) {
2561 continue;
2562 }
2563
Craig Mautnere3a74d52013-02-22 14:14:58 -08002564 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002565 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002566 topOptions = p.takeOptionsLocked();
2567 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002568 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002569 }
2570 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002571 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2572 "Removing activity " + p + " from task=" + task + " adding to task="
2573 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002574 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2575 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002576 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002577 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002578
Wale Ogunwale706ed792015-08-02 10:29:44 -07002579 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002580 }
2581
Wale Ogunwale706ed792015-08-02 10:29:44 -07002582 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002583 if (VALIDATE_TOKENS) {
2584 validateAppTokensLocked();
2585 }
2586
2587 replyChainEnd = -1;
2588 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2589 // If the activity should just be removed -- either
2590 // because it asks for it, or the task should be
2591 // cleared -- then finish it and anything that is
2592 // part of its reply chain.
2593 int end;
2594 if (clearWhenTaskReset) {
2595 // In this case, we want to finish this activity
2596 // and everything above it, so be sneaky and pretend
2597 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002598 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002599 } else if (replyChainEnd < 0) {
2600 end = i;
2601 } else {
2602 end = replyChainEnd;
2603 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002604 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002605 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002606 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002607 if (p.finishing) {
2608 continue;
2609 }
2610 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002611 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002612 topOptions = p.takeOptionsLocked();
2613 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002614 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002615 }
2616 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002617 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002618 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002619 if (finishActivityLocked(
2620 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002621 end--;
2622 srcPos--;
2623 }
2624 }
2625 replyChainEnd = -1;
2626 } else {
2627 // If we were in the middle of a chain, well the
2628 // activity that started it all doesn't want anything
2629 // special, so leave it all as-is.
2630 replyChainEnd = -1;
2631 }
2632 }
2633
2634 return topOptions;
2635 }
2636
2637 /**
2638 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2639 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2640 * @param affinityTask The task we are looking for an affinity to.
2641 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2642 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2643 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2644 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002645 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002646 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002647 int replyChainEnd = -1;
2648 final int taskId = task.taskId;
2649 final String taskAffinity = task.affinity;
2650
2651 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2652 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002653 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2654
2655 // Do not operate on or below the effective root Activity.
2656 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002657 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002658 if (target.frontOfTask)
2659 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002660
2661 final int flags = target.info.flags;
2662 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2663 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2664
2665 if (target.resultTo != null) {
2666 // If this activity is sending a reply to a previous
2667 // activity, we can't do anything with it now until
2668 // we reach the start of the reply chain.
2669 // XXX note that we are assuming the result is always
2670 // to the previous activity, which is almost always
2671 // the case but we really shouldn't count on.
2672 if (replyChainEnd < 0) {
2673 replyChainEnd = i;
2674 }
2675 } else if (topTaskIsHigher
2676 && allowTaskReparenting
2677 && taskAffinity != null
2678 && taskAffinity.equals(target.taskAffinity)) {
2679 // This activity has an affinity for our task. Either remove it if we are
2680 // clearing or move it over to our task. Note that
2681 // we currently punt on the case where we are resetting a
2682 // task that is not at the top but who has activities above
2683 // with an affinity to it... this is really not a normal
2684 // case, and we will need to later pull that task to the front
2685 // and usually at that point we will do the reset and pick
2686 // up those remaining activities. (This only happens if
2687 // someone starts an activity in a new task from an activity
2688 // in a task that is not currently on top.)
2689 if (forceReset || finishOnTaskLaunch) {
2690 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002691 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2692 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002693 for (int srcPos = start; srcPos >= i; --srcPos) {
2694 final ActivityRecord p = activities.get(srcPos);
2695 if (p.finishing) {
2696 continue;
2697 }
Todd Kennedy539db512014-12-15 09:57:55 -08002698 finishActivityLocked(
2699 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002700 }
2701 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002702 if (taskInsertionPoint < 0) {
2703 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002704
Craig Mautner77878772013-03-04 19:46:24 -08002705 }
Craig Mautner77878772013-03-04 19:46:24 -08002706
2707 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002708 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2709 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2710 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002711 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002712 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002713 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002714 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002715
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002716 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2717 "Removing and adding activity " + p + " to stack at " + task
2718 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002719 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2720 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002721 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002722 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002723 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002724 if (VALIDATE_TOKENS) {
2725 validateAppTokensLocked();
2726 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002727
2728 // Now we've moved it in to place... but what if this is
2729 // a singleTop activity and we have put it on top of another
2730 // instance of the same activity? Then we drop the instance
2731 // below so it remains singleTop.
2732 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2733 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002734 int targetNdx = taskActivities.indexOf(target);
2735 if (targetNdx > 0) {
2736 ActivityRecord p = taskActivities.get(targetNdx - 1);
2737 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002738 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2739 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002740 }
2741 }
2742 }
2743 }
2744
2745 replyChainEnd = -1;
2746 }
2747 }
Craig Mautner77878772013-03-04 19:46:24 -08002748 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002749 }
2750
Craig Mautner8849a5e2013-04-02 16:41:03 -07002751 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002752 ActivityRecord newActivity) {
2753 boolean forceReset =
2754 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2755 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2756 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2757 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2758 forceReset = true;
2759 }
2760 }
2761
2762 final TaskRecord task = taskTop.task;
2763
2764 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2765 * for remaining tasks. Used for later tasks to reparent to task. */
2766 boolean taskFound = false;
2767
2768 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2769 ActivityOptions topOptions = null;
2770
Craig Mautner77878772013-03-04 19:46:24 -08002771 // Preserve the location for reparenting in the new task.
2772 int reparentInsertionPoint = -1;
2773
Craig Mautnere3a74d52013-02-22 14:14:58 -08002774 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2775 final TaskRecord targetTask = mTaskHistory.get(i);
2776
2777 if (targetTask == task) {
2778 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2779 taskFound = true;
2780 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002781 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2782 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002783 }
2784 }
2785
Craig Mautner70a86932013-02-28 22:37:44 -08002786 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002787 if (taskNdx >= 0) {
2788 do {
2789 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2790 } while (taskTop == null && taskNdx >= 0);
2791 }
Craig Mautner70a86932013-02-28 22:37:44 -08002792
Craig Mautnere3a74d52013-02-22 14:14:58 -08002793 if (topOptions != null) {
2794 // If we got some ActivityOptions from an activity on top that
2795 // was removed from the task, propagate them to the new real top.
2796 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002797 taskTop.updateOptionsLocked(topOptions);
2798 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002799 topOptions.abort();
2800 }
2801 }
2802
2803 return taskTop;
2804 }
2805
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002806 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2807 String resultWho, int requestCode, int resultCode, Intent data) {
2808
2809 if (callingUid > 0) {
2810 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002811 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002812 }
2813
2814 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2815 + " : who=" + resultWho + " req=" + requestCode
2816 + " res=" + resultCode + " data=" + data);
2817 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2818 try {
2819 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2820 list.add(new ResultInfo(resultWho, requestCode,
2821 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002822 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002823 return;
2824 } catch (Exception e) {
2825 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2826 }
2827 }
2828
2829 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2830 }
2831
Craig Mautner299f9602015-01-26 09:47:33 -08002832 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002833 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2834 return;
2835 }
2836
2837 final ActivityRecord next = topRunningActivityLocked();
2838 final String myReason = reason + " adjustFocus";
2839 if (next != r) {
2840 if (next != null && StackId.keepFocusInStackIfPossible(mStackId)) {
2841 // For freeform, docked, and pinned stacks we always keep the focus within the
2842 // stack as long as there is a running activity in the stack that we can adjust
2843 // focus to.
2844 mService.setFocusedActivityLocked(next, myReason);
2845 return;
2846 } else {
2847 final TaskRecord task = r.task;
2848 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2849 // For non-fullscreen stack, we want to move the focus to the next visible
2850 // stack to prevent the home screen from moving to the top and obscuring
2851 // other visible stacks.
2852 if (!mFullscreen
2853 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2854 return;
2855 }
2856 // Move the home stack to the top if this stack is fullscreen or there is no
2857 // other visible stack.
2858 if (mStackSupervisor.moveHomeStackTaskToTop(
2859 task.getTaskToReturnTo(), myReason)) {
2860 // Activity focus was already adjusted. Nothing else to do...
2861 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002862 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002863 }
2864 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08002865 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002866
Wale Ogunwaled045c822015-12-02 09:14:28 -08002867 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2868 if (top != null) {
2869 mService.setFocusedActivityLocked(top, myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002870 }
2871 }
2872
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002873 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2874 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2875 final String myReason = reason + " adjustFocusToNextVisibleStack";
2876 if (stack == null) {
2877 return false;
2878 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002879 final ActivityRecord top = stack.topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002880 if (top == null) {
2881 return false;
2882 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002883 mService.setFocusedActivityLocked(top, myReason);
2884 return true;
2885 }
2886
Craig Mautnerf3333272013-04-22 10:55:53 -07002887 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002888 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002889 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2890 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2891 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002892 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002893 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002894 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2895 "stop-no-history", false)) {
2896 // Activity was finished, no need to continue trying to schedule stop.
2897 adjustFocusedActivityLocked(r, "stopActivityFinished");
2898 r.resumeKeyDispatchingLocked();
2899 return;
2900 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002901 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002902 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002903 + " on stop because we're just sleeping");
2904 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002905 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002906 }
2907
2908 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002909 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002910 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002911 try {
2912 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002913 if (DEBUG_STATES) Slog.v(TAG_STATES,
2914 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002915 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002916 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2917 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002918 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002919 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002920 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002921 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002922 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002923 r.setSleeping(true);
2924 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002925 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002926 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 } catch (Exception e) {
2928 // Maybe just ignore exceptions here... if the process
2929 // has crashed, our death notification will clean things
2930 // up.
2931 Slog.w(TAG, "Exception thrown during pause", e);
2932 // Just in case, assume it to be stopped.
2933 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002934 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002935 r.state = ActivityState.STOPPED;
2936 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002937 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002938 }
2939 }
2940 }
2941 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002942
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002943 /**
2944 * @return Returns true if the activity is being finished, false if for
2945 * some reason it is being left as-is.
2946 */
2947 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002948 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002949 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002950 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2951 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002952 + ", result=" + resultCode + ", data=" + resultData
2953 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002954 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002955 return false;
2956 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002957
Craig Mautnerd44711d2013-02-23 11:24:36 -08002958 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002959 return true;
2960 }
2961
Craig Mautnerd2328952013-03-05 12:46:26 -08002962 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002963 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2964 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2965 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2966 ActivityRecord r = activities.get(activityNdx);
2967 if (r.resultTo == self && r.requestCode == requestCode) {
2968 if ((r.resultWho == null && resultWho == null) ||
2969 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2970 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2971 false);
2972 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002973 }
2974 }
2975 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002976 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002977 }
2978
Todd Kennedy539db512014-12-15 09:57:55 -08002979 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002980 ActivityRecord r = topRunningActivityLocked();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002981 if (r != null && r.app == app) {
2982 // If the top running activity is from this crashing
2983 // process, then terminate it to avoid getting in a loop.
2984 Slog.w(TAG, " Force finishing activity "
2985 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002986 int taskNdx = mTaskHistory.indexOf(r.task);
2987 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002988 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002989 // Also terminate any activities below it that aren't yet
2990 // stopped, to avoid a situation where one will get
2991 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002992 --activityNdx;
2993 if (activityNdx < 0) {
2994 do {
2995 --taskNdx;
2996 if (taskNdx < 0) {
2997 break;
2998 }
2999 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3000 } while (activityNdx < 0);
3001 }
3002 if (activityNdx >= 0) {
3003 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003004 if (r.state == ActivityState.RESUMED
3005 || r.state == ActivityState.PAUSING
3006 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07003007 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003008 Slog.w(TAG, " Force finishing activity "
3009 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08003010 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003011 }
3012 }
3013 }
3014 }
3015 }
3016
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003017 final void finishVoiceTask(IVoiceInteractionSession session) {
3018 IBinder sessionBinder = session.asBinder();
3019 boolean didOne = false;
3020 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3021 TaskRecord tr = mTaskHistory.get(taskNdx);
3022 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3023 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3024 ActivityRecord r = tr.mActivities.get(activityNdx);
3025 if (!r.finishing) {
3026 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3027 false);
3028 didOne = true;
3029 }
3030 }
3031 }
3032 }
3033 if (didOne) {
3034 mService.updateOomAdjLocked();
3035 }
3036 }
3037
Craig Mautnerd2328952013-03-05 12:46:26 -08003038 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003039 ArrayList<ActivityRecord> activities = r.task.mActivities;
3040 for (int index = activities.indexOf(r); index >= 0; --index) {
3041 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003042 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003043 break;
3044 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003045 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003046 }
3047 return true;
3048 }
3049
Dianne Hackborn5c607432012-02-28 14:44:19 -08003050 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3051 // send the result
3052 ActivityRecord resultTo = r.resultTo;
3053 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003054 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003055 + " who=" + r.resultWho + " req=" + r.requestCode
3056 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003057 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003058 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003059 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003060 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003061 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003062 if (r.info.applicationInfo.uid > 0) {
3063 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3064 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003065 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003066 }
3067 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3068 resultData);
3069 r.resultTo = null;
3070 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003071 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003072
3073 // Make sure this HistoryRecord is not holding on to other resources,
3074 // because clients have remote IPC references to this object so we
3075 // can't assume that will go away and want to avoid circular IPC refs.
3076 r.results = null;
3077 r.pendingResults = null;
3078 r.newIntents = null;
3079 r.icicle = null;
3080 }
3081
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003082 /**
3083 * @return Returns true if this activity has been removed from the history
3084 * list, or false if it is still in the list and will be removed later.
3085 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003086 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3087 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003088 if (r.finishing) {
3089 Slog.w(TAG, "Duplicate finish request for " + r);
3090 return false;
3091 }
3092
Wale Ogunwale7d701172015-03-11 15:36:30 -07003093 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003094 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003095 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003096 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003097 task.taskId, r.shortComponentName, reason);
3098 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003099 final int index = activities.indexOf(r);
3100 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003101 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003102 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003103 // If the caller asked that this activity (and all above it)
3104 // be cleared when the task is reset, don't lose that information,
3105 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003106 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003107 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003108 }
3109 }
3110
3111 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003112
Craig Mautner299f9602015-01-26 09:47:33 -08003113 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003114
Dianne Hackborn5c607432012-02-28 14:44:19 -08003115 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003116
Craig Mautnerde4ef022013-04-07 19:01:33 -07003117 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003118 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003119 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003120 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003121 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003122 ? AppTransition.TRANSIT_TASK_CLOSE
3123 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003124
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003125 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003126 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003127
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003128 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003129 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3130 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3131 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003132 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003133 }
3134
Craig Mautneraea74a52014-03-08 14:23:10 -08003135 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003136 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003137 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003138 } else if (r.state != ActivityState.PAUSING) {
3139 // If the activity is PAUSING, we will complete the finish once
3140 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003141 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003142 if (r.visible) {
3143 mWindowManager.setAppVisibility(r.appToken, false);
3144 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003145 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003146 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003147 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003148 }
3149
3150 return false;
3151 }
3152
Craig Mautnerf3333272013-04-22 10:55:53 -07003153 static final int FINISH_IMMEDIATELY = 0;
3154 static final int FINISH_AFTER_PAUSE = 1;
3155 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003156
Craig Mautnerf3333272013-04-22 10:55:53 -07003157 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003158 // First things first: if this activity is currently visible,
3159 // and the resumed activity is not yet visible, then hold off on
3160 // finishing until the resumed one becomes visible.
3161 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003162 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
3163 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07003164 if (mStackSupervisor.mStoppingActivities.size() > 3
3165 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003166 // If we already have a few activities waiting to stop,
3167 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07003168 // them out. Or if r is the last of activity of the last task the stack
3169 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07003170 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003171 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003172 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003173 }
3174 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003175 if (DEBUG_STATES) Slog.v(TAG_STATES,
3176 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003177 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003178 if (oomAdj) {
3179 mService.updateOomAdjLocked();
3180 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003181 return r;
3182 }
3183
3184 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003185 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003186 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003187 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003188 if (mResumedActivity == r) {
3189 mResumedActivity = null;
3190 }
3191 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003192 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003193 r.state = ActivityState.FINISHING;
3194
3195 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003196 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003197 || prevState == ActivityState.STOPPED
3198 || prevState == ActivityState.INITIALIZING) {
3199 // If this activity is already stopped, we can just finish
3200 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003201 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003202 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003203 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003204 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003205 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003206 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003207 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3208 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003209 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003210 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003211
3212 // Need to go through the full pause cycle to get this
3213 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003214 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003215 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003216 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003217 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003218 return r;
3219 }
3220
Craig Mautneree36c772014-07-16 14:56:05 -07003221 void finishAllActivitiesLocked(boolean immediately) {
3222 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003223 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3224 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3225 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3226 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003227 noActivitiesInStack = false;
3228 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003229 continue;
3230 }
Craig Mautneree36c772014-07-16 14:56:05 -07003231 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003232 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3233 }
3234 }
Craig Mautneree36c772014-07-16 14:56:05 -07003235 if (noActivitiesInStack) {
3236 mActivityContainer.onTaskListEmptyLocked();
3237 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003238 }
3239
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003240 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3241 // Basic case: for simple app-centric recents, we need to recreate
3242 // the task if the affinity has changed.
3243 if (srec == null || srec.task.affinity == null ||
3244 !srec.task.affinity.equals(destAffinity)) {
3245 return true;
3246 }
3247 // Document-centric case: an app may be split in to multiple documents;
3248 // they need to re-create their task if this current activity is the root
3249 // of a document, unless simply finishing it will return them to the the
3250 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003251 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3252 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003253 // Okay, this activity is at the root of its task. What to do, what to do...
3254 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3255 // Finishing won't return to an application, so we need to recreate.
3256 return true;
3257 }
3258 // We now need to get the task below it to determine what to do.
3259 int taskIdx = mTaskHistory.indexOf(srec.task);
3260 if (taskIdx <= 0) {
3261 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3262 return false;
3263 }
3264 if (taskIdx == 0) {
3265 // At the bottom of the stack, nothing to go back to.
3266 return true;
3267 }
3268 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3269 if (!srec.task.affinity.equals(prevTask.affinity)) {
3270 // These are different apps, so need to recreate.
3271 return true;
3272 }
3273 }
3274 return false;
3275 }
3276
Wale Ogunwale7d701172015-03-11 15:36:30 -07003277 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003278 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003279 final TaskRecord task = srec.task;
3280 final ArrayList<ActivityRecord> activities = task.mActivities;
3281 final int start = activities.indexOf(srec);
3282 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003283 return false;
3284 }
3285 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003286 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003287 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003288 final ComponentName dest = destIntent.getComponent();
3289 if (start > 0 && dest != null) {
3290 for (int i = finishTo; i >= 0; i--) {
3291 ActivityRecord r = activities.get(i);
3292 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003293 r.info.name.equals(dest.getClassName())) {
3294 finishTo = i;
3295 parent = r;
3296 foundParentInTask = true;
3297 break;
3298 }
3299 }
3300 }
3301
3302 IActivityController controller = mService.mController;
3303 if (controller != null) {
3304 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3305 if (next != null) {
3306 // ask watcher if this is allowed
3307 boolean resumeOK = true;
3308 try {
3309 resumeOK = controller.activityResuming(next.packageName);
3310 } catch (RemoteException e) {
3311 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003312 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003313 }
3314
3315 if (!resumeOK) {
3316 return false;
3317 }
3318 }
3319 }
3320 final long origId = Binder.clearCallingIdentity();
3321 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003322 ActivityRecord r = activities.get(i);
3323 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003324 // Only return the supplied result for the first activity finished
3325 resultCode = Activity.RESULT_CANCELED;
3326 resultData = null;
3327 }
3328
3329 if (parent != null && foundParentInTask) {
3330 final int parentLaunchMode = parent.info.launchMode;
3331 final int destIntentFlags = destIntent.getFlags();
3332 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3333 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3334 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3335 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003336 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3337 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003338 } else {
3339 try {
3340 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3341 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003342 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003343 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003344 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003345 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003346 foundParentInTask = res == ActivityManager.START_SUCCESS;
3347 } catch (RemoteException e) {
3348 foundParentInTask = false;
3349 }
3350 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003351 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003352 }
3353 }
3354 Binder.restoreCallingIdentity(origId);
3355 return foundParentInTask;
3356 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003357 /**
3358 * Perform the common clean-up of an activity record. This is called both
3359 * as part of destroyActivityLocked() (when destroying the client-side
3360 * representation) and cleaning things up as a result of its hosting
3361 * processing going away, in which case there is no remaining client-side
3362 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003363 *
3364 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003365 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003366 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3367 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003368 if (mResumedActivity == r) {
3369 mResumedActivity = null;
3370 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003371 if (mPausingActivity == r) {
3372 mPausingActivity = null;
3373 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003374 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003375
3376 r.configDestroy = false;
3377 r.frozenBeforeDestroy = false;
3378
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003379 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003380 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003381 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003382 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003383 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003384 }
3385
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003386 // Make sure this record is no longer in the pending finishes list.
3387 // This could happen, for example, if we are trimming activities
3388 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003389 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003390 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003391
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003392 // Remove any pending results.
3393 if (r.finishing && r.pendingResults != null) {
3394 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3395 PendingIntentRecord rec = apr.get();
3396 if (rec != null) {
3397 mService.cancelIntentSenderLocked(rec, false);
3398 }
3399 }
3400 r.pendingResults = null;
3401 }
3402
3403 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003404 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003405 }
3406
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003407 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003408 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003409 if (getVisibleBehindActivity() == r) {
3410 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003411 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003412 }
3413
3414 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003415 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003416 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003417 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003418 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003419 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003420 }
3421
Craig Mautner299f9602015-01-26 09:47:33 -08003422 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003423 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003424 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003425 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003426 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3427 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3428
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003429 r.takeFromHistory();
3430 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003431 if (DEBUG_STATES) Slog.v(TAG_STATES,
3432 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003433 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003434 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003435 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003436 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003437 if (VALIDATE_TOKENS) {
3438 validateAppTokensLocked();
3439 }
Craig Mautner312ba862014-02-10 17:55:01 -08003440 final TaskRecord task = r.task;
3441 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003442 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003443 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003444 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003445 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003446 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003447 }
Craig Mautner299f9602015-01-26 09:47:33 -08003448 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003449 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003450 cleanUpActivityServicesLocked(r);
3451 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003452 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003453
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003454 /**
3455 * Perform clean-up of service connections in an activity record.
3456 */
3457 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3458 // Throw away any services that have been bound by this activity.
3459 if (r.connections != null) {
3460 Iterator<ConnectionRecord> it = r.connections.iterator();
3461 while (it.hasNext()) {
3462 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003463 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003464 }
3465 r.connections = null;
3466 }
3467 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003468
Craig Mautneree2e45a2014-06-27 12:10:03 -07003469 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003470 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003471 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003472 mHandler.sendMessage(msg);
3473 }
3474
Craig Mautneree2e45a2014-06-27 12:10:03 -07003475 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003476 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003477 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003478 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3479 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3480 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3481 final ActivityRecord r = activities.get(activityNdx);
3482 if (r.finishing) {
3483 continue;
3484 }
3485 if (r.fullscreen) {
3486 lastIsOpaque = true;
3487 }
3488 if (owner != null && r.app != owner) {
3489 continue;
3490 }
3491 if (!lastIsOpaque) {
3492 continue;
3493 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003494 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003495 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003496 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003497 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003498 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003499 activityRemoved = true;
3500 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003501 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003502 }
3503 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003504 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003505 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003506 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003507 }
3508
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003509 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3510 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003511 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3512 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003513 + " pausing=" + mPausingActivity + " for reason " + reason);
3514 return destroyActivityLocked(r, true, reason);
3515 }
3516 return false;
3517 }
3518
3519 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3520 String reason) {
3521 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003522 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003523 int maxTasks = tasks.size() / 4;
3524 if (maxTasks < 1) {
3525 maxTasks = 1;
3526 }
3527 int numReleased = 0;
3528 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3529 final TaskRecord task = mTaskHistory.get(taskNdx);
3530 if (!tasks.contains(task)) {
3531 continue;
3532 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003533 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003534 int curNum = 0;
3535 final ArrayList<ActivityRecord> activities = task.mActivities;
3536 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3537 final ActivityRecord activity = activities.get(actNdx);
3538 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003539 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003540 + " in state " + activity.state + " resumed=" + mResumedActivity
3541 + " pausing=" + mPausingActivity + " for reason " + reason);
3542 destroyActivityLocked(activity, true, reason);
3543 if (activities.get(actNdx) != activity) {
3544 // Was removed from list, back up so we don't miss the next one.
3545 actNdx--;
3546 }
3547 curNum++;
3548 }
3549 }
3550 if (curNum > 0) {
3551 numReleased += curNum;
3552 maxTasks--;
3553 if (mTaskHistory.get(taskNdx) != task) {
3554 // The entire task got removed, back up so we don't miss the next one.
3555 taskNdx--;
3556 }
3557 }
3558 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003559 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3560 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003561 return numReleased;
3562 }
3563
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003564 /**
3565 * Destroy the current CLIENT SIDE instance of an activity. This may be
3566 * called both when actually finishing an activity, or when performing
3567 * a configuration switch where we destroy the current client-side object
3568 * but then create a new client-side object for this same HistoryRecord.
3569 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003570 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003571 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3572 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003573 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003574 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003575 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003576 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003577
3578 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003579
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003580 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003581
3582 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003583
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003584 if (hadApp) {
3585 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003586 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003587 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3588 mService.mHeavyWeightProcess = null;
3589 mService.mHandler.sendEmptyMessage(
3590 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3591 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003592 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003593 // Update any services we are bound to that might care about whether
3594 // their client may have activities.
3595 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003596 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003597 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003598 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003599 }
3600 }
3601
3602 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003603
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003604 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003605 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003606 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003607 r.configChangeFlags);
3608 } catch (Exception e) {
3609 // We can just ignore exceptions here... if the process
3610 // has crashed, our death notification will clean things
3611 // up.
3612 //Slog.w(TAG, "Exception thrown during finish", e);
3613 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003614 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003615 removedFromHistory = true;
3616 skipDestroy = true;
3617 }
3618 }
3619
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003620 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003621
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003622 // If the activity is finishing, we need to wait on removing it
3623 // from the list to give it a chance to do its cleanup. During
3624 // that time it may make calls back with its token so we need to
3625 // be able to find it on the list and so we don't want to remove
3626 // it from the list yet. Otherwise, we can just immediately put
3627 // it in the destroyed state since we are not removing it from the
3628 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003629 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003630 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003631 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003632 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003633 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003634 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3635 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003636 if (DEBUG_STATES) Slog.v(TAG_STATES,
3637 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003638 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003639 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003640 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003641 }
3642 } else {
3643 // remove this record from the history.
3644 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003645 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003646 removedFromHistory = true;
3647 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003648 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003649 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003650 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003651 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003652 }
3653 }
3654
3655 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003656
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003657 if (!mLRUActivities.remove(r) && hadApp) {
3658 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3659 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003660
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003661 return removedFromHistory;
3662 }
3663
Craig Mautner299f9602015-01-26 09:47:33 -08003664 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003665 final long origId = Binder.clearCallingIdentity();
3666 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003667 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003668 if (r != null) {
3669 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003670 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003671 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003672
Wale Ogunwale60454db2015-01-23 16:05:07 -08003673 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003674 if (r.state == ActivityState.DESTROYING) {
3675 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003676 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003677 }
3678 }
Craig Mautner05d29032013-05-03 13:40:13 -07003679 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003680 } finally {
3681 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003682 }
3683 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003684
Todd Kennedyaab56db2015-01-30 09:39:53 -08003685 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003686 if (hasVisibleBehindActivity() &&
3687 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003688 if (r == topRunningActivityLocked()) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003689 // Don't release the top activity if it has requested to run behind the next
3690 // activity.
3691 return;
3692 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003693 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003694 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003695 " thread=" + r.app.thread);
3696 if (r != null && r.app != null && r.app.thread != null) {
3697 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003698 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003699 } catch (RemoteException e) {
3700 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003701 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003702 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003703 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003704 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003705 }
3706 }
3707 }
3708
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003709 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003710 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3711 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003712 if (r != null) {
3713 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003714 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003715 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003716 }
3717 mStackSupervisor.resumeTopActivitiesLocked();
3718 }
3719
Jose Lima4b6c6692014-08-12 17:41:12 -07003720 boolean hasVisibleBehindActivity() {
3721 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003722 }
3723
Jose Lima4b6c6692014-08-12 17:41:12 -07003724 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003725 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003726 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003727 }
3728 }
3729
Jose Lima4b6c6692014-08-12 17:41:12 -07003730 ActivityRecord getVisibleBehindActivity() {
3731 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003732 }
3733
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003734 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3735 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003736 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003737 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3738 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003739 while (i > 0) {
3740 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003741 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003742 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003743 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003744 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003745 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003746 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003747 }
3748 }
3749 }
3750
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003751 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3752 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003753 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3754 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003755 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3756 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003757 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003758 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003759 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3760 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003761
3762 boolean hasVisibleActivities = false;
3763
3764 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003765 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003766 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3767 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003768 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3769 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3770 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3771 final ActivityRecord r = activities.get(activityNdx);
3772 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003773 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3774 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003775 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003776 if (r.visible) {
3777 hasVisibleActivities = true;
3778 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003779 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003780 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3781 // Don't currently have state for the activity, or
3782 // it is finishing -- always remove it.
3783 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003784 } else if (!r.visible && r.launchCount > 2 &&
3785 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003786 // We have launched this activity too many times since it was
3787 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003788 // (Note if the activity is visible, we don't remove the record.
3789 // We leave the dead window on the screen but the process will
3790 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003791 remove = true;
3792 } else {
3793 // The process may be gone, but the activity lives on!
3794 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003795 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003796 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003797 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3798 "Removing activity " + r + " from stack at " + i
3799 + ": haveState=" + r.haveState
3800 + " stateNotNeeded=" + r.stateNotNeeded
3801 + " finishing=" + r.finishing
3802 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003803 if (!r.finishing) {
3804 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3805 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3806 r.userId, System.identityHashCode(r),
3807 r.task.taskId, r.shortComponentName,
3808 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003809 if (r.state == ActivityState.RESUMED) {
3810 mService.updateUsageStats(r, false);
3811 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003812 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003813 } else {
3814 // We have the current state for this activity, so
3815 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003816 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3817 if (DEBUG_APP) Slog.v(TAG_APP,
3818 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003819 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003820 // Set nowVisible to previous visible state. If the app was visible while
3821 // it died, we leave the dead window on screen so it's basically visible.
3822 // This is needed when user later tap on the dead window, we need to stop
3823 // other apps when user transfers focus to the restarted activity.
3824 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003825 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003826 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003827 "App died, clearing saved state of " + r);
3828 r.icicle = null;
3829 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003830 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003831 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003832 if (remove) {
3833 removeActivityFromHistoryLocked(r, "appDied");
3834 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003835 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003836 }
3837 }
3838
3839 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003840 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003841
Chong Zhang280d3322015-11-03 17:27:26 -08003842 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003843 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003844 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003845 if (r != null && r.state != ActivityState.RESUMED) {
3846 r.updateOptionsLocked(options);
3847 } else {
3848 ActivityOptions.abort(options);
3849 }
3850 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003851 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003852 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003853
Craig Mautner21d24a22014-04-23 11:45:37 -07003854 void updateTaskMovement(TaskRecord task, boolean toFront) {
3855 if (task.isPersistable) {
3856 task.mLastTimeMoved = System.currentTimeMillis();
3857 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3858 // recently will be most negative, tasks sent to the bottom before that will be less
3859 // negative. Similarly for recent tasks moved to the top which will be most positive.
3860 if (!toFront) {
3861 task.mLastTimeMoved *= -1;
3862 }
3863 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003864 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003865 }
3866
Craig Mautner84984fa2014-06-19 11:19:20 -07003867 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003868 final int top = mTaskHistory.size() - 1;
3869 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3870 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003871 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003872 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3873 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003874 mTaskHistory.remove(taskNdx);
3875 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003876 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003877 return;
3878 }
3879 }
3880 }
3881
Chong Zhang280d3322015-11-03 17:27:26 -08003882 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003883 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003884 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003885
Craig Mautner11bf9a52013-02-19 14:08:51 -08003886 final int numTasks = mTaskHistory.size();
3887 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003888 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003889 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003890 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003891 ActivityOptions.abort(options);
3892 } else {
3893 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3894 }
3895 return;
3896 }
3897
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003898 if (timeTracker != null) {
3899 // The caller wants a time tracker associated with this task.
3900 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3901 tr.mActivities.get(i).appTimeTracker = timeTracker;
3902 }
3903 }
3904
Craig Mautner11bf9a52013-02-19 14:08:51 -08003905 // Shift all activities with this task up to the top
3906 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003907 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003908
Chong Zhang45c25ce2015-08-10 22:18:26 -07003909 // Don't refocus if invisible to current user
3910 ActivityRecord top = tr.getTopActivity();
3911 if (!okToShowLocked(top)) {
3912 addRecentActivityLocked(top);
3913 ActivityOptions.abort(options);
3914 return;
3915 }
3916
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003917 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003918 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003919 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003920
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003921 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003922 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003923 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003924 if (r != null) {
3925 mNoAnimActivities.add(r);
3926 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003927 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003928 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003929 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003930 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003931
Craig Mautner05d29032013-05-03 13:40:13 -07003932 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003933 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003934
3935 if (VALIDATE_TOKENS) {
3936 validateAppTokensLocked();
3937 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003938 }
3939
3940 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003941 * Worker method for rearranging history stack. Implements the function of moving all
3942 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003943 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003944 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003945 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3946 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003947 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003948 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003949 * @return Returns true if the move completed, false if not.
3950 */
Craig Mautner299f9602015-01-26 09:47:33 -08003951 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003952 final TaskRecord tr = taskForIdLocked(taskId);
3953 if (tr == null) {
3954 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3955 return false;
3956 }
3957
3958 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003959 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003960
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003961 // If we have a watcher, preflight the move before committing to it. First check
3962 // for *other* available tasks, but if none are available, then try again allowing the
3963 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003964 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003965 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003966 if (next == null) {
3967 next = topRunningActivityLocked(null, 0);
3968 }
3969 if (next != null) {
3970 // ask watcher if this is allowed
3971 boolean moveOK = true;
3972 try {
3973 moveOK = mService.mController.activityResuming(next.packageName);
3974 } catch (RemoteException e) {
3975 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003976 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003977 }
3978 if (!moveOK) {
3979 return false;
3980 }
3981 }
3982 }
3983
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003984 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003985
riddle_hsuc215a4f2014-12-27 12:10:45 +08003986 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07003987
3988 // If true, we should resume the home activity next if the task we are moving to the
3989 // back is over the home stack. We force to false if the task we are moving to back
3990 // is the home task and we don't want it resumed after moving to the back.
3991 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
3992 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08003993 final TaskRecord nextTask = getNextTask(tr);
3994 if (nextTask != null) {
3995 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3996 } else {
3997 prevIsHome = true;
3998 }
3999 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004000 mTaskHistory.remove(tr);
4001 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004002 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004003
Craig Mautnerc8143c62013-09-03 12:15:57 -07004004 // There is an assumption that moving a task to the back moves it behind the home activity.
4005 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004006 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004007 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4008 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004009 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004010 break;
4011 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004012 if (taskNdx == 1) {
4013 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004014 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004015 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004016 }
4017
Craig Mautner299f9602015-01-26 09:47:33 -08004018 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004019 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004020
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004021 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004022 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004023 }
4024
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004025 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004026 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004027 if (!mService.mBooting && !mService.mBooted) {
4028 // Not ready yet!
4029 return false;
4030 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004031 final int taskToReturnTo = tr.getTaskToReturnTo();
4032 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004033 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004034 }
4035
Craig Mautner05d29032013-05-03 13:40:13 -07004036 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004037 return true;
4038 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004039
Craig Mautner8849a5e2013-04-02 16:41:03 -07004040 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004041 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004042 final Uri data = r.intent.getData();
4043 final String strData = data != null ? data.toSafeString() : null;
4044
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004045 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004046 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004047 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004048 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004049 }
4050
4051 /**
4052 * Make sure the given activity matches the current configuration. Returns
4053 * false if the activity had to be destroyed. Returns true if the
4054 * configuration is the same, or the activity will remain running as-is
4055 * for whatever reason. Ensures the HistoryRecord is updated with the
4056 * correct configuration and all other bookkeeping is handled.
4057 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004058 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4059 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004060 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004061 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004062 "Skipping config check (will change): " + r);
4063 return true;
4064 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004065
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004066 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004067 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004068
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004069 // Short circuit: if the two configurations are the exact same
4070 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004071 final Configuration newConfig = mService.mConfiguration;
4072 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004073 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004074 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004075 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004076 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004077 "Configuration unchanged in " + r);
4078 return true;
4079 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004080
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004081 // We don't worry about activities that are finishing.
4082 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004083 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004084 "Configuration doesn't matter in finishing " + r);
4085 r.stopFreezingScreenLocked(false);
4086 return true;
4087 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004088
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004089 // Okay we now are going to make this activity have the new config.
4090 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004091 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004092 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004093 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004094 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004095
Filip Gruszczynski23493322015-07-29 17:02:59 -07004096 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004097 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004098 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004099 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004100 "Configuration no differences in " + r);
4101 return true;
4102 }
4103
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004104 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4105 "Configuration changes for " + r + " ; taskChanges="
4106 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4107 + Configuration.configurationDiffToString(changes));
4108
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004109 // If the activity isn't currently running, just leave the new
4110 // configuration and it will pick that up next time it starts.
4111 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004112 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004113 "Configuration doesn't matter not running " + r);
4114 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004115 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004116 return true;
4117 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004118
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004119 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004120 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4121 "Checking to restart " + r.info.name + ": changed=0x"
4122 + Integer.toHexString(changes) + ", handles=0x"
4123 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
4124
Dianne Hackborne6676352011-06-01 16:51:20 -07004125 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004126 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4127 r.configChangeFlags |= changes;
4128 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004129 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004130 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004131 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004132 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004133 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004134 } else if (r.state == ActivityState.PAUSING) {
4135 // A little annoying: we are waiting for this activity to
4136 // finish pausing. Let's not do anything now, but just
4137 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004138 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004139 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004140 r.configDestroy = true;
4141 return true;
4142 } else if (r.state == ActivityState.RESUMED) {
4143 // Try to optimize this case: the configuration is changing
4144 // and we need to restart the top, resumed activity.
4145 // Instead of doing the normal handshaking, just say
4146 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004147 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004148 "Config is relaunching resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004149 relaunchActivityLocked(r, r.configChangeFlags, true,
4150 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004151 r.configChangeFlags = 0;
4152 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004153 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004154 "Config is relaunching non-resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004155 relaunchActivityLocked(r, r.configChangeFlags, false,
4156 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004157 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004158 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004159
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004160 // All done... tell the caller we weren't able to keep this
4161 // activity around.
4162 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004163 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004164
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004165 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004166 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004167 // changes is always sent to all processes when they happen so it can just use whatever
4168 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004169 if (r.app != null && r.app.thread != null) {
4170 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004171 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004172 r.app.thread.scheduleActivityConfigurationChanged(
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004173 r.appToken, new Configuration(taskConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004174 } catch (RemoteException e) {
4175 // If process died, whatever.
4176 }
4177 }
4178 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004180 return true;
4181 }
4182
Filip Gruszczynski23493322015-07-29 17:02:59 -07004183 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4184 Configuration oldTaskOverride) {
4185 // Determine what has changed. May be nothing, if this is a config
4186 // that has come back from the app after going idle. In that case
4187 // we just want to leave the official config object now in the
4188 // activity and do nothing else.
4189 int taskChanges = oldTaskOverride.diff(taskConfig);
4190 // We don't want to use size changes if they don't cross boundaries that are important to
4191 // the app.
4192 if ((taskChanges & ActivityInfo.CONFIG_SCREEN_SIZE) != 0) {
4193 final boolean crosses = record.crossesHorizontalSizeThreshold(
4194 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4195 || record.crossesVerticalSizeThreshold(
4196 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4197 if (!crosses) {
4198 taskChanges &= ~ActivityInfo.CONFIG_SCREEN_SIZE;
4199 }
4200 }
4201 if ((taskChanges & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4202 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4203 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004204 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004205 taskChanges &= ~ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4206 }
4207 }
4208 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4209 }
4210
4211 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4212 Configuration oldTaskOverride, int taskChanges) {
4213 if (taskChanges == 0) {
4214 // {@link Configuration#diff} doesn't catch changes from unset values.
4215 // Check for changes we care about.
4216 if (oldTaskOverride.orientation != taskConfig.orientation) {
4217 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
4218 }
4219 // We want to explicitly track situations where the size configuration goes from
4220 // undefined to defined. We don't care about crossing the threshold in that case,
4221 // because there is no threshold.
4222 final int oldHeight = oldTaskOverride.screenHeightDp;
4223 final int newHeight = taskConfig.screenHeightDp;
4224 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4225 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4226 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4227 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4228 }
4229 final int oldWidth = oldTaskOverride.screenWidthDp;
4230 final int newWidth = taskConfig.screenWidthDp;
4231 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4232 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4233 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4234 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4235 }
4236 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4237 final int newSmallest = taskConfig.smallestScreenWidthDp;
4238 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4239 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4240 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4241 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4242 }
4243 }
4244 return taskChanges;
4245 }
4246
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004247 private static boolean isResizeOnlyChange(int change) {
4248 return (change & ~(ActivityInfo.CONFIG_SCREEN_SIZE
4249 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_ORIENTATION)) == 0;
4250 }
4251
Wale Ogunwale83301a92015-09-24 15:54:08 -07004252 private void relaunchActivityLocked(
4253 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4254 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
4255 return;
4256 }
4257
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004258 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004259 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004260 if (andResume) {
4261 results = r.results;
4262 newIntents = r.newIntents;
4263 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004264 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4265 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004266 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004267 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004268 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004269 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004270
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004271 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004272
Craig Mautner34b73df2014-01-12 21:11:08 -08004273 mStackSupervisor.removeChildActivityContainers(r);
4274
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004275 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004276 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4277 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004278 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004279 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4280 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004281 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004282 // Note: don't need to call pauseIfSleepingLocked() here, because
4283 // the caller will only pass in 'andResume' if this activity is
4284 // currently resumed, which implies we aren't sleeping.
4285 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004286 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004287 }
4288
4289 if (andResume) {
4290 r.results = null;
4291 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004292 r.state = ActivityState.RESUMED;
4293 } else {
4294 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4295 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004296 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004297 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004298
4299 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004300 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4301 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4302 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4303 final ActivityRecord r = activities.get(activityNdx);
4304 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004305 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004306 }
4307 if (r.fullscreen && !r.finishing) {
4308 return false;
4309 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004310 }
4311 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004312 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004313 if (r == null) {
4314 return false;
4315 }
4316 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4317 + " would have returned true for r=" + r);
4318 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004319 }
4320
4321 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004322 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4323 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4324 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4325 final ActivityRecord r = activities.get(activityNdx);
4326 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004327 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004328 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004329 }
4330 }
4331 }
4332
Wale Ogunwale540e1232015-05-01 15:35:39 -07004333 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4334 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004335 boolean didSomething = false;
4336 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004337 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004338 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4339 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4340 int numActivities = activities.size();
4341 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4342 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004343 final boolean sameComponent =
4344 (r.packageName.equals(packageName) && (filterByClasses == null
4345 || filterByClasses.contains(r.realActivity.getClassName())))
4346 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004347 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004348 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004349 && (r.app == null || evenPersistent || !r.app.persistent)) {
4350 if (!doit) {
4351 if (r.finishing) {
4352 // If this activity is just finishing, then it is not
4353 // interesting as far as something to stop.
4354 continue;
4355 }
4356 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004357 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004358 if (r.isHomeActivity()) {
4359 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4360 Slog.i(TAG, "Skip force-stop again " + r);
4361 continue;
4362 } else {
4363 homeActivity = r.realActivity;
4364 }
4365 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004366 didSomething = true;
4367 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004368 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004369 if (r.app != null) {
4370 r.app.removed = true;
4371 }
4372 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004373 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004374 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004375 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4376 true)) {
4377 // r has been deleted from mActivities, accommodate.
4378 --numActivities;
4379 --activityNdx;
4380 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004381 }
4382 }
4383 }
4384 return didSomething;
4385 }
4386
Dianne Hackborn09233282014-04-30 11:33:59 -07004387 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004388 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4389 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004390 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004391 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004392 if (task.getTopActivity() == null) {
4393 continue;
4394 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004395 ActivityRecord r = null;
4396 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004397 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004398 int numActivities = 0;
4399 int numRunning = 0;
4400 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004401 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004402 continue;
4403 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004404 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004405 tmp = activities.get(activityNdx);
4406 if (tmp.finishing) {
4407 continue;
4408 }
4409 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004410
Craig Mautneraab647e2013-02-28 16:31:36 -08004411 // Initialize state for next task if needed.
4412 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4413 top = r;
4414 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004415 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004416
4417 // Add 'r' into the current task.
4418 numActivities++;
4419 if (r.app != null && r.app.thread != null) {
4420 numRunning++;
4421 }
4422
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004423 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004424 TAG, r.intent.getComponent().flattenToShortString()
4425 + ": task=" + r.task);
4426 }
4427
4428 RunningTaskInfo ci = new RunningTaskInfo();
4429 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004430 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004431 ci.baseActivity = r.intent.getComponent();
4432 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004433 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004434 if (focusedStack && topTask) {
4435 // Give the latest time to ensure foreground task can be sorted
4436 // at the first, because lastActiveTime of creating task is 0.
4437 ci.lastActiveTime = System.currentTimeMillis();
4438 topTask = false;
4439 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004440
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004441 if (top.task != null) {
4442 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004443 }
4444 ci.numActivities = numActivities;
4445 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004446 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004447 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004448 }
4449
4450 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004451 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004452 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004453 if (top >= 0) {
4454 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4455 int activityTop = activities.size() - 1;
4456 if (activityTop > 0) {
4457 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4458 "unhandled-back", true);
4459 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004460 }
4461 }
4462
Craig Mautner6b74cb52013-09-27 17:02:21 -07004463 /**
4464 * Reset local parameters because an app's activity died.
4465 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004466 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004467 */
4468 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004469 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004470 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004471 "App died while pausing: " + mPausingActivity);
4472 mPausingActivity = null;
4473 }
4474 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4475 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004476 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004477 }
4478
Craig Mautner19091252013-10-05 00:03:53 -07004479 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004480 }
4481
Craig Mautnercae015f2013-02-08 14:31:27 -08004482 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004483 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4484 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4485 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4486 final ActivityRecord r = activities.get(activityNdx);
4487 if (r.app == app) {
4488 Slog.w(TAG, " Force finishing activity "
4489 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004490 // Force the destroy to skip right to removal.
4491 r.app = null;
4492 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004493 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004494 }
4495 }
4496 }
4497
Dianne Hackborn390517b2013-05-30 15:03:32 -07004498 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004499 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004500 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004501 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4502 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004503 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4504 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004505 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004506 " Task id #" + task.taskId + "\n" +
4507 " mFullscreen=" + task.mFullscreen + "\n" +
4508 " mBounds=" + task.mBounds + "\n" +
4509 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004510 if (printed) {
4511 header = null;
4512 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004513 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004514 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004515 }
4516
4517 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4518 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4519
4520 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004521 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4522 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004523 }
4524 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004525 final int top = mTaskHistory.size() - 1;
4526 if (top >= 0) {
4527 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4528 int listTop = list.size() - 1;
4529 if (listTop >= 0) {
4530 activities.add(list.get(listTop));
4531 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004532 }
4533 } else {
4534 ItemMatcher matcher = new ItemMatcher();
4535 matcher.build(name);
4536
Craig Mautneraab647e2013-02-28 16:31:36 -08004537 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4538 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4539 if (matcher.match(r1, r1.intent.getComponent())) {
4540 activities.add(r1);
4541 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004542 }
4543 }
4544 }
4545
4546 return activities;
4547 }
4548
4549 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004550 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004551
4552 // All activities that came from the package must be
4553 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004554 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4555 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4556 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4557 final ActivityRecord a = activities.get(activityNdx);
4558 if (a.info.packageName.equals(packageName)) {
4559 a.forceNewConfig = true;
4560 if (starting != null && a == starting && a.visible) {
4561 a.startFreezingScreenLocked(starting.app,
4562 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4563 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004564 }
4565 }
4566 }
4567
4568 return starting;
4569 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004570
Craig Mautner299f9602015-01-26 09:47:33 -08004571 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004572 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004573 }
4574
Wale Ogunwale000957c2015-04-03 08:19:12 -07004575 /**
4576 * Removes the input task from this stack.
4577 * @param task to remove.
4578 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004579 * @param moving task to another stack. In the case we are moving we don't want to perform
4580 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004581 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004582 void removeTask(TaskRecord task, String reason, boolean moving) {
4583 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004584 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004585 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004586 if (!StackId.persistTaskBounds(mStackId)) {
4587 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4588 // default configuration the next time it launches.
4589 task.updateOverrideConfiguration(null);
4590 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004591 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004592
Craig Mautner04a0ea62014-01-13 12:51:26 -08004593 final ActivityRecord r = mResumedActivity;
4594 if (r != null && r.task == task) {
4595 mResumedActivity = null;
4596 }
4597
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004598 final int taskNdx = mTaskHistory.indexOf(task);
4599 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004600 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4601 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4602 if (!nextTask.isOverHomeStack()) {
4603 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4604 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004605 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004606 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004607 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004608
Wale Ogunwale040b4702015-08-06 18:10:50 -07004609 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004610 final boolean isVoiceSession = task.voiceSession != null;
4611 if (isVoiceSession) {
4612 try {
4613 task.voiceSession.taskFinished(task.intent, task.taskId);
4614 } catch (RemoteException e) {
4615 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004616 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004617 if (task.autoRemoveFromRecents() || isVoiceSession) {
4618 // Task creator asked to remove this when done, or this task was a voice
4619 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004620 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004621 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004622 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004623 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004624
4625 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004626 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004627 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004628 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004629 String myReason = reason + " leftTaskHistoryEmpty";
4630 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004631 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004632 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004633 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004634 if (mStacks != null) {
4635 mStacks.remove(this);
4636 mStacks.add(0, this);
4637 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004638 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004639 mActivityContainer.onTaskListEmptyLocked();
4640 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004641 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004642
4643 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004644 }
4645
Dianne Hackborn91097de2014-04-04 18:02:06 -07004646 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4647 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4648 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004649 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4650 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004651 // add the task to stack first, mTaskPositioner might need the stack association
4652 addTask(task, toTop, false);
Chong Zhang75b37202015-12-04 14:16:36 -08004653 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
4654 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.mResizeable
4655 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004656 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004657 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004658 return task;
4659 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004660
Wale Ogunwale935e5022015-11-10 12:36:10 -08004661 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4662 if (mTaskPositioner == null) {
4663 return false;
4664 }
4665 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4666 return true;
4667 }
4668
Craig Mautnerc00204b2013-03-05 15:02:14 -08004669 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004670 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004671 }
4672
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004673 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004674 task.stack = this;
4675 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004676 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004677 } else {
4678 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004679 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004680 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004681 if (!moving && task.voiceSession != null) {
4682 try {
4683 task.voiceSession.taskStarted(task.intent, task.taskId);
4684 } catch (RemoteException e) {
4685 }
4686 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004687 }
4688
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004689 void positionTask(final TaskRecord task, int position, boolean moving) {
4690 task.stack = this;
4691 insertTaskAtPosition(task, position);
4692 if (!moving && task.voiceSession != null) {
4693 try {
4694 task.voiceSession.taskStarted(task.intent, task.taskId);
4695 } catch (RemoteException e) {
4696 }
4697 }
4698 }
4699
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004700 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07004701 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004702 task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004703 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4704 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
4705 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
4706 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
Wale Ogunwale61b009e2015-09-16 15:43:05 -07004707 bounds, task.mOverrideConfig, !r.isHomeActivity());
Chong Zhangb15758a2015-11-17 12:12:03 -08004708 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004709 r.taskConfigOverride = task.mOverrideConfig;
4710 }
4711
Wale Ogunwale079a0042015-10-24 11:44:07 -07004712 void setFocusAndResumeStateIfNeeded(
4713 ActivityRecord r, boolean setFocus, boolean setResume, String reason) {
4714 // If the activity had focus before move focus to this stack.
4715 if (setFocus) {
4716 // If the activity owns the last resumed activity, transfer that together,
4717 // so that we don't resume the same activity again in the new stack.
4718 // Apps may depend on onResume()/onPause() being called in pairs.
4719 if (setResume) {
4720 mResumedActivity = r;
4721 // Move the stack in which we are placing the activity to the front. We don't use
4722 // ActivityManagerService.setFocusedActivityLocked, because if the activity is
4723 // already focused, the call will short-circuit and do nothing.
4724 moveToFront(reason);
4725 } else {
4726 // We need to not only move the stack to the front, but also have the activity
4727 // focused. This will achieve both goals.
4728 mService.setFocusedActivityLocked(r, reason);
4729 }
4730 }
4731 }
4732
4733 /**
4734 * Moves the input activity from its current stack to this one.
4735 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4736 * created on this stack which the activity is added to.
4737 * */
4738 void moveActivityToStack(ActivityRecord r) {
4739 final ActivityStack prevStack = r.task.stack;
4740 if (prevStack.mStackId == mStackId) {
4741 // You are already in the right stack silly...
4742 return;
4743 }
4744
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004745 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004746 && (mStackSupervisor.topRunningActivityLocked() == r);
4747 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4748
4749 final TaskRecord task = createTaskRecord(
4750 mStackSupervisor.getNextTaskId(), r.info, r.intent, null, null, true);
4751 r.setTask(task, null);
4752 task.addActivityToTop(r);
4753 setAppTask(r, task);
4754 setFocusAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4755 }
4756
Wale Ogunwale706ed792015-08-02 10:29:44 -07004757 private void setAppTask(ActivityRecord r, TaskRecord task) {
4758 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004759 task.updateOverrideConfiguration(bounds);
4760 mWindowManager.setAppTask(
Wale Ogunwale2998eef2015-12-02 19:46:29 -08004761 r.appToken, task.taskId, mStackId, task.getLaunchBounds(), task.mOverrideConfig);
Chong Zhangb15758a2015-11-17 12:12:03 -08004762 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004763 r.taskConfigOverride = task.mOverrideConfig;
4764 }
4765
Craig Mautnerc00204b2013-03-05 15:02:14 -08004766 public int getStackId() {
4767 return mStackId;
4768 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004769
4770 @Override
4771 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004772 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4773 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004774 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004775
Craig Mautner15df08a2015-04-01 12:17:18 -07004776 void onLockTaskPackagesUpdatedLocked() {
4777 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4778 mTaskHistory.get(taskNdx).setLockTaskAuth();
4779 }
4780 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004781}