blob: ee4dbd4ca4513542fa727c67ec1ffc0d2ffc2454 [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 Ogunwaleb34a7ad2015-08-14 11:05:30 -070019import static android.app.ActivityManager.DOCKED_STACK_ID;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070020import static android.app.ActivityManager.FIRST_STATIC_STACK_ID;
Wale Ogunwale61803ee2015-08-07 19:59:47 -070021import static android.app.ActivityManager.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070022import static android.app.ActivityManager.FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -070023import static android.app.ActivityManager.HOME_STACK_ID;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070024import static android.app.ActivityManager.LAST_STATIC_STACK_ID;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070025import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070026
Wale Ogunwalee23149f2015-03-06 15:39:44 -080027import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner0eea92c2013-05-16 13:35:39 -070028
Craig Mautner84984fa2014-06-19 11:19:20 -070029import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
30import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070031
Wale Ogunwale040b4702015-08-06 18:10:50 -070032import static com.android.server.am.ActivityStackSupervisor.MOVING;
33
Wale Ogunwale706ed792015-08-02 10:29:44 -070034import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070035import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070036
Dianne Hackborn91097de2014-04-04 18:02:06 -070037import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080038import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070039import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070040import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080041import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080042import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080043import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080044import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070045import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070046
47import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070048import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070049import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070050import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080051import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080053import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080059import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080060import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070062import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080063import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.os.Handler;
65import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090066import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070068import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.RemoteException;
70import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080071import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070072import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070073import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070076import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077
Craig Mautnercae015f2013-02-08 14:31:27 -080078import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080079import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070080import java.lang.ref.WeakReference;
81import java.util.ArrayList;
82import java.util.Iterator;
83import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080084import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070085import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070086
87/**
88 * State and management of a single stack of activities.
89 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070090final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080091
Wale Ogunwalee23149f2015-03-06 15:39:44 -080092 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070093 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
94 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080095 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070096 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070097 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070098 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070099 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700100 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700101 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
102 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700103 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700104 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700105 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
106 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
107 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
108 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
109 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700110
111 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800112
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700113 // Ticks during which we check progress while waiting for an app to launch.
114 static final int LAUNCH_TICK = 500;
115
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700116 // How long we wait until giving up on the last activity to pause. This
117 // is short because it directly impacts the responsiveness of starting the
118 // next activity.
119 static final int PAUSE_TIMEOUT = 500;
120
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700121 // How long we wait for the activity to tell us it has stopped before
122 // giving up. This is a good amount of time because we really need this
123 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700124 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700125
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700126 // How long we wait until giving up on an activity telling us it has
127 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700128 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800129
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700130 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800131 // disabled.
132 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800133
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700134 // How long between activity launches that we consider safe to not warn
135 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700136 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800137
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700138 // Set to false to disable the preview that is shown while a new activity
139 // is being started.
140 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800141
Craig Mautner5eda9b32013-07-02 11:58:16 -0700142 // How long to wait for all background Activities to redraw following a call to
143 // convertToTranslucent().
144 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
145
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700146 enum ActivityState {
147 INITIALIZING,
148 RESUMED,
149 PAUSING,
150 PAUSED,
151 STOPPING,
152 STOPPED,
153 FINISHING,
154 DESTROYING,
155 DESTROYED
156 }
157
158 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700159 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800160 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800161
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700162 /**
163 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800164 * running) activities. It contains #TaskRecord objects.
165 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800166 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800167
168 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800169 * Used for validating app tokens with window manager.
170 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800171 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800172
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173 /**
174 * List of running activities, sorted by recent usage.
175 * The first entry in the list is the least recently used.
176 * It contains HistoryRecord objects.
177 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800178 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700179
180 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700181 * Animations that for the current transition have requested not to
182 * be considered for the transition animation.
183 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800184 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700185
186 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700187 * When we are in the process of pausing an activity, before starting the
188 * next one, this variable holds the activity that is currently being paused.
189 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800190 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700191
192 /**
193 * This is the last activity that we put into the paused state. This is
194 * used to determine if we need to do an activity transition while sleeping,
195 * when we normally hold the top activity paused.
196 */
197 ActivityRecord mLastPausedActivity = null;
198
199 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700200 * Activities that specify No History must be removed once the user navigates away from them.
201 * If the device goes to sleep with such an activity in the paused state then we save it here
202 * and finish it later if another activity replaces it on wakeup.
203 */
204 ActivityRecord mLastNoHistoryActivity = null;
205
206 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700207 * Current activity that is resumed, or null if there is none.
208 */
209 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800210
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700211 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700212 * This is the last activity that has been started. It is only used to
213 * identify when multiple activities are started at once so that the user
214 * can be warned they may not be in the activity they think they are.
215 */
216 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800217
Craig Mautner5eda9b32013-07-02 11:58:16 -0700218 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
219 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
220 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
221 // Activity in mTranslucentActivityWaiting is notified via
222 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
223 // background activity being drawn then the same call will be made with a true value.
224 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700225 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700226
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700227 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700228 * Set when we know we are going to be calling updateConfiguration()
229 * soon, so want to skip intermediate config checks.
230 */
231 boolean mConfigWillChange;
232
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700233 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800234 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700235 // Current bounds of the stack or null if fullscreen.
236 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800237
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700238 long mLaunchStartTime = 0;
239 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800240
Craig Mautner858d8a62013-04-23 17:08:34 -0700241 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700242
Craig Mautnerc00204b2013-03-05 15:02:14 -0800243 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800244 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800245 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
246 ArrayList<ActivityStack> mStacks;
247 /** The attached Display's unique identifier, or -1 if detached */
248 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800249
Craig Mautner27084302013-03-25 08:05:25 -0700250 /** Run all ActivityStacks through this */
251 final ActivityStackSupervisor mStackSupervisor;
252
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700253 private final LaunchingTaskPositioner mTaskPositioner;
254
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700255 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700256 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
257 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
258 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
259 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700260 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700261 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700262 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700263
264 static class ScheduleDestroyArgs {
265 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700266 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700267 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700268 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700269 mReason = reason;
270 }
271 }
272
Zoran Marcetaf958b322012-08-09 20:27:12 +0900273 final Handler mHandler;
274
275 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800276
Craig Mautnerc8143c62013-09-03 12:15:57 -0700277 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900278 super(looper);
279 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700280
Zoran Marcetaf958b322012-08-09 20:27:12 +0900281 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700282 public void handleMessage(Message msg) {
283 switch (msg.what) {
284 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800285 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700286 // We don't at this point know if the activity is fullscreen,
287 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800288 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700289 synchronized (mService) {
290 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700291 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700292 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700293 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800294 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700295 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700296 case LAUNCH_TICK_MSG: {
297 ActivityRecord r = (ActivityRecord)msg.obj;
298 synchronized (mService) {
299 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700300 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700301 }
302 }
303 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700304 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800305 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700306 // We don't at this point know if the activity is fullscreen,
307 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800308 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800309 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800310 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800311 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700312 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700313 case STOP_TIMEOUT_MSG: {
314 ActivityRecord r = (ActivityRecord)msg.obj;
315 // We don't at this point know if the activity is fullscreen,
316 // so we need to be conservative and assume it isn't.
317 Slog.w(TAG, "Activity stop timeout for " + r);
318 synchronized (mService) {
319 if (r.isInHistory()) {
320 activityStoppedLocked(r, null, null, null);
321 }
322 }
323 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700324 case DESTROY_ACTIVITIES_MSG: {
325 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
326 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700327 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700328 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700329 } break;
330 case TRANSLUCENT_TIMEOUT_MSG: {
331 synchronized (mService) {
332 notifyActivityDrawnLocked(null);
333 }
334 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700335 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700336 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700337 final ActivityRecord r = getVisibleBehindActivity();
338 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700339 if (r != null) {
340 mService.killAppAtUsersRequest(r.app, null);
341 }
342 }
343 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700344 }
345 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800346 }
347
Craig Mautner34b73df2014-01-12 21:11:08 -0800348 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800349 int count = 0;
350 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
351 count += mTaskHistory.get(taskNdx).mActivities.size();
352 }
353 return count;
354 }
355
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800356 int numTasks() {
357 return mTaskHistory.size();
358 }
359
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800360 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
361 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800362 mActivityContainer = activityContainer;
363 mStackSupervisor = activityContainer.getOuter();
364 mService = mStackSupervisor.mService;
365 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
366 mWindowManager = mService.mWindowManager;
367 mStackId = activityContainer.mStackId;
368 mCurrentUser = mService.mCurrentUserId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800369 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700370 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
371 ? new LaunchingTaskPositioner() : null;
372 }
373
374 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
375 mDisplayId = activityDisplay.mDisplayId;
376 mStacks = activityDisplay.mStacks;
377 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
378 mFullscreen = mBounds == null;
379 if (mTaskPositioner != null) {
380 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
381 mTaskPositioner.configure(mBounds);
382 }
383 }
384
385 void detachDisplay() {
386 mDisplayId = Display.INVALID_DISPLAY;
387 mStacks = null;
388 if (mTaskPositioner != null) {
389 mTaskPositioner.reset();
390 }
391 mWindowManager.detachStack(mStackId);
392 }
393
394 void setBounds(Rect bounds) {
395 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700396 if (mTaskPositioner != null) {
397 mTaskPositioner.configure(bounds);
398 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700399 }
Craig Mautner5962b122012-10-05 14:45:52 -0700400
Amith Yamasani734983f2014-03-04 16:48:05 -0800401 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700402 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700403 }
404
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700405 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800406 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700407 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
408 if (r != null) {
409 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800410 }
411 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700412 return null;
413 }
414
415 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800416 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
417 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800418 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800419 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
420 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800421 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800422 return r;
423 }
424 }
425 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700426 return null;
427 }
428
429 /**
430 * This is a simplified version of topRunningActivityLocked that provides a number of
431 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800432 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700433 * @param token If non-null, any history records matching this token will be skipped.
434 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800435 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700436 * @return Returns the HistoryRecord of the next activity on the stack.
437 */
438 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800439 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
440 TaskRecord task = mTaskHistory.get(taskNdx);
441 if (task.taskId == taskId) {
442 continue;
443 }
444 ArrayList<ActivityRecord> activities = task.mActivities;
445 for (int i = activities.size() - 1; i >= 0; --i) {
446 final ActivityRecord r = activities.get(i);
447 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800448 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800449 return r;
450 }
451 }
452 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700453 return null;
454 }
455
Craig Mautner8849a5e2013-04-02 16:41:03 -0700456 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700457 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
458 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700459 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
460 final ActivityRecord r = activities.get(activityNdx);
461 if (!r.finishing) {
462 return r;
463 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700464 }
465 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700466 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700467 }
468
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700469 final TaskRecord topTask() {
470 final int size = mTaskHistory.size();
471 if (size > 0) {
472 return mTaskHistory.get(size - 1);
473 }
474 return null;
475 }
476
Craig Mautnerd2328952013-03-05 12:46:26 -0800477 TaskRecord taskForIdLocked(int id) {
478 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
479 final TaskRecord task = mTaskHistory.get(taskNdx);
480 if (task.taskId == id) {
481 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800482 }
483 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700484 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700485 }
486
Craig Mautnerd2328952013-03-05 12:46:26 -0800487 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700488 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800489 return isInStackLocked(r);
490 }
491
492 ActivityRecord isInStackLocked(ActivityRecord r) {
493 if (r == null) {
494 return null;
495 }
496 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700497 if (task != null && task.stack != null
498 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800499 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800500 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800501 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800502 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800503 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800504 }
505
Craig Mautner2420ead2013-04-01 17:13:20 -0700506 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700507 final boolean hadit = mLRUActivities.remove(r);
508 mLRUActivities.add(r);
509 return hadit;
510 }
511
Craig Mautnerde4ef022013-04-07 19:01:33 -0700512 final boolean isHomeStack() {
513 return mStackId == HOME_STACK_ID;
514 }
515
Craig Mautnere0a38842013-12-16 16:14:02 -0800516 final boolean isOnHomeDisplay() {
517 return isAttached() &&
518 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
519 }
520
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700521 void moveToFront(String reason) {
522 moveToFront(reason, null);
523 }
524
525 /**
526 * @param reason The reason for moving the stack to the front.
527 * @param task If non-null, the task will be moved to the top of the stack.
528 * */
529 void moveToFront(String reason, TaskRecord task) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800530 if (isAttached()) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800531 final boolean homeStack = isHomeStack()
532 || (mActivityContainer.mParentActivity != null
533 && mActivityContainer.mParentActivity.isHomeActivity());
534 ActivityStack lastFocusStack = null;
535 if (!homeStack) {
536 // Need to move this stack to the front before calling
537 // {@link ActivityStackSupervisor#moveHomeStack} below.
538 lastFocusStack = mStacks.get(mStacks.size() - 1);
539 mStacks.remove(this);
540 mStacks.add(this);
Wale Ogunwaleaf0e4482015-02-24 12:27:31 -0800541 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700542 // TODO(multi-display): Focus stack currently adjusted in call to move home stack.
543 // Needs to also work if focus is moving to the non-home display.
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800544 if (isOnHomeDisplay()) {
545 mStackSupervisor.moveHomeStack(homeStack, reason, lastFocusStack);
546 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700547 if (task != null) {
548 insertTaskAtTop(task, null);
549 } else {
550 task = topTask();
551 }
Craig Mautner6b904ef2014-12-17 15:45:03 -0800552 if (task != null) {
553 mWindowManager.moveTaskToTop(task.taskId);
554 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800555 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800556 }
557
558 final boolean isAttached() {
559 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800560 }
561
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700562 /**
563 * Returns the top activity in any existing task matching the given
564 * Intent. Returns null if no such task is found.
565 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700566 ActivityRecord findTaskLocked(ActivityRecord target) {
567 Intent intent = target.intent;
568 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700569 ComponentName cls = intent.getComponent();
570 if (info.targetActivity != null) {
571 cls = new ComponentName(info.packageName, info.targetActivity);
572 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700573 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700574 boolean isDocument = intent != null & intent.isDocument();
575 // If documentData is non-null then it must match the existing task data.
576 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800577
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700578 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800579 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
580 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700581 if (task.voiceSession != null) {
582 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700583 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700584 continue;
585 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700586 if (task.userId != userId) {
587 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700588 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700589 continue;
590 }
Craig Mautner000f0022013-02-26 15:04:29 -0800591 final ActivityRecord r = task.getTopActivity();
592 if (r == null || r.finishing || r.userId != userId ||
593 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700594 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800595 continue;
596 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700597 if (r.mActivityType != target.mActivityType) {
598 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
599 continue;
600 }
Craig Mautner000f0022013-02-26 15:04:29 -0800601
Craig Mautnerd00f4742014-03-12 14:17:26 -0700602 final Intent taskIntent = task.intent;
603 final Intent affinityIntent = task.affinityIntent;
604 final boolean taskIsDocument;
605 final Uri taskDocumentData;
606 if (taskIntent != null && taskIntent.isDocument()) {
607 taskIsDocument = true;
608 taskDocumentData = taskIntent.getData();
609 } else if (affinityIntent != null && affinityIntent.isDocument()) {
610 taskIsDocument = true;
611 taskDocumentData = affinityIntent.getData();
612 } else {
613 taskIsDocument = false;
614 taskDocumentData = null;
615 }
616
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700617 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700618 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700619 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700620 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700621 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
622 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700623 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700624 return r;
625 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700626 } else if (taskIntent != null && taskIntent.getComponent() != null &&
627 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700628 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700629 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800630 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700631 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
632 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800633 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700634 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
635 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700636 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700637 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800638 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700639 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
640 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800641 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700642 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700643 }
644
645 return null;
646 }
647
648 /**
649 * Returns the first activity (starting from the top of the stack) that
650 * is the same as the given activity. Returns null if no such activity
651 * is found.
652 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700653 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700654 ComponentName cls = intent.getComponent();
655 if (info.targetActivity != null) {
656 cls = new ComponentName(info.packageName, info.targetActivity);
657 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700658 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700659
Craig Mautner000f0022013-02-26 15:04:29 -0800660 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700661 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700662 final boolean notCurrentUserTask =
663 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700664 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700665
Craig Mautner000f0022013-02-26 15:04:29 -0800666 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
667 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700668 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700669 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700670 }
Craig Mautner000f0022013-02-26 15:04:29 -0800671 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700672 return r;
673 }
674 }
675 }
676
677 return null;
678 }
679
Amith Yamasani742a6712011-05-04 14:49:28 -0700680 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700681 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700682 */
Craig Mautner93529a42013-10-04 15:03:13 -0700683 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800684 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700685 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800686 }
687 mCurrentUser = userId;
688
689 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800690 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800691 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700692 final TaskRecord task = mTaskHistory.get(i);
693
694 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
695 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700696 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700697 || task.topRunningActivityLocked(null) != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700698 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700699 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800700 mTaskHistory.remove(i);
701 mTaskHistory.add(task);
702 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800703 // Use same value for i.
704 } else {
705 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800706 }
707 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700708 if (VALIDATE_TOKENS) {
709 validateAppTokensLocked();
710 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700711 }
712
Craig Mautner2420ead2013-04-01 17:13:20 -0700713 void minimalResumeActivityLocked(ActivityRecord r) {
714 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700715 if (DEBUG_STATES) Slog.v(TAG_STATES,
716 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700717 r.stopped = false;
718 mResumedActivity = r;
719 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800720 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700721 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700722 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700723 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700724 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
725 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700726 }
727
Chong Zhang45c25ce2015-08-10 22:18:26 -0700728 private void addRecentActivityLocked(ActivityRecord r) {
729 if (r != null) {
730 mRecentTasks.addLocked(r.task);
731 r.task.touchActiveTime();
732 }
733 }
734
Narayan Kamath7829c812015-06-08 17:39:43 +0100735 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700736 if (mFullyDrawnStartTime != 0) {
737 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
738 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100739 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700740 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
741 }
742
743 private void stopFullyDrawnTraceIfNeeded() {
744 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
745 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
746 mFullyDrawnStartTime = 0;
747 }
748 }
749
Craig Mautnere79d42682013-04-01 19:01:53 -0700750 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700751 if (r.displayStartTime == 0) {
752 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
753 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100754 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700755 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700756 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700757 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100758 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700759 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700760 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700761 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800762
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700763 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700764 // Make sure that there is no activity waiting for this to launch.
765 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
766 r.displayStartTime = r.fullyDrawnStartTime = 0;
767 } else {
768 mStackSupervisor.removeTimeoutsForActivityLocked(r);
769 mStackSupervisor.scheduleIdleTimeoutLocked(r);
770 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700771 }
772
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800773 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800774 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800775 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
776 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
777 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
778 activities.get(activityNdx).setSleeping(false);
779 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800780 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800781 if (mPausingActivity != null) {
782 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
783 activityPausedLocked(mPausingActivity.appToken, true);
784 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800785 }
786
Craig Mautner0eea92c2013-05-16 13:35:39 -0700787 /**
788 * @return true if something must be done before going to sleep.
789 */
790 boolean checkReadyForSleepLocked() {
791 if (mResumedActivity != null) {
792 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700793 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
794 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
795 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700796 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700797 return true;
798 }
799 if (mPausingActivity != null) {
800 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700801 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700802 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800803 }
804
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700805 if (hasVisibleBehindActivity()) {
806 // Stop visible behind activity before going to sleep.
807 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
808 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700809 if (DEBUG_STATES) Slog.v(TAG_STATES,
810 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700811 return true;
812 }
813
Craig Mautner0eea92c2013-05-16 13:35:39 -0700814 return false;
815 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800816
Craig Mautner0eea92c2013-05-16 13:35:39 -0700817 void goToSleep() {
818 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800819
Craig Mautner0eea92c2013-05-16 13:35:39 -0700820 // Make sure any stopped but visible activities are now sleeping.
821 // This ensures that the activity's onStop() is called.
822 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
823 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
824 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
825 final ActivityRecord r = activities.get(activityNdx);
826 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
827 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800828 }
829 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800830 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700831 }
Craig Mautner59c00972012-07-30 12:10:24 -0700832
Dianne Hackbornd2835932010-12-13 16:28:46 -0800833 public final Bitmap screenshotActivities(ActivityRecord who) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700834 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800835 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700836 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800837 return null;
838 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800839
Winson Chung083baf92014-07-11 10:32:42 -0700840 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700841 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800842 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700843 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700844 return null;
845 }
846
Winson Chung48a10a52014-08-27 14:36:51 -0700847 int w = mService.mThumbnailWidth;
848 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800849 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700850 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800851 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Dianne Hackborn27eac1d2015-03-16 17:15:53 -0700852 w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800853 }
Winson Chung376543b2014-05-21 17:43:28 -0700854 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800855 return null;
856 }
857
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700858 /**
859 * Start pausing the currently resumed activity. It is an error to call this if there
860 * is already an activity being paused or there is no resumed activity.
861 *
862 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
863 * @param uiSleeping True if this is happening with the user interface going to sleep (the
864 * screen turning off).
865 * @param resuming True if this is being called as part of resuming the top activity, so
866 * we shouldn't try to instigate a resume here.
867 * @param dontWait True if the caller does not want to wait for the pause to complete. If
868 * set to true, we will immediately complete the pause here before returning.
869 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
870 * it to tell us when it is done.
871 */
872 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
873 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800874 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800875 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
876 + " state=" + mPausingActivity.state);
877 if (!mService.isSleeping()) {
878 // Avoid recursion among check for sleep and complete pause during sleeping.
879 // Because activity will be paused immediately after resume, just let pause
880 // be completed by the order of activity paused from clients.
881 completePauseLocked(false);
882 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800883 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700884 ActivityRecord prev = mResumedActivity;
885 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700886 if (!resuming) {
887 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
888 mStackSupervisor.resumeTopActivitiesLocked();
889 }
890 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700891 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800892
893 if (mActivityContainer.mParentActivity == null) {
894 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700895 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800896 }
897
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700898 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700899 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700900 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800901 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700902 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700903 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
904 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700905 prev.state = ActivityState.PAUSING;
906 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700907 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700908 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Skuhne17690ff2015-08-27 09:47:20 -0700909 // In freeform mode we only update the thumbnail when there is no thumbnail yet since every
910 // focus change will request a thumbnail to be taken.
911 // Note furthermore that since windows can change their content in freeform mode all the
912 // time a thumbnail is possibly constantly outdated.
913 if (mService.mHasRecents &&
914 (next == null || next.noDisplay || next.task != prev.task || uiSleeping) &&
915 (!prev.task.hasThumbnail() ||
916 prev.task.stack.mStackId != FREEFORM_WORKSPACE_STACK_ID)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800917 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700918 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700919 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700920
921 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800922
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700923 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700924 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700925 try {
926 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700927 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700928 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700929 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800930 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700931 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932 } catch (Exception e) {
933 // Ignore exception, if process died other code will cleanup.
934 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800935 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700936 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700937 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700938 }
939 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800940 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700941 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700942 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700943 }
944
945 // If we are not going to sleep, we want to ensure the device is
946 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700947 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700948 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700949 }
950
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800951 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700952 // Have the window manager pause its key dispatching until the new
953 // activity has started. If we're pausing the activity just because
954 // the screen is being turned off and the UI is sleeping, don't interrupt
955 // key dispatch; the same activity will pick it up again on wakeup.
956 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800957 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700958 } else if (DEBUG_PAUSE) {
959 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700960 }
961
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700962 if (dontWait) {
963 // If the caller said they don't want to wait for the pause, then complete
964 // the pause now.
965 completePauseLocked(false);
966 return false;
967
968 } else {
969 // Schedule a pause timeout in case the app doesn't respond.
970 // We don't give it much time because this directly impacts the
971 // responsiveness seen by the user.
972 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
973 msg.obj = prev;
974 prev.pauseTime = SystemClock.uptimeMillis();
975 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700976 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700977 return true;
978 }
979
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700980 } else {
981 // This activity failed to schedule the
982 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700983 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700984 if (!resuming) {
985 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
986 }
987 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700988 }
989 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700990
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700991 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700992 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
993 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700994
Craig Mautnerd2328952013-03-05 12:46:26 -0800995 final ActivityRecord r = isInStackLocked(token);
996 if (r != null) {
997 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
998 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700999 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001000 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001001 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -08001002 } else {
1003 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1004 r.userId, System.identityHashCode(r), r.shortComponentName,
1005 mPausingActivity != null
1006 ? mPausingActivity.shortComponentName : "(none)");
louis_chang047dfd42015-04-08 16:35:55 +08001007 if (r.finishing && r.state == ActivityState.PAUSING) {
1008 if (DEBUG_PAUSE) Slog.v(TAG,
1009 "Executing finish of failed to pause activity: " + r);
1010 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1011 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001012 }
1013 }
1014 }
1015
Craig Mautnera0026042014-04-23 11:45:37 -07001016 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1017 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001018 if (r.state != ActivityState.STOPPING) {
1019 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1020 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1021 return;
1022 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001023 if (persistentState != null) {
1024 r.persistentState = persistentState;
1025 mService.notifyTaskPersisterLocked(r.task, false);
1026 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001027 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001028 if (icicle != null) {
1029 // If icicle is null, this is happening due to a timeout, so we
1030 // haven't really saved the state.
1031 r.icicle = icicle;
1032 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001033 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001034 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001035 }
1036 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001037 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001038 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1039 r.stopped = true;
1040 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -07001041 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
1042 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001043 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001044 if (r.finishing) {
1045 r.clearOptionsLocked();
1046 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001047 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001048 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -07001049 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001050 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001051 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001052 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001053 }
1054 }
1055 }
1056
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001057 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001058 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001059 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001060
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001061 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001062 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001063 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001064 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001065 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001066 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001067 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001068 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001069 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1070 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001071 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001072 if (prev.configDestroy) {
1073 // The previous is being paused because the configuration
1074 // is changing, which means it is actually stopping...
1075 // To juggle the fact that we are also starting a new
1076 // instance right now, we need to first completely stop
1077 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001078 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001079 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001080 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001081 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001082 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001083 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001084 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1085 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001086 // If we already have a few activities waiting to stop,
1087 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001088 // them out. Or if r is the last of activity of the last task the stack
1089 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001090 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001091 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001092 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001093 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001094 }
1095 }
1096 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001097 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001098 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001099 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001100 // It is possible the activity was freezing the screen before it was paused.
1101 // In that case go ahead and remove the freeze this activity has on the screen
1102 // since it is no longer visible.
1103 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001104 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001105 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001106
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001107 if (resumeNext) {
1108 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1109 if (!mService.isSleepingOrShuttingDown()) {
1110 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1111 } else {
1112 mStackSupervisor.checkReadyForSleepLocked();
1113 ActivityRecord top = topStack.topRunningActivityLocked(null);
1114 if (top == null || (prev != null && top != prev)) {
1115 // If there are no more activities available to run,
1116 // do resume anyway to start something. Also if the top
1117 // activity on the stack is not the just paused activity,
1118 // we need to go ahead and resume it to ensure we complete
1119 // an in-flight app switch.
1120 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1121 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001122 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001123 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001124
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001125 if (prev != null) {
1126 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001127
Craig Mautner525f3d92013-05-07 14:01:50 -07001128 if (prev.app != null && prev.cpuTimeAtResume > 0
1129 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001130 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1131 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001132 if (diff > 0) {
1133 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1134 synchronized (bsi) {
1135 BatteryStatsImpl.Uid.Proc ps =
1136 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001137 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001138 if (ps != null) {
1139 ps.addForegroundTimeLocked(diff);
1140 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001141 }
1142 }
1143 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001144 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001145 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001146
1147 // Notfiy when the task stack has changed
1148 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001149 }
1150
1151 /**
1152 * Once we know that we have asked an application to put an activity in
1153 * the resumed state (either by launching it or explicitly telling it),
1154 * this function updates the rest of our state to match that fact.
1155 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001156 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001157 next.idle = false;
1158 next.results = null;
1159 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001160
1161 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1162 ProcessRecord app = next.task.mActivities.get(0).app;
1163 if (app != null && app != mService.mHomeProcess) {
1164 mService.mHomeProcess = app;
1165 }
1166 }
1167
Craig Mautner07566322013-09-26 16:42:55 -07001168 if (next.nowVisible) {
1169 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001170 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001171 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001172
1173 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001174 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001175
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001176 mStackSupervisor.reportResumedActivityLocked(next);
1177
1178 next.resumeKeyDispatchingLocked();
1179 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001180
1181 // Mark the point when the activity is resuming
1182 // TODO: To be more accurate, the mark should be before the onCreate,
1183 // not after the onResume. But for subsequent starts, onResume is fine.
1184 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001185 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001186 } else {
1187 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1188 }
Winson Chung376543b2014-05-21 17:43:28 -07001189
George Mount6ba042b2014-07-28 11:12:28 -07001190 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001191
1192 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1193 // When resuming an activity, require it to call requestVisibleBehind() again.
1194 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1195 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001196 }
1197
Craig Mautner2568c3a2015-03-26 14:22:34 -07001198 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001199 r.visible = visible;
1200 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001201 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001202 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001203 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001204 container.setVisible(visible);
1205 }
1206 }
1207
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001208 // Find the first visible activity above the passed activity and if it is translucent return it
1209 // otherwise return null;
1210 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1211 TaskRecord task = r.task;
1212 if (task == null) {
1213 return null;
1214 }
1215
1216 ActivityStack stack = task.stack;
1217 if (stack == null) {
1218 return null;
1219 }
1220
1221 int stackNdx = mStacks.indexOf(stack);
1222
1223 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1224 int taskNdx = tasks.indexOf(task);
1225
1226 ArrayList<ActivityRecord> activities = task.mActivities;
1227 int activityNdx = activities.indexOf(r) + 1;
1228
1229 final int numStacks = mStacks.size();
1230 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001231 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001232 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001233 final int numTasks = tasks.size();
1234 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001235 final TaskRecord currentTask = tasks.get(taskNdx);
1236 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001237 final int numActivities = activities.size();
1238 while (activityNdx < numActivities) {
1239 final ActivityRecord activity = activities.get(activityNdx);
1240 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001241 return historyStack.mFullscreen
1242 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001243 }
1244 ++activityNdx;
1245 }
1246 activityNdx = 0;
1247 ++taskNdx;
1248 }
1249 taskNdx = 0;
1250 ++stackNdx;
1251 }
1252
1253 return null;
1254 }
1255
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001256 private ActivityStack getNextVisibleStackLocked() {
1257 ArrayList<ActivityStack> stacks = mStacks;
1258 final ActivityRecord parent = mActivityContainer.mParentActivity;
1259 if (parent != null) {
1260 stacks = parent.task.stack.mStacks;
1261 }
1262 if (stacks != null) {
1263 for (int i = stacks.size() - 1; i >= 0; --i) {
1264 ActivityStack stack = stacks.get(i);
1265 if (stack != this && stack.isStackVisibleLocked()) {
1266 return stack;
1267 }
1268 }
1269 }
1270 return null;
1271 }
1272
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001273 /** Returns true if the stack contains a fullscreen task. */
1274 private boolean hasFullscreenTask() {
1275 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1276 final TaskRecord task = mTaskHistory.get(i);
1277 if (task.mFullscreen) {
1278 return true;
1279 }
1280 }
1281 return false;
1282 }
1283
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001284 private boolean hasTranslucentActivity(ActivityStack stack) {
1285 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1286 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1287 final TaskRecord task = tasks.get(taskNdx);
1288 final ArrayList<ActivityRecord> activities = task.mActivities;
1289 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1290 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001291
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001292 // Conditions for an activity to obscure the stack we're
1293 // examining:
1294 // 1. Not Finishing AND Visible AND:
1295 // 2. Either:
1296 // - Full Screen Activity OR
1297 // - On top of Home and our stack is NOT home
1298 if (!r.finishing && r.visible && (r.fullscreen ||
1299 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1300 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001301 }
1302 }
1303 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001304 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001305 }
1306
1307 /** Returns true if the stack is considered visible. */
Todd Kennedyaab56db2015-01-30 09:39:53 -08001308 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001309 if (!isAttached()) {
1310 return false;
1311 }
1312
1313 if (mStackSupervisor.isFrontStack(this)) {
1314 return true;
1315 }
1316
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001317 final int stackIndex = mStacks.indexOf(this);
1318
1319 if (stackIndex == mStacks.size() - 1) {
1320 Slog.wtf(TAG,
1321 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1322 return false;
1323 }
1324
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001325 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1326 final int focusedStackId = focusedStack.mStackId;
1327
1328 if (mStackId == DOCKED_STACK_ID) {
1329 // Docked stack is always visible, except in the case where the home activity
1330 // is the top running activity in the focused home stack.
1331 if (focusedStackId != HOME_STACK_ID) {
1332 return true;
1333 }
1334 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked(null);
1335 return topHomeActivity == null || !topHomeActivity.isHomeActivity();
1336 }
1337
1338 if (focusedStackId == DOCKED_STACK_ID
1339 && stackIndex == (mStacks.indexOf(focusedStack) - 1)) {
1340 // Stacks directly behind the docked stack are always visible.
1341 return true;
1342 }
1343
1344 if (mStackId == HOME_STACK_ID && focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
1345 // Home stack is always visible behind the fullscreen stack with a translucent activity.
1346 // This is done so that the home stack can act as a background to the translucent
1347 // activity.
1348 return hasTranslucentActivity(focusedStack);
1349 }
1350
1351 if (mStackId >= FIRST_STATIC_STACK_ID && mStackId <= LAST_STATIC_STACK_ID) {
1352 // Visibility of any static stack should have been determined by the conditions above.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001353 return false;
1354 }
1355
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001356 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001357 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001358
1359 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1360 continue;
1361 }
1362
1363 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001364 || stack.mStackId == HOME_STACK_ID
1365 || stack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
1366 // These stacks can't have any dynamic stacks visible behind them.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001367 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001368 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001369
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001370 if (!hasTranslucentActivity(stack)) {
1371 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001372 }
1373 }
1374
1375 return true;
1376 }
1377
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001378 /**
1379 * Make sure that all activities that need to be visible (that is, they
1380 * currently can be seen by the user) actually are.
1381 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001382 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1383 ActivityRecord top = topRunningActivityLocked(null);
1384 if (top == null) {
1385 return;
1386 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001387 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1388 "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001389 + " configChanges=0x" + Integer.toHexString(configChanges));
1390
Craig Mautner5eda9b32013-07-02 11:58:16 -07001391 if (mTranslucentActivityWaiting != top) {
1392 mUndrawnActivitiesBelowTopTranslucent.clear();
1393 if (mTranslucentActivityWaiting != null) {
1394 // Call the callback with a timeout indication.
1395 notifyActivityDrawnLocked(null);
1396 mTranslucentActivityWaiting = null;
1397 }
1398 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1399 }
1400
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001401 // If the top activity is not fullscreen, then we need to
1402 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001403 boolean aboveTop = true;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001404 final boolean stackInvisible = !isStackVisibleLocked();
1405 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001406 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001407
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001408 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001409 final TaskRecord task = mTaskHistory.get(taskNdx);
1410 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001411
Craig Mautnerd44711d2013-02-23 11:24:36 -08001412 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1413 final ActivityRecord r = activities.get(activityNdx);
1414 if (r.finishing) {
1415 continue;
1416 }
1417 if (aboveTop && r != top) {
1418 continue;
1419 }
1420 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001421 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1422 // but must be drawn initially for the animation as though they were visible.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001423 if (!behindFullscreenActivity || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001424 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1425 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001426 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001427
Craig Mautnerd44711d2013-02-23 11:24:36 -08001428 // First: if this is not the current activity being started, make
1429 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001430 if (r != starting) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07001431 ensureActivityConfigurationLocked(r, 0, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001432 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001433
1434 if (r.app == null || r.app.thread == null) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001435 // This activity needs to be visible, but isn't even running...
1436 // get it started and resume if no other stack in this stack is resumed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001437 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1438 "Start and freeze screen for " + r);
Craig Mautnerbb742462014-07-07 15:28:55 -07001439 if (r != starting) {
1440 r.startFreezingScreenLocked(r.app, configChanges);
1441 }
1442 if (!r.visible || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001443 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1444 "Starting and making visible: " + r);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001445 setVisible(r, true);
Craig Mautnerbb742462014-07-07 15:28:55 -07001446 }
1447 if (r != starting) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001448 mStackSupervisor.startSpecificActivityLocked(
1449 r, noStackActivityResumed, false);
riddle_hsu36ee73d2015-06-05 16:38:38 +08001450 if (activityNdx >= activities.size()) {
1451 // Record may be removed if its process needs to restart.
1452 activityNdx = activities.size() - 1;
1453 } else {
1454 noStackActivityResumed = false;
1455 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001456 }
1457
1458 } else if (r.visible) {
1459 // If this activity is already visible, then there is nothing
1460 // else to do here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001461 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1462 "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001463 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001464 try {
George Mount6ba042b2014-07-28 11:12:28 -07001465 if (r.returningOptions != null) {
1466 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1467 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001468 }
1469 } catch(RemoteException e) {
1470 }
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001471 if (r.state == ActivityState.RESUMED) {
1472 noStackActivityResumed = false;
1473 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001474 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001475 // This activity is not currently visible, but is running.
1476 // Tell it to become visible.
1477 r.visible = true;
1478 if (r.state != ActivityState.RESUMED && r != starting) {
1479 // If this activity is paused, tell it
1480 // to now show its window.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001481 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1482 "Making visible and scheduling visibility: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001483 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001484 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001485 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001486 mUndrawnActivitiesBelowTopTranslucent.add(r);
1487 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001488 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001489 r.sleeping = false;
1490 r.app.pendingUiClean = true;
1491 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1492 r.stopFreezingScreenLocked(false);
1493 } catch (Exception e) {
1494 // Just skip on any failure; we'll make it
1495 // visible when it next restarts.
1496 Slog.w(TAG, "Exception thrown making visibile: "
1497 + r.intent.getComponent(), e);
1498 }
1499 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001500 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001501
Craig Mautnerd44711d2013-02-23 11:24:36 -08001502 // Aggregate current change flags.
1503 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001504
Craig Mautnerd44711d2013-02-23 11:24:36 -08001505 if (r.fullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001506 // At this point, nothing else needs to be shown in this task.
1507 behindFullscreenActivity = true;
1508 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001509 + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001510 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautner84984fa2014-06-19 11:19:20 -07001511 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001512 behindFullscreenActivity = true;
1513 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001514 + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001515 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001516 }
1517 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001518 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001519 "Make invisible? " + r + " finishing=" + r.finishing
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001520 + " state=" + r.state + " stackInvisible=" + stackInvisible
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001521 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001522 // Now for any activities that aren't visible to the user, make
1523 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001524 if (r.visible) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001525 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001526 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001527 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001528 switch (r.state) {
1529 case STOPPING:
1530 case STOPPED:
1531 if (r.app != null && r.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001532 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1533 "Scheduling invisibility: " + r);
Craig Mautner4addfc52013-06-25 08:05:45 -07001534 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1535 }
1536 break;
1537
1538 case INITIALIZING:
1539 case RESUMED:
1540 case PAUSING:
1541 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001542 // This case created for transitioning activities from
1543 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001544 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001545 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001546 } else {
1547 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1548 mStackSupervisor.mStoppingActivities.add(r);
1549 }
1550 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001551 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001552 break;
1553
1554 default:
1555 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001556 }
1557 } catch (Exception e) {
1558 // Just skip on any failure; we'll make it
1559 // visible when it next restarts.
1560 Slog.w(TAG, "Exception thrown making hidden: "
1561 + r.intent.getComponent(), e);
1562 }
1563 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001564 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001565 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001566 }
1567 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001568 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1569 // The visibility of tasks and the activities they contain in freeform stack are
1570 // determined individually unlike other stacks where the visibility or fullscreen
1571 // status of an activity in a previous task affects other.
1572 behindFullscreenActivity = stackInvisible;
1573 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001574 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001575
1576 if (mTranslucentActivityWaiting != null &&
1577 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1578 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1579 notifyActivityDrawnLocked(null);
1580 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001581 }
Craig Mautner58547802013-03-05 08:23:53 -08001582
Todd Kennedyaab56db2015-01-30 09:39:53 -08001583 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001584 mTranslucentActivityWaiting = r;
1585 mUndrawnActivitiesBelowTopTranslucent.clear();
1586 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1587 }
1588
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001589 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1590 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1591 final TaskRecord task = mTaskHistory.get(taskNdx);
1592 final ArrayList<ActivityRecord> activities = task.mActivities;
1593 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1594 final ActivityRecord r = activities.get(activityNdx);
1595 if ( r.appTimeTracker != except) {
1596 r.appTimeTracker = null;
1597 }
1598 }
1599 }
1600 }
1601
Craig Mautner5eda9b32013-07-02 11:58:16 -07001602 /**
1603 * Called as activities below the top translucent activity are redrawn. When the last one is
1604 * redrawn notify the top activity by calling
1605 * {@link Activity#onTranslucentConversionComplete}.
1606 *
1607 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1608 * occurred and the activity will be notified immediately.
1609 */
1610 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001611 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001612 if ((r == null)
1613 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1614 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1615 // The last undrawn activity below the top has just been drawn. If there is an
1616 // opaque activity at the top, notify it that it can become translucent safely now.
1617 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1618 mTranslucentActivityWaiting = null;
1619 mUndrawnActivitiesBelowTopTranslucent.clear();
1620 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1621
Craig Mautner71dd1b62014-02-18 15:48:52 -08001622 if (waitingActivity != null) {
1623 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1624 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1625 try {
1626 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1627 waitingActivity.appToken, r != null);
1628 } catch (RemoteException e) {
1629 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001630 }
1631 }
1632 }
1633 }
1634
Craig Mautnera61bc652013-10-28 15:43:18 -07001635 /** If any activities below the top running one are in the INITIALIZING state and they have a
1636 * starting window displayed then remove that starting window. It is possible that the activity
1637 * in this state will never resumed in which case that starting window will be orphaned. */
1638 void cancelInitializingActivities() {
1639 final ActivityRecord topActivity = topRunningActivityLocked(null);
1640 boolean aboveTop = true;
1641 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1642 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1643 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1644 final ActivityRecord r = activities.get(activityNdx);
1645 if (aboveTop) {
1646 if (r == topActivity) {
1647 aboveTop = false;
1648 }
1649 continue;
1650 }
1651
1652 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001653 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1654 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001655 r.mStartingWindowShown = false;
1656 mWindowManager.removeAppStartingWindow(r.appToken);
1657 }
1658 }
1659 }
1660 }
1661
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001662 /**
1663 * Ensure that the top activity in the stack is resumed.
1664 *
1665 * @param prev The previously resumed activity, for when in the process
1666 * of pausing; can be null to call from elsewhere.
1667 *
1668 * @return Returns true if something is being resumed, or false if
1669 * nothing happened.
1670 */
1671 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001672 return resumeTopActivityLocked(prev, null);
1673 }
1674
1675 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001676 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001677 // Don't even start recursing.
1678 return false;
1679 }
1680
1681 boolean result = false;
1682 try {
1683 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001684 mStackSupervisor.inResumeTopActivity = true;
1685 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1686 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001687 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001688 }
Craig Mautner544efa72014-09-04 16:41:20 -07001689 result = resumeTopActivityInnerLocked(prev, options);
1690 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001691 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001692 }
1693 return result;
1694 }
1695
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001696 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001697 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001698
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001699 if (!mService.mBooting && !mService.mBooted) {
1700 // Not ready yet!
1701 return false;
1702 }
1703
Craig Mautnerdf88d732014-01-27 09:21:32 -08001704 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001705 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001706 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001707 // Do not resume this stack if its parent is not resumed.
1708 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1709 return false;
1710 }
1711
Craig Mautnera61bc652013-10-28 15:43:18 -07001712 cancelInitializingActivities();
1713
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001714 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001715 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001716
1717 // Remember how we'll process this pause/resume situation, and ensure
1718 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001719 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1720 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001721
Craig Mautner84984fa2014-06-19 11:19:20 -07001722 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001723 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001724 // There are no more activities!
1725 final String reason = "noMoreActivities";
1726 if (!mFullscreen) {
1727 // Try to move focus to the next visible stack with a running activity if this
1728 // stack is not covering the entire screen.
1729 final ActivityStack stack = getNextVisibleStackLocked();
1730 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1731 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1732 }
1733 }
1734 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001735 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001736 if (DEBUG_STATES) Slog.d(TAG_STATES,
1737 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001738 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001739 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001740 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1741 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1742 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001743 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001744 }
1745
1746 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001747
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001748 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001749 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1750 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001751 // Make sure we have executed any pending transitions, since there
1752 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001753 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001754 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001755 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001756 if (DEBUG_STATES) Slog.d(TAG_STATES,
1757 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001758 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001759 return false;
1760 }
1761
Craig Mautner525f3d92013-05-07 14:01:50 -07001762 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001763 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001764 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001765 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001766 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001767 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001768 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001769 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001770 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001771 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001772 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001773 } else if (!isOnHomeDisplay()) {
1774 return false;
1775 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001776 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001777 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001778 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1779 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1780 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001781 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001782 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001783 }
1784
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001785 // If we are sleeping, and there is no resumed activity, and the top
1786 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001787 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001788 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001789 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001790 // Make sure we have executed any pending transitions, since there
1791 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001792 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001793 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001794 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001795 if (DEBUG_STATES) Slog.d(TAG_STATES,
1796 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001797 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001798 return false;
1799 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001800
1801 // Make sure that the user who owns this activity is started. If not,
1802 // we will just leave it as is because someone should be bringing
1803 // another user's activities to the top of the stack.
1804 if (mService.mStartedUsers.get(next.userId) == null) {
1805 Slog.w(TAG, "Skipping resume of top activity " + next
1806 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001807 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001808 return false;
1809 }
1810
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001811 // The activity may be waiting for stop, but that is no longer
1812 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001813 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001814 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001815 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001816 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001817
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001818 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001819
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001820 // If we are currently pausing an activity, then don't do anything
1821 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001822 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001823 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001824 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001825 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001826 return false;
1827 }
1828
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001829 // Okay we are now going to start a switch, to 'next'. We may first
1830 // have to pause the current activity, but this is an important point
1831 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001832 // XXX "App Redirected" dialog is getting too many false positives
1833 // at this point, so turn off for now.
1834 if (false) {
1835 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1836 long now = SystemClock.uptimeMillis();
1837 final boolean inTime = mLastStartedActivity.startTime != 0
1838 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1839 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1840 final int nextUid = next.info.applicationInfo.uid;
1841 if (inTime && lastUid != nextUid
1842 && lastUid != next.launchedFromUid
1843 && mService.checkPermission(
1844 android.Manifest.permission.STOP_APP_SWITCHES,
1845 -1, next.launchedFromUid)
1846 != PackageManager.PERMISSION_GRANTED) {
1847 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1848 } else {
1849 next.startTime = now;
1850 mLastStartedActivity = next;
1851 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001852 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001853 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001854 mLastStartedActivity = next;
1855 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001856 }
Craig Mautner58547802013-03-05 08:23:53 -08001857
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001858 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1859
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001860 // We need to start pausing the current activity so the top one
1861 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001862 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1863 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001864 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001865 if (DEBUG_STATES) Slog.d(TAG_STATES,
1866 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001867 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001868 }
1869 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001870 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001871 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001872 // At this point we want to put the upcoming activity's process
1873 // at the top of the LRU list, since we know we will be needing it
1874 // very soon and it would be a waste to let it get killed if it
1875 // happens to be sitting towards the end.
1876 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001877 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001878 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001879 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001880 return true;
1881 }
1882
Christopher Tated3f175c2012-06-14 14:16:54 -07001883 // If the most recent activity was noHistory but was only stopped rather
1884 // than stopped+finished because the device went to sleep, we need to make
1885 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001886 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001887 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001888 if (DEBUG_STATES) Slog.d(TAG_STATES,
1889 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001890 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001891 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001892 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001893 }
1894
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001895 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001896 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1897 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001898 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001899 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1900 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001901 } else {
1902 // The next activity is already visible, so hide the previous
1903 // activity's windows right now so we can show the new one ASAP.
1904 // We only do this if the previous is finishing, which should mean
1905 // it is on top of the one being resumed so hiding it quickly
1906 // is good. Otherwise, we want to do the normal route of allowing
1907 // the resumed activity to be shown so we can decide if the
1908 // previous should actually be hidden depending on whether the
1909 // new one is found to be full-screen or not.
1910 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001911 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001912 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1913 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001914 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001915 + ", nowVisible=" + next.nowVisible);
1916 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001917 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001918 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001919 + ", waitingVisible="
1920 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1921 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001922 }
1923 }
1924 }
1925
Dianne Hackborne7f97212011-02-24 14:40:20 -08001926 // Launching this app's activity, make sure the app is no longer
1927 // considered stopped.
1928 try {
1929 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001930 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001931 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001932 } catch (IllegalArgumentException e) {
1933 Slog.w(TAG, "Failed trying to unstop package "
1934 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001935 }
1936
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001937 // We are starting up the next activity, so tell the window manager
1938 // that the previous one will be hidden soon. This way it can know
1939 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001940 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001941 if (prev != null) {
1942 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001943 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001944 "Prepare close transition: prev=" + prev);
1945 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001946 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001947 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001948 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001949 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001950 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1951 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001952 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001953 mWindowManager.setAppWillBeHidden(prev.appToken);
1954 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001955 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001956 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
1957 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001958 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001959 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001960 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001961 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001962 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001963 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001964 : next.mLaunchTaskBehind
1965 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1966 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001967 }
1968 }
1969 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001970 mWindowManager.setAppWillBeHidden(prev.appToken);
1971 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001972 }
Craig Mautner967212c2013-04-13 21:10:58 -07001973 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001974 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001975 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001976 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001977 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001978 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001979 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001980 }
1981 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001982
1983 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001984 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001985 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1986 if (opts != null) {
1987 resumeAnimOptions = opts.toBundle();
1988 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001989 next.applyOptionsLocked();
1990 } else {
1991 next.clearOptionsLocked();
1992 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001993
Craig Mautnercf910b02013-04-23 11:23:27 -07001994 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001995 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001996 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001997
1998 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001999 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002000
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002001 // schedule launch ticks to collect information about slow apps.
2002 next.startLaunchTickingLocked();
2003
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002004 ActivityRecord lastResumedActivity =
2005 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002006 ActivityState lastState = next.state;
2007
2008 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002009
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002010 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002011 next.state = ActivityState.RESUMED;
2012 mResumedActivity = next;
2013 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002014 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002015 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002016 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002017 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002018
2019 // Have the window manager re-evaluate the orientation of
2020 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002021 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002022 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002023 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002024 mService.mConfiguration,
2025 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2026 if (config != null) {
2027 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002028 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002029 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002030 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002031
Craig Mautner525f3d92013-05-07 14:01:50 -07002032 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002033 // The configuration update wasn't able to keep the existing
2034 // instance of the activity, and instead started a new one.
2035 // We should be all done, but let's just make sure our activity
2036 // is still at the top and schedule another run if something
2037 // weird happened.
2038 ActivityRecord nextNext = topRunningActivityLocked(null);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002039 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002040 "Activity config changed during resume: " + next
2041 + ", new next: " + nextNext);
2042 if (nextNext != next) {
2043 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002044 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002045 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002046 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002047 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002048 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002049 return true;
2050 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002051 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002052 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002053 }
Craig Mautner58547802013-03-05 08:23:53 -08002054
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002055 try {
2056 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002057 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002058 if (a != null) {
2059 final int N = a.size();
2060 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002061 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2062 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002063 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002064 }
2065 }
2066
2067 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002068 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002069 }
2070
Craig Mautner299f9602015-01-26 09:47:33 -08002071 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2072 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002073
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002074 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002075 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002076 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002077 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002078 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002079 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002080 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002081
Craig Mautner0eea92c2013-05-16 13:35:39 -07002082 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002083
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002084 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002085 } catch (Exception e) {
2086 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002087 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002088 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002089 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002090 if (lastStack != null) {
2091 lastStack.mResumedActivity = lastResumedActivity;
2092 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002093 Slog.i(TAG, "Restarting because process died: " + next);
2094 if (!next.hasBeenLaunched) {
2095 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002096 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2097 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002098 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002099 next.appToken, next.packageName, next.theme,
2100 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002101 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2102 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002103 }
George Mount2c92c972014-03-20 09:38:23 -07002104 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002105 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002106 return true;
2107 }
2108
2109 // From this point on, if something goes wrong there is no way
2110 // to recover the activity.
2111 try {
2112 next.visible = true;
2113 completeResumeLocked(next);
2114 } catch (Exception e) {
2115 // If any exception gets thrown, toss away this
2116 // activity and try the next one.
2117 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002118 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002119 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002120 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002121 return true;
2122 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002123 next.stopped = false;
2124
2125 } else {
2126 // Whoops, need to restart this activity!
2127 if (!next.hasBeenLaunched) {
2128 next.hasBeenLaunched = true;
2129 } else {
2130 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002131 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002132 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002133 mService.compatibilityInfoForPackageLocked(
2134 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002135 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002136 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002137 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002138 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002139 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002140 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002141 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002142 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002143 }
2144
Craig Mautnercf910b02013-04-23 11:23:27 -07002145 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002146 return true;
2147 }
2148
riddle_hsuc215a4f2014-12-27 12:10:45 +08002149 private TaskRecord getNextTask(TaskRecord targetTask) {
2150 final int index = mTaskHistory.indexOf(targetTask);
2151 if (index >= 0) {
2152 final int numTasks = mTaskHistory.size();
2153 for (int i = index + 1; i < numTasks; ++i) {
2154 TaskRecord task = mTaskHistory.get(i);
2155 if (task.userId == targetTask.userId) {
2156 return task;
2157 }
2158 }
2159 }
2160 return null;
2161 }
2162
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002163 private void insertTaskAtPosition(TaskRecord task, int position) {
2164 if (position >= mTaskHistory.size()) {
2165 insertTaskAtTop(task, null);
2166 return;
2167 }
2168 // Calculate maximum possible position for this task.
2169 int maxPosition = mTaskHistory.size();
2170 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
2171 && task.topRunningActivityLocked(null) == null) {
2172 // Put non-current user tasks below current user tasks.
2173 while (maxPosition > 0) {
2174 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2175 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
2176 || tmpTask.topRunningActivityLocked(null) == null) {
2177 break;
2178 }
2179 maxPosition--;
2180 }
2181 }
2182 position = Math.min(position, maxPosition);
2183 mTaskHistory.remove(task);
2184 mTaskHistory.add(position, task);
2185 updateTaskMovement(task, true);
2186 }
2187
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002188 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002189 // If the moving task is over home stack, transfer its return type to next task
2190 if (task.isOverHomeStack()) {
2191 final TaskRecord nextTask = getNextTask(task);
2192 if (nextTask != null) {
2193 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2194 }
2195 }
2196
Craig Mautner9c85c202013-10-04 20:11:26 -07002197 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002198 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002199 if (isOnHomeDisplay()) {
2200 ActivityStack lastStack = mStackSupervisor.getLastStack();
2201 final boolean fromHome = lastStack.isHomeStack();
2202 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002203 task.setTaskToReturnTo(fromHome
2204 ? lastStack.topTask() == null
2205 ? HOME_ACTIVITY_TYPE
2206 : lastStack.topTask().taskType
2207 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002208 }
2209 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002210 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002211 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002212
Craig Mautnerac6f8432013-07-17 13:24:59 -07002213 mTaskHistory.remove(task);
2214 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002215 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002216 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002217 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002218 || (newActivity == null && task.topRunningActivityLocked(null) == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002219 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002220 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002221 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002222 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002223 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002224 || tmpTask.topRunningActivityLocked(null) == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002225 break;
2226 }
2227 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002228 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002229 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002230 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002231 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002232 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002233
Craig Mautner8849a5e2013-04-02 16:41:03 -07002234 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002235 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002236 TaskRecord rTask = r.task;
2237 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002238 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2239 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002240 // Last activity in task had been removed or ActivityManagerService is reusing task.
2241 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002242 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002243 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002244 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002245 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002246 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002247 if (!newTask) {
2248 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002249 boolean startIt = true;
2250 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2251 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002252 if (task.getTopActivity() == null) {
2253 // All activities in task are finishing.
2254 continue;
2255 }
Craig Mautner70a86932013-02-28 22:37:44 -08002256 if (task == r.task) {
2257 // Here it is! Now, if this is not yet visible to the
2258 // user, then just add it without starting; it will
2259 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002260 if (!startIt) {
2261 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2262 + task, new RuntimeException("here").fillInStackTrace());
2263 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002264 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002265 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002266 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002267 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002268 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002269 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002270 return;
2271 }
2272 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002273 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002274 startIt = false;
2275 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002276 }
2277 }
2278
2279 // Place a new activity at top of stack, so it is next to interact
2280 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002281
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002282 // If we are not placing the new activity frontmost, we do not want
2283 // to deliver the onUserLeaving callback to the actual frontmost
2284 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002285 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002286 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002287 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002288 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002289 }
Craig Mautner70a86932013-02-28 22:37:44 -08002290
2291 task = r.task;
2292
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002293 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002294 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002295 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002296 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002297 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002298
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002299 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002300 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002301 // We want to show the starting preview window if we are
2302 // switching to a new task, or the next activity's process is
2303 // not currently running.
2304 boolean showStartingIcon = newTask;
2305 ProcessRecord proc = r.app;
2306 if (proc == null) {
2307 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2308 }
2309 if (proc == null || proc.thread == null) {
2310 showStartingIcon = true;
2311 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002312 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002313 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002314 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002315 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002316 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002317 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002318 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002319 ? r.mLaunchTaskBehind
2320 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2321 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002322 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002323 mNoAnimActivities.remove(r);
2324 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002325 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002326 boolean doShow = true;
2327 if (newTask) {
2328 // Even though this activity is starting fresh, we still need
2329 // to reset it to make sure we apply affinities to move any
2330 // existing activities from other tasks in to it.
2331 // If the caller has requested that the target task be
2332 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002333 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002334 resetTaskIfNeededLocked(r, r);
2335 doShow = topRunningNonDelayedActivityLocked(null) == r;
2336 }
George Mount70778d72014-07-01 16:33:45 -07002337 } else if (options != null && new ActivityOptions(options).getAnimationType()
2338 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2339 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002340 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002341 if (r.mLaunchTaskBehind) {
2342 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2343 // tell WindowManager that r is visible even though it is at the back of the stack.
2344 mWindowManager.setAppVisibility(r.appToken, true);
2345 ensureActivitiesVisibleLocked(null, 0);
2346 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002347 // Figure out if we are transitioning from another activity that is
2348 // "has the same starting icon" as the next one. This allows the
2349 // window manager to keep the previous window it had previously
2350 // created, if it still had one.
2351 ActivityRecord prev = mResumedActivity;
2352 if (prev != null) {
2353 // We don't want to reuse the previous starting preview if:
2354 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002355 if (prev.task != r.task) {
2356 prev = null;
2357 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002358 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002359 else if (prev.nowVisible) {
2360 prev = null;
2361 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002362 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002363 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002364 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002365 mService.compatibilityInfoForPackageLocked(
2366 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002367 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002368 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002369 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002370 }
2371 } else {
2372 // If this is the first activity, don't do any fancy animations,
2373 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002374 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002375 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002376 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002377 }
2378 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002379 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002380 }
2381
2382 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002383 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002384 } else {
2385 addRecentActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002386 }
2387 }
2388
Dianne Hackbornbe707852011-11-11 14:32:10 -08002389 final void validateAppTokensLocked() {
2390 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002391 mValidateAppTokens.ensureCapacity(numActivities());
2392 final int numTasks = mTaskHistory.size();
2393 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2394 TaskRecord task = mTaskHistory.get(taskNdx);
2395 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002396 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002397 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002398 }
Craig Mautner000f0022013-02-26 15:04:29 -08002399 TaskGroup group = new TaskGroup();
2400 group.taskId = task.taskId;
2401 mValidateAppTokens.add(group);
2402 final int numActivities = activities.size();
2403 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2404 final ActivityRecord r = activities.get(activityNdx);
2405 group.tokens.add(r.appToken);
2406 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002407 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002408 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002409 }
2410
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002411 /**
2412 * Perform a reset of the given task, if needed as part of launching it.
2413 * Returns the new HistoryRecord at the top of the task.
2414 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002415 /**
2416 * Helper method for #resetTaskIfNeededLocked.
2417 * We are inside of the task being reset... we'll either finish this activity, push it out
2418 * for another task, or leave it as-is.
2419 * @param task The task containing the Activity (taskTop) that might be reset.
2420 * @param forceReset
2421 * @return An ActivityOptions that needs to be processed.
2422 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002423 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002424 ActivityOptions topOptions = null;
2425
2426 int replyChainEnd = -1;
2427 boolean canMoveOptions = true;
2428
2429 // We only do this for activities that are not the root of the task (since if we finish
2430 // the root, we may no longer have the task!).
2431 final ArrayList<ActivityRecord> activities = task.mActivities;
2432 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002433 final int rootActivityNdx = task.findEffectiveRootIndex();
2434 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002435 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002436 if (target.frontOfTask)
2437 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002438
2439 final int flags = target.info.flags;
2440 final boolean finishOnTaskLaunch =
2441 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2442 final boolean allowTaskReparenting =
2443 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2444 final boolean clearWhenTaskReset =
2445 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2446
2447 if (!finishOnTaskLaunch
2448 && !clearWhenTaskReset
2449 && target.resultTo != null) {
2450 // If this activity is sending a reply to a previous
2451 // activity, we can't do anything with it now until
2452 // we reach the start of the reply chain.
2453 // XXX note that we are assuming the result is always
2454 // to the previous activity, which is almost always
2455 // the case but we really shouldn't count on.
2456 if (replyChainEnd < 0) {
2457 replyChainEnd = i;
2458 }
2459 } else if (!finishOnTaskLaunch
2460 && !clearWhenTaskReset
2461 && allowTaskReparenting
2462 && target.taskAffinity != null
2463 && !target.taskAffinity.equals(task.affinity)) {
2464 // If this activity has an affinity for another
2465 // task, then we need to move it out of here. We will
2466 // move it as far out of the way as possible, to the
2467 // bottom of the activity stack. This also keeps it
2468 // correctly ordered with any activities we previously
2469 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002470 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002471 final ActivityRecord bottom =
2472 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002473 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002474 if (bottom != null && target.taskAffinity != null
2475 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002476 // If the activity currently at the bottom has the
2477 // same task affinity as the one we are moving,
2478 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002479 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002480 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002481 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002482 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002483 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002484 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002485 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002486 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002487 + " out to new task " + target.task);
2488 }
2489
Wale Ogunwale706ed792015-08-02 10:29:44 -07002490 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002491
Craig Mautner525f3d92013-05-07 14:01:50 -07002492 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002493 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2494 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002495 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002496 if (p.finishing) {
2497 continue;
2498 }
2499
Craig Mautnere3a74d52013-02-22 14:14:58 -08002500 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002501 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002502 topOptions = p.takeOptionsLocked();
2503 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002504 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002505 }
2506 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002507 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2508 "Removing activity " + p + " from task=" + task + " adding to task="
2509 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002510 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2511 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002512 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002513 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002514
Wale Ogunwale706ed792015-08-02 10:29:44 -07002515 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002516 }
2517
Wale Ogunwale706ed792015-08-02 10:29:44 -07002518 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002519 if (VALIDATE_TOKENS) {
2520 validateAppTokensLocked();
2521 }
2522
2523 replyChainEnd = -1;
2524 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2525 // If the activity should just be removed -- either
2526 // because it asks for it, or the task should be
2527 // cleared -- then finish it and anything that is
2528 // part of its reply chain.
2529 int end;
2530 if (clearWhenTaskReset) {
2531 // In this case, we want to finish this activity
2532 // and everything above it, so be sneaky and pretend
2533 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002534 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002535 } else if (replyChainEnd < 0) {
2536 end = i;
2537 } else {
2538 end = replyChainEnd;
2539 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002540 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002541 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002542 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002543 if (p.finishing) {
2544 continue;
2545 }
2546 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002547 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002548 topOptions = p.takeOptionsLocked();
2549 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002550 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002551 }
2552 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002553 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002554 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002555 if (finishActivityLocked(
2556 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002557 end--;
2558 srcPos--;
2559 }
2560 }
2561 replyChainEnd = -1;
2562 } else {
2563 // If we were in the middle of a chain, well the
2564 // activity that started it all doesn't want anything
2565 // special, so leave it all as-is.
2566 replyChainEnd = -1;
2567 }
2568 }
2569
2570 return topOptions;
2571 }
2572
2573 /**
2574 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2575 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2576 * @param affinityTask The task we are looking for an affinity to.
2577 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2578 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2579 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2580 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002581 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002582 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002583 int replyChainEnd = -1;
2584 final int taskId = task.taskId;
2585 final String taskAffinity = task.affinity;
2586
2587 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2588 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002589 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2590
2591 // Do not operate on or below the effective root Activity.
2592 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002593 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002594 if (target.frontOfTask)
2595 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002596
2597 final int flags = target.info.flags;
2598 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2599 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2600
2601 if (target.resultTo != null) {
2602 // If this activity is sending a reply to a previous
2603 // activity, we can't do anything with it now until
2604 // we reach the start of the reply chain.
2605 // XXX note that we are assuming the result is always
2606 // to the previous activity, which is almost always
2607 // the case but we really shouldn't count on.
2608 if (replyChainEnd < 0) {
2609 replyChainEnd = i;
2610 }
2611 } else if (topTaskIsHigher
2612 && allowTaskReparenting
2613 && taskAffinity != null
2614 && taskAffinity.equals(target.taskAffinity)) {
2615 // This activity has an affinity for our task. Either remove it if we are
2616 // clearing or move it over to our task. Note that
2617 // we currently punt on the case where we are resetting a
2618 // task that is not at the top but who has activities above
2619 // with an affinity to it... this is really not a normal
2620 // case, and we will need to later pull that task to the front
2621 // and usually at that point we will do the reset and pick
2622 // up those remaining activities. (This only happens if
2623 // someone starts an activity in a new task from an activity
2624 // in a task that is not currently on top.)
2625 if (forceReset || finishOnTaskLaunch) {
2626 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002627 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2628 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002629 for (int srcPos = start; srcPos >= i; --srcPos) {
2630 final ActivityRecord p = activities.get(srcPos);
2631 if (p.finishing) {
2632 continue;
2633 }
Todd Kennedy539db512014-12-15 09:57:55 -08002634 finishActivityLocked(
2635 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002636 }
2637 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002638 if (taskInsertionPoint < 0) {
2639 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002640
Craig Mautner77878772013-03-04 19:46:24 -08002641 }
Craig Mautner77878772013-03-04 19:46:24 -08002642
2643 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002644 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2645 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2646 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002647 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002648 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002649 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002650 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002651
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002652 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2653 "Removing and adding activity " + p + " to stack at " + task
2654 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002655 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2656 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002657 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002658 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002659 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002660 if (VALIDATE_TOKENS) {
2661 validateAppTokensLocked();
2662 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002663
2664 // Now we've moved it in to place... but what if this is
2665 // a singleTop activity and we have put it on top of another
2666 // instance of the same activity? Then we drop the instance
2667 // below so it remains singleTop.
2668 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2669 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002670 int targetNdx = taskActivities.indexOf(target);
2671 if (targetNdx > 0) {
2672 ActivityRecord p = taskActivities.get(targetNdx - 1);
2673 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002674 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2675 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002676 }
2677 }
2678 }
2679 }
2680
2681 replyChainEnd = -1;
2682 }
2683 }
Craig Mautner77878772013-03-04 19:46:24 -08002684 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002685 }
2686
Craig Mautner8849a5e2013-04-02 16:41:03 -07002687 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002688 ActivityRecord newActivity) {
2689 boolean forceReset =
2690 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2691 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2692 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2693 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2694 forceReset = true;
2695 }
2696 }
2697
2698 final TaskRecord task = taskTop.task;
2699
2700 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2701 * for remaining tasks. Used for later tasks to reparent to task. */
2702 boolean taskFound = false;
2703
2704 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2705 ActivityOptions topOptions = null;
2706
Craig Mautner77878772013-03-04 19:46:24 -08002707 // Preserve the location for reparenting in the new task.
2708 int reparentInsertionPoint = -1;
2709
Craig Mautnere3a74d52013-02-22 14:14:58 -08002710 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2711 final TaskRecord targetTask = mTaskHistory.get(i);
2712
2713 if (targetTask == task) {
2714 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2715 taskFound = true;
2716 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002717 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2718 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002719 }
2720 }
2721
Craig Mautner70a86932013-02-28 22:37:44 -08002722 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002723 if (taskNdx >= 0) {
2724 do {
2725 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2726 } while (taskTop == null && taskNdx >= 0);
2727 }
Craig Mautner70a86932013-02-28 22:37:44 -08002728
Craig Mautnere3a74d52013-02-22 14:14:58 -08002729 if (topOptions != null) {
2730 // If we got some ActivityOptions from an activity on top that
2731 // was removed from the task, propagate them to the new real top.
2732 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002733 taskTop.updateOptionsLocked(topOptions);
2734 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002735 topOptions.abort();
2736 }
2737 }
2738
2739 return taskTop;
2740 }
2741
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002742 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2743 String resultWho, int requestCode, int resultCode, Intent data) {
2744
2745 if (callingUid > 0) {
2746 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002747 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002748 }
2749
2750 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2751 + " : who=" + resultWho + " req=" + requestCode
2752 + " res=" + resultCode + " data=" + data);
2753 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2754 try {
2755 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2756 list.add(new ResultInfo(resultWho, requestCode,
2757 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002758 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002759 return;
2760 } catch (Exception e) {
2761 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2762 }
2763 }
2764
2765 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2766 }
2767
Craig Mautner299f9602015-01-26 09:47:33 -08002768 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002769 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2770 ActivityRecord next = topRunningActivityLocked(null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002771 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002772 if (next != r) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002773 if (next != null && (mStackId == FREEFORM_WORKSPACE_STACK_ID
2774 || mStackId == DOCKED_STACK_ID)) {
2775 // For freeform and docked stacks we always keep the focus within the stack as
2776 // long as there is a running activity in the stack that we can adjust focus to.
Wale Ogunwale61803ee2015-08-07 19:59:47 -07002777 mService.setFocusedActivityLocked(next, myReason);
2778 return;
2779 } else {
2780 final TaskRecord task = r.task;
2781 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2782 // For non-fullscreen stack, we want to move the focus to the next visible
2783 // stack to prevent the home screen from moving to the top and obscuring
2784 // other visible stacks.
2785 if (!mFullscreen
2786 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2787 return;
2788 }
2789 // Move the home stack to the top if this stack is fullscreen or there is no
2790 // other visible stack.
2791 if (mStackSupervisor.moveHomeStackTaskToTop(
2792 task.getTaskToReturnTo(), myReason)) {
2793 // Activity focus was already adjusted. Nothing else to do...
2794 return;
2795 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002796 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002797 }
2798 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002799
2800 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002801 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002802 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002803 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002804 }
2805 }
2806
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002807 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2808 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2809 final String myReason = reason + " adjustFocusToNextVisibleStack";
2810 if (stack == null) {
2811 return false;
2812 }
2813 final ActivityRecord top = stack.topRunningActivityLocked(null);
2814 if (top == null) {
2815 return false;
2816 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002817 mService.setFocusedActivityLocked(top, myReason);
2818 return true;
2819 }
2820
Craig Mautnerf3333272013-04-22 10:55:53 -07002821 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002822 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002823 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2824 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2825 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002826 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002827 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002828 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2829 "stop-no-history", false)) {
2830 // Activity was finished, no need to continue trying to schedule stop.
2831 adjustFocusedActivityLocked(r, "stopActivityFinished");
2832 r.resumeKeyDispatchingLocked();
2833 return;
2834 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002835 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002836 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002837 + " on stop because we're just sleeping");
2838 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002839 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002840 }
2841
2842 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002843 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002844 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002845 try {
2846 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002847 if (DEBUG_STATES) Slog.v(TAG_STATES,
2848 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002849 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002850 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2851 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002852 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002853 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002854 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002855 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002856 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002857 r.setSleeping(true);
2858 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002859 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002860 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002861 } catch (Exception e) {
2862 // Maybe just ignore exceptions here... if the process
2863 // has crashed, our death notification will clean things
2864 // up.
2865 Slog.w(TAG, "Exception thrown during pause", e);
2866 // Just in case, assume it to be stopped.
2867 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002868 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002869 r.state = ActivityState.STOPPED;
2870 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002871 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002872 }
2873 }
2874 }
2875 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002876
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002877 /**
2878 * @return Returns true if the activity is being finished, false if for
2879 * some reason it is being left as-is.
2880 */
2881 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002882 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002883 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002884 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2885 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002886 + ", result=" + resultCode + ", data=" + resultData
2887 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002888 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002889 return false;
2890 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002891
Craig Mautnerd44711d2013-02-23 11:24:36 -08002892 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002893 return true;
2894 }
2895
Craig Mautnerd2328952013-03-05 12:46:26 -08002896 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002897 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2898 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2899 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2900 ActivityRecord r = activities.get(activityNdx);
2901 if (r.resultTo == self && r.requestCode == requestCode) {
2902 if ((r.resultWho == null && resultWho == null) ||
2903 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2904 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2905 false);
2906 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002907 }
2908 }
2909 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002910 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002911 }
2912
Todd Kennedy539db512014-12-15 09:57:55 -08002913 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002914 ActivityRecord r = topRunningActivityLocked(null);
2915 if (r != null && r.app == app) {
2916 // If the top running activity is from this crashing
2917 // process, then terminate it to avoid getting in a loop.
2918 Slog.w(TAG, " Force finishing activity "
2919 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002920 int taskNdx = mTaskHistory.indexOf(r.task);
2921 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002922 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002923 // Also terminate any activities below it that aren't yet
2924 // stopped, to avoid a situation where one will get
2925 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002926 --activityNdx;
2927 if (activityNdx < 0) {
2928 do {
2929 --taskNdx;
2930 if (taskNdx < 0) {
2931 break;
2932 }
2933 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2934 } while (activityNdx < 0);
2935 }
2936 if (activityNdx >= 0) {
2937 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002938 if (r.state == ActivityState.RESUMED
2939 || r.state == ActivityState.PAUSING
2940 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002941 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002942 Slog.w(TAG, " Force finishing activity "
2943 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002944 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002945 }
2946 }
2947 }
2948 }
2949 }
2950
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002951 final void finishVoiceTask(IVoiceInteractionSession session) {
2952 IBinder sessionBinder = session.asBinder();
2953 boolean didOne = false;
2954 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2955 TaskRecord tr = mTaskHistory.get(taskNdx);
2956 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2957 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2958 ActivityRecord r = tr.mActivities.get(activityNdx);
2959 if (!r.finishing) {
2960 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2961 false);
2962 didOne = true;
2963 }
2964 }
2965 }
2966 }
2967 if (didOne) {
2968 mService.updateOomAdjLocked();
2969 }
2970 }
2971
Craig Mautnerd2328952013-03-05 12:46:26 -08002972 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002973 ArrayList<ActivityRecord> activities = r.task.mActivities;
2974 for (int index = activities.indexOf(r); index >= 0; --index) {
2975 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002976 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002977 break;
2978 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002979 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002980 }
2981 return true;
2982 }
2983
Dianne Hackborn5c607432012-02-28 14:44:19 -08002984 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2985 // send the result
2986 ActivityRecord resultTo = r.resultTo;
2987 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002988 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08002989 + " who=" + r.resultWho + " req=" + r.requestCode
2990 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002991 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002992 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01002993 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002994 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002995 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002996 if (r.info.applicationInfo.uid > 0) {
2997 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2998 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002999 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003000 }
3001 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3002 resultData);
3003 r.resultTo = null;
3004 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003005 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003006
3007 // Make sure this HistoryRecord is not holding on to other resources,
3008 // because clients have remote IPC references to this object so we
3009 // can't assume that will go away and want to avoid circular IPC refs.
3010 r.results = null;
3011 r.pendingResults = null;
3012 r.newIntents = null;
3013 r.icicle = null;
3014 }
3015
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003016 /**
3017 * @return Returns true if this activity has been removed from the history
3018 * list, or false if it is still in the list and will be removed later.
3019 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003020 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3021 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003022 if (r.finishing) {
3023 Slog.w(TAG, "Duplicate finish request for " + r);
3024 return false;
3025 }
3026
Wale Ogunwale7d701172015-03-11 15:36:30 -07003027 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003028 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003029 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003030 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003031 task.taskId, r.shortComponentName, reason);
3032 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003033 final int index = activities.indexOf(r);
3034 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003035 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003036 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003037 // If the caller asked that this activity (and all above it)
3038 // be cleared when the task is reset, don't lose that information,
3039 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003040 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003041 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003042 }
3043 }
3044
3045 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003046
Craig Mautner299f9602015-01-26 09:47:33 -08003047 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003048
Dianne Hackborn5c607432012-02-28 14:44:19 -08003049 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003050
Craig Mautnerde4ef022013-04-07 19:01:33 -07003051 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003052 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003053 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003054 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003055 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003056 ? AppTransition.TRANSIT_TASK_CLOSE
3057 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003058
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003059 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003060 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003061
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003062 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003063 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3064 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3065 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003066 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003067 }
3068
Craig Mautneraea74a52014-03-08 14:23:10 -08003069 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003070 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003071 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003072 } else if (r.state != ActivityState.PAUSING) {
3073 // If the activity is PAUSING, we will complete the finish once
3074 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003075 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003076 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003077 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003078 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003079 }
3080
3081 return false;
3082 }
3083
Craig Mautnerf3333272013-04-22 10:55:53 -07003084 static final int FINISH_IMMEDIATELY = 0;
3085 static final int FINISH_AFTER_PAUSE = 1;
3086 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003087
Craig Mautnerf3333272013-04-22 10:55:53 -07003088 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003089 // First things first: if this activity is currently visible,
3090 // and the resumed activity is not yet visible, then hold off on
3091 // finishing until the resumed one becomes visible.
3092 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003093 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
3094 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07003095 if (mStackSupervisor.mStoppingActivities.size() > 3
3096 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003097 // If we already have a few activities waiting to stop,
3098 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07003099 // them out. Or if r is the last of activity of the last task the stack
3100 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07003101 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003102 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003103 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003104 }
3105 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003106 if (DEBUG_STATES) Slog.v(TAG_STATES,
3107 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003108 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003109 if (oomAdj) {
3110 mService.updateOomAdjLocked();
3111 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003112 return r;
3113 }
3114
3115 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003116 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003117 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003118 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003119 if (mResumedActivity == r) {
3120 mResumedActivity = null;
3121 }
3122 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003123 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003124 r.state = ActivityState.FINISHING;
3125
3126 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003127 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003128 || prevState == ActivityState.STOPPED
3129 || prevState == ActivityState.INITIALIZING) {
3130 // If this activity is already stopped, we can just finish
3131 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003132 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003133 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003134 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003135 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003136 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003137 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003138 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3139 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003140 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003141 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003142
3143 // Need to go through the full pause cycle to get this
3144 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003145 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003146 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003147 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003148 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003149 return r;
3150 }
3151
Craig Mautneree36c772014-07-16 14:56:05 -07003152 void finishAllActivitiesLocked(boolean immediately) {
3153 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003154 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3155 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3156 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3157 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003158 noActivitiesInStack = false;
3159 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003160 continue;
3161 }
Craig Mautneree36c772014-07-16 14:56:05 -07003162 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003163 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3164 }
3165 }
Craig Mautneree36c772014-07-16 14:56:05 -07003166 if (noActivitiesInStack) {
3167 mActivityContainer.onTaskListEmptyLocked();
3168 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003169 }
3170
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003171 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3172 // Basic case: for simple app-centric recents, we need to recreate
3173 // the task if the affinity has changed.
3174 if (srec == null || srec.task.affinity == null ||
3175 !srec.task.affinity.equals(destAffinity)) {
3176 return true;
3177 }
3178 // Document-centric case: an app may be split in to multiple documents;
3179 // they need to re-create their task if this current activity is the root
3180 // of a document, unless simply finishing it will return them to the the
3181 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003182 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3183 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003184 // Okay, this activity is at the root of its task. What to do, what to do...
3185 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3186 // Finishing won't return to an application, so we need to recreate.
3187 return true;
3188 }
3189 // We now need to get the task below it to determine what to do.
3190 int taskIdx = mTaskHistory.indexOf(srec.task);
3191 if (taskIdx <= 0) {
3192 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3193 return false;
3194 }
3195 if (taskIdx == 0) {
3196 // At the bottom of the stack, nothing to go back to.
3197 return true;
3198 }
3199 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3200 if (!srec.task.affinity.equals(prevTask.affinity)) {
3201 // These are different apps, so need to recreate.
3202 return true;
3203 }
3204 }
3205 return false;
3206 }
3207
Wale Ogunwale7d701172015-03-11 15:36:30 -07003208 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003209 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003210 final TaskRecord task = srec.task;
3211 final ArrayList<ActivityRecord> activities = task.mActivities;
3212 final int start = activities.indexOf(srec);
3213 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003214 return false;
3215 }
3216 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003217 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003218 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003219 final ComponentName dest = destIntent.getComponent();
3220 if (start > 0 && dest != null) {
3221 for (int i = finishTo; i >= 0; i--) {
3222 ActivityRecord r = activities.get(i);
3223 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003224 r.info.name.equals(dest.getClassName())) {
3225 finishTo = i;
3226 parent = r;
3227 foundParentInTask = true;
3228 break;
3229 }
3230 }
3231 }
3232
3233 IActivityController controller = mService.mController;
3234 if (controller != null) {
3235 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3236 if (next != null) {
3237 // ask watcher if this is allowed
3238 boolean resumeOK = true;
3239 try {
3240 resumeOK = controller.activityResuming(next.packageName);
3241 } catch (RemoteException e) {
3242 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003243 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003244 }
3245
3246 if (!resumeOK) {
3247 return false;
3248 }
3249 }
3250 }
3251 final long origId = Binder.clearCallingIdentity();
3252 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003253 ActivityRecord r = activities.get(i);
3254 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003255 // Only return the supplied result for the first activity finished
3256 resultCode = Activity.RESULT_CANCELED;
3257 resultData = null;
3258 }
3259
3260 if (parent != null && foundParentInTask) {
3261 final int parentLaunchMode = parent.info.launchMode;
3262 final int destIntentFlags = destIntent.getFlags();
3263 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3264 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3265 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3266 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003267 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3268 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003269 } else {
3270 try {
3271 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3272 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003273 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003274 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003275 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003276 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003277 foundParentInTask = res == ActivityManager.START_SUCCESS;
3278 } catch (RemoteException e) {
3279 foundParentInTask = false;
3280 }
3281 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003282 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003283 }
3284 }
3285 Binder.restoreCallingIdentity(origId);
3286 return foundParentInTask;
3287 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003288 /**
3289 * Perform the common clean-up of an activity record. This is called both
3290 * as part of destroyActivityLocked() (when destroying the client-side
3291 * representation) and cleaning things up as a result of its hosting
3292 * processing going away, in which case there is no remaining client-side
3293 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003294 *
3295 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003296 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003297 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3298 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003299 if (mResumedActivity == r) {
3300 mResumedActivity = null;
3301 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003302 if (mPausingActivity == r) {
3303 mPausingActivity = null;
3304 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003305 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003306
3307 r.configDestroy = false;
3308 r.frozenBeforeDestroy = false;
3309
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003310 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003311 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003312 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003313 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003314 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003315 }
3316
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003317 // Make sure this record is no longer in the pending finishes list.
3318 // This could happen, for example, if we are trimming activities
3319 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003320 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003321 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003322
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003323 // Remove any pending results.
3324 if (r.finishing && r.pendingResults != null) {
3325 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3326 PendingIntentRecord rec = apr.get();
3327 if (rec != null) {
3328 mService.cancelIntentSenderLocked(rec, false);
3329 }
3330 }
3331 r.pendingResults = null;
3332 }
3333
3334 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003335 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003336 }
3337
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003338 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003339 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003340 if (getVisibleBehindActivity() == r) {
3341 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003342 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003343 }
3344
3345 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003346 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003347 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003348 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003349 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003350 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003351 }
3352
Craig Mautner299f9602015-01-26 09:47:33 -08003353 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003354 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003355 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003356 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003357 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3358 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3359
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003360 r.takeFromHistory();
3361 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003362 if (DEBUG_STATES) Slog.v(TAG_STATES,
3363 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003364 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003365 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003366 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003367 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003368 if (VALIDATE_TOKENS) {
3369 validateAppTokensLocked();
3370 }
Craig Mautner312ba862014-02-10 17:55:01 -08003371 final TaskRecord task = r.task;
3372 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003373 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003374 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003375 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3376 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003377 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003378 }
Craig Mautner299f9602015-01-26 09:47:33 -08003379 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003380 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003381 cleanUpActivityServicesLocked(r);
3382 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003383 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003384
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003385 /**
3386 * Perform clean-up of service connections in an activity record.
3387 */
3388 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3389 // Throw away any services that have been bound by this activity.
3390 if (r.connections != null) {
3391 Iterator<ConnectionRecord> it = r.connections.iterator();
3392 while (it.hasNext()) {
3393 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003394 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003395 }
3396 r.connections = null;
3397 }
3398 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003399
Craig Mautneree2e45a2014-06-27 12:10:03 -07003400 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003401 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003402 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003403 mHandler.sendMessage(msg);
3404 }
3405
Craig Mautneree2e45a2014-06-27 12:10:03 -07003406 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003407 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003408 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003409 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3410 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3411 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3412 final ActivityRecord r = activities.get(activityNdx);
3413 if (r.finishing) {
3414 continue;
3415 }
3416 if (r.fullscreen) {
3417 lastIsOpaque = true;
3418 }
3419 if (owner != null && r.app != owner) {
3420 continue;
3421 }
3422 if (!lastIsOpaque) {
3423 continue;
3424 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003425 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003426 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003427 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003428 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003429 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003430 activityRemoved = true;
3431 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003432 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003433 }
3434 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003435 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003436 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003437 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003438 }
3439
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003440 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3441 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003442 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3443 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003444 + " pausing=" + mPausingActivity + " for reason " + reason);
3445 return destroyActivityLocked(r, true, reason);
3446 }
3447 return false;
3448 }
3449
3450 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3451 String reason) {
3452 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003453 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003454 int maxTasks = tasks.size() / 4;
3455 if (maxTasks < 1) {
3456 maxTasks = 1;
3457 }
3458 int numReleased = 0;
3459 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3460 final TaskRecord task = mTaskHistory.get(taskNdx);
3461 if (!tasks.contains(task)) {
3462 continue;
3463 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003464 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003465 int curNum = 0;
3466 final ArrayList<ActivityRecord> activities = task.mActivities;
3467 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3468 final ActivityRecord activity = activities.get(actNdx);
3469 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003470 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003471 + " in state " + activity.state + " resumed=" + mResumedActivity
3472 + " pausing=" + mPausingActivity + " for reason " + reason);
3473 destroyActivityLocked(activity, true, reason);
3474 if (activities.get(actNdx) != activity) {
3475 // Was removed from list, back up so we don't miss the next one.
3476 actNdx--;
3477 }
3478 curNum++;
3479 }
3480 }
3481 if (curNum > 0) {
3482 numReleased += curNum;
3483 maxTasks--;
3484 if (mTaskHistory.get(taskNdx) != task) {
3485 // The entire task got removed, back up so we don't miss the next one.
3486 taskNdx--;
3487 }
3488 }
3489 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003490 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3491 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003492 return numReleased;
3493 }
3494
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003495 /**
3496 * Destroy the current CLIENT SIDE instance of an activity. This may be
3497 * called both when actually finishing an activity, or when performing
3498 * a configuration switch where we destroy the current client-side object
3499 * but then create a new client-side object for this same HistoryRecord.
3500 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003501 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003502 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3503 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003504 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003505 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003506 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003507 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003508
3509 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003510
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003511 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003512
3513 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003514
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003515 if (hadApp) {
3516 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003517 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003518 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3519 mService.mHeavyWeightProcess = null;
3520 mService.mHandler.sendEmptyMessage(
3521 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3522 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003523 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003524 // Update any services we are bound to that might care about whether
3525 // their client may have activities.
3526 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003527 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003528 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003529 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003530 }
3531 }
3532
3533 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003534
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003535 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003536 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003537 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003538 r.configChangeFlags);
3539 } catch (Exception e) {
3540 // We can just ignore exceptions here... if the process
3541 // has crashed, our death notification will clean things
3542 // up.
3543 //Slog.w(TAG, "Exception thrown during finish", e);
3544 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003545 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003546 removedFromHistory = true;
3547 skipDestroy = true;
3548 }
3549 }
3550
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003551 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003552
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003553 // If the activity is finishing, we need to wait on removing it
3554 // from the list to give it a chance to do its cleanup. During
3555 // that time it may make calls back with its token so we need to
3556 // be able to find it on the list and so we don't want to remove
3557 // it from the list yet. Otherwise, we can just immediately put
3558 // it in the destroyed state since we are not removing it from the
3559 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003560 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003561 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003562 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003563 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003564 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003565 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3566 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003567 if (DEBUG_STATES) Slog.v(TAG_STATES,
3568 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003569 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003570 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003571 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003572 }
3573 } else {
3574 // remove this record from the history.
3575 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003576 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003577 removedFromHistory = true;
3578 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003579 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003580 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003581 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003582 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003583 }
3584 }
3585
3586 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003587
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003588 if (!mLRUActivities.remove(r) && hadApp) {
3589 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3590 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003591
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003592 return removedFromHistory;
3593 }
3594
Craig Mautner299f9602015-01-26 09:47:33 -08003595 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003596 final long origId = Binder.clearCallingIdentity();
3597 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003598 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003599 if (r != null) {
3600 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003601 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003602 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003603
Wale Ogunwale60454db2015-01-23 16:05:07 -08003604 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003605 if (r.state == ActivityState.DESTROYING) {
3606 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003607 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003608 }
3609 }
Craig Mautner05d29032013-05-03 13:40:13 -07003610 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003611 } finally {
3612 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003613 }
3614 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003615
Todd Kennedyaab56db2015-01-30 09:39:53 -08003616 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003617 if (hasVisibleBehindActivity() &&
3618 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003619 if (r == topRunningActivityLocked(null)) {
3620 // Don't release the top activity if it has requested to run behind the next
3621 // activity.
3622 return;
3623 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003624 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003625 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003626 " thread=" + r.app.thread);
3627 if (r != null && r.app != null && r.app.thread != null) {
3628 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003629 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003630 } catch (RemoteException e) {
3631 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003632 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003633 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003634 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003635 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003636 }
3637 }
3638 }
3639
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003640 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003641 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3642 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003643 if (r != null) {
3644 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003645 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003646 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003647 }
3648 mStackSupervisor.resumeTopActivitiesLocked();
3649 }
3650
Jose Lima4b6c6692014-08-12 17:41:12 -07003651 boolean hasVisibleBehindActivity() {
3652 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003653 }
3654
Jose Lima4b6c6692014-08-12 17:41:12 -07003655 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003656 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003657 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003658 }
3659 }
3660
Jose Lima4b6c6692014-08-12 17:41:12 -07003661 ActivityRecord getVisibleBehindActivity() {
3662 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003663 }
3664
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003665 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3666 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003667 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003668 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3669 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003670 while (i > 0) {
3671 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003672 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003673 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003674 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003675 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003676 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003677 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003678 }
3679 }
3680 }
3681
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003682 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3683 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003684 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3685 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003686 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3687 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003688 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003689 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003690 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3691 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003692
3693 boolean hasVisibleActivities = false;
3694
3695 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003696 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003697 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3698 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003699 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3700 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3701 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3702 final ActivityRecord r = activities.get(activityNdx);
3703 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003704 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3705 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003706 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003707 if (r.visible) {
3708 hasVisibleActivities = true;
3709 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003710 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003711 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3712 // Don't currently have state for the activity, or
3713 // it is finishing -- always remove it.
3714 remove = true;
3715 } else if (r.launchCount > 2 &&
3716 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3717 // We have launched this activity too many times since it was
3718 // able to run, so give up and remove it.
3719 remove = true;
3720 } else {
3721 // The process may be gone, but the activity lives on!
3722 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003723 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003724 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003725 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3726 "Removing activity " + r + " from stack at " + i
3727 + ": haveState=" + r.haveState
3728 + " stateNotNeeded=" + r.stateNotNeeded
3729 + " finishing=" + r.finishing
3730 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003731 if (!r.finishing) {
3732 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3733 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3734 r.userId, System.identityHashCode(r),
3735 r.task.taskId, r.shortComponentName,
3736 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003737 if (r.state == ActivityState.RESUMED) {
3738 mService.updateUsageStats(r, false);
3739 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003740 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003741 } else {
3742 // We have the current state for this activity, so
3743 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003744 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3745 if (DEBUG_APP) Slog.v(TAG_APP,
3746 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003747 r.app = null;
3748 r.nowVisible = false;
3749 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003750 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003751 "App died, clearing saved state of " + r);
3752 r.icicle = null;
3753 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003754 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003755 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003756 if (remove) {
3757 removeActivityFromHistoryLocked(r, "appDied");
3758 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003759 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003760 }
3761 }
3762
3763 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003764 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003765
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003766 final void updateTransitLocked(int transit, Bundle options) {
3767 if (options != null) {
3768 ActivityRecord r = topRunningActivityLocked(null);
3769 if (r != null && r.state != ActivityState.RESUMED) {
3770 r.updateOptionsLocked(options);
3771 } else {
3772 ActivityOptions.abort(options);
3773 }
3774 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003775 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003776 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003777
Craig Mautner21d24a22014-04-23 11:45:37 -07003778 void updateTaskMovement(TaskRecord task, boolean toFront) {
3779 if (task.isPersistable) {
3780 task.mLastTimeMoved = System.currentTimeMillis();
3781 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3782 // recently will be most negative, tasks sent to the bottom before that will be less
3783 // negative. Similarly for recent tasks moved to the top which will be most positive.
3784 if (!toFront) {
3785 task.mLastTimeMoved *= -1;
3786 }
3787 }
3788 }
3789
Craig Mautner84984fa2014-06-19 11:19:20 -07003790 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003791 final int top = mTaskHistory.size() - 1;
3792 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3793 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003794 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003795 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3796 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003797 mTaskHistory.remove(taskNdx);
3798 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003799 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003800 return;
3801 }
3802 }
3803 }
3804
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003805 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003806 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003807 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003808
Craig Mautner11bf9a52013-02-19 14:08:51 -08003809 final int numTasks = mTaskHistory.size();
3810 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003811 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003812 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003813 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003814 ActivityOptions.abort(options);
3815 } else {
3816 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3817 }
3818 return;
3819 }
3820
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003821 if (timeTracker != null) {
3822 // The caller wants a time tracker associated with this task.
3823 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3824 tr.mActivities.get(i).appTimeTracker = timeTracker;
3825 }
3826 }
3827
Craig Mautner11bf9a52013-02-19 14:08:51 -08003828 // Shift all activities with this task up to the top
3829 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003830 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003831
Chong Zhang45c25ce2015-08-10 22:18:26 -07003832 // Don't refocus if invisible to current user
3833 ActivityRecord top = tr.getTopActivity();
3834 if (!okToShowLocked(top)) {
3835 addRecentActivityLocked(top);
3836 ActivityOptions.abort(options);
3837 return;
3838 }
3839
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003840 // Set focus to the top running activity of this stack.
3841 ActivityRecord r = topRunningActivityLocked(null);
3842 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003843
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003844 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003845 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003846 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003847 if (r != null) {
3848 mNoAnimActivities.add(r);
3849 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003850 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003851 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003852 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003853 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003854
Craig Mautner05d29032013-05-03 13:40:13 -07003855 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003856 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003857
3858 if (VALIDATE_TOKENS) {
3859 validateAppTokensLocked();
3860 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003861 }
3862
3863 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003864 * Worker method for rearranging history stack. Implements the function of moving all
3865 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003866 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003867 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003868 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3869 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003870 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003871 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003872 * @return Returns true if the move completed, false if not.
3873 */
Craig Mautner299f9602015-01-26 09:47:33 -08003874 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003875 final TaskRecord tr = taskForIdLocked(taskId);
3876 if (tr == null) {
3877 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3878 return false;
3879 }
3880
3881 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003882 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003883
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003884 // If we have a watcher, preflight the move before committing to it. First check
3885 // for *other* available tasks, but if none are available, then try again allowing the
3886 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003887 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003888 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003889 if (next == null) {
3890 next = topRunningActivityLocked(null, 0);
3891 }
3892 if (next != null) {
3893 // ask watcher if this is allowed
3894 boolean moveOK = true;
3895 try {
3896 moveOK = mService.mController.activityResuming(next.packageName);
3897 } catch (RemoteException e) {
3898 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003899 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003900 }
3901 if (!moveOK) {
3902 return false;
3903 }
3904 }
3905 }
3906
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003907 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003908
riddle_hsuc215a4f2014-12-27 12:10:45 +08003909 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07003910
3911 // If true, we should resume the home activity next if the task we are moving to the
3912 // back is over the home stack. We force to false if the task we are moving to back
3913 // is the home task and we don't want it resumed after moving to the back.
3914 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
3915 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08003916 final TaskRecord nextTask = getNextTask(tr);
3917 if (nextTask != null) {
3918 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3919 } else {
3920 prevIsHome = true;
3921 }
3922 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003923 mTaskHistory.remove(tr);
3924 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003925 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003926
Craig Mautnerc8143c62013-09-03 12:15:57 -07003927 // There is an assumption that moving a task to the back moves it behind the home activity.
3928 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003929 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003930 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3931 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003932 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003933 break;
3934 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003935 if (taskNdx == 1) {
3936 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003937 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003938 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003939 }
3940
Craig Mautner299f9602015-01-26 09:47:33 -08003941 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003942 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003943
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003944 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003945 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003946 }
3947
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003948 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07003949 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003950 if (!mService.mBooting && !mService.mBooted) {
3951 // Not ready yet!
3952 return false;
3953 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003954 final int taskToReturnTo = tr.getTaskToReturnTo();
3955 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003956 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003957 }
3958
Craig Mautner05d29032013-05-03 13:40:13 -07003959 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003960 return true;
3961 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003962
Craig Mautner8849a5e2013-04-02 16:41:03 -07003963 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003964 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003965 final Uri data = r.intent.getData();
3966 final String strData = data != null ? data.toSafeString() : null;
3967
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003968 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003969 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003970 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003971 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003972 }
3973
3974 /**
3975 * Make sure the given activity matches the current configuration. Returns
3976 * false if the activity had to be destroyed. Returns true if the
3977 * configuration is the same, or the activity will remain running as-is
3978 * for whatever reason. Ensures the HistoryRecord is updated with the
3979 * correct configuration and all other bookkeeping is handled.
3980 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003981 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
3982 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003983 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003984 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003985 "Skipping config check (will change): " + r);
3986 return true;
3987 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003988
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003989 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003990 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003991
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003992 // Short circuit: if the two configurations are the exact same
3993 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003994 final Configuration newConfig = mService.mConfiguration;
3995 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003996 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003997 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08003998 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003999 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004000 "Configuration unchanged in " + r);
4001 return true;
4002 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004003
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004004 // We don't worry about activities that are finishing.
4005 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004006 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004007 "Configuration doesn't matter in finishing " + r);
4008 r.stopFreezingScreenLocked(false);
4009 return true;
4010 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004011
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004012 // Okay we now are going to make this activity have the new config.
4013 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004014 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004015 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004016 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004017 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004018
Filip Gruszczynski23493322015-07-29 17:02:59 -07004019 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004020 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004021 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004022 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004023 "Configuration no differences in " + r);
4024 return true;
4025 }
4026
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004027 // If the activity isn't currently running, just leave the new
4028 // configuration and it will pick that up next time it starts.
4029 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004030 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004031 "Configuration doesn't matter not running " + r);
4032 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004033 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004034 return true;
4035 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004036
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004037 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004038 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4039 "Checking to restart " + r.info.name + ": changed=0x"
4040 + Integer.toHexString(changes) + ", handles=0x"
4041 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
4042
Dianne Hackborne6676352011-06-01 16:51:20 -07004043 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004044 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4045 r.configChangeFlags |= changes;
4046 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004047 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004048 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004049 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004050 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004051 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004052 } else if (r.state == ActivityState.PAUSING) {
4053 // A little annoying: we are waiting for this activity to
4054 // finish pausing. Let's not do anything now, but just
4055 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004056 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004057 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004058 r.configDestroy = true;
4059 return true;
4060 } else if (r.state == ActivityState.RESUMED) {
4061 // Try to optimize this case: the configuration is changing
4062 // and we need to restart the top, resumed activity.
4063 // Instead of doing the normal handshaking, just say
4064 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004065 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004066 "Config is relaunching resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004067 relaunchActivityLocked(r, r.configChangeFlags, true,
4068 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004069 r.configChangeFlags = 0;
4070 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004071 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004072 "Config is relaunching non-resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004073 relaunchActivityLocked(r, r.configChangeFlags, false,
4074 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004075 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004076 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004077
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004078 // All done... tell the caller we weren't able to keep this
4079 // activity around.
4080 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004081 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004082
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004083 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004084 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004085 // changes is always sent to all processes when they happen so it can just use whatever
4086 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004087 if (r.app != null && r.app.thread != null) {
4088 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004089 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004090 r.app.thread.scheduleActivityConfigurationChanged(
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004091 r.appToken, new Configuration(taskConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004092 } catch (RemoteException e) {
4093 // If process died, whatever.
4094 }
4095 }
4096 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004097
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004098 return true;
4099 }
4100
Filip Gruszczynski23493322015-07-29 17:02:59 -07004101 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4102 Configuration oldTaskOverride) {
4103 // Determine what has changed. May be nothing, if this is a config
4104 // that has come back from the app after going idle. In that case
4105 // we just want to leave the official config object now in the
4106 // activity and do nothing else.
4107 int taskChanges = oldTaskOverride.diff(taskConfig);
4108 // We don't want to use size changes if they don't cross boundaries that are important to
4109 // the app.
4110 if ((taskChanges & ActivityInfo.CONFIG_SCREEN_SIZE) != 0) {
4111 final boolean crosses = record.crossesHorizontalSizeThreshold(
4112 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4113 || record.crossesVerticalSizeThreshold(
4114 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4115 if (!crosses) {
4116 taskChanges &= ~ActivityInfo.CONFIG_SCREEN_SIZE;
4117 }
4118 }
4119 if ((taskChanges & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4120 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4121 final int newSmallest = taskConfig.smallestScreenWidthDp;
4122 final boolean crosses = record.crossesHorizontalSizeThreshold(oldSmallest, newSmallest)
4123 || record.crossesVerticalSizeThreshold(oldSmallest, newSmallest);
4124 if (!crosses) {
4125 taskChanges &= ~ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4126 }
4127 }
4128 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4129 }
4130
4131 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4132 Configuration oldTaskOverride, int taskChanges) {
4133 if (taskChanges == 0) {
4134 // {@link Configuration#diff} doesn't catch changes from unset values.
4135 // Check for changes we care about.
4136 if (oldTaskOverride.orientation != taskConfig.orientation) {
4137 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
4138 }
4139 // We want to explicitly track situations where the size configuration goes from
4140 // undefined to defined. We don't care about crossing the threshold in that case,
4141 // because there is no threshold.
4142 final int oldHeight = oldTaskOverride.screenHeightDp;
4143 final int newHeight = taskConfig.screenHeightDp;
4144 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4145 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4146 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4147 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4148 }
4149 final int oldWidth = oldTaskOverride.screenWidthDp;
4150 final int newWidth = taskConfig.screenWidthDp;
4151 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4152 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4153 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4154 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4155 }
4156 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4157 final int newSmallest = taskConfig.smallestScreenWidthDp;
4158 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4159 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4160 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4161 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4162 }
4163 }
4164 return taskChanges;
4165 }
4166
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004167 private static boolean isResizeOnlyChange(int change) {
4168 return (change & ~(ActivityInfo.CONFIG_SCREEN_SIZE
4169 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_ORIENTATION)) == 0;
4170 }
4171
4172 private boolean relaunchActivityLocked(ActivityRecord r, int changes, boolean andResume,
4173 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004174 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004175 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004176 if (andResume) {
4177 results = r.results;
4178 newIntents = r.newIntents;
4179 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004180 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4181 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004182 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004183 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004184 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004185 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004186
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004187 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004188
Craig Mautner34b73df2014-01-12 21:11:08 -08004189 mStackSupervisor.removeChildActivityContainers(r);
4190
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004191 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004192 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4193 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004194 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004195 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4196 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004197 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004198 // Note: don't need to call pauseIfSleepingLocked() here, because
4199 // the caller will only pass in 'andResume' if this activity is
4200 // currently resumed, which implies we aren't sleeping.
4201 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004202 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004203 }
4204
4205 if (andResume) {
4206 r.results = null;
4207 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004208 r.state = ActivityState.RESUMED;
4209 } else {
4210 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4211 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004212 }
4213
4214 return true;
4215 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004216
4217 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004218 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4219 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4220 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4221 final ActivityRecord r = activities.get(activityNdx);
4222 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004223 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004224 }
4225 if (r.fullscreen && !r.finishing) {
4226 return false;
4227 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004228 }
4229 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004230 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004231 if (r == null) {
4232 return false;
4233 }
4234 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4235 + " would have returned true for r=" + r);
4236 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004237 }
4238
4239 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004240 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4241 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4242 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4243 final ActivityRecord r = activities.get(activityNdx);
4244 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004245 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004246 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004247 }
4248 }
4249 }
4250
Wale Ogunwale540e1232015-05-01 15:35:39 -07004251 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4252 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004253 boolean didSomething = false;
4254 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004255 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004256 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4257 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4258 int numActivities = activities.size();
4259 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4260 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004261 final boolean sameComponent =
4262 (r.packageName.equals(packageName) && (filterByClasses == null
4263 || filterByClasses.contains(r.realActivity.getClassName())))
4264 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004265 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004266 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004267 && (r.app == null || evenPersistent || !r.app.persistent)) {
4268 if (!doit) {
4269 if (r.finishing) {
4270 // If this activity is just finishing, then it is not
4271 // interesting as far as something to stop.
4272 continue;
4273 }
4274 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004275 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004276 if (r.isHomeActivity()) {
4277 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4278 Slog.i(TAG, "Skip force-stop again " + r);
4279 continue;
4280 } else {
4281 homeActivity = r.realActivity;
4282 }
4283 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004284 didSomething = true;
4285 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004286 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004287 if (r.app != null) {
4288 r.app.removed = true;
4289 }
4290 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004291 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004292 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004293 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4294 true)) {
4295 // r has been deleted from mActivities, accommodate.
4296 --numActivities;
4297 --activityNdx;
4298 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004299 }
4300 }
4301 }
4302 return didSomething;
4303 }
4304
Dianne Hackborn09233282014-04-30 11:33:59 -07004305 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004306 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4307 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004308 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004309 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004310 if (task.getTopActivity() == null) {
4311 continue;
4312 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004313 ActivityRecord r = null;
4314 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004315 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004316 int numActivities = 0;
4317 int numRunning = 0;
4318 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004319 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004320 continue;
4321 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004322 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004323 tmp = activities.get(activityNdx);
4324 if (tmp.finishing) {
4325 continue;
4326 }
4327 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004328
Craig Mautneraab647e2013-02-28 16:31:36 -08004329 // Initialize state for next task if needed.
4330 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4331 top = r;
4332 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004333 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004334
4335 // Add 'r' into the current task.
4336 numActivities++;
4337 if (r.app != null && r.app.thread != null) {
4338 numRunning++;
4339 }
4340
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004341 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004342 TAG, r.intent.getComponent().flattenToShortString()
4343 + ": task=" + r.task);
4344 }
4345
4346 RunningTaskInfo ci = new RunningTaskInfo();
4347 ci.id = task.taskId;
4348 ci.baseActivity = r.intent.getComponent();
4349 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004350 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004351 if (focusedStack && topTask) {
4352 // Give the latest time to ensure foreground task can be sorted
4353 // at the first, because lastActiveTime of creating task is 0.
4354 ci.lastActiveTime = System.currentTimeMillis();
4355 topTask = false;
4356 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004357
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004358 if (top.task != null) {
4359 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004360 }
4361 ci.numActivities = numActivities;
4362 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004363 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004364 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004365 }
4366
4367 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004368 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004369 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004370 if (top >= 0) {
4371 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4372 int activityTop = activities.size() - 1;
4373 if (activityTop > 0) {
4374 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4375 "unhandled-back", true);
4376 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004377 }
4378 }
4379
Craig Mautner6b74cb52013-09-27 17:02:21 -07004380 /**
4381 * Reset local parameters because an app's activity died.
4382 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004383 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004384 */
4385 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004386 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004387 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004388 "App died while pausing: " + mPausingActivity);
4389 mPausingActivity = null;
4390 }
4391 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4392 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004393 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004394 }
4395
Craig Mautner19091252013-10-05 00:03:53 -07004396 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004397 }
4398
Craig Mautnercae015f2013-02-08 14:31:27 -08004399 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004400 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4401 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4402 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4403 final ActivityRecord r = activities.get(activityNdx);
4404 if (r.app == app) {
4405 Slog.w(TAG, " Force finishing activity "
4406 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004407 // Force the destroy to skip right to removal.
4408 r.app = null;
4409 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004410 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004411 }
4412 }
4413 }
4414
Dianne Hackborn390517b2013-05-30 15:03:32 -07004415 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004416 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004417 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004418 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4419 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004420 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4421 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004422 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004423 " Task id #" + task.taskId + "\n" +
4424 " mFullscreen=" + task.mFullscreen + "\n" +
4425 " mBounds=" + task.mBounds + "\n" +
4426 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004427 if (printed) {
4428 header = null;
4429 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004430 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004431 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004432 }
4433
4434 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4435 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4436
4437 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004438 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4439 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004440 }
4441 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004442 final int top = mTaskHistory.size() - 1;
4443 if (top >= 0) {
4444 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4445 int listTop = list.size() - 1;
4446 if (listTop >= 0) {
4447 activities.add(list.get(listTop));
4448 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004449 }
4450 } else {
4451 ItemMatcher matcher = new ItemMatcher();
4452 matcher.build(name);
4453
Craig Mautneraab647e2013-02-28 16:31:36 -08004454 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4455 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4456 if (matcher.match(r1, r1.intent.getComponent())) {
4457 activities.add(r1);
4458 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004459 }
4460 }
4461 }
4462
4463 return activities;
4464 }
4465
4466 ActivityRecord restartPackage(String packageName) {
4467 ActivityRecord starting = topRunningActivityLocked(null);
4468
4469 // All activities that came from the package must be
4470 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004471 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4472 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4473 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4474 final ActivityRecord a = activities.get(activityNdx);
4475 if (a.info.packageName.equals(packageName)) {
4476 a.forceNewConfig = true;
4477 if (starting != null && a == starting && a.visible) {
4478 a.startFreezingScreenLocked(starting.app,
4479 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4480 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004481 }
4482 }
4483 }
4484
4485 return starting;
4486 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004487
Craig Mautner299f9602015-01-26 09:47:33 -08004488 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004489 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004490 }
4491
Wale Ogunwale000957c2015-04-03 08:19:12 -07004492 /**
4493 * Removes the input task from this stack.
4494 * @param task to remove.
4495 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004496 * @param moving task to another stack. In the case we are moving we don't want to perform
4497 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004498 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004499 void removeTask(TaskRecord task, String reason, boolean moving) {
4500 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004501 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004502 mWindowManager.removeTask(task.taskId);
4503 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004504
Craig Mautner04a0ea62014-01-13 12:51:26 -08004505 final ActivityRecord r = mResumedActivity;
4506 if (r != null && r.task == task) {
4507 mResumedActivity = null;
4508 }
4509
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004510 final int taskNdx = mTaskHistory.indexOf(task);
4511 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004512 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4513 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4514 if (!nextTask.isOverHomeStack()) {
4515 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4516 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004517 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004518 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004519 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004520
Wale Ogunwale040b4702015-08-06 18:10:50 -07004521 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004522 final boolean isVoiceSession = task.voiceSession != null;
4523 if (isVoiceSession) {
4524 try {
4525 task.voiceSession.taskFinished(task.intent, task.taskId);
4526 } catch (RemoteException e) {
4527 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004528 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004529 if (task.autoRemoveFromRecents() || isVoiceSession) {
4530 // Task creator asked to remove this when done, or this task was a voice
4531 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004532 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004533 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004534 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004535 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004536
4537 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004538 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004539 final boolean notHomeStack = !isHomeStack();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004540 if (isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004541 String myReason = reason + " leftTaskHistoryEmpty";
4542 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
4543 mStackSupervisor.moveHomeStack(notHomeStack, myReason);
4544 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004545 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004546 if (mStacks != null) {
4547 mStacks.remove(this);
4548 mStacks.add(0, this);
4549 }
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004550 if (notHomeStack) {
4551 mActivityContainer.onTaskListEmptyLocked();
4552 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004553 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004554
4555 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004556 }
4557
Dianne Hackborn91097de2014-04-04 18:02:06 -07004558 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4559 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4560 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004561 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4562 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004563 // add the task to stack first, mTaskPositioner might need the stack association
4564 addTask(task, toTop, false);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004565 if (mTaskPositioner != null) {
Filip Gruszczynski9b1ce522015-08-20 18:37:19 -07004566 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, info.initialLayout);
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004567 } else if (mBounds != null && task.mResizeable) {
4568 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004569 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004570 return task;
4571 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004572
4573 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004574 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004575 }
4576
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004577 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004578 task.stack = this;
4579 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004580 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004581 } else {
4582 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004583 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004584 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004585 if (!moving && task.voiceSession != null) {
4586 try {
4587 task.voiceSession.taskStarted(task.intent, task.taskId);
4588 } catch (RemoteException e) {
4589 }
4590 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004591 }
4592
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004593 void positionTask(final TaskRecord task, int position, boolean moving) {
4594 task.stack = this;
4595 insertTaskAtPosition(task, position);
4596 if (!moving && task.voiceSession != null) {
4597 try {
4598 task.voiceSession.taskStarted(task.intent, task.taskId);
4599 } catch (RemoteException e) {
4600 }
4601 }
4602 }
4603
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004604 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07004605 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004606 task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004607 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4608 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
4609 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
4610 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
Wale Ogunwale61b009e2015-09-16 15:43:05 -07004611 bounds, task.mOverrideConfig, !r.isHomeActivity());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004612 r.taskConfigOverride = task.mOverrideConfig;
4613 }
4614
4615 private void setAppTask(ActivityRecord r, TaskRecord task) {
4616 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004617 task.updateOverrideConfiguration(bounds);
4618 mWindowManager.setAppTask(
4619 r.appToken, task.taskId, task.getLaunchBounds(), task.mOverrideConfig);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004620 r.taskConfigOverride = task.mOverrideConfig;
4621 }
4622
Craig Mautnerc00204b2013-03-05 15:02:14 -08004623 public int getStackId() {
4624 return mStackId;
4625 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004626
4627 @Override
4628 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004629 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4630 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004631 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004632
Craig Mautner15df08a2015-04-01 12:17:18 -07004633 void onLockTaskPackagesUpdatedLocked() {
4634 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4635 mTaskHistory.get(taskNdx).setLockTaskAuth();
4636 }
4637 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004638}