blob: a9ef1d60ebf62c939227e70b49c86cb3ecf0ecc8 [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Wale Ogunwale56d75cf2016-03-09 15:14:47 -080023import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070024import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080025import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
26import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
27import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
28import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080029import static android.content.pm.ActivityInfo.FLAG_RESUME_WHILE_PAUSING;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070030import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070031
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080032import static android.content.res.Configuration.SCREENLAYOUT_UNDEFINED;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080033import static com.android.server.am.ActivityManagerDebugConfig.*;
Chong Zhang75b37202015-12-04 14:16:36 -080034import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
Craig Mautner84984fa2014-06-19 11:19:20 -070035import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
36import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070037
Wale Ogunwalef40c11b2016-02-26 08:16:02 -080038import static com.android.server.am.ActivityRecord.STARTING_WINDOW_REMOVED;
39import static com.android.server.am.ActivityRecord.STARTING_WINDOW_SHOWN;
Wale Ogunwale39381972015-12-17 17:15:29 -080040import static com.android.server.am.ActivityStackSupervisor.FindTaskResult;
Wale Ogunwale040b4702015-08-06 18:10:50 -070041import static com.android.server.am.ActivityStackSupervisor.MOVING;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070042import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwale040b4702015-08-06 18:10:50 -070043
Winsonc809cbb2015-11-02 12:06:15 -080044import android.graphics.Point;
Wale Ogunwale706ed792015-08-02 10:29:44 -070045import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070046import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070047
Dianne Hackborn91097de2014-04-04 18:02:06 -070048import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080049import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070050import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070051import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080052import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080053import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080054import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080055import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070056import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057
58import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070059import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070060import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070061import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080063import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080065import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070068import android.content.pm.ActivityInfo;
Todd Kennedy39bfee52016-02-24 10:28:21 -080069import android.content.pm.ApplicationInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080071import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080072import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070074import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080075import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.os.Handler;
77import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090078import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070080import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070081import android.os.RemoteException;
82import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080083import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070084import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070085import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070086import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070087import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070088import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070089
Craig Mautnercae015f2013-02-08 14:31:27 -080090import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080091import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070092import java.lang.ref.WeakReference;
93import java.util.ArrayList;
94import java.util.Iterator;
95import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080096import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070097import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070098
99/**
100 * State and management of a single stack of activities.
101 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700102final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800103
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800104 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700105 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
106 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800107 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700108 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700109 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700110 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700111 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700112 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700113 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
114 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700115 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700116 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700117 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
118 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
119 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
120 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
121 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700122
123 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800124
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700125 // Ticks during which we check progress while waiting for an app to launch.
126 static final int LAUNCH_TICK = 500;
127
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700128 // How long we wait until giving up on the last activity to pause. This
129 // is short because it directly impacts the responsiveness of starting the
130 // next activity.
131 static final int PAUSE_TIMEOUT = 500;
132
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700133 // How long we wait for the activity to tell us it has stopped before
134 // giving up. This is a good amount of time because we really need this
135 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700136 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700137
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700138 // How long we wait until giving up on an activity telling us it has
139 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700140 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800141
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700142 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800143 // disabled.
144 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800145
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700146 // How long between activity launches that we consider safe to not warn
147 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700148 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800149
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700150 // Set to false to disable the preview that is shown while a new activity
151 // is being started.
152 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800153
Craig Mautner5eda9b32013-07-02 11:58:16 -0700154 // How long to wait for all background Activities to redraw following a call to
155 // convertToTranslucent().
156 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
157
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800158 // How many activities have to be scheduled to stop to force a stop pass.
159 private static final int MAX_STOPPING_TO_FORCE = 3;
160
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161 enum ActivityState {
162 INITIALIZING,
163 RESUMED,
164 PAUSING,
165 PAUSED,
166 STOPPING,
167 STOPPED,
168 FINISHING,
169 DESTROYING,
170 DESTROYED
171 }
172
Filip Gruszczynski0e381e22016-01-14 16:31:33 -0800173 // Stack is not considered visible.
174 static final int STACK_INVISIBLE = 0;
175 // Stack is considered visible
176 static final int STACK_VISIBLE = 1;
177 // Stack is considered visible, but only becuase it has activity that is visible behind other
178 // activities and there is a specific combination of stacks.
179 static final int STACK_VISIBLE_ACTIVITY_BEHIND = 2;
180
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700181 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700182 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800183 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800184
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700185 /**
186 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800187 * running) activities. It contains #TaskRecord objects.
188 */
Todd Kennedy39bfee52016-02-24 10:28:21 -0800189 private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800190
191 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800192 * Used for validating app tokens with window manager.
193 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800194 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800195
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700196 /**
197 * List of running activities, sorted by recent usage.
198 * The first entry in the list is the least recently used.
199 * It contains HistoryRecord objects.
200 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800201 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700202
203 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700204 * Animations that for the current transition have requested not to
205 * be considered for the transition animation.
206 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800207 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700208
209 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700210 * When we are in the process of pausing an activity, before starting the
211 * next one, this variable holds the activity that is currently being paused.
212 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800213 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700214
215 /**
216 * This is the last activity that we put into the paused state. This is
217 * used to determine if we need to do an activity transition while sleeping,
218 * when we normally hold the top activity paused.
219 */
220 ActivityRecord mLastPausedActivity = null;
221
222 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700223 * Activities that specify No History must be removed once the user navigates away from them.
224 * If the device goes to sleep with such an activity in the paused state then we save it here
225 * and finish it later if another activity replaces it on wakeup.
226 */
227 ActivityRecord mLastNoHistoryActivity = null;
228
229 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700230 * Current activity that is resumed, or null if there is none.
231 */
232 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800233
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700234 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700235 * This is the last activity that has been started. It is only used to
236 * identify when multiple activities are started at once so that the user
237 * can be warned they may not be in the activity they think they are.
238 */
239 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800240
Craig Mautner5eda9b32013-07-02 11:58:16 -0700241 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
242 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
243 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
244 // Activity in mTranslucentActivityWaiting is notified via
245 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
246 // background activity being drawn then the same call will be made with a true value.
247 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700248 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700249
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700250 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700251 * Set when we know we are going to be calling updateConfiguration()
252 * soon, so want to skip intermediate config checks.
253 */
254 boolean mConfigWillChange;
255
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700256 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800257 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700258 // Current bounds of the stack or null if fullscreen.
259 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800260
Jorim Jaggi192086e2016-03-11 17:17:03 +0100261 boolean mUpdateBoundsDeferred;
262 boolean mUpdateBoundsDeferredCalled;
263 final Rect mDeferredBounds = new Rect();
264 final Rect mDeferredTaskBounds = new Rect();
265 final Rect mDeferredTaskInsetBounds = new Rect();
266
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700267 long mLaunchStartTime = 0;
268 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800269
Craig Mautner858d8a62013-04-23 17:08:34 -0700270 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700271
Craig Mautnerc00204b2013-03-05 15:02:14 -0800272 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800273 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800274 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
275 ArrayList<ActivityStack> mStacks;
276 /** The attached Display's unique identifier, or -1 if detached */
277 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800278
Craig Mautner27084302013-03-25 08:05:25 -0700279 /** Run all ActivityStacks through this */
280 final ActivityStackSupervisor mStackSupervisor;
281
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700282 private final LaunchingTaskPositioner mTaskPositioner;
283
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700284 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700285 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
286 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
287 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
288 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700289 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700290 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700291 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700292
293 static class ScheduleDestroyArgs {
294 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700295 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700296 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700297 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700298 mReason = reason;
299 }
300 }
301
Zoran Marcetaf958b322012-08-09 20:27:12 +0900302 final Handler mHandler;
303
304 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800305
Craig Mautnerc8143c62013-09-03 12:15:57 -0700306 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900307 super(looper);
308 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700309
Zoran Marcetaf958b322012-08-09 20:27:12 +0900310 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700311 public void handleMessage(Message msg) {
312 switch (msg.what) {
313 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800314 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700315 // We don't at this point know if the activity is fullscreen,
316 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800317 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700318 synchronized (mService) {
319 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700320 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700321 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700322 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800323 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700324 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700325 case LAUNCH_TICK_MSG: {
326 ActivityRecord r = (ActivityRecord)msg.obj;
327 synchronized (mService) {
328 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700329 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700330 }
331 }
332 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700333 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800334 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700335 // We don't at this point know if the activity is fullscreen,
336 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800337 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800338 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800339 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800340 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700341 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700342 case STOP_TIMEOUT_MSG: {
343 ActivityRecord r = (ActivityRecord)msg.obj;
344 // We don't at this point know if the activity is fullscreen,
345 // so we need to be conservative and assume it isn't.
346 Slog.w(TAG, "Activity stop timeout for " + r);
347 synchronized (mService) {
348 if (r.isInHistory()) {
349 activityStoppedLocked(r, null, null, null);
350 }
351 }
352 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700353 case DESTROY_ACTIVITIES_MSG: {
354 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
355 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700356 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700357 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700358 } break;
359 case TRANSLUCENT_TIMEOUT_MSG: {
360 synchronized (mService) {
361 notifyActivityDrawnLocked(null);
362 }
363 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700364 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700365 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700366 final ActivityRecord r = getVisibleBehindActivity();
367 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700368 if (r != null) {
369 mService.killAppAtUsersRequest(r.app, null);
370 }
371 }
372 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700373 }
374 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800375 }
376
Craig Mautner34b73df2014-01-12 21:11:08 -0800377 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800378 int count = 0;
379 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
380 count += mTaskHistory.get(taskNdx).mActivities.size();
381 }
382 return count;
383 }
384
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800385 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
386 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800387 mActivityContainer = activityContainer;
388 mStackSupervisor = activityContainer.getOuter();
389 mService = mStackSupervisor.mService;
390 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
391 mWindowManager = mService.mWindowManager;
392 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700393 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800394 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700395 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
396 ? new LaunchingTaskPositioner() : null;
397 }
398
399 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
400 mDisplayId = activityDisplay.mDisplayId;
401 mStacks = activityDisplay.mStacks;
402 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
403 mFullscreen = mBounds == null;
404 if (mTaskPositioner != null) {
405 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
406 mTaskPositioner.configure(mBounds);
407 }
Wale Ogunwale961f4852016-02-01 20:25:54 -0800408
409 if (mStackId == DOCKED_STACK_ID) {
410 // If we created a docked stack we want to resize it so it resizes all other stacks
411 // in the system.
412 mStackSupervisor.resizeDockedStackLocked(
413 mBounds, null, null, null, null, PRESERVE_WINDOWS);
414 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700415 }
416
417 void detachDisplay() {
418 mDisplayId = Display.INVALID_DISPLAY;
419 mStacks = null;
420 if (mTaskPositioner != null) {
421 mTaskPositioner.reset();
422 }
423 mWindowManager.detachStack(mStackId);
Jorim Jaggi899327f2016-02-25 20:44:18 -0500424 if (mStackId == DOCKED_STACK_ID) {
425 // If we removed a docked stack we want to resize it so it resizes all other stacks
426 // in the system to fullscreen.
427 mStackSupervisor.resizeDockedStackLocked(
428 null, null, null, null, null, PRESERVE_WINDOWS);
429 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700430 }
431
Winsonc809cbb2015-11-02 12:06:15 -0800432 public void getDisplaySize(Point out) {
433 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
434 }
435
Jorim Jaggi192086e2016-03-11 17:17:03 +0100436 /**
437 * Defers updating the bounds of the stack. If the stack was resized/repositioned while
438 * deferring, the bounds will update in {@link #continueUpdateBounds()}.
439 */
440 void deferUpdateBounds() {
441 if (!mUpdateBoundsDeferred) {
442 mUpdateBoundsDeferred = true;
443 mUpdateBoundsDeferredCalled = false;
444 }
445 }
446
447 /**
448 * Continues updating bounds after updates have been deferred. If there was a resize attempt
449 * between {@link #deferUpdateBounds()} and {@link #continueUpdateBounds()}, the stack will
450 * be resized to that bounds.
451 */
452 void continueUpdateBounds() {
453 final boolean wasDeferred = mUpdateBoundsDeferred;
454 mUpdateBoundsDeferred = false;
455 if (wasDeferred && mUpdateBoundsDeferredCalled) {
456 mStackSupervisor.resizeStackUncheckedLocked(this,
457 mDeferredBounds.isEmpty() ? null : mDeferredBounds,
458 mDeferredTaskBounds.isEmpty() ? null : mDeferredTaskBounds,
459 mDeferredTaskInsetBounds.isEmpty() ? null : mDeferredTaskInsetBounds);
460 }
461 }
462
463 boolean updateBoundsAllowed(Rect bounds, Rect tempTaskBounds,
464 Rect tempTaskInsetBounds) {
465 if (!mUpdateBoundsDeferred) {
466 return true;
467 }
468 if (bounds != null) {
469 mDeferredBounds.set(bounds);
470 } else {
471 mDeferredBounds.setEmpty();
472 }
473 if (tempTaskBounds != null) {
474 mDeferredTaskBounds.set(tempTaskBounds);
475 } else {
476 mDeferredTaskBounds.setEmpty();
477 }
478 if (tempTaskInsetBounds != null) {
479 mDeferredTaskInsetBounds.set(tempTaskInsetBounds);
480 } else {
481 mDeferredTaskInsetBounds.setEmpty();
482 }
483 mUpdateBoundsDeferredCalled = true;
484 return false;
485 }
486
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700487 void setBounds(Rect bounds) {
488 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700489 if (mTaskPositioner != null) {
490 mTaskPositioner.configure(bounds);
491 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700492 }
Craig Mautner5962b122012-10-05 14:45:52 -0700493
Amith Yamasani734983f2014-03-04 16:48:05 -0800494 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700495 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700496 }
497
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700498 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800499 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700500 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700501 if (r != null) {
502 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800503 }
504 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700505 return null;
506 }
507
508 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800509 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
510 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800511 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800512 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
513 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800514 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800515 return r;
516 }
517 }
518 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700519 return null;
520 }
521
522 /**
523 * This is a simplified version of topRunningActivityLocked that provides a number of
524 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800525 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700526 * @param token If non-null, any history records matching this token will be skipped.
527 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800528 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700529 * @return Returns the HistoryRecord of the next activity on the stack.
530 */
531 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800532 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
533 TaskRecord task = mTaskHistory.get(taskNdx);
534 if (task.taskId == taskId) {
535 continue;
536 }
537 ArrayList<ActivityRecord> activities = task.mActivities;
538 for (int i = activities.size() - 1; i >= 0; --i) {
539 final ActivityRecord r = activities.get(i);
540 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800541 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800542 return r;
543 }
544 }
545 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700546 return null;
547 }
548
Craig Mautner8849a5e2013-04-02 16:41:03 -0700549 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700550 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
551 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700552 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
553 final ActivityRecord r = activities.get(activityNdx);
554 if (!r.finishing) {
555 return r;
556 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700557 }
558 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700559 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700560 }
561
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700562 final TaskRecord topTask() {
563 final int size = mTaskHistory.size();
564 if (size > 0) {
565 return mTaskHistory.get(size - 1);
566 }
567 return null;
568 }
569
Craig Mautnerd2328952013-03-05 12:46:26 -0800570 TaskRecord taskForIdLocked(int id) {
571 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
572 final TaskRecord task = mTaskHistory.get(taskNdx);
573 if (task.taskId == id) {
574 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800575 }
576 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700577 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700578 }
579
Craig Mautnerd2328952013-03-05 12:46:26 -0800580 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700581 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800582 return isInStackLocked(r);
583 }
584
585 ActivityRecord isInStackLocked(ActivityRecord r) {
586 if (r == null) {
587 return null;
588 }
589 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700590 if (task != null && task.stack != null
591 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800592 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800593 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800594 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800595 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800596 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800597 }
598
Craig Mautner2420ead2013-04-01 17:13:20 -0700599 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700600 final boolean hadit = mLRUActivities.remove(r);
601 mLRUActivities.add(r);
602 return hadit;
603 }
604
Craig Mautnerde4ef022013-04-07 19:01:33 -0700605 final boolean isHomeStack() {
606 return mStackId == HOME_STACK_ID;
607 }
608
Winson Chung0583d3d2015-12-18 10:03:12 -0500609 final boolean isDockedStack() {
610 return mStackId == DOCKED_STACK_ID;
611 }
612
613 final boolean isPinnedStack() {
614 return mStackId == PINNED_STACK_ID;
615 }
616
Craig Mautnere0a38842013-12-16 16:14:02 -0800617 final boolean isOnHomeDisplay() {
618 return isAttached() &&
619 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
620 }
621
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700622 void moveToFront(String reason) {
623 moveToFront(reason, null);
624 }
625
626 /**
627 * @param reason The reason for moving the stack to the front.
628 * @param task If non-null, the task will be moved to the top of the stack.
629 * */
630 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700631 if (!isAttached()) {
632 return;
633 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700634
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700635 mStacks.remove(this);
636 int addIndex = mStacks.size();
637
638 if (addIndex > 0) {
639 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800640 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
641 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700642 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800643 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700644 }
645
646 mStacks.add(addIndex, this);
647
648 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
649 if (isOnHomeDisplay()) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800650 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700651 }
652 if (task != null) {
653 insertTaskAtTop(task, null);
654 } else {
655 task = topTask();
656 }
657 if (task != null) {
658 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800659 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800660 }
661
Wale Ogunwaled046a012015-12-24 13:05:59 -0800662 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800663 if (StackId.canReceiveKeys(mStackId)) {
664 return true;
665 }
666 // The stack isn't focusable. See if its top activity is focusable to force focus on the
667 // stack.
668 final ActivityRecord r = topRunningActivityLocked();
669 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800670 }
671
Craig Mautnere0a38842013-12-16 16:14:02 -0800672 final boolean isAttached() {
673 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800674 }
675
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700676 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800677 * Returns the top activity in any existing task matching the given Intent in the input result.
678 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700679 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800680 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700681 Intent intent = target.intent;
682 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700683 ComponentName cls = intent.getComponent();
684 if (info.targetActivity != null) {
685 cls = new ComponentName(info.packageName, info.targetActivity);
686 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700687 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700688 boolean isDocument = intent != null & intent.isDocument();
689 // If documentData is non-null then it must match the existing task data.
690 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800691
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700692 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800693 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
694 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700695 if (task.voiceSession != null) {
696 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700697 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700698 continue;
699 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700700 if (task.userId != userId) {
701 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700702 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700703 continue;
704 }
Craig Mautner000f0022013-02-26 15:04:29 -0800705 final ActivityRecord r = task.getTopActivity();
706 if (r == null || r.finishing || r.userId != userId ||
707 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700708 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800709 continue;
710 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700711 if (r.mActivityType != target.mActivityType) {
712 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
713 continue;
714 }
Craig Mautner000f0022013-02-26 15:04:29 -0800715
Craig Mautnerd00f4742014-03-12 14:17:26 -0700716 final Intent taskIntent = task.intent;
717 final Intent affinityIntent = task.affinityIntent;
718 final boolean taskIsDocument;
719 final Uri taskDocumentData;
720 if (taskIntent != null && taskIntent.isDocument()) {
721 taskIsDocument = true;
722 taskDocumentData = taskIntent.getData();
723 } else if (affinityIntent != null && affinityIntent.isDocument()) {
724 taskIsDocument = true;
725 taskDocumentData = affinityIntent.getData();
726 } else {
727 taskIsDocument = false;
728 taskDocumentData = null;
729 }
730
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700731 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700732 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700733 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700734 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Wale Ogunwale39381972015-12-17 17:15:29 -0800735 if (!isDocument && !taskIsDocument
736 && result.r == null && task.canMatchRootAffinity()) {
Dianne Hackborn79228822014-09-16 11:11:23 -0700737 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwale39381972015-12-17 17:15:29 -0800738 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
739 // It is possible for multiple tasks to have the same root affinity especially
740 // if they are in separate stacks. We save off this candidate, but keep looking
741 // to see if there is a better candidate.
742 result.r = r;
743 result.matchedByRootAffinity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700744 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700745 } else if (taskIntent != null && taskIntent.getComponent() != null &&
746 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700747 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700748 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800749 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700750 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
751 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800752 result.r = r;
753 result.matchedByRootAffinity = false;
754 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700755 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
756 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700757 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700758 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800759 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700760 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
761 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800762 result.r = r;
763 result.matchedByRootAffinity = false;
764 break;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700765 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700766 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700767 }
768
769 /**
770 * Returns the first activity (starting from the top of the stack) that
771 * is the same as the given activity. Returns null if no such activity
772 * is found.
773 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700774 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700775 ComponentName cls = intent.getComponent();
776 if (info.targetActivity != null) {
777 cls = new ComponentName(info.packageName, info.targetActivity);
778 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700779 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700780
Craig Mautner000f0022013-02-26 15:04:29 -0800781 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700782 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700783 final boolean notCurrentUserTask =
784 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700785 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700786
Craig Mautner000f0022013-02-26 15:04:29 -0800787 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
788 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700789 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700790 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700791 }
Craig Mautner000f0022013-02-26 15:04:29 -0800792 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700793 return r;
794 }
795 }
796 }
797
798 return null;
799 }
800
Amith Yamasani742a6712011-05-04 14:49:28 -0700801 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700802 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700803 */
Craig Mautner93529a42013-10-04 15:03:13 -0700804 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800805 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700806 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800807 }
808 mCurrentUser = userId;
809
810 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800811 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800812 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700813 final TaskRecord task = mTaskHistory.get(i);
814
815 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
816 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700817 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700818 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700819 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700820 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800821 mTaskHistory.remove(i);
822 mTaskHistory.add(task);
823 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800824 // Use same value for i.
825 } else {
826 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800827 }
828 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700829 if (VALIDATE_TOKENS) {
830 validateAppTokensLocked();
831 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700832 }
833
Craig Mautner2420ead2013-04-01 17:13:20 -0700834 void minimalResumeActivityLocked(ActivityRecord r) {
835 r.state = ActivityState.RESUMED;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800836 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
837 + " callers=" + Debug.getCallers(5));
Craig Mautner2420ead2013-04-01 17:13:20 -0700838 mResumedActivity = r;
839 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800840 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700841 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700842 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700843 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700844 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
845 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700846 }
847
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800848 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700849 if (r != null) {
850 mRecentTasks.addLocked(r.task);
851 r.task.touchActiveTime();
852 }
853 }
854
Narayan Kamath7829c812015-06-08 17:39:43 +0100855 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700856 if (mFullyDrawnStartTime != 0) {
857 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
858 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100859 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700860 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
861 }
862
863 private void stopFullyDrawnTraceIfNeeded() {
864 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
865 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
866 mFullyDrawnStartTime = 0;
867 }
868 }
869
Craig Mautnere79d42682013-04-01 19:01:53 -0700870 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700871 if (r.displayStartTime == 0) {
872 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
873 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100874 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700875 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700876 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700877 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100878 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700879 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700880 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700881 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800882
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700883 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700884 // Make sure that there is no activity waiting for this to launch.
885 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
886 r.displayStartTime = r.fullyDrawnStartTime = 0;
887 } else {
888 mStackSupervisor.removeTimeoutsForActivityLocked(r);
889 mStackSupervisor.scheduleIdleTimeoutLocked(r);
890 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700891 }
892
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800893 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800894 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800895 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
896 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
897 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
898 activities.get(activityNdx).setSleeping(false);
899 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800900 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800901 if (mPausingActivity != null) {
902 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
903 activityPausedLocked(mPausingActivity.appToken, true);
904 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800905 }
906
Todd Kennedy39bfee52016-02-24 10:28:21 -0800907 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
908 final String packageName = aInfo.packageName;
909 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
910 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
911 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
912 if (packageName.equals(activities.get(activityNdx).packageName)) {
913 activities.get(activityNdx).info.applicationInfo = aInfo;
914 }
915 }
916 }
917 }
918
Craig Mautner0eea92c2013-05-16 13:35:39 -0700919 /**
920 * @return true if something must be done before going to sleep.
921 */
922 boolean checkReadyForSleepLocked() {
923 if (mResumedActivity != null) {
924 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700925 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
926 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
927 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700928 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700929 return true;
930 }
931 if (mPausingActivity != null) {
932 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700933 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700934 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800935 }
936
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700937 if (hasVisibleBehindActivity()) {
938 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800939 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700940 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800941 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700942 return true;
943 }
944
Craig Mautner0eea92c2013-05-16 13:35:39 -0700945 return false;
946 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800947
Craig Mautner0eea92c2013-05-16 13:35:39 -0700948 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700949 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800950
Craig Mautner0eea92c2013-05-16 13:35:39 -0700951 // Make sure any stopped but visible activities are now sleeping.
952 // This ensures that the activity's onStop() is called.
953 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
954 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
955 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
956 final ActivityRecord r = activities.get(activityNdx);
957 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
958 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800959 }
960 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800961 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700962 }
Craig Mautner59c00972012-07-30 12:10:24 -0700963
Winson8b1871d2015-11-20 09:56:20 -0800964 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
965 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800966 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700967 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800968 return null;
969 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800970
Winson Chung083baf92014-07-11 10:32:42 -0700971 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700972 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800973 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700974 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700975 return null;
976 }
977
Winson Chung48a10a52014-08-27 14:36:51 -0700978 int w = mService.mThumbnailWidth;
979 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800980 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800981 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700982 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800983
984 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800985 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
986 // use within SystemUI while keeping memory usage low.
Winson Chungead5c0f2015-12-14 11:18:57 -0500987 if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
Winson8b1871d2015-11-20 09:56:20 -0800988 w = h = -1;
Winson21700932016-03-24 17:26:23 -0700989 scale = mService.mFullscreenThumbnailScale;
Winson8b1871d2015-11-20 09:56:20 -0800990 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800991 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800992 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800993 }
Winson Chung376543b2014-05-21 17:43:28 -0700994 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800995 return null;
996 }
997
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700998 /**
999 * Start pausing the currently resumed activity. It is an error to call this if there
1000 * is already an activity being paused or there is no resumed activity.
1001 *
1002 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
1003 * @param uiSleeping True if this is happening with the user interface going to sleep (the
1004 * screen turning off).
1005 * @param resuming True if this is being called as part of resuming the top activity, so
1006 * we shouldn't try to instigate a resume here.
1007 * @param dontWait True if the caller does not want to wait for the pause to complete. If
1008 * set to true, we will immediately complete the pause here before returning.
1009 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
1010 * it to tell us when it is done.
1011 */
1012 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
1013 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001014 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +08001015 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
1016 + " state=" + mPausingActivity.state);
1017 if (!mService.isSleeping()) {
1018 // Avoid recursion among check for sleep and complete pause during sleeping.
1019 // Because activity will be paused immediately after resume, just let pause
1020 // be completed by the order of activity paused from clients.
1021 completePauseLocked(false);
1022 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001023 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001024 ActivityRecord prev = mResumedActivity;
1025 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001026 if (!resuming) {
1027 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001028 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001029 }
1030 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001031 }
Craig Mautnerdf88d732014-01-27 09:21:32 -08001032
1033 if (mActivityContainer.mParentActivity == null) {
1034 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001035 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -08001036 }
1037
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001038 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001039 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001040 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001041 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001042 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -07001043 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
1044 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001045 prev.state = ActivityState.PAUSING;
1046 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001047 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001048 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -07001049 if (mService.mHasRecents
1050 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001051 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -07001052 }
Dianne Hackborncee04b52013-07-03 17:01:28 -07001053 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001054
1055 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -08001056
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001057 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001058 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001059 try {
1060 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001061 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001062 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -07001063 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001064 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001065 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001066 } catch (Exception e) {
1067 // Ignore exception, if process died other code will cleanup.
1068 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001069 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001070 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001071 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001072 }
1073 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001074 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001075 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001076 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001077 }
1078
1079 // If we are not going to sleep, we want to ensure the device is
1080 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001081 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001082 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001083 }
1084
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001085 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001086 // Have the window manager pause its key dispatching until the new
1087 // activity has started. If we're pausing the activity just because
1088 // the screen is being turned off and the UI is sleeping, don't interrupt
1089 // key dispatch; the same activity will pick it up again on wakeup.
1090 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001091 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001092 } else if (DEBUG_PAUSE) {
1093 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001094 }
1095
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001096 if (dontWait) {
1097 // If the caller said they don't want to wait for the pause, then complete
1098 // the pause now.
1099 completePauseLocked(false);
1100 return false;
1101
1102 } else {
1103 // Schedule a pause timeout in case the app doesn't respond.
1104 // We don't give it much time because this directly impacts the
1105 // responsiveness seen by the user.
1106 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1107 msg.obj = prev;
1108 prev.pauseTime = SystemClock.uptimeMillis();
1109 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001110 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001111 return true;
1112 }
1113
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001114 } else {
1115 // This activity failed to schedule the
1116 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001117 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001118 if (!resuming) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001119 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001120 }
1121 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001122 }
1123 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001124
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001125 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001126 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1127 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001128
Craig Mautnerd2328952013-03-05 12:46:26 -08001129 final ActivityRecord r = isInStackLocked(token);
1130 if (r != null) {
1131 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1132 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001133 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001134 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001135 completePauseLocked(true);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001136 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001137 } else {
1138 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1139 r.userId, System.identityHashCode(r), r.shortComponentName,
1140 mPausingActivity != null
1141 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001142 if (r.state == ActivityState.PAUSING) {
1143 r.state = ActivityState.PAUSED;
1144 if (r.finishing) {
1145 if (DEBUG_PAUSE) Slog.v(TAG,
1146 "Executing finish of failed to pause activity: " + r);
1147 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1148 }
louis_chang047dfd42015-04-08 16:35:55 +08001149 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001150 }
1151 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001152 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001153 }
1154
Chong Zhangd78ddb42016-03-02 17:01:14 -08001155 final void activityResumedLocked(IBinder token) {
1156 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1157 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1158 r.icicle = null;
1159 r.haveState = false;
1160 }
1161
Craig Mautnera0026042014-04-23 11:45:37 -07001162 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1163 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001164 if (r.state != ActivityState.STOPPING) {
1165 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1166 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1167 return;
1168 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001169 if (persistentState != null) {
1170 r.persistentState = persistentState;
1171 mService.notifyTaskPersisterLocked(r.task, false);
1172 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001173 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001174 if (icicle != null) {
1175 // If icicle is null, this is happening due to a timeout, so we
1176 // haven't really saved the state.
1177 r.icicle = icicle;
1178 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001179 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001180 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001181 }
1182 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001183 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001184 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1185 r.stopped = true;
1186 r.state = ActivityState.STOPPED;
Robert Carre12aece2016-02-02 22:43:27 -08001187
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08001188 mWindowManager.notifyAppStopped(r.appToken, true);
Robert Carre12aece2016-02-02 22:43:27 -08001189
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001190 if (getVisibleBehindActivity() == r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001191 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001192 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001193 if (r.finishing) {
1194 r.clearOptionsLocked();
1195 } else {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001196 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001197 destroyActivityLocked(r, true, "stop-config");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001198 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001199 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001200 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001201 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001202 }
1203 }
1204 }
1205
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001206 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001207 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001208 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001209
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001210 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001211 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001212 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001213 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001214 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001215 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001216 } else if (prev.app != null) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001217 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1218 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Craig Mautner8c14c152015-01-15 17:32:07 -08001219 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001220 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1221 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001222 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001223 if (prev.deferRelaunchUntilPaused) {
1224 // Complete the deferred relaunch that was waiting for pause to complete.
1225 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
1226 relaunchActivityLocked(prev, prev.configChangeFlags, false,
1227 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001228 } else if (wasStopping) {
1229 // We are also stopping, the stop request must have gone soon after the pause.
1230 // We can't clobber it, because the stop confirmation will not be handled.
1231 // We don't need to schedule another stop, we only need to let it happen.
1232 prev.state = ActivityState.STOPPING;
Wale Ogunwaled8026642016-02-09 20:40:18 -08001233 } else if ((!prev.visible && !hasVisibleBehindActivity())
1234 || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001235 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001236 // stopping.
Chong Zhang46b1ac62016-02-18 17:53:57 -08001237 addToStopping(prev, true /* immediate */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001238 }
1239 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001240 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001241 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001242 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001243 // It is possible the activity was freezing the screen before it was paused.
1244 // In that case go ahead and remove the freeze this activity has on the screen
1245 // since it is no longer visible.
1246 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001247 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001248 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001249
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001250 if (resumeNext) {
1251 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1252 if (!mService.isSleepingOrShuttingDown()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001253 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001254 } else {
1255 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001256 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001257 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001258 // If there are no more activities available to run, do resume anyway to start
1259 // something. Also if the top activity on the stack is not the just paused
1260 // activity, we need to go ahead and resume it to ensure we complete an
1261 // in-flight app switch.
1262 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001263 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001264 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001265 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001266
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001267 if (prev != null) {
1268 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001269
Craig Mautner525f3d92013-05-07 14:01:50 -07001270 if (prev.app != null && prev.cpuTimeAtResume > 0
1271 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001272 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1273 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001274 if (diff > 0) {
1275 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1276 synchronized (bsi) {
1277 BatteryStatsImpl.Uid.Proc ps =
1278 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001279 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001280 if (ps != null) {
1281 ps.addForegroundTimeLocked(diff);
1282 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001283 }
1284 }
1285 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001286 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001287 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001288
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001289 // Notify when the task stack has changed, but only if visibilities changed (not just focus)
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001290 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause) {
1291 mService.notifyTaskStackChangedLocked();
1292 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1293 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001294
1295 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001296 }
1297
Chong Zhang46b1ac62016-02-18 17:53:57 -08001298 private void addToStopping(ActivityRecord r, boolean immediate) {
1299 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1300 mStackSupervisor.mStoppingActivities.add(r);
1301 }
1302
1303 // If we already have a few activities waiting to stop, then give up
1304 // on things going idle and start clearing them out. Or if r is the
1305 // last of activity of the last task the stack will be empty and must
1306 // be cleared immediately.
1307 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1308 || (r.frontOfTask && mTaskHistory.size() <= 1);
1309
1310 if (immediate || forceIdle) {
1311 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
1312 + forceIdle + "immediate=" + immediate);
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001313 mStackSupervisor.scheduleIdleLocked();
1314 } else {
1315 mStackSupervisor.checkReadyForSleepLocked();
1316 }
1317 }
1318
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001319 /**
1320 * Once we know that we have asked an application to put an activity in
1321 * the resumed state (either by launching it or explicitly telling it),
1322 * this function updates the rest of our state to match that fact.
1323 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001324 private void completeResumeLocked(ActivityRecord next) {
Chong Zhang2b79af12016-02-10 18:47:06 -08001325 next.visible = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001326 next.idle = false;
1327 next.results = null;
1328 next.newIntents = null;
Chong Zhang2b79af12016-02-10 18:47:06 -08001329 next.stopped = false;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001330
Chong Zhang85ee6542015-10-02 13:36:38 -07001331 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001332 ProcessRecord app = next.task.mActivities.get(0).app;
1333 if (app != null && app != mService.mHomeProcess) {
1334 mService.mHomeProcess = app;
1335 }
1336 }
1337
Craig Mautner07566322013-09-26 16:42:55 -07001338 if (next.nowVisible) {
1339 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001340 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001341 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001342
1343 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001344 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001345
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001346 mStackSupervisor.reportResumedActivityLocked(next);
1347
1348 next.resumeKeyDispatchingLocked();
1349 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001350
1351 // Mark the point when the activity is resuming
1352 // TODO: To be more accurate, the mark should be before the onCreate,
1353 // not after the onResume. But for subsequent starts, onResume is fine.
1354 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001355 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001356 } else {
1357 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1358 }
Winson Chung376543b2014-05-21 17:43:28 -07001359
George Mount6ba042b2014-07-28 11:12:28 -07001360 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001361
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001362 if (getVisibleBehindActivity() == next) {
Craig Mautner64ccb702014-10-01 09:38:40 -07001363 // When resuming an activity, require it to call requestVisibleBehind() again.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001364 setVisibleBehindActivity(null);
Craig Mautner64ccb702014-10-01 09:38:40 -07001365 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001366 }
1367
Craig Mautner2568c3a2015-03-26 14:22:34 -07001368 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001369 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001370 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1371 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1372 r.mUpdateTaskThumbnailWhenHidden = false;
1373 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001374 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001375 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001376 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001377 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001378 container.setVisible(visible);
1379 }
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001380 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001381 }
1382
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001383 // Find the first visible activity above the passed activity and if it is translucent return it
1384 // otherwise return null;
1385 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1386 TaskRecord task = r.task;
1387 if (task == null) {
1388 return null;
1389 }
1390
1391 ActivityStack stack = task.stack;
1392 if (stack == null) {
1393 return null;
1394 }
1395
1396 int stackNdx = mStacks.indexOf(stack);
1397
1398 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1399 int taskNdx = tasks.indexOf(task);
1400
1401 ArrayList<ActivityRecord> activities = task.mActivities;
1402 int activityNdx = activities.indexOf(r) + 1;
1403
1404 final int numStacks = mStacks.size();
1405 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001406 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001407 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001408 final int numTasks = tasks.size();
1409 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001410 final TaskRecord currentTask = tasks.get(taskNdx);
1411 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001412 final int numActivities = activities.size();
1413 while (activityNdx < numActivities) {
1414 final ActivityRecord activity = activities.get(activityNdx);
1415 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001416 return historyStack.mFullscreen
1417 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001418 }
1419 ++activityNdx;
1420 }
1421 activityNdx = 0;
1422 ++taskNdx;
1423 }
1424 taskNdx = 0;
1425 ++stackNdx;
1426 }
1427
1428 return null;
1429 }
1430
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001431 ActivityStack getNextFocusableStackLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001432 ArrayList<ActivityStack> stacks = mStacks;
1433 final ActivityRecord parent = mActivityContainer.mParentActivity;
1434 if (parent != null) {
1435 stacks = parent.task.stack.mStacks;
1436 }
1437 if (stacks != null) {
1438 for (int i = stacks.size() - 1; i >= 0; --i) {
1439 ActivityStack stack = stacks.get(i);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001440 if (stack != this && stack.isFocusable()
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001441 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001442 return stack;
1443 }
1444 }
1445 }
1446 return null;
1447 }
1448
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001449 /** Returns true if the stack contains a fullscreen task. */
1450 private boolean hasFullscreenTask() {
1451 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1452 final TaskRecord task = mTaskHistory.get(i);
1453 if (task.mFullscreen) {
1454 return true;
1455 }
1456 }
1457 return false;
1458 }
1459
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001460 /**
1461 * Returns true if the stack is translucent and can have other contents visible behind it if
1462 * needed. A stack is considered translucent if it don't contain a visible or
1463 * starting (about to be visible) activity that is fullscreen (opaque).
1464 * @param starting The currently starting activity or null if there is none.
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001465 * @param stackBehindId The id of the stack directly behind this one.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001466 */
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001467 private boolean isStackTranslucent(ActivityRecord starting, int stackBehindId) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001468 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1469 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001470 final ArrayList<ActivityRecord> activities = task.mActivities;
1471 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1472 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001473
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001474 if (r.finishing) {
1475 // We don't factor in finishing activities when determining translucency since
1476 // they will be gone soon.
1477 continue;
1478 }
1479
1480 if (!r.visible && r != starting) {
1481 // Also ignore invisible activities that are not the currently starting
1482 // activity (about to be visible).
1483 continue;
1484 }
1485
1486 if (r.fullscreen) {
1487 // Stack isn't translucent if it has at least one fullscreen activity
1488 // that is visible.
1489 return false;
1490 }
1491
1492 if (!isHomeStack() && r.frontOfTask
1493 && task.isOverHomeStack() && stackBehindId != HOME_STACK_ID) {
1494 // Stack isn't translucent if it's top activity should have the home stack
1495 // behind it and the stack currently behind it isn't the home stack.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001496 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001497 }
1498 }
1499 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001500 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001501 }
1502
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001503 /**
1504 * Returns stack's visibility: {@link #STACK_INVISIBLE}, {@link #STACK_VISIBLE} or
1505 * {@link #STACK_VISIBLE_ACTIVITY_BEHIND}.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001506 * @param starting The currently starting activity or null if there is none.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001507 */
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001508 int getStackVisibilityLocked(ActivityRecord starting) {
Jose Lima7ba71252014-04-30 12:59:27 -07001509 if (!isAttached()) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001510 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001511 }
1512
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001513 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001514 return STACK_VISIBLE;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001515 }
1516
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001517 final int stackIndex = mStacks.indexOf(this);
1518
1519 if (stackIndex == mStacks.size() - 1) {
1520 Slog.wtf(TAG,
1521 "Stack=" + this + " isn't front stack but is at the top of the stack list");
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001522 return STACK_INVISIBLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001523 }
1524
Chong Zhang75b37202015-12-04 14:16:36 -08001525 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1526 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001527 return STACK_INVISIBLE;
Chong Zhang75b37202015-12-04 14:16:36 -08001528 }
1529
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001530 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1531 final int focusedStackId = focusedStack.mStackId;
1532
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001533 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001534 && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID
1535 && !focusedStack.topActivity().fullscreen) {
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001536 // The fullscreen stack should be visible if it has a visible behind activity behind
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001537 // the home stack that is translucent.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001538 return STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001539 }
1540
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001541 if (mStackId == DOCKED_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001542 // Docked stack is always visible, except in the case where the top running activity
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001543 // task in the focus stack doesn't support any form of resizing but we show it for the
1544 // home task even though it's not resizable.
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001545 final ActivityRecord r = focusedStack.topRunningActivityLocked();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001546 final TaskRecord task = r != null ? r.task : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001547 return task == null || task.canGoInDockedStack() || task.isHomeTask() ? STACK_VISIBLE
1548 : STACK_INVISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001549 }
1550
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001551 // Find the first stack behind focused stack that actually got something visible.
1552 int stackBehindFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1553 while (stackBehindFocusedIndex >= 0 &&
1554 mStacks.get(stackBehindFocusedIndex).topRunningActivityLocked() == null) {
1555 stackBehindFocusedIndex--;
Chong Zhangb16cf342015-11-12 17:22:40 -08001556 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001557 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001558 && stackIndex == stackBehindFocusedIndex) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001559 // Stacks directly behind the docked or pinned stack are always visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001560 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001561 }
1562
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001563 final int stackBehindFocusedId = (stackBehindFocusedIndex >= 0)
1564 ? mStacks.get(stackBehindFocusedIndex).mStackId : INVALID_STACK_ID;
1565
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001566 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001567 && focusedStack.isStackTranslucent(starting, stackBehindFocusedId)) {
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001568 // Stacks behind the fullscreen stack with a translucent activity are always
1569 // visible so they can act as a backdrop to the translucent activity.
1570 // For example, dialog activities
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001571 if (stackIndex == stackBehindFocusedIndex) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001572 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001573 }
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001574 if (stackBehindFocusedIndex >= 0) {
1575 if ((stackBehindFocusedId == DOCKED_STACK_ID
1576 || stackBehindFocusedId == PINNED_STACK_ID)
1577 && stackIndex == (stackBehindFocusedIndex - 1)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001578 // The stack behind the docked or pinned stack is also visible so we can have a
1579 // complete backdrop to the translucent activity when the docked stack is up.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001580 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001581 }
1582 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001583 }
1584
Wale Ogunwale3797c222015-10-27 14:21:58 -07001585 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001586 // Visibility of any static stack should have been determined by the conditions above.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001587 return STACK_INVISIBLE;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001588 }
1589
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001590 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001591 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001592
1593 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1594 continue;
1595 }
1596
Wale Ogunwale3797c222015-10-27 14:21:58 -07001597 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001598 // These stacks can't have any dynamic stacks visible behind them.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001599 return STACK_INVISIBLE;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001600 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001601
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001602 if (!stack.isStackTranslucent(starting, INVALID_STACK_ID)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001603 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001604 }
1605 }
1606
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001607 return STACK_VISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001608 }
1609
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001610 final int rankTaskLayers(int baseLayer) {
1611 int layer = 0;
1612 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1613 final TaskRecord task = mTaskHistory.get(taskNdx);
1614 ActivityRecord r = task.topRunningActivityLocked();
1615 if (r == null || r.finishing || !r.visible) {
1616 task.mLayerRank = -1;
1617 } else {
1618 task.mLayerRank = baseLayer + layer++;
1619 }
1620 }
1621 return layer;
1622 }
1623
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001624 /**
1625 * Make sure that all activities that need to be visible (that is, they
1626 * currently can be seen by the user) actually are.
1627 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001628 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1629 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001630 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001631 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001632 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001633 if (top != null) {
1634 checkTranslucentActivityWaiting(top);
1635 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001636
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001637 // If the top activity is not fullscreen, then we need to
1638 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001639 boolean aboveTop = top != null;
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001640 final int stackVisibility = getStackVisibilityLocked(starting);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001641 final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
1642 final boolean stackVisibleBehind = stackVisibility == STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001643 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaled046a012015-12-24 13:05:59 -08001644 boolean resumeNextActivity = isFocusable() && (isInStackLocked(starting) == null);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001645 boolean behindTranslucentActivity = false;
1646 final ActivityRecord visibleBehind = getVisibleBehindActivity();
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001647 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001648 final TaskRecord task = mTaskHistory.get(taskNdx);
1649 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001650 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1651 final ActivityRecord r = activities.get(activityNdx);
1652 if (r.finishing) {
1653 continue;
1654 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001655 final boolean isTop = r == top;
1656 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001657 continue;
1658 }
1659 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001660 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1661 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001662 final boolean activityVisibleBehind =
1663 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == r;
1664 final boolean isVisible = (!behindFullscreenActivity || r.mLaunchTaskBehind
1665 || activityVisibleBehind) && okToShowLocked(r);
1666 if (isVisible) {
1667 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1668 + " finishing=" + r.finishing + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001669 // First: if this is not the current activity being started, make
1670 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001671 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001672 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001673 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001674
1675 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001676 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001677 resumeNextActivity, r)) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001678 if (activityNdx >= activities.size()) {
1679 // Record may be removed if its process needs to restart.
1680 activityNdx = activities.size() - 1;
1681 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001682 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001683 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001684 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001685 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001686 // If this activity is already visible, then there is nothing to do here.
1687 if (handleAlreadyVisible(r)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001688 resumeNextActivity = false;
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001689 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001690 } else {
Jorim Jaggie66edb12016-02-05 12:41:17 -08001691 makeVisibleIfNeeded(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001692 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001693 // Aggregate current change flags.
1694 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001695 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1696 behindFullscreenActivity, task, r);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001697 if (behindFullscreenActivity && !r.fullscreen) {
1698 behindTranslucentActivity = true;
1699 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001700 } else {
Wale Ogunwale834c2362016-01-23 18:14:58 -08001701 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001702 + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1703 + stackInvisible + " behindFullscreenActivity="
1704 + behindFullscreenActivity + " mLaunchTaskBehind="
1705 + r.mLaunchTaskBehind);
1706 makeInvisible(r, visibleBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001707 }
1708 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001709 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1710 // The visibility of tasks and the activities they contain in freeform stack are
1711 // determined individually unlike other stacks where the visibility or fullscreen
1712 // status of an activity in a previous task affects other.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001713 behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001714 } else if (mStackId == HOME_STACK_ID) {
1715 if (task.isHomeTask()) {
1716 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
1717 + " stackInvisible=" + stackInvisible
1718 + " behindFullscreenActivity=" + behindFullscreenActivity);
1719 // No other task in the home stack should be visible behind the home activity.
1720 // Home activities is usually a translucent activity with the wallpaper behind
1721 // them. However, when they don't have the wallpaper behind them, we want to
1722 // show activities in the next application stack behind them vs. another
1723 // task in the home stack like recents.
1724 behindFullscreenActivity = true;
1725 } else if (task.isRecentsTask()
1726 && task.getTaskToReturnTo() == APPLICATION_ACTIVITY_TYPE) {
1727 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1728 "Recents task returning to app: at " + task
1729 + " stackInvisible=" + stackInvisible
1730 + " behindFullscreenActivity=" + behindFullscreenActivity);
1731 // We don't want any other tasks in the home stack visible if the recents
1732 // activity is going to be returning to an application activity type.
1733 // We do this to preserve the visible order the user used to get into the
1734 // recents activity. The recents activity is normally translucent and if it
1735 // doesn't have the wallpaper behind it the next activity in the home stack
1736 // shouldn't be visible when the home stack is brought to the front to display
1737 // the recents activity from an app.
1738 behindFullscreenActivity = true;
1739 }
1740
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001741 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001742 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001743
1744 if (mTranslucentActivityWaiting != null &&
1745 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1746 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1747 notifyActivityDrawnLocked(null);
1748 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001749 }
Craig Mautner58547802013-03-05 08:23:53 -08001750
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001751 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1752 if (mTranslucentActivityWaiting != top) {
1753 mUndrawnActivitiesBelowTopTranslucent.clear();
1754 if (mTranslucentActivityWaiting != null) {
1755 // Call the callback with a timeout indication.
1756 notifyActivityDrawnLocked(null);
1757 mTranslucentActivityWaiting = null;
1758 }
1759 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1760 }
1761 }
1762
1763 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001764 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001765 // We need to make sure the app is running if it's the top, or it is just made visible from
1766 // invisible. If the app is already visible, it must have died while it was visible. In this
1767 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1768 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001769 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001770 // This activity needs to be visible, but isn't even running...
1771 // get it started and resume if no other stack in this stack is resumed.
1772 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1773 if (r != starting) {
1774 r.startFreezingScreenLocked(r.app, configChanges);
1775 }
1776 if (!r.visible || r.mLaunchTaskBehind) {
1777 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1778 setVisible(r, true);
1779 }
1780 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001781 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001782 return true;
1783 }
1784 }
1785 return false;
1786 }
1787
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001788 private void makeInvisible(ActivityRecord r, ActivityRecord visibleBehind) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001789 if (!r.visible) {
1790 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1791 return;
1792 }
1793 // Now for any activities that aren't visible to the user, make sure they no longer are
1794 // keeping the screen frozen.
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001795 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001796 try {
1797 setVisible(r, false);
1798 switch (r.state) {
1799 case STOPPING:
1800 case STOPPED:
1801 if (r.app != null && r.app.thread != null) {
1802 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1803 "Scheduling invisibility: " + r);
1804 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1805 }
1806 break;
1807
1808 case INITIALIZING:
1809 case RESUMED:
1810 case PAUSING:
1811 case PAUSED:
1812 // This case created for transitioning activities from
1813 // translucent to opaque {@link Activity#convertToOpaque}.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001814 if (visibleBehind == r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001815 releaseBackgroundResources(r);
1816 } else {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001817 addToStopping(r, true /* immediate */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001818 }
1819 break;
1820
1821 default:
1822 break;
1823 }
1824 } catch (Exception e) {
1825 // Just skip on any failure; we'll make it visible when it next restarts.
1826 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1827 }
1828 }
1829
1830 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1831 TaskRecord task, ActivityRecord r) {
1832 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001833 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08001834 + " stackInvisible=" + stackInvisible
1835 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001836 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001837 behindFullscreenActivity = true;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001838 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001839 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1840 + " stackInvisible=" + stackInvisible
1841 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001842 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001843 }
1844 return behindFullscreenActivity;
1845 }
1846
Jorim Jaggie66edb12016-02-05 12:41:17 -08001847 private void makeVisibleIfNeeded(ActivityRecord starting, ActivityRecord r) {
1848
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001849 // This activity is not currently visible, but is running. Tell it to become visible.
Jorim Jaggie66edb12016-02-05 12:41:17 -08001850 if (r.state == ActivityState.RESUMED || r == starting) {
Chong Zhange05db742016-02-16 16:58:37 -08001851 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1852 "Not making visible, r=" + r + " state=" + r.state + " starting=" + starting);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001853 return;
1854 }
1855
1856 // If this activity is paused, tell it to now show its window.
1857 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1858 "Making visible and scheduling visibility: " + r);
1859 try {
1860 if (mTranslucentActivityWaiting != null) {
1861 r.updateOptionsLocked(r.returningOptions);
1862 mUndrawnActivitiesBelowTopTranslucent.add(r);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001863 }
Jorim Jaggie66edb12016-02-05 12:41:17 -08001864 setVisible(r, true);
1865 r.sleeping = false;
1866 r.app.pendingUiClean = true;
1867 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1868 r.stopFreezingScreenLocked(false);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001869
1870 // The activity may be waiting for stop, but that is no longer
1871 // appropriate for it.
1872 mStackSupervisor.mStoppingActivities.remove(r);
1873 mStackSupervisor.mGoingToSleepActivities.remove(r);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001874 } catch (Exception e) {
1875 // Just skip on any failure; we'll make it
1876 // visible when it next restarts.
1877 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001878 }
1879 }
1880
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001881 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001882 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1883 r.stopFreezingScreenLocked(false);
1884 try {
1885 if (r.returningOptions != null) {
1886 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1887 }
1888 } catch(RemoteException e) {
1889 }
1890 return r.state == ActivityState.RESUMED;
1891 }
1892
Todd Kennedyaab56db2015-01-30 09:39:53 -08001893 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001894 mTranslucentActivityWaiting = r;
1895 mUndrawnActivitiesBelowTopTranslucent.clear();
1896 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1897 }
1898
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001899 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1900 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1901 final TaskRecord task = mTaskHistory.get(taskNdx);
1902 final ArrayList<ActivityRecord> activities = task.mActivities;
1903 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1904 final ActivityRecord r = activities.get(activityNdx);
1905 if ( r.appTimeTracker != except) {
1906 r.appTimeTracker = null;
1907 }
1908 }
1909 }
1910 }
1911
Craig Mautner5eda9b32013-07-02 11:58:16 -07001912 /**
1913 * Called as activities below the top translucent activity are redrawn. When the last one is
1914 * redrawn notify the top activity by calling
1915 * {@link Activity#onTranslucentConversionComplete}.
1916 *
1917 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1918 * occurred and the activity will be notified immediately.
1919 */
1920 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001921 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001922 if ((r == null)
1923 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1924 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1925 // The last undrawn activity below the top has just been drawn. If there is an
1926 // opaque activity at the top, notify it that it can become translucent safely now.
1927 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1928 mTranslucentActivityWaiting = null;
1929 mUndrawnActivitiesBelowTopTranslucent.clear();
1930 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1931
Craig Mautner71dd1b62014-02-18 15:48:52 -08001932 if (waitingActivity != null) {
1933 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1934 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1935 try {
1936 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1937 waitingActivity.appToken, r != null);
1938 } catch (RemoteException e) {
1939 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001940 }
1941 }
1942 }
1943 }
1944
Craig Mautnera61bc652013-10-28 15:43:18 -07001945 /** If any activities below the top running one are in the INITIALIZING state and they have a
1946 * starting window displayed then remove that starting window. It is possible that the activity
1947 * in this state will never resumed in which case that starting window will be orphaned. */
1948 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001949 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001950 boolean aboveTop = true;
1951 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1952 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1953 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1954 final ActivityRecord r = activities.get(activityNdx);
1955 if (aboveTop) {
1956 if (r == topActivity) {
1957 aboveTop = false;
1958 }
1959 continue;
1960 }
1961
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08001962 if (r.state == ActivityState.INITIALIZING
1963 && r.mStartingWindowState == STARTING_WINDOW_SHOWN) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001964 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1965 "Found orphaned starting window " + r);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08001966 r.mStartingWindowState = STARTING_WINDOW_REMOVED;
Craig Mautnera61bc652013-10-28 15:43:18 -07001967 mWindowManager.removeAppStartingWindow(r.appToken);
1968 }
1969 }
1970 }
1971 }
1972
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001973 /**
1974 * Ensure that the top activity in the stack is resumed.
1975 *
1976 * @param prev The previously resumed activity, for when in the process
1977 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001978 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001979 *
1980 * @return Returns true if something is being resumed, or false if
1981 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001982 *
1983 * NOTE: It is not safe to call this method directly as it can cause an activity in a
1984 * non-focused stack to be resumed.
1985 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
1986 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001987 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08001988 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001989 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001990 // Don't even start recursing.
1991 return false;
1992 }
1993
1994 boolean result = false;
1995 try {
1996 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001997 mStackSupervisor.inResumeTopActivity = true;
1998 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1999 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08002000 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08002001 }
Craig Mautner544efa72014-09-04 16:41:20 -07002002 result = resumeTopActivityInnerLocked(prev, options);
2003 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08002004 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07002005 }
2006 return result;
2007 }
2008
Chong Zhang280d3322015-11-03 17:27:26 -08002009 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002010 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07002011
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002012 if (!mService.mBooting && !mService.mBooted) {
2013 // Not ready yet!
2014 return false;
2015 }
2016
Craig Mautnerdf88d732014-01-27 09:21:32 -08002017 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002018 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07002019 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08002020 // Do not resume this stack if its parent is not resumed.
2021 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
2022 return false;
2023 }
2024
Wale Ogunwale2be760d2016-02-17 11:16:10 -08002025 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07002026
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002027 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002028 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002029
2030 // Remember how we'll process this pause/resume situation, and ensure
2031 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002032 final boolean userLeaving = mStackSupervisor.mUserLeaving;
2033 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002034
Craig Mautner84984fa2014-06-19 11:19:20 -07002035 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002036 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002037 // There are no more activities!
2038 final String reason = "noMoreActivities";
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07002039 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack()
2040 ? HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2041 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(returnTaskType, reason)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002042 // Try to move focus to the next visible stack with a running activity if this
2043 // stack is not covering the entire screen.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002044 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
2045 mStackSupervisor.getFocusedStack(), prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002046 }
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002047
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002048 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07002049 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002050 if (DEBUG_STATES) Slog.d(TAG_STATES,
2051 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07002052 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08002053 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07002054 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002055 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002056 }
2057
2058 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08002059
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002060 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002061 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2062 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002063 // Make sure we have executed any pending transitions, since there
2064 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002065 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002066 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002067 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002068 if (DEBUG_STATES) Slog.d(TAG_STATES,
2069 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07002070 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002071 return false;
2072 }
2073
Craig Mautner525f3d92013-05-07 14:01:50 -07002074 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07002075 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07002076 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002077 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07002078 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002079 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07002080 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07002081 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07002082 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07002083 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07002084 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08002085 } else if (!isOnHomeDisplay()) {
2086 return false;
2087 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002088 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08002089 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07002090 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
2091 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2092 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08002093 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07002094 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002095 }
2096
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002097 // If we are sleeping, and there is no resumed activity, and the top
2098 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07002099 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09002100 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002101 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002102 // Make sure we have executed any pending transitions, since there
2103 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002104 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002105 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002106 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002107 if (DEBUG_STATES) Slog.d(TAG_STATES,
2108 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002109 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002110 return false;
2111 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002112
2113 // Make sure that the user who owns this activity is started. If not,
2114 // we will just leave it as is because someone should be bringing
2115 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002116 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002117 Slog.w(TAG, "Skipping resume of top activity " + next
2118 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002119 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002120 return false;
2121 }
2122
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002123 // The activity may be waiting for stop, but that is no longer
2124 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002125 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002126 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002127 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002128 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002129
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002130 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002131
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002132 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002133 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002134 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002135 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002136 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002137 return false;
2138 }
2139
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002140 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2141
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002142 // We need to start pausing the current activity so the top one can be resumed...
2143 final boolean dontWaitForPause = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002144 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002145 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002146 if (DEBUG_STATES) Slog.d(TAG_STATES,
2147 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08002148 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002149 }
2150 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002151 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002152 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002153 // At this point we want to put the upcoming activity's process
2154 // at the top of the LRU list, since we know we will be needing it
2155 // very soon and it would be a waste to let it get killed if it
2156 // happens to be sitting towards the end.
2157 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002158 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002159 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002160 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002161 return true;
2162 }
2163
Christopher Tated3f175c2012-06-14 14:16:54 -07002164 // If the most recent activity was noHistory but was only stopped rather
2165 // than stopped+finished because the device went to sleep, we need to make
2166 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07002167 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002168 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002169 if (DEBUG_STATES) Slog.d(TAG_STATES,
2170 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002171 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002172 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002173 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002174 }
2175
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002176 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002177 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2178 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002179 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002180 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2181 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002182 } else {
2183 // The next activity is already visible, so hide the previous
2184 // activity's windows right now so we can show the new one ASAP.
2185 // We only do this if the previous is finishing, which should mean
2186 // it is on top of the one being resumed so hiding it quickly
2187 // is good. Otherwise, we want to do the normal route of allowing
2188 // the resumed activity to be shown so we can decide if the
2189 // previous should actually be hidden depending on whether the
2190 // new one is found to be full-screen or not.
2191 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002192 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002193 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2194 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002195 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002196 + ", nowVisible=" + next.nowVisible);
2197 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002198 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002199 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002200 + ", waitingVisible="
2201 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2202 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002203 }
2204 }
2205 }
2206
Dianne Hackborne7f97212011-02-24 14:40:20 -08002207 // Launching this app's activity, make sure the app is no longer
2208 // considered stopped.
2209 try {
2210 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002211 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002212 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002213 } catch (IllegalArgumentException e) {
2214 Slog.w(TAG, "Failed trying to unstop package "
2215 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002216 }
2217
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002218 // We are starting up the next activity, so tell the window manager
2219 // that the previous one will be hidden soon. This way it can know
2220 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002221 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002222 if (prev != null) {
2223 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002224 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002225 "Prepare close transition: prev=" + prev);
2226 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002227 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002228 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002229 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002230 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002231 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2232 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002233 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002234 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002235 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002236 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2237 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002238 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002239 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002240 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002241 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002242 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002243 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002244 : next.mLaunchTaskBehind
2245 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2246 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002247 }
2248 }
Craig Mautner967212c2013-04-13 21:10:58 -07002249 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002250 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002251 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002252 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002253 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002254 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002255 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002256 }
2257 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002258
2259 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002260 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002261 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2262 if (opts != null) {
2263 resumeAnimOptions = opts.toBundle();
2264 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002265 next.applyOptionsLocked();
2266 } else {
2267 next.clearOptionsLocked();
2268 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002269
Craig Mautnercf910b02013-04-23 11:23:27 -07002270 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002271 if (next.app != null && next.app.thread != null) {
Chong Zhangdea4bd92016-03-15 12:50:03 -07002272 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next
2273 + " stopped=" + next.stopped + " visible=" + next.visible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002274
2275 // This activity is now becoming visible.
Chong Zhangdea4bd92016-03-15 12:50:03 -07002276 if (!next.visible || next.stopped) {
Jorim Jaggi1b025a62016-02-03 19:27:49 -08002277 mWindowManager.setAppVisibility(next.appToken, true);
2278 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002279
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002280 // schedule launch ticks to collect information about slow apps.
2281 next.startLaunchTickingLocked();
2282
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002283 ActivityRecord lastResumedActivity =
2284 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002285 ActivityState lastState = next.state;
2286
2287 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002288
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002289 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002290 next.state = ActivityState.RESUMED;
2291 mResumedActivity = next;
2292 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002293 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002294 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002295 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002296 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002297
2298 // Have the window manager re-evaluate the orientation of
2299 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002300 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002301 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002302 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002303 mService.mConfiguration,
2304 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2305 if (config != null) {
2306 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002307 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002308 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002309 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002310
Craig Mautner525f3d92013-05-07 14:01:50 -07002311 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002312 // The configuration update wasn't able to keep the existing
2313 // instance of the activity, and instead started a new one.
2314 // We should be all done, but let's just make sure our activity
2315 // is still at the top and schedule another run if something
2316 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002317 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002318 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002319 "Activity config changed during resume: " + next
2320 + ", new next: " + nextNext);
2321 if (nextNext != next) {
2322 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002323 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002324 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002325 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002326 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002327 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002328 return true;
2329 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002330 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002331 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002332 }
Craig Mautner58547802013-03-05 08:23:53 -08002333
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002334 try {
2335 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002336 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002337 if (a != null) {
2338 final int N = a.size();
2339 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002340 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2341 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002342 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002343 }
2344 }
2345
2346 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002347 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 }
2349
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002350 // Well the app will no longer be stopped.
2351 // Clear app token stopped state in window manager if needed.
2352 mWindowManager.notifyAppStopped(next.appToken, false);
2353
Craig Mautner299f9602015-01-26 09:47:33 -08002354 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2355 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002356
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002357 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002358 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002359 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002360 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002361 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002362 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002363 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002364
Craig Mautner0eea92c2013-05-16 13:35:39 -07002365 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002366
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002367 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002368 } catch (Exception e) {
2369 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002370 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002371 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002372 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002373 if (lastStack != null) {
2374 lastStack.mResumedActivity = lastResumedActivity;
2375 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002376 Slog.i(TAG, "Restarting because process died: " + next);
2377 if (!next.hasBeenLaunched) {
2378 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002379 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2380 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002381 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002382 next.appToken, next.packageName, next.theme,
2383 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002384 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2385 next.windowFlags, null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002386 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002387 }
George Mount2c92c972014-03-20 09:38:23 -07002388 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002389 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002390 return true;
2391 }
2392
2393 // From this point on, if something goes wrong there is no way
2394 // to recover the activity.
2395 try {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002396 completeResumeLocked(next);
2397 } catch (Exception e) {
2398 // If any exception gets thrown, toss away this
2399 // activity and try the next one.
2400 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002401 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002402 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002403 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002404 return true;
2405 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002406 } else {
2407 // Whoops, need to restart this activity!
2408 if (!next.hasBeenLaunched) {
2409 next.hasBeenLaunched = true;
2410 } else {
2411 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002412 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002413 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002414 mService.compatibilityInfoForPackageLocked(
2415 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002416 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002417 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002418 null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002419 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002420 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002421 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002422 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002423 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002424 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002425 }
2426
Craig Mautnercf910b02013-04-23 11:23:27 -07002427 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002428 return true;
2429 }
2430
riddle_hsuc215a4f2014-12-27 12:10:45 +08002431 private TaskRecord getNextTask(TaskRecord targetTask) {
2432 final int index = mTaskHistory.indexOf(targetTask);
2433 if (index >= 0) {
2434 final int numTasks = mTaskHistory.size();
2435 for (int i = index + 1; i < numTasks; ++i) {
2436 TaskRecord task = mTaskHistory.get(i);
2437 if (task.userId == targetTask.userId) {
2438 return task;
2439 }
2440 }
2441 }
2442 return null;
2443 }
2444
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002445 private void insertTaskAtPosition(TaskRecord task, int position) {
2446 if (position >= mTaskHistory.size()) {
2447 insertTaskAtTop(task, null);
2448 return;
2449 }
2450 // Calculate maximum possible position for this task.
2451 int maxPosition = mTaskHistory.size();
2452 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002453 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002454 // Put non-current user tasks below current user tasks.
2455 while (maxPosition > 0) {
2456 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2457 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002458 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002459 break;
2460 }
2461 maxPosition--;
2462 }
2463 }
2464 position = Math.min(position, maxPosition);
2465 mTaskHistory.remove(task);
2466 mTaskHistory.add(position, task);
2467 updateTaskMovement(task, true);
2468 }
2469
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002470 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002471 // If the moving task is over home stack, transfer its return type to next task
2472 if (task.isOverHomeStack()) {
2473 final TaskRecord nextTask = getNextTask(task);
2474 if (nextTask != null) {
2475 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2476 }
2477 }
2478
Craig Mautner9c85c202013-10-04 20:11:26 -07002479 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002480 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002481 if (isOnHomeDisplay()) {
2482 ActivityStack lastStack = mStackSupervisor.getLastStack();
2483 final boolean fromHome = lastStack.isHomeStack();
2484 if (!isHomeStack() && (fromHome || topTask() != task)) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002485 int returnToType = APPLICATION_ACTIVITY_TYPE;
2486 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2487 returnToType = lastStack.topTask() == null
2488 ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2489 }
2490 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002491 }
2492 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002493 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002494 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002495
Craig Mautnerac6f8432013-07-17 13:24:59 -07002496 mTaskHistory.remove(task);
2497 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002498 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002499 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002500 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002501 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002502 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002503 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002504 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002505 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002506 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002507 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002508 break;
2509 }
2510 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002511 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002512 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002513 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002514 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002515 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002516
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002517 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2518 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002519 TaskRecord rTask = r.task;
2520 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002521 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2522 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002523 // Last activity in task had been removed or ActivityManagerService is reusing task.
2524 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002525 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002526 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002527 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002528 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002529 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002530 if (!newTask) {
2531 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002532 boolean startIt = true;
2533 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2534 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002535 if (task.getTopActivity() == null) {
2536 // All activities in task are finishing.
2537 continue;
2538 }
Craig Mautner70a86932013-02-28 22:37:44 -08002539 if (task == r.task) {
2540 // Here it is! Now, if this is not yet visible to the
2541 // user, then just add it without starting; it will
2542 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002543 if (!startIt) {
2544 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2545 + task, new RuntimeException("here").fillInStackTrace());
2546 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002547 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002548 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002549 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002550 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002551 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002552 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002553 return;
2554 }
2555 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002556 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002557 startIt = false;
2558 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002559 }
2560 }
2561
2562 // Place a new activity at top of stack, so it is next to interact
2563 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002564
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002565 // If we are not placing the new activity frontmost, we do not want
2566 // to deliver the onUserLeaving callback to the actual frontmost
2567 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002568 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002569 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002570 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002571 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002572 }
Craig Mautner70a86932013-02-28 22:37:44 -08002573
2574 task = r.task;
2575
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002576 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002577 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002578 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002579 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002580 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002581
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002582 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002583 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002584 // We want to show the starting preview window if we are
2585 // switching to a new task, or the next activity's process is
2586 // not currently running.
2587 boolean showStartingIcon = newTask;
2588 ProcessRecord proc = r.app;
2589 if (proc == null) {
2590 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2591 }
2592 if (proc == null || proc.thread == null) {
2593 showStartingIcon = true;
2594 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002595 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002596 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002597 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002598 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002599 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002600 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002601 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002602 ? r.mLaunchTaskBehind
2603 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2604 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002605 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002606 mNoAnimActivities.remove(r);
2607 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002608 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002609 boolean doShow = true;
2610 if (newTask) {
2611 // Even though this activity is starting fresh, we still need
2612 // to reset it to make sure we apply affinities to move any
2613 // existing activities from other tasks in to it.
2614 // If the caller has requested that the target task be
2615 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002616 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002617 resetTaskIfNeededLocked(r, r);
2618 doShow = topRunningNonDelayedActivityLocked(null) == r;
2619 }
Chong Zhang280d3322015-11-03 17:27:26 -08002620 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002621 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2622 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002623 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002624 if (r.mLaunchTaskBehind) {
2625 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2626 // tell WindowManager that r is visible even though it is at the back of the stack.
2627 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002628 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002629 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002630 // Figure out if we are transitioning from another activity that is
2631 // "has the same starting icon" as the next one. This allows the
2632 // window manager to keep the previous window it had previously
2633 // created, if it still had one.
2634 ActivityRecord prev = mResumedActivity;
2635 if (prev != null) {
2636 // We don't want to reuse the previous starting preview if:
2637 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002638 if (prev.task != r.task) {
2639 prev = null;
2640 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002641 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002642 else if (prev.nowVisible) {
2643 prev = null;
2644 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002645 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002646 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002647 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002648 mService.compatibilityInfoForPackageLocked(
2649 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002650 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002651 prev != null ? prev.appToken : null, showStartingIcon);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002652 r.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002653 }
2654 } else {
2655 // If this is the first activity, don't do any fancy animations,
2656 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002657 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002658 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002659 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002660 }
2661 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002662 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002663 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002664 }
2665
Dianne Hackbornbe707852011-11-11 14:32:10 -08002666 final void validateAppTokensLocked() {
2667 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002668 mValidateAppTokens.ensureCapacity(numActivities());
2669 final int numTasks = mTaskHistory.size();
2670 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2671 TaskRecord task = mTaskHistory.get(taskNdx);
2672 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002673 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002674 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002675 }
Craig Mautner000f0022013-02-26 15:04:29 -08002676 TaskGroup group = new TaskGroup();
2677 group.taskId = task.taskId;
2678 mValidateAppTokens.add(group);
2679 final int numActivities = activities.size();
2680 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2681 final ActivityRecord r = activities.get(activityNdx);
2682 group.tokens.add(r.appToken);
2683 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002684 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002685 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002686 }
2687
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002688 /**
2689 * Perform a reset of the given task, if needed as part of launching it.
2690 * Returns the new HistoryRecord at the top of the task.
2691 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002692 /**
2693 * Helper method for #resetTaskIfNeededLocked.
2694 * We are inside of the task being reset... we'll either finish this activity, push it out
2695 * for another task, or leave it as-is.
2696 * @param task The task containing the Activity (taskTop) that might be reset.
2697 * @param forceReset
2698 * @return An ActivityOptions that needs to be processed.
2699 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002700 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002701 ActivityOptions topOptions = null;
2702
2703 int replyChainEnd = -1;
2704 boolean canMoveOptions = true;
2705
2706 // We only do this for activities that are not the root of the task (since if we finish
2707 // the root, we may no longer have the task!).
2708 final ArrayList<ActivityRecord> activities = task.mActivities;
2709 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002710 final int rootActivityNdx = task.findEffectiveRootIndex();
2711 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002712 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002713 if (target.frontOfTask)
2714 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002715
2716 final int flags = target.info.flags;
2717 final boolean finishOnTaskLaunch =
2718 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2719 final boolean allowTaskReparenting =
2720 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2721 final boolean clearWhenTaskReset =
2722 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2723
2724 if (!finishOnTaskLaunch
2725 && !clearWhenTaskReset
2726 && target.resultTo != null) {
2727 // If this activity is sending a reply to a previous
2728 // activity, we can't do anything with it now until
2729 // we reach the start of the reply chain.
2730 // XXX note that we are assuming the result is always
2731 // to the previous activity, which is almost always
2732 // the case but we really shouldn't count on.
2733 if (replyChainEnd < 0) {
2734 replyChainEnd = i;
2735 }
2736 } else if (!finishOnTaskLaunch
2737 && !clearWhenTaskReset
2738 && allowTaskReparenting
2739 && target.taskAffinity != null
2740 && !target.taskAffinity.equals(task.affinity)) {
2741 // If this activity has an affinity for another
2742 // task, then we need to move it out of here. We will
2743 // move it as far out of the way as possible, to the
2744 // bottom of the activity stack. This also keeps it
2745 // correctly ordered with any activities we previously
2746 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002747 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002748 final ActivityRecord bottom =
2749 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002750 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002751 if (bottom != null && target.taskAffinity != null
2752 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002753 // If the activity currently at the bottom has the
2754 // same task affinity as the one we are moving,
2755 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002756 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002757 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002758 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002759 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002760 targetTask = createTaskRecord(
2761 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
2762 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002763 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002764 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002765 + " out to new task " + target.task);
2766 }
2767
Wale Ogunwale706ed792015-08-02 10:29:44 -07002768 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002769
Craig Mautner525f3d92013-05-07 14:01:50 -07002770 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002771 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2772 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002773 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002774 if (p.finishing) {
2775 continue;
2776 }
2777
Craig Mautnere3a74d52013-02-22 14:14:58 -08002778 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002779 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002780 topOptions = p.takeOptionsLocked();
2781 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002782 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002783 }
2784 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002785 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2786 "Removing activity " + p + " from task=" + task + " adding to task="
2787 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002788 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2789 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002790 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002791 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002792
Wale Ogunwale706ed792015-08-02 10:29:44 -07002793 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002794 }
2795
Wale Ogunwale706ed792015-08-02 10:29:44 -07002796 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002797 if (VALIDATE_TOKENS) {
2798 validateAppTokensLocked();
2799 }
2800
2801 replyChainEnd = -1;
2802 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2803 // If the activity should just be removed -- either
2804 // because it asks for it, or the task should be
2805 // cleared -- then finish it and anything that is
2806 // part of its reply chain.
2807 int end;
2808 if (clearWhenTaskReset) {
2809 // In this case, we want to finish this activity
2810 // and everything above it, so be sneaky and pretend
2811 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002812 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002813 } else if (replyChainEnd < 0) {
2814 end = i;
2815 } else {
2816 end = replyChainEnd;
2817 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002818 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002819 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002820 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002821 if (p.finishing) {
2822 continue;
2823 }
2824 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002825 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002826 topOptions = p.takeOptionsLocked();
2827 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002828 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002829 }
2830 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002831 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002832 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002833 if (finishActivityLocked(
2834 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002835 end--;
2836 srcPos--;
2837 }
2838 }
2839 replyChainEnd = -1;
2840 } else {
2841 // If we were in the middle of a chain, well the
2842 // activity that started it all doesn't want anything
2843 // special, so leave it all as-is.
2844 replyChainEnd = -1;
2845 }
2846 }
2847
2848 return topOptions;
2849 }
2850
2851 /**
2852 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2853 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2854 * @param affinityTask The task we are looking for an affinity to.
2855 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2856 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2857 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2858 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002859 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002860 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002861 int replyChainEnd = -1;
2862 final int taskId = task.taskId;
2863 final String taskAffinity = task.affinity;
2864
2865 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2866 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002867 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2868
2869 // Do not operate on or below the effective root Activity.
2870 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002871 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002872 if (target.frontOfTask)
2873 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002874
2875 final int flags = target.info.flags;
2876 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2877 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2878
2879 if (target.resultTo != null) {
2880 // If this activity is sending a reply to a previous
2881 // activity, we can't do anything with it now until
2882 // we reach the start of the reply chain.
2883 // XXX note that we are assuming the result is always
2884 // to the previous activity, which is almost always
2885 // the case but we really shouldn't count on.
2886 if (replyChainEnd < 0) {
2887 replyChainEnd = i;
2888 }
2889 } else if (topTaskIsHigher
2890 && allowTaskReparenting
2891 && taskAffinity != null
2892 && taskAffinity.equals(target.taskAffinity)) {
2893 // This activity has an affinity for our task. Either remove it if we are
2894 // clearing or move it over to our task. Note that
2895 // we currently punt on the case where we are resetting a
2896 // task that is not at the top but who has activities above
2897 // with an affinity to it... this is really not a normal
2898 // case, and we will need to later pull that task to the front
2899 // and usually at that point we will do the reset and pick
2900 // up those remaining activities. (This only happens if
2901 // someone starts an activity in a new task from an activity
2902 // in a task that is not currently on top.)
2903 if (forceReset || finishOnTaskLaunch) {
2904 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002905 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2906 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002907 for (int srcPos = start; srcPos >= i; --srcPos) {
2908 final ActivityRecord p = activities.get(srcPos);
2909 if (p.finishing) {
2910 continue;
2911 }
Todd Kennedy539db512014-12-15 09:57:55 -08002912 finishActivityLocked(
2913 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002914 }
2915 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002916 if (taskInsertionPoint < 0) {
2917 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002918
Craig Mautner77878772013-03-04 19:46:24 -08002919 }
Craig Mautner77878772013-03-04 19:46:24 -08002920
2921 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002922 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2923 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2924 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002925 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002926 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002927 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002928 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002929
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002930 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2931 "Removing and adding activity " + p + " to stack at " + task
2932 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002933 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2934 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002935 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002936 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002937 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002938 if (VALIDATE_TOKENS) {
2939 validateAppTokensLocked();
2940 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002941
2942 // Now we've moved it in to place... but what if this is
2943 // a singleTop activity and we have put it on top of another
2944 // instance of the same activity? Then we drop the instance
2945 // below so it remains singleTop.
2946 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2947 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002948 int targetNdx = taskActivities.indexOf(target);
2949 if (targetNdx > 0) {
2950 ActivityRecord p = taskActivities.get(targetNdx - 1);
2951 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002952 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2953 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002954 }
2955 }
2956 }
2957 }
2958
2959 replyChainEnd = -1;
2960 }
2961 }
Craig Mautner77878772013-03-04 19:46:24 -08002962 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002963 }
2964
Craig Mautner8849a5e2013-04-02 16:41:03 -07002965 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002966 ActivityRecord newActivity) {
2967 boolean forceReset =
2968 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2969 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2970 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2971 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2972 forceReset = true;
2973 }
2974 }
2975
2976 final TaskRecord task = taskTop.task;
2977
2978 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2979 * for remaining tasks. Used for later tasks to reparent to task. */
2980 boolean taskFound = false;
2981
2982 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2983 ActivityOptions topOptions = null;
2984
Craig Mautner77878772013-03-04 19:46:24 -08002985 // Preserve the location for reparenting in the new task.
2986 int reparentInsertionPoint = -1;
2987
Craig Mautnere3a74d52013-02-22 14:14:58 -08002988 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2989 final TaskRecord targetTask = mTaskHistory.get(i);
2990
2991 if (targetTask == task) {
2992 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2993 taskFound = true;
2994 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002995 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2996 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002997 }
2998 }
2999
Craig Mautner70a86932013-02-28 22:37:44 -08003000 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08003001 if (taskNdx >= 0) {
3002 do {
3003 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
3004 } while (taskTop == null && taskNdx >= 0);
3005 }
Craig Mautner70a86932013-02-28 22:37:44 -08003006
Craig Mautnere3a74d52013-02-22 14:14:58 -08003007 if (topOptions != null) {
3008 // If we got some ActivityOptions from an activity on top that
3009 // was removed from the task, propagate them to the new real top.
3010 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003011 taskTop.updateOptionsLocked(topOptions);
3012 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08003013 topOptions.abort();
3014 }
3015 }
3016
3017 return taskTop;
3018 }
3019
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003020 void sendActivityResultLocked(int callingUid, ActivityRecord r,
3021 String resultWho, int requestCode, int resultCode, Intent data) {
3022
3023 if (callingUid > 0) {
3024 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003025 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003026 }
3027
3028 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
3029 + " : who=" + resultWho + " req=" + requestCode
3030 + " res=" + resultCode + " data=" + data);
3031 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
3032 try {
3033 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
3034 list.add(new ResultInfo(resultWho, requestCode,
3035 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08003036 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003037 return;
3038 } catch (Exception e) {
3039 Slog.w(TAG, "Exception thrown sending result to " + r, e);
3040 }
3041 }
3042
3043 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
3044 }
3045
Craig Mautner299f9602015-01-26 09:47:33 -08003046 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003047 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
3048 return;
3049 }
3050
3051 final ActivityRecord next = topRunningActivityLocked();
3052 final String myReason = reason + " adjustFocus";
3053 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003054 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003055 // For freeform, docked, and pinned stacks we always keep the focus within the
3056 // stack as long as there is a running activity in the stack that we can adjust
3057 // focus to.
3058 mService.setFocusedActivityLocked(next, myReason);
3059 return;
3060 } else {
3061 final TaskRecord task = r.task;
3062 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003063 final int taskToReturnTo = task.getTaskToReturnTo();
3064
Wale Ogunwaled045c822015-12-02 09:14:28 -08003065 // For non-fullscreen stack, we want to move the focus to the next visible
3066 // stack to prevent the home screen from moving to the top and obscuring
3067 // other visible stacks.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003068 if (!mFullscreen
3069 && adjustFocusToNextFocusableStackLocked(taskToReturnTo, myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003070 return;
3071 }
3072 // Move the home stack to the top if this stack is fullscreen or there is no
3073 // other visible stack.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003074 if (mStackSupervisor.moveHomeStackTaskToTop(taskToReturnTo, myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003075 // Activity focus was already adjusted. Nothing else to do...
3076 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003077 }
Craig Mautner04f0b702013-10-22 12:31:01 -07003078 }
3079 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08003080 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003081
Wale Ogunwaled046a012015-12-24 13:05:59 -08003082 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07003083 }
3084
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003085 private boolean adjustFocusToNextFocusableStackLocked(int taskToReturnTo, String reason) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003086 final ActivityStack stack = getNextFocusableStackLocked();
3087 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003088 if (stack == null) {
3089 return false;
3090 }
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003091
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003092 final ActivityRecord top = stack.topRunningActivityLocked();
3093
3094 if (stack.isHomeStack() && (top == null || !top.visible)) {
3095 // If we will be focusing on the home stack next and its current top activity isn't
3096 // visible, then use the task return to value to determine the home task to display next.
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07003097 return mStackSupervisor.moveHomeStackTaskToTop(taskToReturnTo, reason);
3098 }
Wale Ogunwalec750f5f2016-03-28 07:43:51 -07003099 return mService.setFocusedActivityLocked(top, myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003100 }
3101
Craig Mautnerf3333272013-04-22 10:55:53 -07003102 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003103 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003104 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3105 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3106 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07003107 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003108 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003109 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3110 "stop-no-history", false)) {
3111 // Activity was finished, no need to continue trying to schedule stop.
3112 adjustFocusedActivityLocked(r, "stopActivityFinished");
3113 r.resumeKeyDispatchingLocked();
3114 return;
3115 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003116 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003117 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003118 + " on stop because we're just sleeping");
3119 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003120 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003121 }
3122
3123 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08003124 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003125 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003126 try {
3127 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003128 if (DEBUG_STATES) Slog.v(TAG_STATES,
3129 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003130 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003131 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3132 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003133 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003134 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003135 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003136 EventLogTags.writeAmStopActivity(
3137 r.userId, System.identityHashCode(r), r.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003138 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07003139 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003140 r.setSleeping(true);
3141 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003142 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003143 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003144 } catch (Exception e) {
3145 // Maybe just ignore exceptions here... if the process
3146 // has crashed, our death notification will clean things
3147 // up.
3148 Slog.w(TAG, "Exception thrown during pause", e);
3149 // Just in case, assume it to be stopped.
3150 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003151 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003152 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003153 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003154 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003155 }
3156 }
3157 }
3158 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003159
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003160 /**
3161 * @return Returns true if the activity is being finished, false if for
3162 * some reason it is being left as-is.
3163 */
3164 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003165 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003166 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003167 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3168 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003169 + ", result=" + resultCode + ", data=" + resultData
3170 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003171 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003172 return false;
3173 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003174
Craig Mautnerd44711d2013-02-23 11:24:36 -08003175 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003176 return true;
3177 }
3178
Craig Mautnerd2328952013-03-05 12:46:26 -08003179 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003180 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3181 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3182 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3183 ActivityRecord r = activities.get(activityNdx);
3184 if (r.resultTo == self && r.requestCode == requestCode) {
3185 if ((r.resultWho == null && resultWho == null) ||
3186 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3187 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3188 false);
3189 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003190 }
3191 }
3192 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003193 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003194 }
3195
Adrian Roos20d7df32016-01-12 18:59:43 +01003196 final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003197 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003198 TaskRecord finishedTask = null;
3199 if (r == null || r.app != app) {
3200 return null;
3201 }
3202 Slog.w(TAG, " Force finishing activity "
3203 + r.intent.getComponent().flattenToShortString());
3204 int taskNdx = mTaskHistory.indexOf(r.task);
3205 int activityNdx = r.task.mActivities.indexOf(r);
3206 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3207 finishedTask = r.task;
3208 // Also terminate any activities below it that aren't yet
3209 // stopped, to avoid a situation where one will get
3210 // re-start our crashing activity once it gets resumed again.
3211 --activityNdx;
3212 if (activityNdx < 0) {
3213 do {
3214 --taskNdx;
3215 if (taskNdx < 0) {
3216 break;
3217 }
3218 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3219 } while (activityNdx < 0);
3220 }
3221 if (activityNdx >= 0) {
3222 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3223 if (r.state == ActivityState.RESUMED
3224 || r.state == ActivityState.PAUSING
3225 || r.state == ActivityState.PAUSED) {
3226 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3227 Slog.w(TAG, " Force finishing activity "
3228 + r.intent.getComponent().flattenToShortString());
3229 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003230 }
3231 }
3232 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003233 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003234 }
3235
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003236 final void finishVoiceTask(IVoiceInteractionSession session) {
3237 IBinder sessionBinder = session.asBinder();
3238 boolean didOne = false;
3239 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3240 TaskRecord tr = mTaskHistory.get(taskNdx);
3241 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3242 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3243 ActivityRecord r = tr.mActivities.get(activityNdx);
3244 if (!r.finishing) {
3245 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3246 false);
3247 didOne = true;
3248 }
3249 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003250 } else {
3251 // Check if any of the activities are using voice
3252 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3253 ActivityRecord r = tr.mActivities.get(activityNdx);
3254 if (r.voiceSession != null
3255 && r.voiceSession.asBinder() == sessionBinder) {
3256 // Inform of cancellation
3257 r.clearVoiceSessionLocked();
3258 try {
3259 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3260 null);
3261 } catch (RemoteException re) {
3262 // Ok
3263 }
3264 // TODO: VI This is redundant in some cases
3265 mService.finishRunningVoiceLocked();
3266 break;
3267 }
3268 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003269 }
3270 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003271 Slog.d(TAG, "ActivityStack.finishVoiceTask()");
3272
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003273 if (didOne) {
3274 mService.updateOomAdjLocked();
3275 }
3276 }
3277
Craig Mautnerd2328952013-03-05 12:46:26 -08003278 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003279 ArrayList<ActivityRecord> activities = r.task.mActivities;
3280 for (int index = activities.indexOf(r); index >= 0; --index) {
3281 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003282 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003283 break;
3284 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003285 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003286 }
3287 return true;
3288 }
3289
Dianne Hackborn5c607432012-02-28 14:44:19 -08003290 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3291 // send the result
3292 ActivityRecord resultTo = r.resultTo;
3293 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003294 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003295 + " who=" + r.resultWho + " req=" + r.requestCode
3296 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003297 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003298 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003299 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003300 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003301 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003302 if (r.info.applicationInfo.uid > 0) {
3303 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3304 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003305 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003306 }
3307 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3308 resultData);
3309 r.resultTo = null;
3310 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003311 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003312
3313 // Make sure this HistoryRecord is not holding on to other resources,
3314 // because clients have remote IPC references to this object so we
3315 // can't assume that will go away and want to avoid circular IPC refs.
3316 r.results = null;
3317 r.pendingResults = null;
3318 r.newIntents = null;
3319 r.icicle = null;
3320 }
3321
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003322 /**
3323 * @return Returns true if this activity has been removed from the history
3324 * list, or false if it is still in the list and will be removed later.
3325 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003326 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3327 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003328 if (r.finishing) {
3329 Slog.w(TAG, "Duplicate finish request for " + r);
3330 return false;
3331 }
3332
Wale Ogunwale7d701172015-03-11 15:36:30 -07003333 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003334 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003335 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003336 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003337 task.taskId, r.shortComponentName, reason);
3338 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003339 final int index = activities.indexOf(r);
3340 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003341 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003342 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003343 // If the caller asked that this activity (and all above it)
3344 // be cleared when the task is reset, don't lose that information,
3345 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003346 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003347 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003348 }
3349 }
3350
3351 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003352
Craig Mautner299f9602015-01-26 09:47:33 -08003353 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003354
Dianne Hackborn5c607432012-02-28 14:44:19 -08003355 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003356
Craig Mautnerde4ef022013-04-07 19:01:33 -07003357 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003358 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003359 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003360 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003361 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003362 ? AppTransition.TRANSIT_TASK_CLOSE
3363 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003364
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003365 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003366 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003367
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003368 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003369 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3370 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3371 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003372 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003373 }
3374
Craig Mautneraea74a52014-03-08 14:23:10 -08003375 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003376 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003377 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003378 } else if (r.state != ActivityState.PAUSING) {
3379 // If the activity is PAUSING, we will complete the finish once
3380 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003381 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003382 if (r.visible) {
3383 mWindowManager.setAppVisibility(r.appToken, false);
3384 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003385 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003386 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003387 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003388 }
3389
3390 return false;
3391 }
3392
Craig Mautnerf3333272013-04-22 10:55:53 -07003393 static final int FINISH_IMMEDIATELY = 0;
3394 static final int FINISH_AFTER_PAUSE = 1;
3395 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003396
Craig Mautnerf3333272013-04-22 10:55:53 -07003397 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003398 // First things first: if this activity is currently visible,
3399 // and the resumed activity is not yet visible, then hold off on
3400 // finishing until the resumed one becomes visible.
3401 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003402 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08003403 addToStopping(r, false /* immediate */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003404 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003405 if (DEBUG_STATES) Slog.v(TAG_STATES,
3406 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003407 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003408 if (oomAdj) {
3409 mService.updateOomAdjLocked();
3410 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003411 return r;
3412 }
3413
3414 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003415 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003416 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003417 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003418 if (mResumedActivity == r) {
3419 mResumedActivity = null;
3420 }
3421 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003422 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003423 r.state = ActivityState.FINISHING;
3424
3425 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003426 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003427 || prevState == ActivityState.STOPPED
3428 || prevState == ActivityState.INITIALIZING) {
3429 // If this activity is already stopped, we can just finish
3430 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003431 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003432 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003433 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003434 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003435 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003436 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003437 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3438 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003439 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003440 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003441
3442 // Need to go through the full pause cycle to get this
3443 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003444 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003445 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003446 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003447 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003448 return r;
3449 }
3450
Craig Mautneree36c772014-07-16 14:56:05 -07003451 void finishAllActivitiesLocked(boolean immediately) {
3452 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003453 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3454 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3455 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3456 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003457 noActivitiesInStack = false;
3458 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003459 continue;
3460 }
Craig Mautneree36c772014-07-16 14:56:05 -07003461 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003462 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3463 }
3464 }
Craig Mautneree36c772014-07-16 14:56:05 -07003465 if (noActivitiesInStack) {
3466 mActivityContainer.onTaskListEmptyLocked();
3467 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003468 }
3469
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003470 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3471 // Basic case: for simple app-centric recents, we need to recreate
3472 // the task if the affinity has changed.
3473 if (srec == null || srec.task.affinity == null ||
3474 !srec.task.affinity.equals(destAffinity)) {
3475 return true;
3476 }
3477 // Document-centric case: an app may be split in to multiple documents;
3478 // they need to re-create their task if this current activity is the root
3479 // of a document, unless simply finishing it will return them to the the
3480 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003481 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3482 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003483 // Okay, this activity is at the root of its task. What to do, what to do...
3484 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3485 // Finishing won't return to an application, so we need to recreate.
3486 return true;
3487 }
3488 // We now need to get the task below it to determine what to do.
3489 int taskIdx = mTaskHistory.indexOf(srec.task);
3490 if (taskIdx <= 0) {
3491 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3492 return false;
3493 }
3494 if (taskIdx == 0) {
3495 // At the bottom of the stack, nothing to go back to.
3496 return true;
3497 }
3498 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3499 if (!srec.task.affinity.equals(prevTask.affinity)) {
3500 // These are different apps, so need to recreate.
3501 return true;
3502 }
3503 }
3504 return false;
3505 }
3506
Wale Ogunwale7d701172015-03-11 15:36:30 -07003507 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003508 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003509 final TaskRecord task = srec.task;
3510 final ArrayList<ActivityRecord> activities = task.mActivities;
3511 final int start = activities.indexOf(srec);
3512 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003513 return false;
3514 }
3515 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003516 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003517 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003518 final ComponentName dest = destIntent.getComponent();
3519 if (start > 0 && dest != null) {
3520 for (int i = finishTo; i >= 0; i--) {
3521 ActivityRecord r = activities.get(i);
3522 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003523 r.info.name.equals(dest.getClassName())) {
3524 finishTo = i;
3525 parent = r;
3526 foundParentInTask = true;
3527 break;
3528 }
3529 }
3530 }
3531
3532 IActivityController controller = mService.mController;
3533 if (controller != null) {
3534 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3535 if (next != null) {
3536 // ask watcher if this is allowed
3537 boolean resumeOK = true;
3538 try {
3539 resumeOK = controller.activityResuming(next.packageName);
3540 } catch (RemoteException e) {
3541 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003542 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003543 }
3544
3545 if (!resumeOK) {
3546 return false;
3547 }
3548 }
3549 }
3550 final long origId = Binder.clearCallingIdentity();
3551 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003552 ActivityRecord r = activities.get(i);
3553 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003554 // Only return the supplied result for the first activity finished
3555 resultCode = Activity.RESULT_CANCELED;
3556 resultData = null;
3557 }
3558
3559 if (parent != null && foundParentInTask) {
3560 final int parentLaunchMode = parent.info.launchMode;
3561 final int destIntentFlags = destIntent.getFlags();
3562 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3563 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3564 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3565 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003566 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3567 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003568 } else {
3569 try {
3570 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3571 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003572 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3573 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3574 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003575 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3576 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003577 foundParentInTask = res == ActivityManager.START_SUCCESS;
3578 } catch (RemoteException e) {
3579 foundParentInTask = false;
3580 }
3581 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003582 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003583 }
3584 }
3585 Binder.restoreCallingIdentity(origId);
3586 return foundParentInTask;
3587 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003588 /**
3589 * Perform the common clean-up of an activity record. This is called both
3590 * as part of destroyActivityLocked() (when destroying the client-side
3591 * representation) and cleaning things up as a result of its hosting
3592 * processing going away, in which case there is no remaining client-side
3593 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003594 *
3595 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003596 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003597 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3598 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003599 if (mResumedActivity == r) {
3600 mResumedActivity = null;
3601 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003602 if (mPausingActivity == r) {
3603 mPausingActivity = null;
3604 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003605 mService.resetFocusedActivityIfNeededLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003606
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003607 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003608 r.frozenBeforeDestroy = false;
3609
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003610 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003611 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003612 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003613 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003614 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003615 }
3616
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003617 // Make sure this record is no longer in the pending finishes list.
3618 // This could happen, for example, if we are trimming activities
3619 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003620 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003621 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003622
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003623 // Remove any pending results.
3624 if (r.finishing && r.pendingResults != null) {
3625 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3626 PendingIntentRecord rec = apr.get();
3627 if (rec != null) {
3628 mService.cancelIntentSenderLocked(rec, false);
3629 }
3630 }
3631 r.pendingResults = null;
3632 }
3633
3634 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003635 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003636 }
3637
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003638 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003639 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003640 if (getVisibleBehindActivity() == r) {
3641 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003642 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003643 }
3644
3645 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003646 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003647 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003648 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003649 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003650 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003651 }
3652
Craig Mautner299f9602015-01-26 09:47:33 -08003653 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003654 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003655 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003656 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003657 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3658 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3659
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003660 r.takeFromHistory();
3661 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003662 if (DEBUG_STATES) Slog.v(TAG_STATES,
3663 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003664 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003665 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003666 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003667 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003668 if (VALIDATE_TOKENS) {
3669 validateAppTokensLocked();
3670 }
Craig Mautner312ba862014-02-10 17:55:01 -08003671 final TaskRecord task = r.task;
3672 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003673 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003674 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003675 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003676 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003677 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003678 }
Craig Mautner299f9602015-01-26 09:47:33 -08003679 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003680 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003681 cleanUpActivityServicesLocked(r);
3682 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003683 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003684
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003685 /**
3686 * Perform clean-up of service connections in an activity record.
3687 */
3688 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3689 // Throw away any services that have been bound by this activity.
3690 if (r.connections != null) {
3691 Iterator<ConnectionRecord> it = r.connections.iterator();
3692 while (it.hasNext()) {
3693 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003694 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003695 }
3696 r.connections = null;
3697 }
3698 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003699
Craig Mautneree2e45a2014-06-27 12:10:03 -07003700 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003701 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003702 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003703 mHandler.sendMessage(msg);
3704 }
3705
Craig Mautneree2e45a2014-06-27 12:10:03 -07003706 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003707 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003708 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003709 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3710 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3711 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3712 final ActivityRecord r = activities.get(activityNdx);
3713 if (r.finishing) {
3714 continue;
3715 }
3716 if (r.fullscreen) {
3717 lastIsOpaque = true;
3718 }
3719 if (owner != null && r.app != owner) {
3720 continue;
3721 }
3722 if (!lastIsOpaque) {
3723 continue;
3724 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003725 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003726 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003727 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003728 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003729 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003730 activityRemoved = true;
3731 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003732 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003733 }
3734 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003735 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003736 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003737 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003738 }
3739
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003740 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3741 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003742 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3743 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003744 + " pausing=" + mPausingActivity + " for reason " + reason);
3745 return destroyActivityLocked(r, true, reason);
3746 }
3747 return false;
3748 }
3749
3750 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3751 String reason) {
3752 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003753 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003754 int maxTasks = tasks.size() / 4;
3755 if (maxTasks < 1) {
3756 maxTasks = 1;
3757 }
3758 int numReleased = 0;
3759 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3760 final TaskRecord task = mTaskHistory.get(taskNdx);
3761 if (!tasks.contains(task)) {
3762 continue;
3763 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003764 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003765 int curNum = 0;
3766 final ArrayList<ActivityRecord> activities = task.mActivities;
3767 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3768 final ActivityRecord activity = activities.get(actNdx);
3769 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003770 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003771 + " in state " + activity.state + " resumed=" + mResumedActivity
3772 + " pausing=" + mPausingActivity + " for reason " + reason);
3773 destroyActivityLocked(activity, true, reason);
3774 if (activities.get(actNdx) != activity) {
3775 // Was removed from list, back up so we don't miss the next one.
3776 actNdx--;
3777 }
3778 curNum++;
3779 }
3780 }
3781 if (curNum > 0) {
3782 numReleased += curNum;
3783 maxTasks--;
3784 if (mTaskHistory.get(taskNdx) != task) {
3785 // The entire task got removed, back up so we don't miss the next one.
3786 taskNdx--;
3787 }
3788 }
3789 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003790 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3791 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003792 return numReleased;
3793 }
3794
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003795 /**
3796 * Destroy the current CLIENT SIDE instance of an activity. This may be
3797 * called both when actually finishing an activity, or when performing
3798 * a configuration switch where we destroy the current client-side object
3799 * but then create a new client-side object for this same HistoryRecord.
3800 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003801 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003802 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3803 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003804 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003805 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003806 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003807 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003808
3809 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003810
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003811 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003812
3813 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003814
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003815 if (hadApp) {
3816 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003817 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003818 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3819 mService.mHeavyWeightProcess = null;
3820 mService.mHandler.sendEmptyMessage(
3821 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3822 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003823 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003824 // Update any services we are bound to that might care about whether
3825 // their client may have activities.
3826 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003827 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003828 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003829 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003830 }
3831 }
3832
3833 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003834
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003835 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003836 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003837 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003838 r.configChangeFlags);
3839 } catch (Exception e) {
3840 // We can just ignore exceptions here... if the process
3841 // has crashed, our death notification will clean things
3842 // up.
3843 //Slog.w(TAG, "Exception thrown during finish", e);
3844 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003845 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003846 removedFromHistory = true;
3847 skipDestroy = true;
3848 }
3849 }
3850
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003851 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003852
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003853 // If the activity is finishing, we need to wait on removing it
3854 // from the list to give it a chance to do its cleanup. During
3855 // that time it may make calls back with its token so we need to
3856 // be able to find it on the list and so we don't want to remove
3857 // it from the list yet. Otherwise, we can just immediately put
3858 // it in the destroyed state since we are not removing it from the
3859 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003860 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003861 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003862 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003863 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003864 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003865 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3866 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003867 if (DEBUG_STATES) Slog.v(TAG_STATES,
3868 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003869 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003870 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003871 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003872 }
3873 } else {
3874 // remove this record from the history.
3875 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003876 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003877 removedFromHistory = true;
3878 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003879 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003880 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003881 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003882 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003883 }
3884 }
3885
3886 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003887
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003888 if (!mLRUActivities.remove(r) && hadApp) {
3889 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3890 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003891
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003892 return removedFromHistory;
3893 }
3894
Craig Mautner299f9602015-01-26 09:47:33 -08003895 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003896 final long origId = Binder.clearCallingIdentity();
3897 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003898 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003899 if (r != null) {
3900 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003901 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003902 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003903
Wale Ogunwale60454db2015-01-23 16:05:07 -08003904 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003905 if (r.state == ActivityState.DESTROYING) {
3906 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003907 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003908 }
3909 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003910 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003911 } finally {
3912 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003913 }
3914 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003915
Todd Kennedyaab56db2015-01-30 09:39:53 -08003916 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003917 if (hasVisibleBehindActivity() &&
3918 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08003919 if (r == topRunningActivityLocked()
3920 && getStackVisibilityLocked(null) == STACK_VISIBLE) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003921 // Don't release the top activity if it has requested to run behind the next
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08003922 // activity and the stack is currently visible.
Craig Mautner64ccb702014-10-01 09:38:40 -07003923 return;
3924 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003925 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003926 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003927 " thread=" + r.app.thread);
3928 if (r != null && r.app != null && r.app.thread != null) {
3929 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003930 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003931 } catch (RemoteException e) {
3932 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003933 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003934 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003935 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003936 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003937 }
3938 }
3939 }
3940
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003941 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003942 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3943 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003944 if (r != null) {
3945 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003946 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003947 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003948 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003949 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003950 }
3951
Jose Lima4b6c6692014-08-12 17:41:12 -07003952 boolean hasVisibleBehindActivity() {
3953 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003954 }
3955
Jose Lima4b6c6692014-08-12 17:41:12 -07003956 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003957 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003958 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003959 }
3960 }
3961
Jose Lima4b6c6692014-08-12 17:41:12 -07003962 ActivityRecord getVisibleBehindActivity() {
3963 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003964 }
3965
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003966 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3967 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003968 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003969 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3970 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003971 while (i > 0) {
3972 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003973 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003974 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003975 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003976 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003977 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003978 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003979 }
3980 }
3981 }
3982
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003983 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3984 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003985 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3986 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003987 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3988 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003989 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003990 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003991 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3992 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003993
3994 boolean hasVisibleActivities = false;
3995
3996 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003997 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003998 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3999 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08004000 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4001 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4002 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4003 final ActivityRecord r = activities.get(activityNdx);
4004 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004005 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
4006 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08004007 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08004008 if (r.visible) {
4009 hasVisibleActivities = true;
4010 }
Craig Mautneracebdc82015-02-24 10:53:03 -08004011 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08004012 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
4013 // Don't currently have state for the activity, or
4014 // it is finishing -- always remove it.
4015 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004016 } else if (!r.visible && r.launchCount > 2 &&
4017 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08004018 // We have launched this activity too many times since it was
4019 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08004020 // (Note if the activity is visible, we don't remove the record.
4021 // We leave the dead window on the screen but the process will
4022 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08004023 remove = true;
4024 } else {
4025 // The process may be gone, but the activity lives on!
4026 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004027 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004028 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004029 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
4030 "Removing activity " + r + " from stack at " + i
4031 + ": haveState=" + r.haveState
4032 + " stateNotNeeded=" + r.stateNotNeeded
4033 + " finishing=" + r.finishing
4034 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08004035 if (!r.finishing) {
4036 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
4037 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
4038 r.userId, System.identityHashCode(r),
4039 r.task.taskId, r.shortComponentName,
4040 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07004041 if (r.state == ActivityState.RESUMED) {
4042 mService.updateUsageStats(r, false);
4043 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004044 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004045 } else {
4046 // We have the current state for this activity, so
4047 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004048 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
4049 if (DEBUG_APP) Slog.v(TAG_APP,
4050 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08004051 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08004052 // Set nowVisible to previous visible state. If the app was visible while
4053 // it died, we leave the dead window on screen so it's basically visible.
4054 // This is needed when user later tap on the dead window, we need to stop
4055 // other apps when user transfers focus to the restarted activity.
4056 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08004057 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07004058 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08004059 "App died, clearing saved state of " + r);
4060 r.icicle = null;
4061 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004062 }
Craig Mautnerd2328952013-03-05 12:46:26 -08004063 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08004064 if (remove) {
4065 removeActivityFromHistoryLocked(r, "appDied");
4066 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004067 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004068 }
4069 }
4070
4071 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004072 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004073
Chong Zhang280d3322015-11-03 17:27:26 -08004074 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004075 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004076 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004077 if (r != null && r.state != ActivityState.RESUMED) {
4078 r.updateOptionsLocked(options);
4079 } else {
4080 ActivityOptions.abort(options);
4081 }
4082 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004083 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004084 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004085
Craig Mautner21d24a22014-04-23 11:45:37 -07004086 void updateTaskMovement(TaskRecord task, boolean toFront) {
4087 if (task.isPersistable) {
4088 task.mLastTimeMoved = System.currentTimeMillis();
4089 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
4090 // recently will be most negative, tasks sent to the bottom before that will be less
4091 // negative. Similarly for recent tasks moved to the top which will be most positive.
4092 if (!toFront) {
4093 task.mLastTimeMoved *= -1;
4094 }
4095 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07004096 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07004097 }
4098
Craig Mautner84984fa2014-06-19 11:19:20 -07004099 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07004100 final int top = mTaskHistory.size() - 1;
4101 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
4102 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004103 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004104 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4105 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07004106 mTaskHistory.remove(taskNdx);
4107 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004108 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07004109 return;
4110 }
4111 }
4112 }
4113
Chong Zhang280d3322015-11-03 17:27:26 -08004114 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004115 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004116 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004117
Craig Mautner11bf9a52013-02-19 14:08:51 -08004118 final int numTasks = mTaskHistory.size();
4119 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004120 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004121 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004122 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004123 ActivityOptions.abort(options);
4124 } else {
4125 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
4126 }
4127 return;
4128 }
4129
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004130 if (timeTracker != null) {
4131 // The caller wants a time tracker associated with this task.
4132 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4133 tr.mActivities.get(i).appTimeTracker = timeTracker;
4134 }
4135 }
4136
Craig Mautner11bf9a52013-02-19 14:08:51 -08004137 // Shift all activities with this task up to the top
4138 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004139 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004140
Chong Zhang45c25ce2015-08-10 22:18:26 -07004141 // Don't refocus if invisible to current user
4142 ActivityRecord top = tr.getTopActivity();
4143 if (!okToShowLocked(top)) {
4144 addRecentActivityLocked(top);
4145 ActivityOptions.abort(options);
4146 return;
4147 }
4148
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004149 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004150 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004151 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004152
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004153 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004154 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004155 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004156 if (r != null) {
4157 mNoAnimActivities.add(r);
4158 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004159 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004160 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08004161 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004162 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004163
Wale Ogunwaled046a012015-12-24 13:05:59 -08004164 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004165 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004166
4167 if (VALIDATE_TOKENS) {
4168 validateAppTokensLocked();
4169 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004170 }
4171
4172 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004173 * Worker method for rearranging history stack. Implements the function of moving all
4174 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004175 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004176 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004177 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4178 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004179 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004180 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004181 * @return Returns true if the move completed, false if not.
4182 */
Craig Mautner299f9602015-01-26 09:47:33 -08004183 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004184 final TaskRecord tr = taskForIdLocked(taskId);
4185 if (tr == null) {
4186 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4187 return false;
4188 }
4189
4190 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07004191 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004192
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004193 // If we have a watcher, preflight the move before committing to it. First check
4194 // for *other* available tasks, but if none are available, then try again allowing the
4195 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004196 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004197 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004198 if (next == null) {
4199 next = topRunningActivityLocked(null, 0);
4200 }
4201 if (next != null) {
4202 // ask watcher if this is allowed
4203 boolean moveOK = true;
4204 try {
4205 moveOK = mService.mController.activityResuming(next.packageName);
4206 } catch (RemoteException e) {
4207 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004208 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004209 }
4210 if (!moveOK) {
4211 return false;
4212 }
4213 }
4214 }
4215
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004216 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004217
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004218 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4219 // For the case where we are moving the home task back and there is an activity visible
4220 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4221 // activity to maintain order with what the user is seeing.
4222 final ActivityStack fullscreenStack =
4223 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4224 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4225 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4226 mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004227 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004228 return true;
4229 }
4230 }
4231
riddle_hsuc215a4f2014-12-27 12:10:45 +08004232 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004233
4234 // If true, we should resume the home activity next if the task we are moving to the
4235 // back is over the home stack. We force to false if the task we are moving to back
4236 // is the home task and we don't want it resumed after moving to the back.
4237 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4238 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004239 final TaskRecord nextTask = getNextTask(tr);
4240 if (nextTask != null) {
4241 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4242 } else {
4243 prevIsHome = true;
4244 }
4245 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004246 mTaskHistory.remove(tr);
4247 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004248 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004249
Craig Mautnerc8143c62013-09-03 12:15:57 -07004250 // There is an assumption that moving a task to the back moves it behind the home activity.
4251 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004252 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004253 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4254 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004255 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004256 break;
4257 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004258 if (taskNdx == 1) {
4259 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004260 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004261 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004262 }
4263
Craig Mautner299f9602015-01-26 09:47:33 -08004264 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004265 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004266
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004267 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004268 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004269 }
4270
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004271 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004272 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004273 if (!mService.mBooting && !mService.mBooted) {
4274 // Not ready yet!
4275 return false;
4276 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004277 final int taskToReturnTo = tr.getTaskToReturnTo();
4278 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004279 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004280 }
4281
Wale Ogunwaled046a012015-12-24 13:05:59 -08004282 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004283 return true;
4284 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004285
Craig Mautner8849a5e2013-04-02 16:41:03 -07004286 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004287 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004288 final Uri data = r.intent.getData();
4289 final String strData = data != null ? data.toSafeString() : null;
4290
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004291 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004292 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004293 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004294 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004295 }
4296
4297 /**
4298 * Make sure the given activity matches the current configuration. Returns
4299 * false if the activity had to be destroyed. Returns true if the
4300 * configuration is the same, or the activity will remain running as-is
4301 * for whatever reason. Ensures the HistoryRecord is updated with the
4302 * correct configuration and all other bookkeeping is handled.
4303 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004304 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4305 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004306 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004307 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004308 "Skipping config check (will change): " + r);
4309 return true;
4310 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004311
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004312 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004313 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004314
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004315 // Short circuit: if the two configurations are the exact same
4316 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004317 final Configuration newConfig = mService.mConfiguration;
4318 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004319 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004320 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004321 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004322 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004323 "Configuration unchanged in " + r);
4324 return true;
4325 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004326
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004327 // We don't worry about activities that are finishing.
4328 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004329 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004330 "Configuration doesn't matter in finishing " + r);
4331 r.stopFreezingScreenLocked(false);
4332 return true;
4333 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004334
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004335 // Okay we now are going to make this activity have the new config.
4336 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004337 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004338 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004339 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004340 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004341
Filip Gruszczynski23493322015-07-29 17:02:59 -07004342 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004343 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004344 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004345 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004346 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004347 // There are no significant differences, so we won't relaunch but should still deliver
4348 // the new configuration to the client process.
4349 r.scheduleConfigurationChanged(taskConfig, false);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004350 return true;
4351 }
4352
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004353 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4354 "Configuration changes for " + r + " ; taskChanges="
4355 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4356 + Configuration.configurationDiffToString(changes));
4357
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004358 // If the activity isn't currently running, just leave the new
4359 // configuration and it will pick that up next time it starts.
4360 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004361 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004362 "Configuration doesn't matter not running " + r);
4363 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004364 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004365 return true;
4366 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004367
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004368 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004369 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4370 "Checking to restart " + r.info.name + ": changed=0x"
4371 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004372 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4373 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004374
Dianne Hackborne6676352011-06-01 16:51:20 -07004375 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004376 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4377 r.configChangeFlags |= changes;
4378 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004379 r.forceNewConfig = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004380 preserveWindow &= isResizeOnlyChange(changes);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004381 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004382 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004383 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004384 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004385 } else if (r.state == ActivityState.PAUSING) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004386 // A little annoying: we are waiting for this activity to finish pausing. Let's not
4387 // do anything now, but just flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004388 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004389 "Config is skipping already pausing " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004390 r.deferRelaunchUntilPaused = true;
4391 r.preserveWindowOnDeferredRelaunch = preserveWindow;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004392 return true;
4393 } else if (r.state == ActivityState.RESUMED) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004394 // Try to optimize this case: the configuration is changing and we need to restart
4395 // the top, resumed activity. Instead of doing the normal handshaking, just say
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004396 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004397 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004398 "Config is relaunching resumed " + r);
Chong Zhang2b79af12016-02-10 18:47:06 -08004399
4400 if (DEBUG_STATES && !r.visible) {
4401 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + r
4402 + " called by " + Debug.getCallers(4));
4403 }
4404
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004405 relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004406 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004407 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004408 "Config is relaunching non-resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004409 relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004410 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004411
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004412 // All done... tell the caller we weren't able to keep this activity around.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004413 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004414 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004415
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004416 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004417 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004418 // changes is always sent to all processes when they happen so it can just use whatever
4419 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004420 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004421 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004422
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004423 return true;
4424 }
4425
Filip Gruszczynski23493322015-07-29 17:02:59 -07004426 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4427 Configuration oldTaskOverride) {
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08004428
4429 // If we went from full-screen to non-full-screen, make sure to use the correct
4430 // configuration task diff, so the diff stays as small as possible.
4431 if (Configuration.EMPTY.equals(oldTaskOverride)
4432 && !Configuration.EMPTY.equals(taskConfig)) {
4433 oldTaskOverride = record.task.extractOverrideConfig(record.configuration);
4434 }
4435
Jorim Jaggi1b025a62016-02-03 19:27:49 -08004436 // Conversely, do the same when going the other direction.
4437 if (Configuration.EMPTY.equals(taskConfig)
4438 && !Configuration.EMPTY.equals(oldTaskOverride)) {
4439 taskConfig = record.task.extractOverrideConfig(record.configuration);
4440 }
4441
Filip Gruszczynski23493322015-07-29 17:02:59 -07004442 // Determine what has changed. May be nothing, if this is a config
4443 // that has come back from the app after going idle. In that case
4444 // we just want to leave the official config object now in the
4445 // activity and do nothing else.
4446 int taskChanges = oldTaskOverride.diff(taskConfig);
4447 // We don't want to use size changes if they don't cross boundaries that are important to
4448 // the app.
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004449 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004450 final boolean crosses = record.crossesHorizontalSizeThreshold(
4451 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4452 || record.crossesVerticalSizeThreshold(
4453 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4454 if (!crosses) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004455 taskChanges &= ~CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004456 }
4457 }
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004458 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004459 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4460 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004461 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004462 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004463 }
4464 }
4465 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4466 }
4467
4468 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4469 Configuration oldTaskOverride, int taskChanges) {
4470 if (taskChanges == 0) {
4471 // {@link Configuration#diff} doesn't catch changes from unset values.
4472 // Check for changes we care about.
4473 if (oldTaskOverride.orientation != taskConfig.orientation) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004474 taskChanges |= CONFIG_ORIENTATION;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004475 }
4476 // We want to explicitly track situations where the size configuration goes from
4477 // undefined to defined. We don't care about crossing the threshold in that case,
4478 // because there is no threshold.
4479 final int oldHeight = oldTaskOverride.screenHeightDp;
4480 final int newHeight = taskConfig.screenHeightDp;
4481 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4482 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4483 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004484 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004485 }
4486 final int oldWidth = oldTaskOverride.screenWidthDp;
4487 final int newWidth = taskConfig.screenWidthDp;
4488 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4489 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4490 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004491 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004492 }
4493 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4494 final int newSmallest = taskConfig.smallestScreenWidthDp;
4495 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4496 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4497 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004498 taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4499 }
4500 final int oldLayout = oldTaskOverride.screenLayout;
4501 final int newLayout = taskConfig.screenLayout;
4502 if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4503 || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4504 taskChanges |= CONFIG_SCREEN_LAYOUT;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004505 }
4506 }
4507 return taskChanges;
4508 }
4509
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004510 private static boolean isResizeOnlyChange(int change) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004511 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4512 | CONFIG_SCREEN_LAYOUT)) == 0;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004513 }
4514
Wale Ogunwale83301a92015-09-24 15:54:08 -07004515 private void relaunchActivityLocked(
4516 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4517 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004518 r.configChangeFlags = 0;
Wale Ogunwale83301a92015-09-24 15:54:08 -07004519 return;
4520 }
4521
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004522 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004523 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004524 if (andResume) {
4525 results = r.results;
4526 newIntents = r.newIntents;
4527 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004528 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4529 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004530 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004531 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004532 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004533 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004534
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004535 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004536
Craig Mautner34b73df2014-01-12 21:11:08 -08004537 mStackSupervisor.removeChildActivityContainers(r);
4538
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004539 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004540 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08004541 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r
4542 + " callers=" + Debug.getCallers(6));
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004543 r.forceNewConfig = false;
Chong Zhangd78ddb42016-03-02 17:01:14 -08004544 mStackSupervisor.activityRelaunchingLocked(r);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004545 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4546 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004547 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004548 // Note: don't need to call pauseIfSleepingLocked() here, because
4549 // the caller will only pass in 'andResume' if this activity is
4550 // currently resumed, which implies we aren't sleeping.
4551 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004552 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004553 }
4554
4555 if (andResume) {
Chong Zhang2b79af12016-02-10 18:47:06 -08004556 if (DEBUG_STATES) {
4557 Slog.d(TAG_STATES, "Resumed after relaunch " + r);
4558 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004559 r.state = ActivityState.RESUMED;
Chong Zhang2b79af12016-02-10 18:47:06 -08004560 // Relaunch-resume could happen either when the app is already in the front,
4561 // or while it's being brought to front. In the latter case, it's marked RESUMED
4562 // but not yet visible (or stopped). We need to complete the resume here as the
4563 // code in resumeTopActivityInnerLocked to complete the resume might be skipped.
4564 if (!r.visible || r.stopped) {
4565 mWindowManager.setAppVisibility(r.appToken, true);
4566 completeResumeLocked(r);
4567 } else {
4568 r.results = null;
4569 r.newIntents = null;
4570 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004571 } else {
4572 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4573 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004574 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004575
4576 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004577 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004578
4579 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004580 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4581 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4582 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4583 final ActivityRecord r = activities.get(activityNdx);
4584 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004585 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004586 }
4587 if (r.fullscreen && !r.finishing) {
4588 return false;
4589 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004590 }
4591 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004592 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004593 if (r == null) {
4594 return false;
4595 }
4596 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4597 + " would have returned true for r=" + r);
4598 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004599 }
4600
4601 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004602 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4603 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4604 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4605 final ActivityRecord r = activities.get(activityNdx);
4606 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004607 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004608 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004609 }
4610 }
4611 }
4612
Wale Ogunwale540e1232015-05-01 15:35:39 -07004613 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4614 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004615 boolean didSomething = false;
4616 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004617 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004618 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4619 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4620 int numActivities = activities.size();
4621 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4622 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004623 final boolean sameComponent =
4624 (r.packageName.equals(packageName) && (filterByClasses == null
4625 || filterByClasses.contains(r.realActivity.getClassName())))
4626 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004627 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004628 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004629 && (r.app == null || evenPersistent || !r.app.persistent)) {
4630 if (!doit) {
4631 if (r.finishing) {
4632 // If this activity is just finishing, then it is not
4633 // interesting as far as something to stop.
4634 continue;
4635 }
4636 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004637 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004638 if (r.isHomeActivity()) {
4639 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4640 Slog.i(TAG, "Skip force-stop again " + r);
4641 continue;
4642 } else {
4643 homeActivity = r.realActivity;
4644 }
4645 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004646 didSomething = true;
4647 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004648 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004649 if (r.app != null) {
4650 r.app.removed = true;
4651 }
4652 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004653 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004654 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004655 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4656 true)) {
4657 // r has been deleted from mActivities, accommodate.
4658 --numActivities;
4659 --activityNdx;
4660 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004661 }
4662 }
4663 }
4664 return didSomething;
4665 }
4666
Dianne Hackborn09233282014-04-30 11:33:59 -07004667 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004668 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4669 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004670 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004671 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004672 if (task.getTopActivity() == null) {
4673 continue;
4674 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004675 ActivityRecord r = null;
4676 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004677 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004678 int numActivities = 0;
4679 int numRunning = 0;
4680 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004681 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004682 continue;
4683 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004684 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004685 tmp = activities.get(activityNdx);
4686 if (tmp.finishing) {
4687 continue;
4688 }
4689 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004690
Craig Mautneraab647e2013-02-28 16:31:36 -08004691 // Initialize state for next task if needed.
4692 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4693 top = r;
4694 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004695 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004696
4697 // Add 'r' into the current task.
4698 numActivities++;
4699 if (r.app != null && r.app.thread != null) {
4700 numRunning++;
4701 }
4702
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004703 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004704 TAG, r.intent.getComponent().flattenToShortString()
4705 + ": task=" + r.task);
4706 }
4707
4708 RunningTaskInfo ci = new RunningTaskInfo();
4709 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004710 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004711 ci.baseActivity = r.intent.getComponent();
4712 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004713 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004714 if (focusedStack && topTask) {
4715 // Give the latest time to ensure foreground task can be sorted
4716 // at the first, because lastActiveTime of creating task is 0.
4717 ci.lastActiveTime = System.currentTimeMillis();
4718 topTask = false;
4719 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004720
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004721 if (top.task != null) {
4722 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004723 }
4724 ci.numActivities = numActivities;
4725 ci.numRunning = numRunning;
Wale Ogunwale21b60582016-01-27 12:34:16 -08004726 ci.isDockable = task.canGoInDockedStack();
Craig Mautneraab647e2013-02-28 16:31:36 -08004727 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004728 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004729 }
4730
4731 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004732 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004733 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004734 if (top >= 0) {
4735 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4736 int activityTop = activities.size() - 1;
4737 if (activityTop > 0) {
4738 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4739 "unhandled-back", true);
4740 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004741 }
4742 }
4743
Craig Mautner6b74cb52013-09-27 17:02:21 -07004744 /**
4745 * Reset local parameters because an app's activity died.
4746 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004747 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004748 */
4749 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004750 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004751 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004752 "App died while pausing: " + mPausingActivity);
4753 mPausingActivity = null;
4754 }
4755 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4756 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004757 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004758 }
4759
Craig Mautner19091252013-10-05 00:03:53 -07004760 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004761 }
4762
Craig Mautnercae015f2013-02-08 14:31:27 -08004763 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004764 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4765 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4766 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4767 final ActivityRecord r = activities.get(activityNdx);
4768 if (r.app == app) {
4769 Slog.w(TAG, " Force finishing activity "
4770 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004771 // Force the destroy to skip right to removal.
4772 r.app = null;
4773 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004774 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004775 }
4776 }
4777 }
4778
Dianne Hackborn390517b2013-05-30 15:03:32 -07004779 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004780 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004781 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004782 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4783 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004784 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4785 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004786 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004787 " Task id #" + task.taskId + "\n" +
4788 " mFullscreen=" + task.mFullscreen + "\n" +
4789 " mBounds=" + task.mBounds + "\n" +
Andrii Kulian771f9e02016-03-28 19:08:58 -07004790 " mMinimalWidth=" + task.mMinimalWidth + "\n" +
4791 " mMinimalHeight=" + task.mMinimalHeight + "\n" +
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004792 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004793 if (printed) {
4794 header = null;
4795 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004796 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004797 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004798 }
4799
4800 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4801 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4802
4803 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004804 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4805 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004806 }
4807 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004808 final int top = mTaskHistory.size() - 1;
4809 if (top >= 0) {
4810 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4811 int listTop = list.size() - 1;
4812 if (listTop >= 0) {
4813 activities.add(list.get(listTop));
4814 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004815 }
4816 } else {
4817 ItemMatcher matcher = new ItemMatcher();
4818 matcher.build(name);
4819
Craig Mautneraab647e2013-02-28 16:31:36 -08004820 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4821 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4822 if (matcher.match(r1, r1.intent.getComponent())) {
4823 activities.add(r1);
4824 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004825 }
4826 }
4827 }
4828
4829 return activities;
4830 }
4831
4832 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004833 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004834
4835 // All activities that came from the package must be
4836 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004837 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4838 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4839 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4840 final ActivityRecord a = activities.get(activityNdx);
4841 if (a.info.packageName.equals(packageName)) {
4842 a.forceNewConfig = true;
4843 if (starting != null && a == starting && a.visible) {
4844 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004845 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08004846 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004847 }
4848 }
4849 }
4850
4851 return starting;
4852 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004853
Craig Mautner299f9602015-01-26 09:47:33 -08004854 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004855 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004856 }
4857
Wale Ogunwale000957c2015-04-03 08:19:12 -07004858 /**
4859 * Removes the input task from this stack.
4860 * @param task to remove.
4861 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004862 * @param moving task to another stack. In the case we are moving we don't want to perform
4863 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004864 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004865 void removeTask(TaskRecord task, String reason, boolean moving) {
4866 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004867 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004868 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004869 if (!StackId.persistTaskBounds(mStackId)) {
4870 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4871 // default configuration the next time it launches.
4872 task.updateOverrideConfiguration(null);
4873 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004874 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004875
Craig Mautner04a0ea62014-01-13 12:51:26 -08004876 final ActivityRecord r = mResumedActivity;
4877 if (r != null && r.task == task) {
4878 mResumedActivity = null;
4879 }
4880
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004881 final int taskNdx = mTaskHistory.indexOf(task);
4882 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004883 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4884 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4885 if (!nextTask.isOverHomeStack()) {
4886 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4887 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004888 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004889 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004890 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004891
Wale Ogunwale040b4702015-08-06 18:10:50 -07004892 if (!moving && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004893 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07004894 final boolean isVoiceSession = task.voiceSession != null;
4895 if (isVoiceSession) {
4896 try {
4897 task.voiceSession.taskFinished(task.intent, task.taskId);
4898 } catch (RemoteException e) {
4899 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004900 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004901 if (task.autoRemoveFromRecents() || isVoiceSession) {
4902 // Task creator asked to remove this when done, or this task was a voice
4903 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004904 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004905 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004906 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004907 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004908
4909 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004910 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004911 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004912 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004913 String myReason = reason + " leftTaskHistoryEmpty";
Wale Ogunwaled34e80c2016-03-23 17:08:44 -07004914 if (mFullscreen
4915 || !adjustFocusToNextFocusableStackLocked(
4916 task.getTaskToReturnTo(), myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004917 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004918 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004919 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004920 if (mStacks != null) {
4921 mStacks.remove(this);
4922 mStacks.add(0, this);
4923 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004924 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004925 mActivityContainer.onTaskListEmptyLocked();
4926 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004927 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004928
4929 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004930 }
4931
Dianne Hackborn91097de2014-04-04 18:02:06 -07004932 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4933 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4934 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004935 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4936 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004937 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08004938 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08004939 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
Andrii Kulian2e751b82016-03-16 16:59:32 -07004940 if (!layoutTaskInStack(task, info.windowLayout) && mBounds != null && task.isResizeable()
Chong Zhang75b37202015-12-04 14:16:36 -08004941 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004942 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004943 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004944 return task;
4945 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004946
Andrii Kulian2e751b82016-03-16 16:59:32 -07004947 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.WindowLayout windowLayout) {
Wale Ogunwale935e5022015-11-10 12:36:10 -08004948 if (mTaskPositioner == null) {
4949 return false;
4950 }
Andrii Kulian2e751b82016-03-16 16:59:32 -07004951 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, windowLayout);
Wale Ogunwale935e5022015-11-10 12:36:10 -08004952 return true;
4953 }
4954
Craig Mautnerc00204b2013-03-05 15:02:14 -08004955 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004956 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004957 }
4958
Wale Ogunwale5f986092015-12-04 15:35:38 -08004959 void addTask(final TaskRecord task, final boolean toTop, String reason) {
4960 final ActivityStack prevStack = preAddTask(task, reason);
4961
Craig Mautnerc00204b2013-03-05 15:02:14 -08004962 task.stack = this;
4963 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004964 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004965 } else {
4966 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004967 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004968 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08004969 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004970 }
4971
Wale Ogunwale5f986092015-12-04 15:35:38 -08004972 void positionTask(final TaskRecord task, int position) {
4973 final ActivityStack prevStack = preAddTask(task, "positionTask");
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004974 task.stack = this;
4975 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004976 postAddTask(task, prevStack);
4977 }
4978
4979 private ActivityStack preAddTask(TaskRecord task, String reason) {
4980 final ActivityStack prevStack = task.stack;
4981 if (prevStack != null && prevStack != this) {
4982 prevStack.removeTask(task, reason, MOVING);
4983 }
4984 return prevStack;
4985 }
4986
4987 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
4988 if (prevStack != null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004989 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004990 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004991 try {
4992 task.voiceSession.taskStarted(task.intent, task.taskId);
4993 } catch (RemoteException e) {
4994 }
4995 }
4996 }
4997
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004998 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004999 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08005000 // TODO: VI deal with activity
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07005001 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
5002 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6cae7652015-12-26 07:36:26 -08005003 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
5004 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08005005 task.mResizeMode, r.isAlwaysFocusable(), task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07005006 r.taskConfigOverride = task.mOverrideConfig;
5007 }
5008
Wale Ogunwaled046a012015-12-24 13:05:59 -08005009 void moveToFrontAndResumeStateIfNeeded(
5010 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
5011 if (!moveToFront) {
5012 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07005013 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08005014
5015 // If the activity owns the last resumed activity, transfer that together,
5016 // so that we don't resume the same activity again in the new stack.
5017 // Apps may depend on onResume()/onPause() being called in pairs.
5018 if (setResume) {
5019 mResumedActivity = r;
5020 }
5021 // Move the stack in which we are placing the activity to the front. The call will also
5022 // make sure the activity focus is set.
5023 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005024 }
5025
5026 /**
5027 * Moves the input activity from its current stack to this one.
5028 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
5029 * created on this stack which the activity is added to.
5030 * */
5031 void moveActivityToStack(ActivityRecord r) {
5032 final ActivityStack prevStack = r.task.stack;
5033 if (prevStack.mStackId == mStackId) {
5034 // You are already in the right stack silly...
5035 return;
5036 }
5037
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07005038 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07005039 && (mStackSupervisor.topRunningActivityLocked() == r);
5040 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
5041
5042 final TaskRecord task = createTaskRecord(
Suprabh Shukla09a88f52015-12-02 14:36:31 -08005043 mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
5044 r.info, r.intent, null, null, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07005045 r.setTask(task, null);
5046 task.addActivityToTop(r);
5047 setAppTask(r, task);
Wale Ogunwale22e25262016-02-01 10:32:02 -08005048 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwaled046a012015-12-24 13:05:59 -08005049 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
5050 if (wasResumed) {
5051 prevStack.mResumedActivity = null;
5052 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07005053 }
5054
Wale Ogunwale706ed792015-08-02 10:29:44 -07005055 private void setAppTask(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08005056 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08005057 mWindowManager.setAppTask(r.appToken, task.taskId, mStackId, bounds, task.mOverrideConfig,
5058 task.mResizeMode, task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07005059 r.taskConfigOverride = task.mOverrideConfig;
5060 }
5061
Craig Mautnerc00204b2013-03-05 15:02:14 -08005062 public int getStackId() {
5063 return mStackId;
5064 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07005065
5066 @Override
5067 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07005068 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
5069 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07005070 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08005071
Craig Mautner15df08a2015-04-01 12:17:18 -07005072 void onLockTaskPackagesUpdatedLocked() {
5073 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5074 mTaskHistory.get(taskNdx).setLockTaskAuth();
5075 }
5076 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005077}