blob: b9ff32c5cf46d710f417227f4fba875787eda0af [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
23import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070024import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070025
Wale Ogunwalee23149f2015-03-06 15:39:44 -080026import static com.android.server.am.ActivityManagerDebugConfig.*;
Chong Zhang75b37202015-12-04 14:16:36 -080027import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
Craig Mautner84984fa2014-06-19 11:19:20 -070028import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
29import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070030
Wale Ogunwale39381972015-12-17 17:15:29 -080031import static com.android.server.am.ActivityStackSupervisor.FindTaskResult;
Wale Ogunwale040b4702015-08-06 18:10:50 -070032import static com.android.server.am.ActivityStackSupervisor.MOVING;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070033import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwale040b4702015-08-06 18:10:50 -070034
Winsonc809cbb2015-11-02 12:06:15 -080035import android.graphics.Point;
Wale Ogunwale706ed792015-08-02 10:29:44 -070036import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070037import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070038
Dianne Hackborn91097de2014-04-04 18:02:06 -070039import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080040import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070041import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070042import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080043import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080044import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080045import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080046import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070047import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070048
49import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070050import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070051import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070052import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080054import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080056import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080062import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080063import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070065import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080066import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.os.Handler;
68import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090069import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070071import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070072import android.os.RemoteException;
73import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080074import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070075import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070076import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070079import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070080
Craig Mautnercae015f2013-02-08 14:31:27 -080081import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080082import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070083import java.lang.ref.WeakReference;
84import java.util.ArrayList;
85import java.util.Iterator;
86import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080087import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070088import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070089
90/**
91 * State and management of a single stack of activities.
92 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070093final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080094
Wale Ogunwalee23149f2015-03-06 15:39:44 -080095 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070096 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
97 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080098 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070099 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700100 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700101 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700102 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700103 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700104 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
105 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700106 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700107 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700108 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
109 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
110 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
111 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
112 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700113
114 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800115
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700116 // Ticks during which we check progress while waiting for an app to launch.
117 static final int LAUNCH_TICK = 500;
118
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700119 // How long we wait until giving up on the last activity to pause. This
120 // is short because it directly impacts the responsiveness of starting the
121 // next activity.
122 static final int PAUSE_TIMEOUT = 500;
123
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700124 // How long we wait for the activity to tell us it has stopped before
125 // giving up. This is a good amount of time because we really need this
126 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700127 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700128
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700129 // How long we wait until giving up on an activity telling us it has
130 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700131 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800132
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700133 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800134 // disabled.
135 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800136
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700137 // How long between activity launches that we consider safe to not warn
138 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700139 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800140
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700141 // Set to false to disable the preview that is shown while a new activity
142 // is being started.
143 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800144
Craig Mautner5eda9b32013-07-02 11:58:16 -0700145 // How long to wait for all background Activities to redraw following a call to
146 // convertToTranslucent().
147 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
148
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800149 // How many activities have to be scheduled to stop to force a stop pass.
150 private static final int MAX_STOPPING_TO_FORCE = 3;
151
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700152 enum ActivityState {
153 INITIALIZING,
154 RESUMED,
155 PAUSING,
156 PAUSED,
157 STOPPING,
158 STOPPED,
159 FINISHING,
160 DESTROYING,
161 DESTROYED
162 }
163
164 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700165 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800166 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800167
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700168 /**
169 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800170 * running) activities. It contains #TaskRecord objects.
171 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800172 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800173
174 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800175 * Used for validating app tokens with window manager.
176 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800177 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800178
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700179 /**
180 * List of running activities, sorted by recent usage.
181 * The first entry in the list is the least recently used.
182 * It contains HistoryRecord objects.
183 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800184 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700185
186 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700187 * Animations that for the current transition have requested not to
188 * be considered for the transition animation.
189 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800190 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700191
192 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700193 * When we are in the process of pausing an activity, before starting the
194 * next one, this variable holds the activity that is currently being paused.
195 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800196 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700197
198 /**
199 * This is the last activity that we put into the paused state. This is
200 * used to determine if we need to do an activity transition while sleeping,
201 * when we normally hold the top activity paused.
202 */
203 ActivityRecord mLastPausedActivity = null;
204
205 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700206 * Activities that specify No History must be removed once the user navigates away from them.
207 * If the device goes to sleep with such an activity in the paused state then we save it here
208 * and finish it later if another activity replaces it on wakeup.
209 */
210 ActivityRecord mLastNoHistoryActivity = null;
211
212 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700213 * Current activity that is resumed, or null if there is none.
214 */
215 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800216
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700217 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700218 * This is the last activity that has been started. It is only used to
219 * identify when multiple activities are started at once so that the user
220 * can be warned they may not be in the activity they think they are.
221 */
222 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800223
Craig Mautner5eda9b32013-07-02 11:58:16 -0700224 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
225 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
226 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
227 // Activity in mTranslucentActivityWaiting is notified via
228 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
229 // background activity being drawn then the same call will be made with a true value.
230 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700231 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700232
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700233 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700234 * Set when we know we are going to be calling updateConfiguration()
235 * soon, so want to skip intermediate config checks.
236 */
237 boolean mConfigWillChange;
238
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700239 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800240 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700241 // Current bounds of the stack or null if fullscreen.
242 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800243
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700244 long mLaunchStartTime = 0;
245 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800246
Craig Mautner858d8a62013-04-23 17:08:34 -0700247 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700248
Craig Mautnerc00204b2013-03-05 15:02:14 -0800249 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800250 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800251 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
252 ArrayList<ActivityStack> mStacks;
253 /** The attached Display's unique identifier, or -1 if detached */
254 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800255
Craig Mautner27084302013-03-25 08:05:25 -0700256 /** Run all ActivityStacks through this */
257 final ActivityStackSupervisor mStackSupervisor;
258
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700259 private final LaunchingTaskPositioner mTaskPositioner;
260
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700261 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700262 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
263 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
264 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
265 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700266 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700267 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700268 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700269
270 static class ScheduleDestroyArgs {
271 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700272 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700273 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700274 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700275 mReason = reason;
276 }
277 }
278
Zoran Marcetaf958b322012-08-09 20:27:12 +0900279 final Handler mHandler;
280
281 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800282
Craig Mautnerc8143c62013-09-03 12:15:57 -0700283 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900284 super(looper);
285 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700286
Zoran Marcetaf958b322012-08-09 20:27:12 +0900287 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 public void handleMessage(Message msg) {
289 switch (msg.what) {
290 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800291 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700292 // We don't at this point know if the activity is fullscreen,
293 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800294 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700295 synchronized (mService) {
296 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700297 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700298 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700299 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800300 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700301 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700302 case LAUNCH_TICK_MSG: {
303 ActivityRecord r = (ActivityRecord)msg.obj;
304 synchronized (mService) {
305 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700306 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700307 }
308 }
309 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700310 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800311 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700312 // We don't at this point know if the activity is fullscreen,
313 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800314 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800315 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800316 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800317 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700318 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700319 case STOP_TIMEOUT_MSG: {
320 ActivityRecord r = (ActivityRecord)msg.obj;
321 // We don't at this point know if the activity is fullscreen,
322 // so we need to be conservative and assume it isn't.
323 Slog.w(TAG, "Activity stop timeout for " + r);
324 synchronized (mService) {
325 if (r.isInHistory()) {
326 activityStoppedLocked(r, null, null, null);
327 }
328 }
329 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700330 case DESTROY_ACTIVITIES_MSG: {
331 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
332 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700333 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700334 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700335 } break;
336 case TRANSLUCENT_TIMEOUT_MSG: {
337 synchronized (mService) {
338 notifyActivityDrawnLocked(null);
339 }
340 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700341 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700342 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700343 final ActivityRecord r = getVisibleBehindActivity();
344 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700345 if (r != null) {
346 mService.killAppAtUsersRequest(r.app, null);
347 }
348 }
349 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700350 }
351 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800352 }
353
Craig Mautner34b73df2014-01-12 21:11:08 -0800354 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800355 int count = 0;
356 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
357 count += mTaskHistory.get(taskNdx).mActivities.size();
358 }
359 return count;
360 }
361
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800362 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
363 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800364 mActivityContainer = activityContainer;
365 mStackSupervisor = activityContainer.getOuter();
366 mService = mStackSupervisor.mService;
367 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
368 mWindowManager = mService.mWindowManager;
369 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700370 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800371 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700372 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
373 ? new LaunchingTaskPositioner() : null;
374 }
375
376 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
377 mDisplayId = activityDisplay.mDisplayId;
378 mStacks = activityDisplay.mStacks;
379 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
380 mFullscreen = mBounds == null;
381 if (mTaskPositioner != null) {
382 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
383 mTaskPositioner.configure(mBounds);
384 }
385 }
386
387 void detachDisplay() {
388 mDisplayId = Display.INVALID_DISPLAY;
389 mStacks = null;
390 if (mTaskPositioner != null) {
391 mTaskPositioner.reset();
392 }
393 mWindowManager.detachStack(mStackId);
394 }
395
Winsonc809cbb2015-11-02 12:06:15 -0800396 public void getDisplaySize(Point out) {
397 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
398 }
399
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700400 void setBounds(Rect bounds) {
401 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700402 if (mTaskPositioner != null) {
403 mTaskPositioner.configure(bounds);
404 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700405 }
Craig Mautner5962b122012-10-05 14:45:52 -0700406
Amith Yamasani734983f2014-03-04 16:48:05 -0800407 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700408 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700409 }
410
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700411 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800412 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700413 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700414 if (r != null) {
415 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800416 }
417 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700418 return null;
419 }
420
421 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800422 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
423 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800424 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800425 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
426 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800427 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800428 return r;
429 }
430 }
431 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700432 return null;
433 }
434
435 /**
436 * This is a simplified version of topRunningActivityLocked that provides a number of
437 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800438 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700439 * @param token If non-null, any history records matching this token will be skipped.
440 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800441 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700442 * @return Returns the HistoryRecord of the next activity on the stack.
443 */
444 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800445 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
446 TaskRecord task = mTaskHistory.get(taskNdx);
447 if (task.taskId == taskId) {
448 continue;
449 }
450 ArrayList<ActivityRecord> activities = task.mActivities;
451 for (int i = activities.size() - 1; i >= 0; --i) {
452 final ActivityRecord r = activities.get(i);
453 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800454 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800455 return r;
456 }
457 }
458 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700459 return null;
460 }
461
Craig Mautner8849a5e2013-04-02 16:41:03 -0700462 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700463 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
464 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700465 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
466 final ActivityRecord r = activities.get(activityNdx);
467 if (!r.finishing) {
468 return r;
469 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700470 }
471 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700472 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700473 }
474
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700475 final TaskRecord topTask() {
476 final int size = mTaskHistory.size();
477 if (size > 0) {
478 return mTaskHistory.get(size - 1);
479 }
480 return null;
481 }
482
Craig Mautnerd2328952013-03-05 12:46:26 -0800483 TaskRecord taskForIdLocked(int id) {
484 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
485 final TaskRecord task = mTaskHistory.get(taskNdx);
486 if (task.taskId == id) {
487 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800488 }
489 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700490 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700491 }
492
Craig Mautnerd2328952013-03-05 12:46:26 -0800493 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700494 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800495 return isInStackLocked(r);
496 }
497
498 ActivityRecord isInStackLocked(ActivityRecord r) {
499 if (r == null) {
500 return null;
501 }
502 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700503 if (task != null && task.stack != null
504 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800505 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800506 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800507 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800508 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800509 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800510 }
511
Craig Mautner2420ead2013-04-01 17:13:20 -0700512 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700513 final boolean hadit = mLRUActivities.remove(r);
514 mLRUActivities.add(r);
515 return hadit;
516 }
517
Craig Mautnerde4ef022013-04-07 19:01:33 -0700518 final boolean isHomeStack() {
519 return mStackId == HOME_STACK_ID;
520 }
521
Winson Chung0583d3d2015-12-18 10:03:12 -0500522 final boolean isDockedStack() {
523 return mStackId == DOCKED_STACK_ID;
524 }
525
526 final boolean isPinnedStack() {
527 return mStackId == PINNED_STACK_ID;
528 }
529
Craig Mautnere0a38842013-12-16 16:14:02 -0800530 final boolean isOnHomeDisplay() {
531 return isAttached() &&
532 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
533 }
534
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700535 void moveToFront(String reason) {
536 moveToFront(reason, null);
537 }
538
539 /**
540 * @param reason The reason for moving the stack to the front.
541 * @param task If non-null, the task will be moved to the top of the stack.
542 * */
543 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700544 if (!isAttached()) {
545 return;
546 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700547
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700548 mStacks.remove(this);
549 int addIndex = mStacks.size();
550
551 if (addIndex > 0) {
552 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800553 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
554 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700555 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800556 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700557 }
558
559 mStacks.add(addIndex, this);
560
561 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
562 if (isOnHomeDisplay()) {
563 mStackSupervisor.setFocusStack(reason, this);
564 }
565 if (task != null) {
566 insertTaskAtTop(task, null);
567 } else {
568 task = topTask();
569 }
570 if (task != null) {
571 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800572 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800573 }
574
Wale Ogunwaled046a012015-12-24 13:05:59 -0800575 boolean isFocusable() {
576 return StackId.canReceiveKeys(mStackId);
577 }
578
Craig Mautnere0a38842013-12-16 16:14:02 -0800579 final boolean isAttached() {
580 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800581 }
582
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700583 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800584 * Returns the top activity in any existing task matching the given Intent in the input result.
585 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700586 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800587 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700588 Intent intent = target.intent;
589 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700590 ComponentName cls = intent.getComponent();
591 if (info.targetActivity != null) {
592 cls = new ComponentName(info.packageName, info.targetActivity);
593 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700594 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700595 boolean isDocument = intent != null & intent.isDocument();
596 // If documentData is non-null then it must match the existing task data.
597 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800598
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700599 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800600 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
601 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700602 if (task.voiceSession != null) {
603 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700604 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700605 continue;
606 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700607 if (task.userId != userId) {
608 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700609 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700610 continue;
611 }
Craig Mautner000f0022013-02-26 15:04:29 -0800612 final ActivityRecord r = task.getTopActivity();
613 if (r == null || r.finishing || r.userId != userId ||
614 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700615 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800616 continue;
617 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700618 if (r.mActivityType != target.mActivityType) {
619 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
620 continue;
621 }
Craig Mautner000f0022013-02-26 15:04:29 -0800622
Craig Mautnerd00f4742014-03-12 14:17:26 -0700623 final Intent taskIntent = task.intent;
624 final Intent affinityIntent = task.affinityIntent;
625 final boolean taskIsDocument;
626 final Uri taskDocumentData;
627 if (taskIntent != null && taskIntent.isDocument()) {
628 taskIsDocument = true;
629 taskDocumentData = taskIntent.getData();
630 } else if (affinityIntent != null && affinityIntent.isDocument()) {
631 taskIsDocument = true;
632 taskDocumentData = affinityIntent.getData();
633 } else {
634 taskIsDocument = false;
635 taskDocumentData = null;
636 }
637
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700638 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700639 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700640 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700641 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Wale Ogunwale39381972015-12-17 17:15:29 -0800642 if (!isDocument && !taskIsDocument
643 && result.r == null && task.canMatchRootAffinity()) {
Dianne Hackborn79228822014-09-16 11:11:23 -0700644 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwale39381972015-12-17 17:15:29 -0800645 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
646 // It is possible for multiple tasks to have the same root affinity especially
647 // if they are in separate stacks. We save off this candidate, but keep looking
648 // to see if there is a better candidate.
649 result.r = r;
650 result.matchedByRootAffinity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700651 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700652 } else if (taskIntent != null && taskIntent.getComponent() != null &&
653 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700654 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700655 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800656 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700657 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
658 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800659 result.r = r;
660 result.matchedByRootAffinity = false;
661 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700662 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
663 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700664 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700665 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800666 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700667 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
668 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800669 result.r = r;
670 result.matchedByRootAffinity = false;
671 break;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700672 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700673 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700674 }
675
676 /**
677 * Returns the first activity (starting from the top of the stack) that
678 * is the same as the given activity. Returns null if no such activity
679 * is found.
680 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700681 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700682 ComponentName cls = intent.getComponent();
683 if (info.targetActivity != null) {
684 cls = new ComponentName(info.packageName, info.targetActivity);
685 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700686 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700687
Craig Mautner000f0022013-02-26 15:04:29 -0800688 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700689 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700690 final boolean notCurrentUserTask =
691 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700692 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700693
Craig Mautner000f0022013-02-26 15:04:29 -0800694 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
695 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700696 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700697 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700698 }
Craig Mautner000f0022013-02-26 15:04:29 -0800699 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700700 return r;
701 }
702 }
703 }
704
705 return null;
706 }
707
Amith Yamasani742a6712011-05-04 14:49:28 -0700708 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700709 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700710 */
Craig Mautner93529a42013-10-04 15:03:13 -0700711 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800712 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700713 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800714 }
715 mCurrentUser = userId;
716
717 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800718 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800719 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700720 final TaskRecord task = mTaskHistory.get(i);
721
722 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
723 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700724 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700725 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700726 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700727 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800728 mTaskHistory.remove(i);
729 mTaskHistory.add(task);
730 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800731 // Use same value for i.
732 } else {
733 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800734 }
735 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700736 if (VALIDATE_TOKENS) {
737 validateAppTokensLocked();
738 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700739 }
740
Craig Mautner2420ead2013-04-01 17:13:20 -0700741 void minimalResumeActivityLocked(ActivityRecord r) {
742 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700743 if (DEBUG_STATES) Slog.v(TAG_STATES,
744 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700745 r.stopped = false;
746 mResumedActivity = r;
747 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800748 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700749 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700750 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700751 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700752 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
753 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700754 }
755
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800756 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700757 if (r != null) {
758 mRecentTasks.addLocked(r.task);
759 r.task.touchActiveTime();
760 }
761 }
762
Narayan Kamath7829c812015-06-08 17:39:43 +0100763 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700764 if (mFullyDrawnStartTime != 0) {
765 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
766 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100767 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700768 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
769 }
770
771 private void stopFullyDrawnTraceIfNeeded() {
772 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
773 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
774 mFullyDrawnStartTime = 0;
775 }
776 }
777
Craig Mautnere79d42682013-04-01 19:01:53 -0700778 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700779 if (r.displayStartTime == 0) {
780 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
781 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100782 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700783 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700784 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700785 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100786 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700787 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700788 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700789 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800790
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700791 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700792 // Make sure that there is no activity waiting for this to launch.
793 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
794 r.displayStartTime = r.fullyDrawnStartTime = 0;
795 } else {
796 mStackSupervisor.removeTimeoutsForActivityLocked(r);
797 mStackSupervisor.scheduleIdleTimeoutLocked(r);
798 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700799 }
800
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800801 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800802 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800803 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
804 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
805 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
806 activities.get(activityNdx).setSleeping(false);
807 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800808 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800809 if (mPausingActivity != null) {
810 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
811 activityPausedLocked(mPausingActivity.appToken, true);
812 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800813 }
814
Craig Mautner0eea92c2013-05-16 13:35:39 -0700815 /**
816 * @return true if something must be done before going to sleep.
817 */
818 boolean checkReadyForSleepLocked() {
819 if (mResumedActivity != null) {
820 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700821 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
822 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
823 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700824 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700825 return true;
826 }
827 if (mPausingActivity != null) {
828 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700829 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700830 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800831 }
832
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700833 if (hasVisibleBehindActivity()) {
834 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800835 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700836 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800837 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700838 return true;
839 }
840
Craig Mautner0eea92c2013-05-16 13:35:39 -0700841 return false;
842 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800843
Craig Mautner0eea92c2013-05-16 13:35:39 -0700844 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700845 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800846
Craig Mautner0eea92c2013-05-16 13:35:39 -0700847 // Make sure any stopped but visible activities are now sleeping.
848 // This ensures that the activity's onStop() is called.
849 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
850 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
851 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
852 final ActivityRecord r = activities.get(activityNdx);
853 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
854 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800855 }
856 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800857 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700858 }
Craig Mautner59c00972012-07-30 12:10:24 -0700859
Winson8b1871d2015-11-20 09:56:20 -0800860 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
861 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800862 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700863 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800864 return null;
865 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800866
Winson Chung083baf92014-07-11 10:32:42 -0700867 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700868 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800869 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700870 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700871 return null;
872 }
873
Winson Chung48a10a52014-08-27 14:36:51 -0700874 int w = mService.mThumbnailWidth;
875 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800876 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800877 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700878 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800879
880 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800881 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
882 // use within SystemUI while keeping memory usage low.
Winson Chungead5c0f2015-12-14 11:18:57 -0500883 if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
Winson8b1871d2015-11-20 09:56:20 -0800884 w = h = -1;
Winson387aac62015-11-25 11:18:56 -0800885 scale = 0.5f;
Winson8b1871d2015-11-20 09:56:20 -0800886 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800887 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800888 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800889 }
Winson Chung376543b2014-05-21 17:43:28 -0700890 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800891 return null;
892 }
893
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700894 /**
895 * Start pausing the currently resumed activity. It is an error to call this if there
896 * is already an activity being paused or there is no resumed activity.
897 *
898 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
899 * @param uiSleeping True if this is happening with the user interface going to sleep (the
900 * screen turning off).
901 * @param resuming True if this is being called as part of resuming the top activity, so
902 * we shouldn't try to instigate a resume here.
903 * @param dontWait True if the caller does not want to wait for the pause to complete. If
904 * set to true, we will immediately complete the pause here before returning.
905 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
906 * it to tell us when it is done.
907 */
908 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
909 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800910 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800911 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
912 + " state=" + mPausingActivity.state);
913 if (!mService.isSleeping()) {
914 // Avoid recursion among check for sleep and complete pause during sleeping.
915 // Because activity will be paused immediately after resume, just let pause
916 // be completed by the order of activity paused from clients.
917 completePauseLocked(false);
918 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800919 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700920 ActivityRecord prev = mResumedActivity;
921 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700922 if (!resuming) {
923 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -0800924 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700925 }
926 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700927 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800928
929 if (mActivityContainer.mParentActivity == null) {
930 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700931 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800932 }
933
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700934 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700935 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700936 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800937 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700938 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700939 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
940 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700941 prev.state = ActivityState.PAUSING;
942 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700943 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700944 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700945 if (mService.mHasRecents
946 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800947 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700948 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700949 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700950
951 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800952
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700953 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700954 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700955 try {
956 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700957 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700958 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700959 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800960 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700961 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700962 } catch (Exception e) {
963 // Ignore exception, if process died other code will cleanup.
964 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800965 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700966 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700967 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700968 }
969 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800970 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700971 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700972 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700973 }
974
975 // If we are not going to sleep, we want to ensure the device is
976 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700977 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700978 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700979 }
980
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800981 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700982 // Have the window manager pause its key dispatching until the new
983 // activity has started. If we're pausing the activity just because
984 // the screen is being turned off and the UI is sleeping, don't interrupt
985 // key dispatch; the same activity will pick it up again on wakeup.
986 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800987 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700988 } else if (DEBUG_PAUSE) {
989 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700990 }
991
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700992 if (dontWait) {
993 // If the caller said they don't want to wait for the pause, then complete
994 // the pause now.
995 completePauseLocked(false);
996 return false;
997
998 } else {
999 // Schedule a pause timeout in case the app doesn't respond.
1000 // We don't give it much time because this directly impacts the
1001 // responsiveness seen by the user.
1002 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1003 msg.obj = prev;
1004 prev.pauseTime = SystemClock.uptimeMillis();
1005 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001006 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001007 return true;
1008 }
1009
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001010 } else {
1011 // This activity failed to schedule the
1012 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001013 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001014 if (!resuming) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001015 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001016 }
1017 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001018 }
1019 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001020
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001021 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001022 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1023 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001024
Craig Mautnerd2328952013-03-05 12:46:26 -08001025 final ActivityRecord r = isInStackLocked(token);
1026 if (r != null) {
1027 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1028 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001029 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001030 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001031 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -08001032 } else {
1033 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1034 r.userId, System.identityHashCode(r), r.shortComponentName,
1035 mPausingActivity != null
1036 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001037 if (r.state == ActivityState.PAUSING) {
1038 r.state = ActivityState.PAUSED;
1039 if (r.finishing) {
1040 if (DEBUG_PAUSE) Slog.v(TAG,
1041 "Executing finish of failed to pause activity: " + r);
1042 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1043 }
louis_chang047dfd42015-04-08 16:35:55 +08001044 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001045 }
1046 }
1047 }
1048
Craig Mautnera0026042014-04-23 11:45:37 -07001049 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1050 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001051 if (r.state != ActivityState.STOPPING) {
1052 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1053 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1054 return;
1055 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001056 if (persistentState != null) {
1057 r.persistentState = persistentState;
1058 mService.notifyTaskPersisterLocked(r.task, false);
1059 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001060 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001061 if (icicle != null) {
1062 // If icicle is null, this is happening due to a timeout, so we
1063 // haven't really saved the state.
1064 r.icicle = icicle;
1065 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001066 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001067 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001068 }
1069 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001070 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001071 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1072 r.stopped = true;
1073 r.state = ActivityState.STOPPED;
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001074 if (getVisibleBehindActivity() == r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001075 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001076 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001077 if (r.finishing) {
1078 r.clearOptionsLocked();
1079 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001080 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001081 destroyActivityLocked(r, true, "stop-config");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001082 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001083 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001084 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001085 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001086 }
1087 }
1088 }
1089
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001090 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001091 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001092 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001093
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001094 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001095 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001096 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001097 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001098 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001099 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001100 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001101 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001102 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001103 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1104 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001105 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001106 if (prev.configDestroy) {
1107 // The previous is being paused because the configuration
1108 // is changing, which means it is actually stopping...
1109 // To juggle the fact that we are also starting a new
1110 // instance right now, we need to first completely stop
1111 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001112 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001113 destroyActivityLocked(prev, true, "pause-config");
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001114 } else if (wasStopping) {
1115 // We are also stopping, the stop request must have gone soon after the pause.
1116 // We can't clobber it, because the stop confirmation will not be handled.
1117 // We don't need to schedule another stop, we only need to let it happen.
1118 prev.state = ActivityState.STOPPING;
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001119 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001120 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001121 // stopping.
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001122 addToStopping(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001123 }
1124 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001125 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001126 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001127 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001128 // It is possible the activity was freezing the screen before it was paused.
1129 // In that case go ahead and remove the freeze this activity has on the screen
1130 // since it is no longer visible.
1131 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001132 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001133 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001134
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001135 if (resumeNext) {
1136 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1137 if (!mService.isSleepingOrShuttingDown()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001138 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001139 } else {
1140 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001141 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001142 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001143 // If there are no more activities available to run, do resume anyway to start
1144 // something. Also if the top activity on the stack is not the just paused
1145 // activity, we need to go ahead and resume it to ensure we complete an
1146 // in-flight app switch.
1147 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001148 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001149 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001150 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001151
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001152 if (prev != null) {
1153 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001154
Craig Mautner525f3d92013-05-07 14:01:50 -07001155 if (prev.app != null && prev.cpuTimeAtResume > 0
1156 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001157 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1158 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001159 if (diff > 0) {
1160 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1161 synchronized (bsi) {
1162 BatteryStatsImpl.Uid.Proc ps =
1163 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001164 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001165 if (ps != null) {
1166 ps.addForegroundTimeLocked(diff);
1167 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001168 }
1169 }
1170 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001171 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001172 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001173
1174 // Notfiy when the task stack has changed
1175 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001176 }
1177
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001178 private void addToStopping(ActivityRecord r) {
1179 mStackSupervisor.mStoppingActivities.add(r);
1180 if (mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE ||
1181 r.frontOfTask && mTaskHistory.size() <= 1) {
1182 // If we already have a few activities waiting to stop,
1183 // then give up on things going idle and start clearing
1184 // them out. Or if r is the last of activity of the last task the stack
1185 // will be empty and must be cleared immediately.
1186 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
1187 mStackSupervisor.scheduleIdleLocked();
1188 } else {
1189 mStackSupervisor.checkReadyForSleepLocked();
1190 }
1191 }
1192
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001193 /**
1194 * Once we know that we have asked an application to put an activity in
1195 * the resumed state (either by launching it or explicitly telling it),
1196 * this function updates the rest of our state to match that fact.
1197 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001198 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001199 next.idle = false;
1200 next.results = null;
1201 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001202
Chong Zhang85ee6542015-10-02 13:36:38 -07001203 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001204 ProcessRecord app = next.task.mActivities.get(0).app;
1205 if (app != null && app != mService.mHomeProcess) {
1206 mService.mHomeProcess = app;
1207 }
1208 }
1209
Craig Mautner07566322013-09-26 16:42:55 -07001210 if (next.nowVisible) {
1211 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001212 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001213 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001214
1215 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001216 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001217
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001218 mStackSupervisor.reportResumedActivityLocked(next);
1219
1220 next.resumeKeyDispatchingLocked();
1221 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001222
1223 // Mark the point when the activity is resuming
1224 // TODO: To be more accurate, the mark should be before the onCreate,
1225 // not after the onResume. But for subsequent starts, onResume is fine.
1226 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001227 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001228 } else {
1229 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1230 }
Winson Chung376543b2014-05-21 17:43:28 -07001231
George Mount6ba042b2014-07-28 11:12:28 -07001232 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001233
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001234 if (getVisibleBehindActivity() == next) {
Craig Mautner64ccb702014-10-01 09:38:40 -07001235 // When resuming an activity, require it to call requestVisibleBehind() again.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001236 setVisibleBehindActivity(null);
Craig Mautner64ccb702014-10-01 09:38:40 -07001237 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001238 }
1239
Craig Mautner2568c3a2015-03-26 14:22:34 -07001240 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001241 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001242 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1243 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1244 r.mUpdateTaskThumbnailWhenHidden = false;
1245 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001246 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001247 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001248 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001249 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001250 container.setVisible(visible);
1251 }
1252 }
1253
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001254 // Find the first visible activity above the passed activity and if it is translucent return it
1255 // otherwise return null;
1256 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1257 TaskRecord task = r.task;
1258 if (task == null) {
1259 return null;
1260 }
1261
1262 ActivityStack stack = task.stack;
1263 if (stack == null) {
1264 return null;
1265 }
1266
1267 int stackNdx = mStacks.indexOf(stack);
1268
1269 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1270 int taskNdx = tasks.indexOf(task);
1271
1272 ArrayList<ActivityRecord> activities = task.mActivities;
1273 int activityNdx = activities.indexOf(r) + 1;
1274
1275 final int numStacks = mStacks.size();
1276 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001277 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001278 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001279 final int numTasks = tasks.size();
1280 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001281 final TaskRecord currentTask = tasks.get(taskNdx);
1282 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001283 final int numActivities = activities.size();
1284 while (activityNdx < numActivities) {
1285 final ActivityRecord activity = activities.get(activityNdx);
1286 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001287 return historyStack.mFullscreen
1288 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001289 }
1290 ++activityNdx;
1291 }
1292 activityNdx = 0;
1293 ++taskNdx;
1294 }
1295 taskNdx = 0;
1296 ++stackNdx;
1297 }
1298
1299 return null;
1300 }
1301
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001302 private ActivityStack getNextVisibleStackLocked() {
1303 ArrayList<ActivityStack> stacks = mStacks;
1304 final ActivityRecord parent = mActivityContainer.mParentActivity;
1305 if (parent != null) {
1306 stacks = parent.task.stack.mStacks;
1307 }
1308 if (stacks != null) {
1309 for (int i = stacks.size() - 1; i >= 0; --i) {
1310 ActivityStack stack = stacks.get(i);
1311 if (stack != this && stack.isStackVisibleLocked()) {
1312 return stack;
1313 }
1314 }
1315 }
1316 return null;
1317 }
1318
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001319 /** Returns true if the stack contains a fullscreen task. */
1320 private boolean hasFullscreenTask() {
1321 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1322 final TaskRecord task = mTaskHistory.get(i);
1323 if (task.mFullscreen) {
1324 return true;
1325 }
1326 }
1327 return false;
1328 }
1329
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001330 private boolean hasTranslucentActivity(ActivityStack stack) {
1331 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1332 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1333 final TaskRecord task = tasks.get(taskNdx);
1334 final ArrayList<ActivityRecord> activities = task.mActivities;
1335 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1336 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001337
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001338 // Conditions for an activity to obscure the stack we're
1339 // examining:
1340 // 1. Not Finishing AND Visible AND:
1341 // 2. Either:
1342 // - Full Screen Activity OR
1343 // - On top of Home and our stack is NOT home
1344 if (!r.finishing && r.visible && (r.fullscreen ||
1345 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1346 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001347 }
1348 }
1349 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001350 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001351 }
1352
1353 /** Returns true if the stack is considered visible. */
Filip Gruszczynski77d94482015-12-11 13:59:52 -08001354 boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001355 if (!isAttached()) {
1356 return false;
1357 }
1358
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001359 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001360 return true;
1361 }
1362
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001363 final int stackIndex = mStacks.indexOf(this);
1364
1365 if (stackIndex == mStacks.size() - 1) {
1366 Slog.wtf(TAG,
1367 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1368 return false;
1369 }
1370
Chong Zhang75b37202015-12-04 14:16:36 -08001371 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1372 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
1373 return false;
1374 }
1375
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001376 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1377 final int focusedStackId = focusedStack.mStackId;
1378
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001379 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1380 && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID) {
1381 // The fullscreen stack should be visible if it has a visible behind activity behind
1382 // the home stack that will be translucent.
1383 return true;
1384 }
1385
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001386 if (mStackId == DOCKED_STACK_ID) {
1387 // Docked stack is always visible, except in the case where the home activity
1388 // is the top running activity in the focused home stack.
1389 if (focusedStackId != HOME_STACK_ID) {
1390 return true;
1391 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001392 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked();
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001393 return topHomeActivity == null || !topHomeActivity.isHomeActivity();
1394 }
1395
Chong Zhangb16cf342015-11-12 17:22:40 -08001396 // Find the first stack below focused stack that actually got something visible.
1397 int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1398 while (belowFocusedIndex >= 0 &&
1399 mStacks.get(belowFocusedIndex).topRunningActivityLocked() == null) {
1400 belowFocusedIndex--;
1401 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001402 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1403 && stackIndex == belowFocusedIndex) {
1404 // Stacks directly behind the docked or pinned stack are always visible.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001405 return true;
1406 }
1407
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001408 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1409 && hasTranslucentActivity(focusedStack)) {
1410 // Stacks behind the fullscreen stack with a translucent activity are always
1411 // visible so they can act as a backdrop to the translucent activity.
1412 // For example, dialog activities
1413 if (stackIndex == belowFocusedIndex) {
1414 return true;
1415 }
1416 if (belowFocusedIndex >= 0) {
1417 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001418 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1419 && stackIndex == (belowFocusedIndex - 1)) {
1420 // The stack behind the docked or pinned stack is also visible so we can have a
1421 // complete backdrop to the translucent activity when the docked stack is up.
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001422 return true;
1423 }
1424 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001425 }
1426
Wale Ogunwale3797c222015-10-27 14:21:58 -07001427 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001428 // Visibility of any static stack should have been determined by the conditions above.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001429 return false;
1430 }
1431
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001432 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001433 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001434
1435 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1436 continue;
1437 }
1438
Wale Ogunwale3797c222015-10-27 14:21:58 -07001439 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001440 // These stacks can't have any dynamic stacks visible behind them.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001441 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001442 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001443
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001444 if (!hasTranslucentActivity(stack)) {
1445 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001446 }
1447 }
1448
1449 return true;
1450 }
1451
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001452 final int rankTaskLayers(int baseLayer) {
1453 int layer = 0;
1454 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1455 final TaskRecord task = mTaskHistory.get(taskNdx);
1456 ActivityRecord r = task.topRunningActivityLocked();
1457 if (r == null || r.finishing || !r.visible) {
1458 task.mLayerRank = -1;
1459 } else {
1460 task.mLayerRank = baseLayer + layer++;
1461 }
1462 }
1463 return layer;
1464 }
1465
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001466 /**
1467 * Make sure that all activities that need to be visible (that is, they
1468 * currently can be seen by the user) actually are.
1469 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001470 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1471 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001472 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001473 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001474 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001475 if (top != null) {
1476 checkTranslucentActivityWaiting(top);
1477 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001478
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001479 // If the top activity is not fullscreen, then we need to
1480 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001481 boolean aboveTop = top != null;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001482 final boolean stackInvisible = !isStackVisibleLocked();
1483 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaled046a012015-12-24 13:05:59 -08001484 boolean resumeNextActivity = isFocusable() && (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001485
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001486 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001487 final TaskRecord task = mTaskHistory.get(taskNdx);
1488 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001489
Craig Mautnerd44711d2013-02-23 11:24:36 -08001490 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1491 final ActivityRecord r = activities.get(activityNdx);
1492 if (r.finishing) {
1493 continue;
1494 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001495 final boolean isTop = r == top;
1496 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001497 continue;
1498 }
1499 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001500 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1501 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001502 if ((!behindFullscreenActivity || r.mLaunchTaskBehind)
1503 && okToShowLocked(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001504 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1505 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001506 + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001507 // First: if this is not the current activity being started, make
1508 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001509 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001510 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001511 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001512
1513 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001514 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001515 resumeNextActivity, r)) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001516 if (activityNdx >= activities.size()) {
1517 // Record may be removed if its process needs to restart.
1518 activityNdx = activities.size() - 1;
1519 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001520 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001521 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001522 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001523 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001524 // If this activity is already visible, then there is nothing to do here.
1525 if (handleAlreadyVisible(r)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001526 resumeNextActivity = false;
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001527 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001528 } else {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001529 makeVisible(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001530 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001531 // Aggregate current change flags.
1532 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001533 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1534 behindFullscreenActivity, task, r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001535 } else {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001536 makeInvisible(stackInvisible, behindFullscreenActivity, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001537 }
1538 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001539 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1540 // The visibility of tasks and the activities they contain in freeform stack are
1541 // determined individually unlike other stacks where the visibility or fullscreen
1542 // status of an activity in a previous task affects other.
1543 behindFullscreenActivity = stackInvisible;
1544 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001545 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001546
1547 if (mTranslucentActivityWaiting != null &&
1548 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1549 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1550 notifyActivityDrawnLocked(null);
1551 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001552 }
Craig Mautner58547802013-03-05 08:23:53 -08001553
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001554 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1555 if (mTranslucentActivityWaiting != top) {
1556 mUndrawnActivitiesBelowTopTranslucent.clear();
1557 if (mTranslucentActivityWaiting != null) {
1558 // Call the callback with a timeout indication.
1559 notifyActivityDrawnLocked(null);
1560 mTranslucentActivityWaiting = null;
1561 }
1562 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1563 }
1564 }
1565
1566 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001567 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001568 // We need to make sure the app is running if it's the top, or it is just made visible from
1569 // invisible. If the app is already visible, it must have died while it was visible. In this
1570 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1571 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001572 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001573 // This activity needs to be visible, but isn't even running...
1574 // get it started and resume if no other stack in this stack is resumed.
1575 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1576 if (r != starting) {
1577 r.startFreezingScreenLocked(r.app, configChanges);
1578 }
1579 if (!r.visible || r.mLaunchTaskBehind) {
1580 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1581 setVisible(r, true);
1582 }
1583 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001584 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001585 return true;
1586 }
1587 }
1588 return false;
1589 }
1590
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001591 private void makeInvisible(boolean stackInvisible, boolean behindFullscreenActivity,
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001592 ActivityRecord r) {
1593 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r + " finishing="
1594 + r.finishing + " state=" + r.state + " stackInvisible=" + stackInvisible
1595 + " behindFullscreenActivity=" + behindFullscreenActivity);
1596 if (!r.visible) {
1597 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1598 return;
1599 }
1600 // Now for any activities that aren't visible to the user, make sure they no longer are
1601 // keeping the screen frozen.
1602 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
1603 try {
1604 setVisible(r, false);
1605 switch (r.state) {
1606 case STOPPING:
1607 case STOPPED:
1608 if (r.app != null && r.app.thread != null) {
1609 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1610 "Scheduling invisibility: " + r);
1611 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1612 }
1613 break;
1614
1615 case INITIALIZING:
1616 case RESUMED:
1617 case PAUSING:
1618 case PAUSED:
1619 // This case created for transitioning activities from
1620 // translucent to opaque {@link Activity#convertToOpaque}.
1621 if (getVisibleBehindActivity() == r) {
1622 releaseBackgroundResources(r);
1623 } else {
1624 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1625 mStackSupervisor.mStoppingActivities.add(r);
1626 }
1627 mStackSupervisor.scheduleIdleLocked();
1628 }
1629 break;
1630
1631 default:
1632 break;
1633 }
1634 } catch (Exception e) {
1635 // Just skip on any failure; we'll make it visible when it next restarts.
1636 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1637 }
1638 }
1639
1640 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1641 TaskRecord task, ActivityRecord r) {
1642 if (r.fullscreen) {
1643 // At this point, nothing else needs to be shown in this task.
1644 behindFullscreenActivity = true;
1645 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
1646 + " stackInvisible=" + stackInvisible
1647 + " behindFullscreenActivity=" + behindFullscreenActivity);
1648 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
1649 behindFullscreenActivity = true;
1650 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1651 + " stackInvisible=" + stackInvisible
1652 + " behindFullscreenActivity=" + behindFullscreenActivity);
1653 }
1654 return behindFullscreenActivity;
1655 }
1656
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001657 private void makeVisible(ActivityRecord starting, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001658 // This activity is not currently visible, but is running. Tell it to become visible.
1659 r.visible = true;
1660 if (r.state != ActivityState.RESUMED && r != starting) {
1661 // If this activity is paused, tell it to now show its window.
1662 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1663 "Making visible and scheduling visibility: " + r);
1664 try {
1665 if (mTranslucentActivityWaiting != null) {
1666 r.updateOptionsLocked(r.returningOptions);
1667 mUndrawnActivitiesBelowTopTranslucent.add(r);
1668 }
1669 setVisible(r, true);
1670 r.sleeping = false;
1671 r.app.pendingUiClean = true;
1672 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1673 r.stopFreezingScreenLocked(false);
1674 } catch (Exception e) {
1675 // Just skip on any failure; we'll make it
1676 // visible when it next restarts.
1677 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
1678 }
1679 }
1680 }
1681
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001682 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001683 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1684 r.stopFreezingScreenLocked(false);
1685 try {
1686 if (r.returningOptions != null) {
1687 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1688 }
1689 } catch(RemoteException e) {
1690 }
1691 return r.state == ActivityState.RESUMED;
1692 }
1693
Todd Kennedyaab56db2015-01-30 09:39:53 -08001694 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001695 mTranslucentActivityWaiting = r;
1696 mUndrawnActivitiesBelowTopTranslucent.clear();
1697 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1698 }
1699
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001700 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1701 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1702 final TaskRecord task = mTaskHistory.get(taskNdx);
1703 final ArrayList<ActivityRecord> activities = task.mActivities;
1704 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1705 final ActivityRecord r = activities.get(activityNdx);
1706 if ( r.appTimeTracker != except) {
1707 r.appTimeTracker = null;
1708 }
1709 }
1710 }
1711 }
1712
Craig Mautner5eda9b32013-07-02 11:58:16 -07001713 /**
1714 * Called as activities below the top translucent activity are redrawn. When the last one is
1715 * redrawn notify the top activity by calling
1716 * {@link Activity#onTranslucentConversionComplete}.
1717 *
1718 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1719 * occurred and the activity will be notified immediately.
1720 */
1721 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001722 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001723 if ((r == null)
1724 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1725 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1726 // The last undrawn activity below the top has just been drawn. If there is an
1727 // opaque activity at the top, notify it that it can become translucent safely now.
1728 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1729 mTranslucentActivityWaiting = null;
1730 mUndrawnActivitiesBelowTopTranslucent.clear();
1731 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1732
Craig Mautner71dd1b62014-02-18 15:48:52 -08001733 if (waitingActivity != null) {
1734 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1735 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1736 try {
1737 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1738 waitingActivity.appToken, r != null);
1739 } catch (RemoteException e) {
1740 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001741 }
1742 }
1743 }
1744 }
1745
Craig Mautnera61bc652013-10-28 15:43:18 -07001746 /** If any activities below the top running one are in the INITIALIZING state and they have a
1747 * starting window displayed then remove that starting window. It is possible that the activity
1748 * in this state will never resumed in which case that starting window will be orphaned. */
1749 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001750 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001751 boolean aboveTop = true;
1752 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1753 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1754 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1755 final ActivityRecord r = activities.get(activityNdx);
1756 if (aboveTop) {
1757 if (r == topActivity) {
1758 aboveTop = false;
1759 }
1760 continue;
1761 }
1762
1763 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001764 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1765 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001766 r.mStartingWindowShown = false;
1767 mWindowManager.removeAppStartingWindow(r.appToken);
1768 }
1769 }
1770 }
1771 }
1772
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001773 /**
1774 * Ensure that the top activity in the stack is resumed.
1775 *
1776 * @param prev The previously resumed activity, for when in the process
1777 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001778 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001779 *
1780 * @return Returns true if something is being resumed, or false if
1781 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001782 *
1783 * NOTE: It is not safe to call this method directly as it can cause an activity in a
1784 * non-focused stack to be resumed.
1785 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
1786 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001787 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08001788 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001789 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001790 // Don't even start recursing.
1791 return false;
1792 }
1793
1794 boolean result = false;
1795 try {
1796 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001797 mStackSupervisor.inResumeTopActivity = true;
1798 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1799 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001800 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001801 }
Craig Mautner544efa72014-09-04 16:41:20 -07001802 result = resumeTopActivityInnerLocked(prev, options);
1803 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001804 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001805 }
1806 return result;
1807 }
1808
Chong Zhang280d3322015-11-03 17:27:26 -08001809 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001810 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001811
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001812 if (!mService.mBooting && !mService.mBooted) {
1813 // Not ready yet!
1814 return false;
1815 }
1816
Craig Mautnerdf88d732014-01-27 09:21:32 -08001817 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001818 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001819 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001820 // Do not resume this stack if its parent is not resumed.
1821 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1822 return false;
1823 }
1824
Craig Mautnera61bc652013-10-28 15:43:18 -07001825 cancelInitializingActivities();
1826
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001827 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001828 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001829
1830 // Remember how we'll process this pause/resume situation, and ensure
1831 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001832 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1833 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001834
Craig Mautner84984fa2014-06-19 11:19:20 -07001835 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001836 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001837 // There are no more activities!
1838 final String reason = "noMoreActivities";
1839 if (!mFullscreen) {
1840 // Try to move focus to the next visible stack with a running activity if this
1841 // stack is not covering the entire screen.
1842 final ActivityStack stack = getNextVisibleStackLocked();
1843 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001844 return mStackSupervisor.resumeFocusedStackTopActivityLocked(stack, prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001845 }
1846 }
1847 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001848 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001849 if (DEBUG_STATES) Slog.d(TAG_STATES,
1850 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001851 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001852 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001853 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1854 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1855 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001856 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001857 }
1858
1859 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001860
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001861 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001862 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1863 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001864 // Make sure we have executed any pending transitions, since there
1865 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001866 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001867 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001868 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001869 if (DEBUG_STATES) Slog.d(TAG_STATES,
1870 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001871 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001872 return false;
1873 }
1874
Craig Mautner525f3d92013-05-07 14:01:50 -07001875 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001876 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001877 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001878 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001879 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001880 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001881 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001882 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001883 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001884 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001885 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001886 } else if (!isOnHomeDisplay()) {
1887 return false;
1888 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001889 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001890 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001891 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1892 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1893 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001894 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001895 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001896 }
1897
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001898 // If we are sleeping, and there is no resumed activity, and the top
1899 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001900 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001901 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001902 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001903 // Make sure we have executed any pending transitions, since there
1904 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001905 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001906 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001907 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001908 if (DEBUG_STATES) Slog.d(TAG_STATES,
1909 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001910 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001911 return false;
1912 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001913
1914 // Make sure that the user who owns this activity is started. If not,
1915 // we will just leave it as is because someone should be bringing
1916 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001917 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001918 Slog.w(TAG, "Skipping resume of top activity " + next
1919 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001920 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001921 return false;
1922 }
1923
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001924 // The activity may be waiting for stop, but that is no longer
1925 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001926 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001927 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001928 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001929 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001930
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001931 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001932
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001933 // If we are currently pausing an activity, then don't do anything
1934 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001935 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001936 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001937 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001938 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001939 return false;
1940 }
1941
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001942 // Okay we are now going to start a switch, to 'next'. We may first
1943 // have to pause the current activity, but this is an important point
1944 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001945 // XXX "App Redirected" dialog is getting too many false positives
1946 // at this point, so turn off for now.
1947 if (false) {
1948 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1949 long now = SystemClock.uptimeMillis();
1950 final boolean inTime = mLastStartedActivity.startTime != 0
1951 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1952 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1953 final int nextUid = next.info.applicationInfo.uid;
1954 if (inTime && lastUid != nextUid
1955 && lastUid != next.launchedFromUid
1956 && mService.checkPermission(
1957 android.Manifest.permission.STOP_APP_SWITCHES,
1958 -1, next.launchedFromUid)
1959 != PackageManager.PERMISSION_GRANTED) {
1960 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1961 } else {
1962 next.startTime = now;
1963 mLastStartedActivity = next;
1964 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001965 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001966 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001967 mLastStartedActivity = next;
1968 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001969 }
Craig Mautner58547802013-03-05 08:23:53 -08001970
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001971 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1972
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001973 // We need to start pausing the current activity so the top one
1974 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001975 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1976 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001977 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001978 if (DEBUG_STATES) Slog.d(TAG_STATES,
1979 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001980 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001981 }
1982 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001983 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001984 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001985 // At this point we want to put the upcoming activity's process
1986 // at the top of the LRU list, since we know we will be needing it
1987 // very soon and it would be a waste to let it get killed if it
1988 // happens to be sitting towards the end.
1989 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001990 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001991 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001992 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001993 return true;
1994 }
1995
Christopher Tated3f175c2012-06-14 14:16:54 -07001996 // If the most recent activity was noHistory but was only stopped rather
1997 // than stopped+finished because the device went to sleep, we need to make
1998 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001999 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002000 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002001 if (DEBUG_STATES) Slog.d(TAG_STATES,
2002 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002003 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002004 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002005 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002006 }
2007
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002008 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002009 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2010 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002011 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002012 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2013 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002014 } else {
2015 // The next activity is already visible, so hide the previous
2016 // activity's windows right now so we can show the new one ASAP.
2017 // We only do this if the previous is finishing, which should mean
2018 // it is on top of the one being resumed so hiding it quickly
2019 // is good. Otherwise, we want to do the normal route of allowing
2020 // the resumed activity to be shown so we can decide if the
2021 // previous should actually be hidden depending on whether the
2022 // new one is found to be full-screen or not.
2023 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002024 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002025 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2026 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002027 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002028 + ", nowVisible=" + next.nowVisible);
2029 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002030 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002031 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002032 + ", waitingVisible="
2033 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2034 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002035 }
2036 }
2037 }
2038
Dianne Hackborne7f97212011-02-24 14:40:20 -08002039 // Launching this app's activity, make sure the app is no longer
2040 // considered stopped.
2041 try {
2042 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002043 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002044 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002045 } catch (IllegalArgumentException e) {
2046 Slog.w(TAG, "Failed trying to unstop package "
2047 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002048 }
2049
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002050 // We are starting up the next activity, so tell the window manager
2051 // that the previous one will be hidden soon. This way it can know
2052 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002053 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002054 if (prev != null) {
2055 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002056 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002057 "Prepare close transition: prev=" + prev);
2058 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002059 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002060 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002061 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002062 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002063 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2064 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002065 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002066 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002067 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002068 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2069 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002070 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002071 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002072 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002073 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002074 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002075 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002076 : next.mLaunchTaskBehind
2077 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2078 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002079 }
2080 }
Craig Mautner967212c2013-04-13 21:10:58 -07002081 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002082 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002083 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002084 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002085 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002086 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002087 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002088 }
2089 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002090
2091 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002092 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002093 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2094 if (opts != null) {
2095 resumeAnimOptions = opts.toBundle();
2096 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002097 next.applyOptionsLocked();
2098 } else {
2099 next.clearOptionsLocked();
2100 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002101
Craig Mautnercf910b02013-04-23 11:23:27 -07002102 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002103 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002104 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002105
2106 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002107 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002108
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002109 // schedule launch ticks to collect information about slow apps.
2110 next.startLaunchTickingLocked();
2111
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002112 ActivityRecord lastResumedActivity =
2113 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002114 ActivityState lastState = next.state;
2115
2116 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002117
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002118 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002119 next.state = ActivityState.RESUMED;
2120 mResumedActivity = next;
2121 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002122 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002123 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002124 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002125 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002126
2127 // Have the window manager re-evaluate the orientation of
2128 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002129 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002130 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002131 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002132 mService.mConfiguration,
2133 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2134 if (config != null) {
2135 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002136 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002137 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002138 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002139
Craig Mautner525f3d92013-05-07 14:01:50 -07002140 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002141 // The configuration update wasn't able to keep the existing
2142 // instance of the activity, and instead started a new one.
2143 // We should be all done, but let's just make sure our activity
2144 // is still at the top and schedule another run if something
2145 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002146 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002147 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002148 "Activity config changed during resume: " + next
2149 + ", new next: " + nextNext);
2150 if (nextNext != next) {
2151 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002152 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002153 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002154 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002155 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002156 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002157 return true;
2158 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002159 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002160 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002161 }
Craig Mautner58547802013-03-05 08:23:53 -08002162
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002163 try {
2164 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002165 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002166 if (a != null) {
2167 final int N = a.size();
2168 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002169 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2170 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002171 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002172 }
2173 }
2174
2175 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002176 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002177 }
2178
Craig Mautner299f9602015-01-26 09:47:33 -08002179 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2180 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002181
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002182 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002183 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002184 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002185 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002186 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002187 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002188 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002189
Craig Mautner0eea92c2013-05-16 13:35:39 -07002190 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002191
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002192 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002193 } catch (Exception e) {
2194 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002195 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002196 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002197 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002198 if (lastStack != null) {
2199 lastStack.mResumedActivity = lastResumedActivity;
2200 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002201 Slog.i(TAG, "Restarting because process died: " + next);
2202 if (!next.hasBeenLaunched) {
2203 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002204 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2205 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002206 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002207 next.appToken, next.packageName, next.theme,
2208 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002209 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2210 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002211 }
George Mount2c92c972014-03-20 09:38:23 -07002212 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002213 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002214 return true;
2215 }
2216
2217 // From this point on, if something goes wrong there is no way
2218 // to recover the activity.
2219 try {
2220 next.visible = true;
2221 completeResumeLocked(next);
2222 } catch (Exception e) {
2223 // If any exception gets thrown, toss away this
2224 // activity and try the next one.
2225 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002226 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002227 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002228 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002229 return true;
2230 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002231 next.stopped = false;
2232
2233 } else {
2234 // Whoops, need to restart this activity!
2235 if (!next.hasBeenLaunched) {
2236 next.hasBeenLaunched = true;
2237 } else {
2238 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002239 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002240 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002241 mService.compatibilityInfoForPackageLocked(
2242 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002243 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002244 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002245 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002246 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002247 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002248 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002249 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002250 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002251 }
2252
Craig Mautnercf910b02013-04-23 11:23:27 -07002253 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002254 return true;
2255 }
2256
riddle_hsuc215a4f2014-12-27 12:10:45 +08002257 private TaskRecord getNextTask(TaskRecord targetTask) {
2258 final int index = mTaskHistory.indexOf(targetTask);
2259 if (index >= 0) {
2260 final int numTasks = mTaskHistory.size();
2261 for (int i = index + 1; i < numTasks; ++i) {
2262 TaskRecord task = mTaskHistory.get(i);
2263 if (task.userId == targetTask.userId) {
2264 return task;
2265 }
2266 }
2267 }
2268 return null;
2269 }
2270
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002271 private void insertTaskAtPosition(TaskRecord task, int position) {
2272 if (position >= mTaskHistory.size()) {
2273 insertTaskAtTop(task, null);
2274 return;
2275 }
2276 // Calculate maximum possible position for this task.
2277 int maxPosition = mTaskHistory.size();
2278 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002279 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002280 // Put non-current user tasks below current user tasks.
2281 while (maxPosition > 0) {
2282 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2283 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002284 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002285 break;
2286 }
2287 maxPosition--;
2288 }
2289 }
2290 position = Math.min(position, maxPosition);
2291 mTaskHistory.remove(task);
2292 mTaskHistory.add(position, task);
2293 updateTaskMovement(task, true);
2294 }
2295
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002296 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002297 // If the moving task is over home stack, transfer its return type to next task
2298 if (task.isOverHomeStack()) {
2299 final TaskRecord nextTask = getNextTask(task);
2300 if (nextTask != null) {
2301 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2302 }
2303 }
2304
Craig Mautner9c85c202013-10-04 20:11:26 -07002305 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002306 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002307 if (isOnHomeDisplay()) {
2308 ActivityStack lastStack = mStackSupervisor.getLastStack();
2309 final boolean fromHome = lastStack.isHomeStack();
2310 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002311 task.setTaskToReturnTo(fromHome
2312 ? lastStack.topTask() == null
2313 ? HOME_ACTIVITY_TYPE
2314 : lastStack.topTask().taskType
2315 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002316 }
2317 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002318 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002319 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002320
Craig Mautnerac6f8432013-07-17 13:24:59 -07002321 mTaskHistory.remove(task);
2322 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002323 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002324 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002325 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002326 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002327 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002328 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002329 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002330 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002331 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002332 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002333 break;
2334 }
2335 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002336 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002337 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002338 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002339 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002340 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002341
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002342 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2343 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002344 TaskRecord rTask = r.task;
2345 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002346 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2347 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002348 // Last activity in task had been removed or ActivityManagerService is reusing task.
2349 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002350 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002351 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002352 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002353 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002354 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002355 if (!newTask) {
2356 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002357 boolean startIt = true;
2358 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2359 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002360 if (task.getTopActivity() == null) {
2361 // All activities in task are finishing.
2362 continue;
2363 }
Craig Mautner70a86932013-02-28 22:37:44 -08002364 if (task == r.task) {
2365 // Here it is! Now, if this is not yet visible to the
2366 // user, then just add it without starting; it will
2367 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002368 if (!startIt) {
2369 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2370 + task, new RuntimeException("here").fillInStackTrace());
2371 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002372 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002373 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002374 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002375 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002376 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002377 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002378 return;
2379 }
2380 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002381 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002382 startIt = false;
2383 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002384 }
2385 }
2386
2387 // Place a new activity at top of stack, so it is next to interact
2388 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002389
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002390 // If we are not placing the new activity frontmost, we do not want
2391 // to deliver the onUserLeaving callback to the actual frontmost
2392 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002393 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002394 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002395 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002396 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002397 }
Craig Mautner70a86932013-02-28 22:37:44 -08002398
2399 task = r.task;
2400
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002401 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002402 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002403 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002404 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002405 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002406
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002407 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002408 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002409 // We want to show the starting preview window if we are
2410 // switching to a new task, or the next activity's process is
2411 // not currently running.
2412 boolean showStartingIcon = newTask;
2413 ProcessRecord proc = r.app;
2414 if (proc == null) {
2415 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2416 }
2417 if (proc == null || proc.thread == null) {
2418 showStartingIcon = true;
2419 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002420 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002421 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002422 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002423 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002424 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002425 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002426 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002427 ? r.mLaunchTaskBehind
2428 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2429 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002430 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002431 mNoAnimActivities.remove(r);
2432 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002433 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002434 boolean doShow = true;
2435 if (newTask) {
2436 // Even though this activity is starting fresh, we still need
2437 // to reset it to make sure we apply affinities to move any
2438 // existing activities from other tasks in to it.
2439 // If the caller has requested that the target task be
2440 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002441 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002442 resetTaskIfNeededLocked(r, r);
2443 doShow = topRunningNonDelayedActivityLocked(null) == r;
2444 }
Chong Zhang280d3322015-11-03 17:27:26 -08002445 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002446 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2447 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002448 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002449 if (r.mLaunchTaskBehind) {
2450 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2451 // tell WindowManager that r is visible even though it is at the back of the stack.
2452 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002453 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002454 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002455 // Figure out if we are transitioning from another activity that is
2456 // "has the same starting icon" as the next one. This allows the
2457 // window manager to keep the previous window it had previously
2458 // created, if it still had one.
2459 ActivityRecord prev = mResumedActivity;
2460 if (prev != null) {
2461 // We don't want to reuse the previous starting preview if:
2462 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002463 if (prev.task != r.task) {
2464 prev = null;
2465 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002466 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002467 else if (prev.nowVisible) {
2468 prev = null;
2469 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002470 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002471 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002472 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002473 mService.compatibilityInfoForPackageLocked(
2474 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002475 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002476 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002477 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002478 }
2479 } else {
2480 // If this is the first activity, don't do any fancy animations,
2481 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002482 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002483 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002484 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002485 }
2486 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002487 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002488 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002489 }
2490
Dianne Hackbornbe707852011-11-11 14:32:10 -08002491 final void validateAppTokensLocked() {
2492 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002493 mValidateAppTokens.ensureCapacity(numActivities());
2494 final int numTasks = mTaskHistory.size();
2495 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2496 TaskRecord task = mTaskHistory.get(taskNdx);
2497 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002498 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002499 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002500 }
Craig Mautner000f0022013-02-26 15:04:29 -08002501 TaskGroup group = new TaskGroup();
2502 group.taskId = task.taskId;
2503 mValidateAppTokens.add(group);
2504 final int numActivities = activities.size();
2505 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2506 final ActivityRecord r = activities.get(activityNdx);
2507 group.tokens.add(r.appToken);
2508 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002509 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002510 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002511 }
2512
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002513 /**
2514 * Perform a reset of the given task, if needed as part of launching it.
2515 * Returns the new HistoryRecord at the top of the task.
2516 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002517 /**
2518 * Helper method for #resetTaskIfNeededLocked.
2519 * We are inside of the task being reset... we'll either finish this activity, push it out
2520 * for another task, or leave it as-is.
2521 * @param task The task containing the Activity (taskTop) that might be reset.
2522 * @param forceReset
2523 * @return An ActivityOptions that needs to be processed.
2524 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002525 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002526 ActivityOptions topOptions = null;
2527
2528 int replyChainEnd = -1;
2529 boolean canMoveOptions = true;
2530
2531 // We only do this for activities that are not the root of the task (since if we finish
2532 // the root, we may no longer have the task!).
2533 final ArrayList<ActivityRecord> activities = task.mActivities;
2534 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002535 final int rootActivityNdx = task.findEffectiveRootIndex();
2536 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002537 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002538 if (target.frontOfTask)
2539 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002540
2541 final int flags = target.info.flags;
2542 final boolean finishOnTaskLaunch =
2543 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2544 final boolean allowTaskReparenting =
2545 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2546 final boolean clearWhenTaskReset =
2547 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2548
2549 if (!finishOnTaskLaunch
2550 && !clearWhenTaskReset
2551 && target.resultTo != null) {
2552 // If this activity is sending a reply to a previous
2553 // activity, we can't do anything with it now until
2554 // we reach the start of the reply chain.
2555 // XXX note that we are assuming the result is always
2556 // to the previous activity, which is almost always
2557 // the case but we really shouldn't count on.
2558 if (replyChainEnd < 0) {
2559 replyChainEnd = i;
2560 }
2561 } else if (!finishOnTaskLaunch
2562 && !clearWhenTaskReset
2563 && allowTaskReparenting
2564 && target.taskAffinity != null
2565 && !target.taskAffinity.equals(task.affinity)) {
2566 // If this activity has an affinity for another
2567 // task, then we need to move it out of here. We will
2568 // move it as far out of the way as possible, to the
2569 // bottom of the activity stack. This also keeps it
2570 // correctly ordered with any activities we previously
2571 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002572 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002573 final ActivityRecord bottom =
2574 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002575 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002576 if (bottom != null && target.taskAffinity != null
2577 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002578 // If the activity currently at the bottom has the
2579 // same task affinity as the one we are moving,
2580 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002581 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002582 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002583 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002584 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002585 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002586 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002587 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002588 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002589 + " out to new task " + target.task);
2590 }
2591
Wale Ogunwale706ed792015-08-02 10:29:44 -07002592 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002593
Craig Mautner525f3d92013-05-07 14:01:50 -07002594 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002595 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2596 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002597 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002598 if (p.finishing) {
2599 continue;
2600 }
2601
Craig Mautnere3a74d52013-02-22 14:14:58 -08002602 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002603 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002604 topOptions = p.takeOptionsLocked();
2605 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002606 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002607 }
2608 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002609 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2610 "Removing activity " + p + " from task=" + task + " adding to task="
2611 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002612 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2613 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002614 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002615 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002616
Wale Ogunwale706ed792015-08-02 10:29:44 -07002617 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002618 }
2619
Wale Ogunwale706ed792015-08-02 10:29:44 -07002620 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002621 if (VALIDATE_TOKENS) {
2622 validateAppTokensLocked();
2623 }
2624
2625 replyChainEnd = -1;
2626 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2627 // If the activity should just be removed -- either
2628 // because it asks for it, or the task should be
2629 // cleared -- then finish it and anything that is
2630 // part of its reply chain.
2631 int end;
2632 if (clearWhenTaskReset) {
2633 // In this case, we want to finish this activity
2634 // and everything above it, so be sneaky and pretend
2635 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002636 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002637 } else if (replyChainEnd < 0) {
2638 end = i;
2639 } else {
2640 end = replyChainEnd;
2641 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002642 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002643 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002644 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002645 if (p.finishing) {
2646 continue;
2647 }
2648 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002649 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002650 topOptions = p.takeOptionsLocked();
2651 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002652 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002653 }
2654 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002655 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002656 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002657 if (finishActivityLocked(
2658 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002659 end--;
2660 srcPos--;
2661 }
2662 }
2663 replyChainEnd = -1;
2664 } else {
2665 // If we were in the middle of a chain, well the
2666 // activity that started it all doesn't want anything
2667 // special, so leave it all as-is.
2668 replyChainEnd = -1;
2669 }
2670 }
2671
2672 return topOptions;
2673 }
2674
2675 /**
2676 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2677 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2678 * @param affinityTask The task we are looking for an affinity to.
2679 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2680 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2681 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2682 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002683 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002684 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002685 int replyChainEnd = -1;
2686 final int taskId = task.taskId;
2687 final String taskAffinity = task.affinity;
2688
2689 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2690 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002691 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2692
2693 // Do not operate on or below the effective root Activity.
2694 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002695 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002696 if (target.frontOfTask)
2697 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002698
2699 final int flags = target.info.flags;
2700 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2701 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2702
2703 if (target.resultTo != null) {
2704 // If this activity is sending a reply to a previous
2705 // activity, we can't do anything with it now until
2706 // we reach the start of the reply chain.
2707 // XXX note that we are assuming the result is always
2708 // to the previous activity, which is almost always
2709 // the case but we really shouldn't count on.
2710 if (replyChainEnd < 0) {
2711 replyChainEnd = i;
2712 }
2713 } else if (topTaskIsHigher
2714 && allowTaskReparenting
2715 && taskAffinity != null
2716 && taskAffinity.equals(target.taskAffinity)) {
2717 // This activity has an affinity for our task. Either remove it if we are
2718 // clearing or move it over to our task. Note that
2719 // we currently punt on the case where we are resetting a
2720 // task that is not at the top but who has activities above
2721 // with an affinity to it... this is really not a normal
2722 // case, and we will need to later pull that task to the front
2723 // and usually at that point we will do the reset and pick
2724 // up those remaining activities. (This only happens if
2725 // someone starts an activity in a new task from an activity
2726 // in a task that is not currently on top.)
2727 if (forceReset || finishOnTaskLaunch) {
2728 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002729 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2730 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002731 for (int srcPos = start; srcPos >= i; --srcPos) {
2732 final ActivityRecord p = activities.get(srcPos);
2733 if (p.finishing) {
2734 continue;
2735 }
Todd Kennedy539db512014-12-15 09:57:55 -08002736 finishActivityLocked(
2737 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002738 }
2739 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002740 if (taskInsertionPoint < 0) {
2741 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002742
Craig Mautner77878772013-03-04 19:46:24 -08002743 }
Craig Mautner77878772013-03-04 19:46:24 -08002744
2745 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002746 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2747 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2748 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002749 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002750 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002751 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002752 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002753
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002754 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2755 "Removing and adding activity " + p + " to stack at " + task
2756 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002757 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2758 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002759 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002760 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002761 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002762 if (VALIDATE_TOKENS) {
2763 validateAppTokensLocked();
2764 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002765
2766 // Now we've moved it in to place... but what if this is
2767 // a singleTop activity and we have put it on top of another
2768 // instance of the same activity? Then we drop the instance
2769 // below so it remains singleTop.
2770 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2771 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002772 int targetNdx = taskActivities.indexOf(target);
2773 if (targetNdx > 0) {
2774 ActivityRecord p = taskActivities.get(targetNdx - 1);
2775 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002776 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2777 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002778 }
2779 }
2780 }
2781 }
2782
2783 replyChainEnd = -1;
2784 }
2785 }
Craig Mautner77878772013-03-04 19:46:24 -08002786 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002787 }
2788
Craig Mautner8849a5e2013-04-02 16:41:03 -07002789 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002790 ActivityRecord newActivity) {
2791 boolean forceReset =
2792 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2793 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2794 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2795 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2796 forceReset = true;
2797 }
2798 }
2799
2800 final TaskRecord task = taskTop.task;
2801
2802 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2803 * for remaining tasks. Used for later tasks to reparent to task. */
2804 boolean taskFound = false;
2805
2806 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2807 ActivityOptions topOptions = null;
2808
Craig Mautner77878772013-03-04 19:46:24 -08002809 // Preserve the location for reparenting in the new task.
2810 int reparentInsertionPoint = -1;
2811
Craig Mautnere3a74d52013-02-22 14:14:58 -08002812 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2813 final TaskRecord targetTask = mTaskHistory.get(i);
2814
2815 if (targetTask == task) {
2816 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2817 taskFound = true;
2818 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002819 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2820 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002821 }
2822 }
2823
Craig Mautner70a86932013-02-28 22:37:44 -08002824 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002825 if (taskNdx >= 0) {
2826 do {
2827 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2828 } while (taskTop == null && taskNdx >= 0);
2829 }
Craig Mautner70a86932013-02-28 22:37:44 -08002830
Craig Mautnere3a74d52013-02-22 14:14:58 -08002831 if (topOptions != null) {
2832 // If we got some ActivityOptions from an activity on top that
2833 // was removed from the task, propagate them to the new real top.
2834 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002835 taskTop.updateOptionsLocked(topOptions);
2836 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002837 topOptions.abort();
2838 }
2839 }
2840
2841 return taskTop;
2842 }
2843
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002844 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2845 String resultWho, int requestCode, int resultCode, Intent data) {
2846
2847 if (callingUid > 0) {
2848 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002849 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002850 }
2851
2852 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2853 + " : who=" + resultWho + " req=" + requestCode
2854 + " res=" + resultCode + " data=" + data);
2855 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2856 try {
2857 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2858 list.add(new ResultInfo(resultWho, requestCode,
2859 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002860 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002861 return;
2862 } catch (Exception e) {
2863 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2864 }
2865 }
2866
2867 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2868 }
2869
Craig Mautner299f9602015-01-26 09:47:33 -08002870 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002871 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2872 return;
2873 }
2874
2875 final ActivityRecord next = topRunningActivityLocked();
2876 final String myReason = reason + " adjustFocus";
2877 if (next != r) {
2878 if (next != null && StackId.keepFocusInStackIfPossible(mStackId)) {
2879 // For freeform, docked, and pinned stacks we always keep the focus within the
2880 // stack as long as there is a running activity in the stack that we can adjust
2881 // focus to.
2882 mService.setFocusedActivityLocked(next, myReason);
2883 return;
2884 } else {
2885 final TaskRecord task = r.task;
2886 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2887 // For non-fullscreen stack, we want to move the focus to the next visible
2888 // stack to prevent the home screen from moving to the top and obscuring
2889 // other visible stacks.
2890 if (!mFullscreen
2891 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2892 return;
2893 }
2894 // Move the home stack to the top if this stack is fullscreen or there is no
2895 // other visible stack.
2896 if (mStackSupervisor.moveHomeStackTaskToTop(
2897 task.getTaskToReturnTo(), myReason)) {
2898 // Activity focus was already adjusted. Nothing else to do...
2899 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002900 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002901 }
2902 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08002903 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002904
Wale Ogunwaled046a012015-12-24 13:05:59 -08002905 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002906 }
2907
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002908 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2909 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2910 final String myReason = reason + " adjustFocusToNextVisibleStack";
2911 if (stack == null) {
2912 return false;
2913 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002914 return mService.setFocusedActivityLocked(stack.topRunningActivityLocked(), myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002915 }
2916
Craig Mautnerf3333272013-04-22 10:55:53 -07002917 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002918 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002919 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2920 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2921 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002922 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002923 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002924 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2925 "stop-no-history", false)) {
2926 // Activity was finished, no need to continue trying to schedule stop.
2927 adjustFocusedActivityLocked(r, "stopActivityFinished");
2928 r.resumeKeyDispatchingLocked();
2929 return;
2930 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002931 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002932 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002933 + " on stop because we're just sleeping");
2934 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002935 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002936 }
2937
2938 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002939 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002940 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002941 try {
2942 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002943 if (DEBUG_STATES) Slog.v(TAG_STATES,
2944 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002945 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002946 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2947 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002948 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002949 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002950 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002951 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002952 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002953 r.setSleeping(true);
2954 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002955 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002956 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002957 } catch (Exception e) {
2958 // Maybe just ignore exceptions here... if the process
2959 // has crashed, our death notification will clean things
2960 // up.
2961 Slog.w(TAG, "Exception thrown during pause", e);
2962 // Just in case, assume it to be stopped.
2963 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002964 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002965 r.state = ActivityState.STOPPED;
2966 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002967 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002968 }
2969 }
2970 }
2971 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002972
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002973 /**
2974 * @return Returns true if the activity is being finished, false if for
2975 * some reason it is being left as-is.
2976 */
2977 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002978 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002979 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002980 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2981 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002982 + ", result=" + resultCode + ", data=" + resultData
2983 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002984 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002985 return false;
2986 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002987
Craig Mautnerd44711d2013-02-23 11:24:36 -08002988 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002989 return true;
2990 }
2991
Craig Mautnerd2328952013-03-05 12:46:26 -08002992 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002993 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2994 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2995 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2996 ActivityRecord r = activities.get(activityNdx);
2997 if (r.resultTo == self && r.requestCode == requestCode) {
2998 if ((r.resultWho == null && resultWho == null) ||
2999 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3000 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3001 false);
3002 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003003 }
3004 }
3005 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003006 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003007 }
3008
Todd Kennedy539db512014-12-15 09:57:55 -08003009 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003010 ActivityRecord r = topRunningActivityLocked();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003011 if (r != null && r.app == app) {
3012 // If the top running activity is from this crashing
3013 // process, then terminate it to avoid getting in a loop.
3014 Slog.w(TAG, " Force finishing activity "
3015 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08003016 int taskNdx = mTaskHistory.indexOf(r.task);
3017 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08003018 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003019 // Also terminate any activities below it that aren't yet
3020 // stopped, to avoid a situation where one will get
3021 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08003022 --activityNdx;
3023 if (activityNdx < 0) {
3024 do {
3025 --taskNdx;
3026 if (taskNdx < 0) {
3027 break;
3028 }
3029 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3030 } while (activityNdx < 0);
3031 }
3032 if (activityNdx >= 0) {
3033 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003034 if (r.state == ActivityState.RESUMED
3035 || r.state == ActivityState.PAUSING
3036 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07003037 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003038 Slog.w(TAG, " Force finishing activity "
3039 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08003040 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003041 }
3042 }
3043 }
3044 }
3045 }
3046
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003047 final void finishVoiceTask(IVoiceInteractionSession session) {
3048 IBinder sessionBinder = session.asBinder();
3049 boolean didOne = false;
3050 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3051 TaskRecord tr = mTaskHistory.get(taskNdx);
3052 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3053 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3054 ActivityRecord r = tr.mActivities.get(activityNdx);
3055 if (!r.finishing) {
3056 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3057 false);
3058 didOne = true;
3059 }
3060 }
3061 }
3062 }
3063 if (didOne) {
3064 mService.updateOomAdjLocked();
3065 }
3066 }
3067
Craig Mautnerd2328952013-03-05 12:46:26 -08003068 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003069 ArrayList<ActivityRecord> activities = r.task.mActivities;
3070 for (int index = activities.indexOf(r); index >= 0; --index) {
3071 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003072 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003073 break;
3074 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003075 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003076 }
3077 return true;
3078 }
3079
Dianne Hackborn5c607432012-02-28 14:44:19 -08003080 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3081 // send the result
3082 ActivityRecord resultTo = r.resultTo;
3083 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003084 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003085 + " who=" + r.resultWho + " req=" + r.requestCode
3086 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003087 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003088 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003089 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003090 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003091 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003092 if (r.info.applicationInfo.uid > 0) {
3093 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3094 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003095 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003096 }
3097 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3098 resultData);
3099 r.resultTo = null;
3100 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003101 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003102
3103 // Make sure this HistoryRecord is not holding on to other resources,
3104 // because clients have remote IPC references to this object so we
3105 // can't assume that will go away and want to avoid circular IPC refs.
3106 r.results = null;
3107 r.pendingResults = null;
3108 r.newIntents = null;
3109 r.icicle = null;
3110 }
3111
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003112 /**
3113 * @return Returns true if this activity has been removed from the history
3114 * list, or false if it is still in the list and will be removed later.
3115 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003116 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3117 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003118 if (r.finishing) {
3119 Slog.w(TAG, "Duplicate finish request for " + r);
3120 return false;
3121 }
3122
Wale Ogunwale7d701172015-03-11 15:36:30 -07003123 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003124 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003125 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003126 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003127 task.taskId, r.shortComponentName, reason);
3128 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003129 final int index = activities.indexOf(r);
3130 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003131 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003132 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003133 // If the caller asked that this activity (and all above it)
3134 // be cleared when the task is reset, don't lose that information,
3135 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003136 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003137 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003138 }
3139 }
3140
3141 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003142
Craig Mautner299f9602015-01-26 09:47:33 -08003143 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003144
Dianne Hackborn5c607432012-02-28 14:44:19 -08003145 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003146
Craig Mautnerde4ef022013-04-07 19:01:33 -07003147 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003148 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003149 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003150 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003151 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003152 ? AppTransition.TRANSIT_TASK_CLOSE
3153 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003154
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003155 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003156 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003157
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003158 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003159 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3160 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3161 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003162 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003163 }
3164
Craig Mautneraea74a52014-03-08 14:23:10 -08003165 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003166 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003167 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003168 } else if (r.state != ActivityState.PAUSING) {
3169 // If the activity is PAUSING, we will complete the finish once
3170 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003171 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003172 if (r.visible) {
3173 mWindowManager.setAppVisibility(r.appToken, false);
3174 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003175 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003176 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003177 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003178 }
3179
3180 return false;
3181 }
3182
Craig Mautnerf3333272013-04-22 10:55:53 -07003183 static final int FINISH_IMMEDIATELY = 0;
3184 static final int FINISH_AFTER_PAUSE = 1;
3185 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003186
Craig Mautnerf3333272013-04-22 10:55:53 -07003187 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003188 // First things first: if this activity is currently visible,
3189 // and the resumed activity is not yet visible, then hold off on
3190 // finishing until the resumed one becomes visible.
3191 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003192 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08003193 addToStopping(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003194 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003195 if (DEBUG_STATES) Slog.v(TAG_STATES,
3196 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003197 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003198 if (oomAdj) {
3199 mService.updateOomAdjLocked();
3200 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003201 return r;
3202 }
3203
3204 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003205 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003206 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003207 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003208 if (mResumedActivity == r) {
3209 mResumedActivity = null;
3210 }
3211 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003212 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003213 r.state = ActivityState.FINISHING;
3214
3215 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003216 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003217 || prevState == ActivityState.STOPPED
3218 || prevState == ActivityState.INITIALIZING) {
3219 // If this activity is already stopped, we can just finish
3220 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003221 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003222 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003223 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003224 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003225 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003226 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003227 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3228 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003229 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003230 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003231
3232 // Need to go through the full pause cycle to get this
3233 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003234 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003235 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003236 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003237 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003238 return r;
3239 }
3240
Craig Mautneree36c772014-07-16 14:56:05 -07003241 void finishAllActivitiesLocked(boolean immediately) {
3242 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003243 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3244 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3245 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3246 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003247 noActivitiesInStack = false;
3248 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003249 continue;
3250 }
Craig Mautneree36c772014-07-16 14:56:05 -07003251 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003252 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3253 }
3254 }
Craig Mautneree36c772014-07-16 14:56:05 -07003255 if (noActivitiesInStack) {
3256 mActivityContainer.onTaskListEmptyLocked();
3257 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003258 }
3259
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003260 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3261 // Basic case: for simple app-centric recents, we need to recreate
3262 // the task if the affinity has changed.
3263 if (srec == null || srec.task.affinity == null ||
3264 !srec.task.affinity.equals(destAffinity)) {
3265 return true;
3266 }
3267 // Document-centric case: an app may be split in to multiple documents;
3268 // they need to re-create their task if this current activity is the root
3269 // of a document, unless simply finishing it will return them to the the
3270 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003271 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3272 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003273 // Okay, this activity is at the root of its task. What to do, what to do...
3274 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3275 // Finishing won't return to an application, so we need to recreate.
3276 return true;
3277 }
3278 // We now need to get the task below it to determine what to do.
3279 int taskIdx = mTaskHistory.indexOf(srec.task);
3280 if (taskIdx <= 0) {
3281 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3282 return false;
3283 }
3284 if (taskIdx == 0) {
3285 // At the bottom of the stack, nothing to go back to.
3286 return true;
3287 }
3288 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3289 if (!srec.task.affinity.equals(prevTask.affinity)) {
3290 // These are different apps, so need to recreate.
3291 return true;
3292 }
3293 }
3294 return false;
3295 }
3296
Wale Ogunwale7d701172015-03-11 15:36:30 -07003297 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003298 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003299 final TaskRecord task = srec.task;
3300 final ArrayList<ActivityRecord> activities = task.mActivities;
3301 final int start = activities.indexOf(srec);
3302 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003303 return false;
3304 }
3305 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003306 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003307 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003308 final ComponentName dest = destIntent.getComponent();
3309 if (start > 0 && dest != null) {
3310 for (int i = finishTo; i >= 0; i--) {
3311 ActivityRecord r = activities.get(i);
3312 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003313 r.info.name.equals(dest.getClassName())) {
3314 finishTo = i;
3315 parent = r;
3316 foundParentInTask = true;
3317 break;
3318 }
3319 }
3320 }
3321
3322 IActivityController controller = mService.mController;
3323 if (controller != null) {
3324 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3325 if (next != null) {
3326 // ask watcher if this is allowed
3327 boolean resumeOK = true;
3328 try {
3329 resumeOK = controller.activityResuming(next.packageName);
3330 } catch (RemoteException e) {
3331 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003332 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003333 }
3334
3335 if (!resumeOK) {
3336 return false;
3337 }
3338 }
3339 }
3340 final long origId = Binder.clearCallingIdentity();
3341 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003342 ActivityRecord r = activities.get(i);
3343 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003344 // Only return the supplied result for the first activity finished
3345 resultCode = Activity.RESULT_CANCELED;
3346 resultData = null;
3347 }
3348
3349 if (parent != null && foundParentInTask) {
3350 final int parentLaunchMode = parent.info.launchMode;
3351 final int destIntentFlags = destIntent.getFlags();
3352 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3353 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3354 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3355 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003356 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3357 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003358 } else {
3359 try {
3360 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3361 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski07a0e492015-12-17 14:16:38 -08003362 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread, destIntent,
Todd Kennedy7440f172015-12-09 14:31:22 -08003363 null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null, null,
3364 parent.appToken, null, 0, -1, parent.launchedFromUid,
3365 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3366 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003367 foundParentInTask = res == ActivityManager.START_SUCCESS;
3368 } catch (RemoteException e) {
3369 foundParentInTask = false;
3370 }
3371 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003372 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003373 }
3374 }
3375 Binder.restoreCallingIdentity(origId);
3376 return foundParentInTask;
3377 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003378 /**
3379 * Perform the common clean-up of an activity record. This is called both
3380 * as part of destroyActivityLocked() (when destroying the client-side
3381 * representation) and cleaning things up as a result of its hosting
3382 * processing going away, in which case there is no remaining client-side
3383 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003384 *
3385 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003386 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003387 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3388 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003389 if (mResumedActivity == r) {
3390 mResumedActivity = null;
3391 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003392 if (mPausingActivity == r) {
3393 mPausingActivity = null;
3394 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003395 mService.resetFocusedActivityIfNeededLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003396
3397 r.configDestroy = false;
3398 r.frozenBeforeDestroy = false;
3399
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003400 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003401 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003402 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003403 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003404 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003405 }
3406
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003407 // Make sure this record is no longer in the pending finishes list.
3408 // This could happen, for example, if we are trimming activities
3409 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003410 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003411 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003412
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003413 // Remove any pending results.
3414 if (r.finishing && r.pendingResults != null) {
3415 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3416 PendingIntentRecord rec = apr.get();
3417 if (rec != null) {
3418 mService.cancelIntentSenderLocked(rec, false);
3419 }
3420 }
3421 r.pendingResults = null;
3422 }
3423
3424 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003425 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003426 }
3427
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003428 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003429 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003430 if (getVisibleBehindActivity() == r) {
3431 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003432 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003433 }
3434
3435 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003436 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003437 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003438 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003439 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003440 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003441 }
3442
Craig Mautner299f9602015-01-26 09:47:33 -08003443 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003444 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003445 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003446 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003447 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3448 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3449
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003450 r.takeFromHistory();
3451 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003452 if (DEBUG_STATES) Slog.v(TAG_STATES,
3453 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003454 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003455 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003456 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003457 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003458 if (VALIDATE_TOKENS) {
3459 validateAppTokensLocked();
3460 }
Craig Mautner312ba862014-02-10 17:55:01 -08003461 final TaskRecord task = r.task;
3462 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003463 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003464 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003465 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003466 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003467 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003468 }
Craig Mautner299f9602015-01-26 09:47:33 -08003469 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003470 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003471 cleanUpActivityServicesLocked(r);
3472 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003473 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003474
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003475 /**
3476 * Perform clean-up of service connections in an activity record.
3477 */
3478 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3479 // Throw away any services that have been bound by this activity.
3480 if (r.connections != null) {
3481 Iterator<ConnectionRecord> it = r.connections.iterator();
3482 while (it.hasNext()) {
3483 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003484 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003485 }
3486 r.connections = null;
3487 }
3488 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003489
Craig Mautneree2e45a2014-06-27 12:10:03 -07003490 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003491 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003492 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003493 mHandler.sendMessage(msg);
3494 }
3495
Craig Mautneree2e45a2014-06-27 12:10:03 -07003496 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003497 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003498 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003499 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3500 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3501 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3502 final ActivityRecord r = activities.get(activityNdx);
3503 if (r.finishing) {
3504 continue;
3505 }
3506 if (r.fullscreen) {
3507 lastIsOpaque = true;
3508 }
3509 if (owner != null && r.app != owner) {
3510 continue;
3511 }
3512 if (!lastIsOpaque) {
3513 continue;
3514 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003515 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003516 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003517 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003518 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003519 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003520 activityRemoved = true;
3521 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003522 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003523 }
3524 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003525 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003526 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003527 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003528 }
3529
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003530 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3531 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003532 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3533 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003534 + " pausing=" + mPausingActivity + " for reason " + reason);
3535 return destroyActivityLocked(r, true, reason);
3536 }
3537 return false;
3538 }
3539
3540 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3541 String reason) {
3542 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003543 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003544 int maxTasks = tasks.size() / 4;
3545 if (maxTasks < 1) {
3546 maxTasks = 1;
3547 }
3548 int numReleased = 0;
3549 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3550 final TaskRecord task = mTaskHistory.get(taskNdx);
3551 if (!tasks.contains(task)) {
3552 continue;
3553 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003554 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003555 int curNum = 0;
3556 final ArrayList<ActivityRecord> activities = task.mActivities;
3557 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3558 final ActivityRecord activity = activities.get(actNdx);
3559 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003560 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003561 + " in state " + activity.state + " resumed=" + mResumedActivity
3562 + " pausing=" + mPausingActivity + " for reason " + reason);
3563 destroyActivityLocked(activity, true, reason);
3564 if (activities.get(actNdx) != activity) {
3565 // Was removed from list, back up so we don't miss the next one.
3566 actNdx--;
3567 }
3568 curNum++;
3569 }
3570 }
3571 if (curNum > 0) {
3572 numReleased += curNum;
3573 maxTasks--;
3574 if (mTaskHistory.get(taskNdx) != task) {
3575 // The entire task got removed, back up so we don't miss the next one.
3576 taskNdx--;
3577 }
3578 }
3579 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003580 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3581 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003582 return numReleased;
3583 }
3584
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003585 /**
3586 * Destroy the current CLIENT SIDE instance of an activity. This may be
3587 * called both when actually finishing an activity, or when performing
3588 * a configuration switch where we destroy the current client-side object
3589 * but then create a new client-side object for this same HistoryRecord.
3590 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003591 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003592 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3593 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003594 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003595 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003596 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003597 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003598
3599 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003600
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003601 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003602
3603 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003604
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003605 if (hadApp) {
3606 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003607 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003608 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3609 mService.mHeavyWeightProcess = null;
3610 mService.mHandler.sendEmptyMessage(
3611 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3612 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003613 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003614 // Update any services we are bound to that might care about whether
3615 // their client may have activities.
3616 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003617 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003618 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003619 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003620 }
3621 }
3622
3623 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003624
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003625 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003626 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003627 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003628 r.configChangeFlags);
3629 } catch (Exception e) {
3630 // We can just ignore exceptions here... if the process
3631 // has crashed, our death notification will clean things
3632 // up.
3633 //Slog.w(TAG, "Exception thrown during finish", e);
3634 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003635 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003636 removedFromHistory = true;
3637 skipDestroy = true;
3638 }
3639 }
3640
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003641 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003642
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003643 // If the activity is finishing, we need to wait on removing it
3644 // from the list to give it a chance to do its cleanup. During
3645 // that time it may make calls back with its token so we need to
3646 // be able to find it on the list and so we don't want to remove
3647 // it from the list yet. Otherwise, we can just immediately put
3648 // it in the destroyed state since we are not removing it from the
3649 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003650 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003651 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003652 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003653 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003654 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003655 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3656 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003657 if (DEBUG_STATES) Slog.v(TAG_STATES,
3658 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003659 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003660 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003661 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003662 }
3663 } else {
3664 // remove this record from the history.
3665 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003666 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003667 removedFromHistory = true;
3668 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003669 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003670 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003671 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003672 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003673 }
3674 }
3675
3676 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003677
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003678 if (!mLRUActivities.remove(r) && hadApp) {
3679 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3680 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003681
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003682 return removedFromHistory;
3683 }
3684
Craig Mautner299f9602015-01-26 09:47:33 -08003685 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003686 final long origId = Binder.clearCallingIdentity();
3687 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003688 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003689 if (r != null) {
3690 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003691 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003692 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003693
Wale Ogunwale60454db2015-01-23 16:05:07 -08003694 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003695 if (r.state == ActivityState.DESTROYING) {
3696 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003697 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003698 }
3699 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003700 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003701 } finally {
3702 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003703 }
3704 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003705
Todd Kennedyaab56db2015-01-30 09:39:53 -08003706 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003707 if (hasVisibleBehindActivity() &&
3708 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003709 if (r == topRunningActivityLocked()) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003710 // Don't release the top activity if it has requested to run behind the next
3711 // activity.
3712 return;
3713 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003714 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003715 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003716 " thread=" + r.app.thread);
3717 if (r != null && r.app != null && r.app.thread != null) {
3718 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003719 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003720 } catch (RemoteException e) {
3721 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003722 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003723 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003724 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003725 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003726 }
3727 }
3728 }
3729
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003730 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003731 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3732 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003733 if (r != null) {
3734 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003735 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003736 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003737 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003738 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003739 }
3740
Jose Lima4b6c6692014-08-12 17:41:12 -07003741 boolean hasVisibleBehindActivity() {
3742 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003743 }
3744
Jose Lima4b6c6692014-08-12 17:41:12 -07003745 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003746 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003747 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003748 }
3749 }
3750
Jose Lima4b6c6692014-08-12 17:41:12 -07003751 ActivityRecord getVisibleBehindActivity() {
3752 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003753 }
3754
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003755 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3756 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003757 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003758 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3759 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003760 while (i > 0) {
3761 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003762 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003763 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003764 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003765 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003766 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003767 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003768 }
3769 }
3770 }
3771
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003772 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3773 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003774 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3775 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003776 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3777 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003778 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003779 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003780 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3781 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003782
3783 boolean hasVisibleActivities = false;
3784
3785 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003786 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003787 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3788 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003789 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3790 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3791 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3792 final ActivityRecord r = activities.get(activityNdx);
3793 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003794 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3795 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003796 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003797 if (r.visible) {
3798 hasVisibleActivities = true;
3799 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003800 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003801 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3802 // Don't currently have state for the activity, or
3803 // it is finishing -- always remove it.
3804 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003805 } else if (!r.visible && r.launchCount > 2 &&
3806 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003807 // We have launched this activity too many times since it was
3808 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003809 // (Note if the activity is visible, we don't remove the record.
3810 // We leave the dead window on the screen but the process will
3811 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003812 remove = true;
3813 } else {
3814 // The process may be gone, but the activity lives on!
3815 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003816 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003817 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003818 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3819 "Removing activity " + r + " from stack at " + i
3820 + ": haveState=" + r.haveState
3821 + " stateNotNeeded=" + r.stateNotNeeded
3822 + " finishing=" + r.finishing
3823 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003824 if (!r.finishing) {
3825 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3826 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3827 r.userId, System.identityHashCode(r),
3828 r.task.taskId, r.shortComponentName,
3829 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003830 if (r.state == ActivityState.RESUMED) {
3831 mService.updateUsageStats(r, false);
3832 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003833 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003834 } else {
3835 // We have the current state for this activity, so
3836 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003837 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3838 if (DEBUG_APP) Slog.v(TAG_APP,
3839 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003840 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003841 // Set nowVisible to previous visible state. If the app was visible while
3842 // it died, we leave the dead window on screen so it's basically visible.
3843 // This is needed when user later tap on the dead window, we need to stop
3844 // other apps when user transfers focus to the restarted activity.
3845 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003846 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003847 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003848 "App died, clearing saved state of " + r);
3849 r.icicle = null;
3850 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003851 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003852 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003853 if (remove) {
3854 removeActivityFromHistoryLocked(r, "appDied");
3855 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003856 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003857 }
3858 }
3859
3860 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003861 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003862
Chong Zhang280d3322015-11-03 17:27:26 -08003863 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003864 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003865 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003866 if (r != null && r.state != ActivityState.RESUMED) {
3867 r.updateOptionsLocked(options);
3868 } else {
3869 ActivityOptions.abort(options);
3870 }
3871 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003872 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003873 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003874
Craig Mautner21d24a22014-04-23 11:45:37 -07003875 void updateTaskMovement(TaskRecord task, boolean toFront) {
3876 if (task.isPersistable) {
3877 task.mLastTimeMoved = System.currentTimeMillis();
3878 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3879 // recently will be most negative, tasks sent to the bottom before that will be less
3880 // negative. Similarly for recent tasks moved to the top which will be most positive.
3881 if (!toFront) {
3882 task.mLastTimeMoved *= -1;
3883 }
3884 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003885 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003886 }
3887
Craig Mautner84984fa2014-06-19 11:19:20 -07003888 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003889 final int top = mTaskHistory.size() - 1;
3890 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3891 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003892 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003893 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3894 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003895 mTaskHistory.remove(taskNdx);
3896 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003897 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003898 return;
3899 }
3900 }
3901 }
3902
Chong Zhang280d3322015-11-03 17:27:26 -08003903 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003904 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003905 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003906
Craig Mautner11bf9a52013-02-19 14:08:51 -08003907 final int numTasks = mTaskHistory.size();
3908 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003909 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003910 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003911 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003912 ActivityOptions.abort(options);
3913 } else {
3914 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3915 }
3916 return;
3917 }
3918
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003919 if (timeTracker != null) {
3920 // The caller wants a time tracker associated with this task.
3921 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3922 tr.mActivities.get(i).appTimeTracker = timeTracker;
3923 }
3924 }
3925
Craig Mautner11bf9a52013-02-19 14:08:51 -08003926 // Shift all activities with this task up to the top
3927 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003928 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003929
Chong Zhang45c25ce2015-08-10 22:18:26 -07003930 // Don't refocus if invisible to current user
3931 ActivityRecord top = tr.getTopActivity();
3932 if (!okToShowLocked(top)) {
3933 addRecentActivityLocked(top);
3934 ActivityOptions.abort(options);
3935 return;
3936 }
3937
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003938 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003939 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003940 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003941
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003942 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003943 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003944 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003945 if (r != null) {
3946 mNoAnimActivities.add(r);
3947 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003948 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003949 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003950 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003951 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003952
Wale Ogunwaled046a012015-12-24 13:05:59 -08003953 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003954 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003955
3956 if (VALIDATE_TOKENS) {
3957 validateAppTokensLocked();
3958 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003959 }
3960
3961 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003962 * Worker method for rearranging history stack. Implements the function of moving all
3963 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003964 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003965 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003966 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3967 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003968 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003969 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003970 * @return Returns true if the move completed, false if not.
3971 */
Craig Mautner299f9602015-01-26 09:47:33 -08003972 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003973 final TaskRecord tr = taskForIdLocked(taskId);
3974 if (tr == null) {
3975 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3976 return false;
3977 }
3978
3979 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003980 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003981
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003982 // If we have a watcher, preflight the move before committing to it. First check
3983 // for *other* available tasks, but if none are available, then try again allowing the
3984 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003985 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003986 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003987 if (next == null) {
3988 next = topRunningActivityLocked(null, 0);
3989 }
3990 if (next != null) {
3991 // ask watcher if this is allowed
3992 boolean moveOK = true;
3993 try {
3994 moveOK = mService.mController.activityResuming(next.packageName);
3995 } catch (RemoteException e) {
3996 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003997 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003998 }
3999 if (!moveOK) {
4000 return false;
4001 }
4002 }
4003 }
4004
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004005 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004006
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004007 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4008 // For the case where we are moving the home task back and there is an activity visible
4009 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4010 // activity to maintain order with what the user is seeing.
4011 final ActivityStack fullscreenStack =
4012 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4013 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4014 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4015 mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004016 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004017 return true;
4018 }
4019 }
4020
riddle_hsuc215a4f2014-12-27 12:10:45 +08004021 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004022
4023 // If true, we should resume the home activity next if the task we are moving to the
4024 // back is over the home stack. We force to false if the task we are moving to back
4025 // is the home task and we don't want it resumed after moving to the back.
4026 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4027 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004028 final TaskRecord nextTask = getNextTask(tr);
4029 if (nextTask != null) {
4030 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4031 } else {
4032 prevIsHome = true;
4033 }
4034 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004035 mTaskHistory.remove(tr);
4036 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004037 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004038
Craig Mautnerc8143c62013-09-03 12:15:57 -07004039 // There is an assumption that moving a task to the back moves it behind the home activity.
4040 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004041 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004042 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4043 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004044 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004045 break;
4046 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004047 if (taskNdx == 1) {
4048 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004049 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004050 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004051 }
4052
Craig Mautner299f9602015-01-26 09:47:33 -08004053 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004054 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004055
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004056 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004057 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004058 }
4059
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004060 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004061 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004062 if (!mService.mBooting && !mService.mBooted) {
4063 // Not ready yet!
4064 return false;
4065 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004066 final int taskToReturnTo = tr.getTaskToReturnTo();
4067 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004068 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004069 }
4070
Wale Ogunwaled046a012015-12-24 13:05:59 -08004071 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004072 return true;
4073 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004074
Craig Mautner8849a5e2013-04-02 16:41:03 -07004075 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004076 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004077 final Uri data = r.intent.getData();
4078 final String strData = data != null ? data.toSafeString() : null;
4079
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004080 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004081 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004082 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004083 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004084 }
4085
4086 /**
4087 * Make sure the given activity matches the current configuration. Returns
4088 * false if the activity had to be destroyed. Returns true if the
4089 * configuration is the same, or the activity will remain running as-is
4090 * for whatever reason. Ensures the HistoryRecord is updated with the
4091 * correct configuration and all other bookkeeping is handled.
4092 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004093 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4094 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004095 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004096 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004097 "Skipping config check (will change): " + r);
4098 return true;
4099 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004100
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004101 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004102 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004103
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004104 // Short circuit: if the two configurations are the exact same
4105 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004106 final Configuration newConfig = mService.mConfiguration;
4107 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004108 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004109 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004110 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004111 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004112 "Configuration unchanged in " + r);
4113 return true;
4114 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004115
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004116 // We don't worry about activities that are finishing.
4117 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004118 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004119 "Configuration doesn't matter in finishing " + r);
4120 r.stopFreezingScreenLocked(false);
4121 return true;
4122 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004123
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004124 // Okay we now are going to make this activity have the new config.
4125 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004126 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004127 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004128 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004129 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004130
Filip Gruszczynski23493322015-07-29 17:02:59 -07004131 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004132 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004133 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004134 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004135 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004136 // There are no significant differences, so we won't relaunch but should still deliver
4137 // the new configuration to the client process.
4138 r.scheduleConfigurationChanged(taskConfig, false);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004139 return true;
4140 }
4141
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004142 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4143 "Configuration changes for " + r + " ; taskChanges="
4144 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4145 + Configuration.configurationDiffToString(changes));
4146
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004147 // If the activity isn't currently running, just leave the new
4148 // configuration and it will pick that up next time it starts.
4149 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004150 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004151 "Configuration doesn't matter not running " + r);
4152 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004153 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004154 return true;
4155 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004156
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004157 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004158 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4159 "Checking to restart " + r.info.name + ": changed=0x"
4160 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004161 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4162 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004163
Dianne Hackborne6676352011-06-01 16:51:20 -07004164 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004165 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4166 r.configChangeFlags |= changes;
4167 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004168 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004169 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004170 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004171 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004172 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004173 } else if (r.state == ActivityState.PAUSING) {
4174 // A little annoying: we are waiting for this activity to
4175 // finish pausing. Let's not do anything now, but just
4176 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004177 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004178 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004179 r.configDestroy = true;
4180 return true;
4181 } else if (r.state == ActivityState.RESUMED) {
4182 // Try to optimize this case: the configuration is changing
4183 // and we need to restart the top, resumed activity.
4184 // Instead of doing the normal handshaking, just say
4185 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004186 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004187 "Config is relaunching resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004188 relaunchActivityLocked(r, r.configChangeFlags, true,
4189 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004190 r.configChangeFlags = 0;
4191 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004192 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004193 "Config is relaunching non-resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004194 relaunchActivityLocked(r, r.configChangeFlags, false,
4195 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004196 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004197 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004198
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004199 // All done... tell the caller we weren't able to keep this
4200 // activity around.
4201 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004202 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004203
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004204 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004205 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004206 // changes is always sent to all processes when they happen so it can just use whatever
4207 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004208 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004209 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004210
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004211 return true;
4212 }
4213
Filip Gruszczynski23493322015-07-29 17:02:59 -07004214 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4215 Configuration oldTaskOverride) {
4216 // Determine what has changed. May be nothing, if this is a config
4217 // that has come back from the app after going idle. In that case
4218 // we just want to leave the official config object now in the
4219 // activity and do nothing else.
4220 int taskChanges = oldTaskOverride.diff(taskConfig);
4221 // We don't want to use size changes if they don't cross boundaries that are important to
4222 // the app.
4223 if ((taskChanges & ActivityInfo.CONFIG_SCREEN_SIZE) != 0) {
4224 final boolean crosses = record.crossesHorizontalSizeThreshold(
4225 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4226 || record.crossesVerticalSizeThreshold(
4227 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4228 if (!crosses) {
4229 taskChanges &= ~ActivityInfo.CONFIG_SCREEN_SIZE;
4230 }
4231 }
4232 if ((taskChanges & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4233 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4234 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004235 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004236 taskChanges &= ~ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4237 }
4238 }
4239 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4240 }
4241
4242 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4243 Configuration oldTaskOverride, int taskChanges) {
4244 if (taskChanges == 0) {
4245 // {@link Configuration#diff} doesn't catch changes from unset values.
4246 // Check for changes we care about.
4247 if (oldTaskOverride.orientation != taskConfig.orientation) {
4248 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
4249 }
4250 // We want to explicitly track situations where the size configuration goes from
4251 // undefined to defined. We don't care about crossing the threshold in that case,
4252 // because there is no threshold.
4253 final int oldHeight = oldTaskOverride.screenHeightDp;
4254 final int newHeight = taskConfig.screenHeightDp;
4255 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4256 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4257 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4258 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4259 }
4260 final int oldWidth = oldTaskOverride.screenWidthDp;
4261 final int newWidth = taskConfig.screenWidthDp;
4262 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4263 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4264 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4265 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4266 }
4267 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4268 final int newSmallest = taskConfig.smallestScreenWidthDp;
4269 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4270 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4271 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4272 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4273 }
4274 }
4275 return taskChanges;
4276 }
4277
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004278 private static boolean isResizeOnlyChange(int change) {
4279 return (change & ~(ActivityInfo.CONFIG_SCREEN_SIZE
4280 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_ORIENTATION)) == 0;
4281 }
4282
Wale Ogunwale83301a92015-09-24 15:54:08 -07004283 private void relaunchActivityLocked(
4284 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4285 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
4286 return;
4287 }
4288
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004289 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004290 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004291 if (andResume) {
4292 results = r.results;
4293 newIntents = r.newIntents;
4294 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004295 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4296 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004297 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004298 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004299 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004300 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004301
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004302 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004303
Craig Mautner34b73df2014-01-12 21:11:08 -08004304 mStackSupervisor.removeChildActivityContainers(r);
4305
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004306 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004307 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4308 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004309 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004310 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4311 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004312 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004313 // Note: don't need to call pauseIfSleepingLocked() here, because
4314 // the caller will only pass in 'andResume' if this activity is
4315 // currently resumed, which implies we aren't sleeping.
4316 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004317 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004318 }
4319
4320 if (andResume) {
4321 r.results = null;
4322 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004323 r.state = ActivityState.RESUMED;
4324 } else {
4325 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4326 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004327 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004328 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004329
4330 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004331 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4332 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4333 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4334 final ActivityRecord r = activities.get(activityNdx);
4335 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004336 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004337 }
4338 if (r.fullscreen && !r.finishing) {
4339 return false;
4340 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004341 }
4342 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004343 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004344 if (r == null) {
4345 return false;
4346 }
4347 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4348 + " would have returned true for r=" + r);
4349 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004350 }
4351
4352 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004353 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4354 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4355 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4356 final ActivityRecord r = activities.get(activityNdx);
4357 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004358 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004359 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004360 }
4361 }
4362 }
4363
Wale Ogunwale540e1232015-05-01 15:35:39 -07004364 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4365 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004366 boolean didSomething = false;
4367 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004368 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004369 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4370 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4371 int numActivities = activities.size();
4372 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4373 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004374 final boolean sameComponent =
4375 (r.packageName.equals(packageName) && (filterByClasses == null
4376 || filterByClasses.contains(r.realActivity.getClassName())))
4377 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004378 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004379 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004380 && (r.app == null || evenPersistent || !r.app.persistent)) {
4381 if (!doit) {
4382 if (r.finishing) {
4383 // If this activity is just finishing, then it is not
4384 // interesting as far as something to stop.
4385 continue;
4386 }
4387 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004388 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004389 if (r.isHomeActivity()) {
4390 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4391 Slog.i(TAG, "Skip force-stop again " + r);
4392 continue;
4393 } else {
4394 homeActivity = r.realActivity;
4395 }
4396 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004397 didSomething = true;
4398 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004399 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004400 if (r.app != null) {
4401 r.app.removed = true;
4402 }
4403 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004404 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004405 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004406 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4407 true)) {
4408 // r has been deleted from mActivities, accommodate.
4409 --numActivities;
4410 --activityNdx;
4411 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004412 }
4413 }
4414 }
4415 return didSomething;
4416 }
4417
Dianne Hackborn09233282014-04-30 11:33:59 -07004418 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004419 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4420 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004421 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004422 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004423 if (task.getTopActivity() == null) {
4424 continue;
4425 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004426 ActivityRecord r = null;
4427 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004428 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004429 int numActivities = 0;
4430 int numRunning = 0;
4431 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004432 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004433 continue;
4434 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004435 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004436 tmp = activities.get(activityNdx);
4437 if (tmp.finishing) {
4438 continue;
4439 }
4440 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004441
Craig Mautneraab647e2013-02-28 16:31:36 -08004442 // Initialize state for next task if needed.
4443 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4444 top = r;
4445 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004446 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004447
4448 // Add 'r' into the current task.
4449 numActivities++;
4450 if (r.app != null && r.app.thread != null) {
4451 numRunning++;
4452 }
4453
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004454 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004455 TAG, r.intent.getComponent().flattenToShortString()
4456 + ": task=" + r.task);
4457 }
4458
4459 RunningTaskInfo ci = new RunningTaskInfo();
4460 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004461 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004462 ci.baseActivity = r.intent.getComponent();
4463 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004464 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004465 if (focusedStack && topTask) {
4466 // Give the latest time to ensure foreground task can be sorted
4467 // at the first, because lastActiveTime of creating task is 0.
4468 ci.lastActiveTime = System.currentTimeMillis();
4469 topTask = false;
4470 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004471
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004472 if (top.task != null) {
4473 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004474 }
4475 ci.numActivities = numActivities;
4476 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004477 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004478 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004479 }
4480
4481 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004482 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004483 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004484 if (top >= 0) {
4485 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4486 int activityTop = activities.size() - 1;
4487 if (activityTop > 0) {
4488 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4489 "unhandled-back", true);
4490 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004491 }
4492 }
4493
Craig Mautner6b74cb52013-09-27 17:02:21 -07004494 /**
4495 * Reset local parameters because an app's activity died.
4496 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004497 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004498 */
4499 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004500 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004501 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004502 "App died while pausing: " + mPausingActivity);
4503 mPausingActivity = null;
4504 }
4505 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4506 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004507 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004508 }
4509
Craig Mautner19091252013-10-05 00:03:53 -07004510 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004511 }
4512
Craig Mautnercae015f2013-02-08 14:31:27 -08004513 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004514 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4515 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4516 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4517 final ActivityRecord r = activities.get(activityNdx);
4518 if (r.app == app) {
4519 Slog.w(TAG, " Force finishing activity "
4520 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004521 // Force the destroy to skip right to removal.
4522 r.app = null;
4523 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004524 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004525 }
4526 }
4527 }
4528
Dianne Hackborn390517b2013-05-30 15:03:32 -07004529 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004530 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004531 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004532 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4533 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004534 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4535 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004536 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004537 " Task id #" + task.taskId + "\n" +
4538 " mFullscreen=" + task.mFullscreen + "\n" +
4539 " mBounds=" + task.mBounds + "\n" +
4540 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004541 if (printed) {
4542 header = null;
4543 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004544 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004545 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004546 }
4547
4548 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4549 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4550
4551 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004552 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4553 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004554 }
4555 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004556 final int top = mTaskHistory.size() - 1;
4557 if (top >= 0) {
4558 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4559 int listTop = list.size() - 1;
4560 if (listTop >= 0) {
4561 activities.add(list.get(listTop));
4562 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004563 }
4564 } else {
4565 ItemMatcher matcher = new ItemMatcher();
4566 matcher.build(name);
4567
Craig Mautneraab647e2013-02-28 16:31:36 -08004568 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4569 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4570 if (matcher.match(r1, r1.intent.getComponent())) {
4571 activities.add(r1);
4572 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004573 }
4574 }
4575 }
4576
4577 return activities;
4578 }
4579
4580 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004581 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004582
4583 // All activities that came from the package must be
4584 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004585 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4586 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4587 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4588 final ActivityRecord a = activities.get(activityNdx);
4589 if (a.info.packageName.equals(packageName)) {
4590 a.forceNewConfig = true;
4591 if (starting != null && a == starting && a.visible) {
4592 a.startFreezingScreenLocked(starting.app,
4593 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4594 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004595 }
4596 }
4597 }
4598
4599 return starting;
4600 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004601
Craig Mautner299f9602015-01-26 09:47:33 -08004602 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004603 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004604 }
4605
Wale Ogunwale000957c2015-04-03 08:19:12 -07004606 /**
4607 * Removes the input task from this stack.
4608 * @param task to remove.
4609 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004610 * @param moving task to another stack. In the case we are moving we don't want to perform
4611 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004612 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004613 void removeTask(TaskRecord task, String reason, boolean moving) {
4614 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004615 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004616 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004617 if (!StackId.persistTaskBounds(mStackId)) {
4618 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4619 // default configuration the next time it launches.
4620 task.updateOverrideConfiguration(null);
4621 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004622 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004623
Craig Mautner04a0ea62014-01-13 12:51:26 -08004624 final ActivityRecord r = mResumedActivity;
4625 if (r != null && r.task == task) {
4626 mResumedActivity = null;
4627 }
4628
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004629 final int taskNdx = mTaskHistory.indexOf(task);
4630 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004631 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4632 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4633 if (!nextTask.isOverHomeStack()) {
4634 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4635 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004636 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004637 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004638 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004639
Wale Ogunwale040b4702015-08-06 18:10:50 -07004640 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004641 final boolean isVoiceSession = task.voiceSession != null;
4642 if (isVoiceSession) {
4643 try {
4644 task.voiceSession.taskFinished(task.intent, task.taskId);
4645 } catch (RemoteException e) {
4646 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004647 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004648 if (task.autoRemoveFromRecents() || isVoiceSession) {
4649 // Task creator asked to remove this when done, or this task was a voice
4650 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004651 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004652 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004653 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004654 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004655
4656 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004657 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004658 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004659 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004660 String myReason = reason + " leftTaskHistoryEmpty";
4661 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004662 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004663 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004664 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004665 if (mStacks != null) {
4666 mStacks.remove(this);
4667 mStacks.add(0, this);
4668 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004669 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004670 mActivityContainer.onTaskListEmptyLocked();
4671 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004672 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004673
4674 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004675 }
4676
Dianne Hackborn91097de2014-04-04 18:02:06 -07004677 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4678 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4679 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004680 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4681 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004682 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08004683 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08004684 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
4685 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.mResizeable
4686 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004687 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004688 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004689 return task;
4690 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004691
Wale Ogunwale935e5022015-11-10 12:36:10 -08004692 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4693 if (mTaskPositioner == null) {
4694 return false;
4695 }
4696 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4697 return true;
4698 }
4699
Craig Mautnerc00204b2013-03-05 15:02:14 -08004700 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004701 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004702 }
4703
Wale Ogunwale5f986092015-12-04 15:35:38 -08004704 void addTask(final TaskRecord task, final boolean toTop, String reason) {
4705 final ActivityStack prevStack = preAddTask(task, reason);
4706
Craig Mautnerc00204b2013-03-05 15:02:14 -08004707 task.stack = this;
4708 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004709 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004710 } else {
4711 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004712 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004713 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08004714 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004715 }
4716
Wale Ogunwale5f986092015-12-04 15:35:38 -08004717 void positionTask(final TaskRecord task, int position) {
4718 final ActivityStack prevStack = preAddTask(task, "positionTask");
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004719 task.stack = this;
4720 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004721 postAddTask(task, prevStack);
4722 }
4723
4724 private ActivityStack preAddTask(TaskRecord task, String reason) {
4725 final ActivityStack prevStack = task.stack;
4726 if (prevStack != null && prevStack != this) {
4727 prevStack.removeTask(task, reason, MOVING);
4728 }
4729 return prevStack;
4730 }
4731
4732 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
4733 if (prevStack != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004734 task.reportPictureInPictureModeChangeIfNeeded(prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004735 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004736 try {
4737 task.voiceSession.taskStarted(task.intent, task.taskId);
4738 } catch (RemoteException e) {
4739 }
4740 }
4741 }
4742
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004743 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07004744 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004745 task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004746 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4747 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
4748 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
4749 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
Wale Ogunwale61b009e2015-09-16 15:43:05 -07004750 bounds, task.mOverrideConfig, !r.isHomeActivity());
Chong Zhangb15758a2015-11-17 12:12:03 -08004751 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004752 r.taskConfigOverride = task.mOverrideConfig;
4753 }
4754
Wale Ogunwaled046a012015-12-24 13:05:59 -08004755 void moveToFrontAndResumeStateIfNeeded(
4756 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
4757 if (!moveToFront) {
4758 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07004759 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004760
4761 // If the activity owns the last resumed activity, transfer that together,
4762 // so that we don't resume the same activity again in the new stack.
4763 // Apps may depend on onResume()/onPause() being called in pairs.
4764 if (setResume) {
4765 mResumedActivity = r;
4766 }
4767 // Move the stack in which we are placing the activity to the front. The call will also
4768 // make sure the activity focus is set.
4769 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004770 }
4771
4772 /**
4773 * Moves the input activity from its current stack to this one.
4774 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4775 * created on this stack which the activity is added to.
4776 * */
4777 void moveActivityToStack(ActivityRecord r) {
4778 final ActivityStack prevStack = r.task.stack;
4779 if (prevStack.mStackId == mStackId) {
4780 // You are already in the right stack silly...
4781 return;
4782 }
4783
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004784 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004785 && (mStackSupervisor.topRunningActivityLocked() == r);
4786 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4787
4788 final TaskRecord task = createTaskRecord(
4789 mStackSupervisor.getNextTaskId(), r.info, r.intent, null, null, true);
4790 r.setTask(task, null);
4791 task.addActivityToTop(r);
4792 setAppTask(r, task);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004793 task.reportPictureInPictureModeChangeIfNeeded(prevStack);
4794 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4795 if (wasResumed) {
4796 prevStack.mResumedActivity = null;
4797 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07004798 }
4799
Wale Ogunwale706ed792015-08-02 10:29:44 -07004800 private void setAppTask(ActivityRecord r, TaskRecord task) {
4801 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004802 task.updateOverrideConfiguration(bounds);
4803 mWindowManager.setAppTask(
Wale Ogunwale2998eef2015-12-02 19:46:29 -08004804 r.appToken, task.taskId, mStackId, task.getLaunchBounds(), task.mOverrideConfig);
Chong Zhangb15758a2015-11-17 12:12:03 -08004805 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004806 r.taskConfigOverride = task.mOverrideConfig;
4807 }
4808
Craig Mautnerc00204b2013-03-05 15:02:14 -08004809 public int getStackId() {
4810 return mStackId;
4811 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004812
4813 @Override
4814 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004815 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4816 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004817 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004818
Craig Mautner15df08a2015-04-01 12:17:18 -07004819 void onLockTaskPackagesUpdatedLocked() {
4820 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4821 mTaskHistory.get(taskNdx).setLockTaskAuth();
4822 }
4823 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004824}