blob: 9e32efa1a1c268b549d186becb49f18cecc1342b [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
23import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070024import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070025
Wale Ogunwalee23149f2015-03-06 15:39:44 -080026import static com.android.server.am.ActivityManagerDebugConfig.*;
Chong Zhang75b37202015-12-04 14:16:36 -080027import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
Craig Mautner84984fa2014-06-19 11:19:20 -070028import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
29import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070030
Wale Ogunwale040b4702015-08-06 18:10:50 -070031import static com.android.server.am.ActivityStackSupervisor.MOVING;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070032import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwale040b4702015-08-06 18:10:50 -070033
Winsonc809cbb2015-11-02 12:06:15 -080034import android.graphics.Point;
Wale Ogunwale706ed792015-08-02 10:29:44 -070035import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070036import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070037
Dianne Hackborn91097de2014-04-04 18:02:06 -070038import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080039import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070040import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070041import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080042import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080043import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080044import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080045import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070046import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070047
48import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070049import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070050import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070051import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080053import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080055import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080061import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080062import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070064import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080065import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Handler;
67import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090068import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070070import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.RemoteException;
72import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080073import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070074import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070075import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.EventLog;
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -080077import android.util.Log;
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
Craig Mautnere0a38842013-12-16 16:14:02 -0800522 final boolean isOnHomeDisplay() {
523 return isAttached() &&
524 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
525 }
526
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700527 void moveToFront(String reason) {
528 moveToFront(reason, null);
529 }
530
531 /**
532 * @param reason The reason for moving the stack to the front.
533 * @param task If non-null, the task will be moved to the top of the stack.
534 * */
535 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700536 if (!isAttached()) {
537 return;
538 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700539
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700540 mStacks.remove(this);
541 int addIndex = mStacks.size();
542
543 if (addIndex > 0) {
544 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800545 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
546 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700547 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800548 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700549 }
550
551 mStacks.add(addIndex, this);
552
553 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
554 if (isOnHomeDisplay()) {
555 mStackSupervisor.setFocusStack(reason, this);
556 }
557 if (task != null) {
558 insertTaskAtTop(task, null);
559 } else {
560 task = topTask();
561 }
562 if (task != null) {
563 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800564 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800565 }
566
567 final boolean isAttached() {
568 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800569 }
570
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700571 /**
572 * Returns the top activity in any existing task matching the given
573 * Intent. Returns null if no such task is found.
574 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700575 ActivityRecord findTaskLocked(ActivityRecord target) {
576 Intent intent = target.intent;
577 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700578 ComponentName cls = intent.getComponent();
579 if (info.targetActivity != null) {
580 cls = new ComponentName(info.packageName, info.targetActivity);
581 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700582 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700583 boolean isDocument = intent != null & intent.isDocument();
584 // If documentData is non-null then it must match the existing task data.
585 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800586
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700587 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800588 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
589 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700590 if (task.voiceSession != null) {
591 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700592 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700593 continue;
594 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700595 if (task.userId != userId) {
596 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700597 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700598 continue;
599 }
Craig Mautner000f0022013-02-26 15:04:29 -0800600 final ActivityRecord r = task.getTopActivity();
601 if (r == null || r.finishing || r.userId != userId ||
602 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700603 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800604 continue;
605 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700606 if (r.mActivityType != target.mActivityType) {
607 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
608 continue;
609 }
Craig Mautner000f0022013-02-26 15:04:29 -0800610
Craig Mautnerd00f4742014-03-12 14:17:26 -0700611 final Intent taskIntent = task.intent;
612 final Intent affinityIntent = task.affinityIntent;
613 final boolean taskIsDocument;
614 final Uri taskDocumentData;
615 if (taskIntent != null && taskIntent.isDocument()) {
616 taskIsDocument = true;
617 taskDocumentData = taskIntent.getData();
618 } else if (affinityIntent != null && affinityIntent.isDocument()) {
619 taskIsDocument = true;
620 taskDocumentData = affinityIntent.getData();
621 } else {
622 taskIsDocument = false;
623 taskDocumentData = null;
624 }
625
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700626 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700627 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700628 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700629 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700630 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
631 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700632 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700633 return r;
634 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700635 } else if (taskIntent != null && taskIntent.getComponent() != null &&
636 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700637 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700638 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800639 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700640 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
641 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800642 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700643 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
644 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700645 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700646 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800647 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700648 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
649 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800650 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700651 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700652 }
653
654 return null;
655 }
656
657 /**
658 * Returns the first activity (starting from the top of the stack) that
659 * is the same as the given activity. Returns null if no such activity
660 * is found.
661 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700662 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700663 ComponentName cls = intent.getComponent();
664 if (info.targetActivity != null) {
665 cls = new ComponentName(info.packageName, info.targetActivity);
666 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700667 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700668
Craig Mautner000f0022013-02-26 15:04:29 -0800669 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700670 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700671 final boolean notCurrentUserTask =
672 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700673 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700674
Craig Mautner000f0022013-02-26 15:04:29 -0800675 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
676 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700677 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700678 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700679 }
Craig Mautner000f0022013-02-26 15:04:29 -0800680 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700681 return r;
682 }
683 }
684 }
685
686 return null;
687 }
688
Amith Yamasani742a6712011-05-04 14:49:28 -0700689 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700690 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700691 */
Craig Mautner93529a42013-10-04 15:03:13 -0700692 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800693 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700694 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800695 }
696 mCurrentUser = userId;
697
698 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800699 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800700 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700701 final TaskRecord task = mTaskHistory.get(i);
702
703 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
704 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700705 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700706 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700707 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700708 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800709 mTaskHistory.remove(i);
710 mTaskHistory.add(task);
711 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800712 // Use same value for i.
713 } else {
714 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800715 }
716 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700717 if (VALIDATE_TOKENS) {
718 validateAppTokensLocked();
719 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700720 }
721
Craig Mautner2420ead2013-04-01 17:13:20 -0700722 void minimalResumeActivityLocked(ActivityRecord r) {
723 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700724 if (DEBUG_STATES) Slog.v(TAG_STATES,
725 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700726 r.stopped = false;
727 mResumedActivity = r;
728 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800729 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700730 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700731 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700732 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700733 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
734 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700735 }
736
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800737 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700738 if (r != null) {
739 mRecentTasks.addLocked(r.task);
740 r.task.touchActiveTime();
741 }
742 }
743
Narayan Kamath7829c812015-06-08 17:39:43 +0100744 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700745 if (mFullyDrawnStartTime != 0) {
746 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
747 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100748 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700749 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
750 }
751
752 private void stopFullyDrawnTraceIfNeeded() {
753 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
754 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
755 mFullyDrawnStartTime = 0;
756 }
757 }
758
Craig Mautnere79d42682013-04-01 19:01:53 -0700759 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700760 if (r.displayStartTime == 0) {
761 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
762 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100763 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700764 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700765 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700766 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100767 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700768 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700769 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700770 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800771
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700772 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700773 // Make sure that there is no activity waiting for this to launch.
774 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
775 r.displayStartTime = r.fullyDrawnStartTime = 0;
776 } else {
777 mStackSupervisor.removeTimeoutsForActivityLocked(r);
778 mStackSupervisor.scheduleIdleTimeoutLocked(r);
779 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700780 }
781
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800782 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800783 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800784 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
785 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
786 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
787 activities.get(activityNdx).setSleeping(false);
788 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800789 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800790 if (mPausingActivity != null) {
791 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
792 activityPausedLocked(mPausingActivity.appToken, true);
793 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800794 }
795
Craig Mautner0eea92c2013-05-16 13:35:39 -0700796 /**
797 * @return true if something must be done before going to sleep.
798 */
799 boolean checkReadyForSleepLocked() {
800 if (mResumedActivity != null) {
801 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700802 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
803 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
804 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700805 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700806 return true;
807 }
808 if (mPausingActivity != null) {
809 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700810 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700811 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800812 }
813
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700814 if (hasVisibleBehindActivity()) {
815 // Stop visible behind activity before going to sleep.
816 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
817 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700818 if (DEBUG_STATES) Slog.v(TAG_STATES,
819 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700820 return true;
821 }
822
Craig Mautner0eea92c2013-05-16 13:35:39 -0700823 return false;
824 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800825
Craig Mautner0eea92c2013-05-16 13:35:39 -0700826 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700827 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800828
Craig Mautner0eea92c2013-05-16 13:35:39 -0700829 // Make sure any stopped but visible activities are now sleeping.
830 // This ensures that the activity's onStop() is called.
831 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
832 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
833 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
834 final ActivityRecord r = activities.get(activityNdx);
835 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
836 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800837 }
838 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800839 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700840 }
Craig Mautner59c00972012-07-30 12:10:24 -0700841
Winson8b1871d2015-11-20 09:56:20 -0800842 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
843 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800844 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700845 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800846 return null;
847 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800848
Winson Chung083baf92014-07-11 10:32:42 -0700849 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700850 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800851 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700852 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700853 return null;
854 }
855
Winson Chung48a10a52014-08-27 14:36:51 -0700856 int w = mService.mThumbnailWidth;
857 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800858 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800859 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700860 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800861
862 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800863 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
864 // use within SystemUI while keeping memory usage low.
Winson8b1871d2015-11-20 09:56:20 -0800865 if (mService.mTakeFullscreenScreenshots) {
Winson8b1871d2015-11-20 09:56:20 -0800866 w = h = -1;
Winson387aac62015-11-25 11:18:56 -0800867 scale = 0.5f;
Winson8b1871d2015-11-20 09:56:20 -0800868 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800869 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800870 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800871 }
Winson Chung376543b2014-05-21 17:43:28 -0700872 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800873 return null;
874 }
875
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700876 /**
877 * Start pausing the currently resumed activity. It is an error to call this if there
878 * is already an activity being paused or there is no resumed activity.
879 *
880 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
881 * @param uiSleeping True if this is happening with the user interface going to sleep (the
882 * screen turning off).
883 * @param resuming True if this is being called as part of resuming the top activity, so
884 * we shouldn't try to instigate a resume here.
885 * @param dontWait True if the caller does not want to wait for the pause to complete. If
886 * set to true, we will immediately complete the pause here before returning.
887 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
888 * it to tell us when it is done.
889 */
890 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
891 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800892 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800893 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
894 + " state=" + mPausingActivity.state);
895 if (!mService.isSleeping()) {
896 // Avoid recursion among check for sleep and complete pause during sleeping.
897 // Because activity will be paused immediately after resume, just let pause
898 // be completed by the order of activity paused from clients.
899 completePauseLocked(false);
900 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800901 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700902 ActivityRecord prev = mResumedActivity;
903 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700904 if (!resuming) {
905 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
906 mStackSupervisor.resumeTopActivitiesLocked();
907 }
908 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700909 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800910
911 if (mActivityContainer.mParentActivity == null) {
912 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700913 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800914 }
915
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700916 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700917 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700918 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800919 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700920 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700921 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
922 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700923 prev.state = ActivityState.PAUSING;
924 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700925 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700926 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700927 if (mService.mHasRecents
928 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800929 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700930 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700931 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932
933 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800934
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700935 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700936 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700937 try {
938 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700939 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700940 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700941 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800942 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700943 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700944 } catch (Exception e) {
945 // Ignore exception, if process died other code will cleanup.
946 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800947 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700948 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700949 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700950 }
951 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800952 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700953 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700954 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700955 }
956
957 // If we are not going to sleep, we want to ensure the device is
958 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700959 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700960 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700961 }
962
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800963 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700964 // Have the window manager pause its key dispatching until the new
965 // activity has started. If we're pausing the activity just because
966 // the screen is being turned off and the UI is sleeping, don't interrupt
967 // key dispatch; the same activity will pick it up again on wakeup.
968 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800969 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700970 } else if (DEBUG_PAUSE) {
971 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700972 }
973
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700974 if (dontWait) {
975 // If the caller said they don't want to wait for the pause, then complete
976 // the pause now.
977 completePauseLocked(false);
978 return false;
979
980 } else {
981 // Schedule a pause timeout in case the app doesn't respond.
982 // We don't give it much time because this directly impacts the
983 // responsiveness seen by the user.
984 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
985 msg.obj = prev;
986 prev.pauseTime = SystemClock.uptimeMillis();
987 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700988 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700989 return true;
990 }
991
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700992 } else {
993 // This activity failed to schedule the
994 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700995 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700996 if (!resuming) {
997 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
998 }
999 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001000 }
1001 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001002
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001003 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001004 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1005 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001006
Craig Mautnerd2328952013-03-05 12:46:26 -08001007 final ActivityRecord r = isInStackLocked(token);
1008 if (r != null) {
1009 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1010 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001011 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001012 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001013 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -08001014 } else {
1015 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1016 r.userId, System.identityHashCode(r), r.shortComponentName,
1017 mPausingActivity != null
1018 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001019 if (r.state == ActivityState.PAUSING) {
1020 r.state = ActivityState.PAUSED;
1021 if (r.finishing) {
1022 if (DEBUG_PAUSE) Slog.v(TAG,
1023 "Executing finish of failed to pause activity: " + r);
1024 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1025 }
louis_chang047dfd42015-04-08 16:35:55 +08001026 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001027 }
1028 }
1029 }
1030
Craig Mautnera0026042014-04-23 11:45:37 -07001031 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1032 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001033 if (r.state != ActivityState.STOPPING) {
1034 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1035 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1036 return;
1037 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001038 if (persistentState != null) {
1039 r.persistentState = persistentState;
1040 mService.notifyTaskPersisterLocked(r.task, false);
1041 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001042 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001043 if (icicle != null) {
1044 // If icicle is null, this is happening due to a timeout, so we
1045 // haven't really saved the state.
1046 r.icicle = icicle;
1047 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001048 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001049 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001050 }
1051 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001052 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001053 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1054 r.stopped = true;
1055 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -07001056 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
1057 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001058 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001059 if (r.finishing) {
1060 r.clearOptionsLocked();
1061 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001062 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001063 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -07001064 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001065 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001066 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001067 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001068 }
1069 }
1070 }
1071
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001072 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001073 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001074 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001075
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001076 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001077 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001078 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001079 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001080 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001081 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001082 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001083 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001084 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001085 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1086 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001087 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001088 if (prev.configDestroy) {
1089 // The previous is being paused because the configuration
1090 // is changing, which means it is actually stopping...
1091 // To juggle the fact that we are also starting a new
1092 // instance right now, we need to first completely stop
1093 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001094 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001095 destroyActivityLocked(prev, true, "pause-config");
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001096 } else if (wasStopping) {
1097 // We are also stopping, the stop request must have gone soon after the pause.
1098 // We can't clobber it, because the stop confirmation will not be handled.
1099 // We don't need to schedule another stop, we only need to let it happen.
1100 prev.state = ActivityState.STOPPING;
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001101 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001102 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001103 // stopping.
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001104 addToStopping(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001105 }
1106 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001107 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001108 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001109 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001110 // It is possible the activity was freezing the screen before it was paused.
1111 // In that case go ahead and remove the freeze this activity has on the screen
1112 // since it is no longer visible.
1113 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001114 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001115 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001116
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001117 if (resumeNext) {
1118 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1119 if (!mService.isSleepingOrShuttingDown()) {
1120 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1121 } else {
1122 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001123 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001124 if (top == null || (prev != null && top != prev)) {
1125 // If there are no more activities available to run,
1126 // do resume anyway to start something. Also if the top
1127 // activity on the stack is not the just paused activity,
1128 // we need to go ahead and resume it to ensure we complete
1129 // an in-flight app switch.
1130 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1131 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001132 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001133 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001134
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001135 if (prev != null) {
1136 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001137
Craig Mautner525f3d92013-05-07 14:01:50 -07001138 if (prev.app != null && prev.cpuTimeAtResume > 0
1139 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001140 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1141 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001142 if (diff > 0) {
1143 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1144 synchronized (bsi) {
1145 BatteryStatsImpl.Uid.Proc ps =
1146 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001147 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001148 if (ps != null) {
1149 ps.addForegroundTimeLocked(diff);
1150 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001151 }
1152 }
1153 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001154 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001155 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001156
1157 // Notfiy when the task stack has changed
1158 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001159 }
1160
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001161 private void addToStopping(ActivityRecord r) {
1162 mStackSupervisor.mStoppingActivities.add(r);
1163 if (mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE ||
1164 r.frontOfTask && mTaskHistory.size() <= 1) {
1165 // If we already have a few activities waiting to stop,
1166 // then give up on things going idle and start clearing
1167 // them out. Or if r is the last of activity of the last task the stack
1168 // will be empty and must be cleared immediately.
1169 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
1170 mStackSupervisor.scheduleIdleLocked();
1171 } else {
1172 mStackSupervisor.checkReadyForSleepLocked();
1173 }
1174 }
1175
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001176 /**
1177 * Once we know that we have asked an application to put an activity in
1178 * the resumed state (either by launching it or explicitly telling it),
1179 * this function updates the rest of our state to match that fact.
1180 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001181 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001182 next.idle = false;
1183 next.results = null;
1184 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001185
Chong Zhang85ee6542015-10-02 13:36:38 -07001186 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001187 ProcessRecord app = next.task.mActivities.get(0).app;
1188 if (app != null && app != mService.mHomeProcess) {
1189 mService.mHomeProcess = app;
1190 }
1191 }
1192
Craig Mautner07566322013-09-26 16:42:55 -07001193 if (next.nowVisible) {
1194 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001195 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001196 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001197
1198 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001199 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001200
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001201 mStackSupervisor.reportResumedActivityLocked(next);
1202
1203 next.resumeKeyDispatchingLocked();
1204 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001205
1206 // Mark the point when the activity is resuming
1207 // TODO: To be more accurate, the mark should be before the onCreate,
1208 // not after the onResume. But for subsequent starts, onResume is fine.
1209 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001210 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001211 } else {
1212 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1213 }
Winson Chung376543b2014-05-21 17:43:28 -07001214
George Mount6ba042b2014-07-28 11:12:28 -07001215 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001216
1217 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1218 // When resuming an activity, require it to call requestVisibleBehind() again.
1219 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1220 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001221 }
1222
Craig Mautner2568c3a2015-03-26 14:22:34 -07001223 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001224 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001225 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1226 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1227 r.mUpdateTaskThumbnailWhenHidden = false;
1228 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001229 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001230 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001231 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001232 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001233 container.setVisible(visible);
1234 }
1235 }
1236
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001237 // Find the first visible activity above the passed activity and if it is translucent return it
1238 // otherwise return null;
1239 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1240 TaskRecord task = r.task;
1241 if (task == null) {
1242 return null;
1243 }
1244
1245 ActivityStack stack = task.stack;
1246 if (stack == null) {
1247 return null;
1248 }
1249
1250 int stackNdx = mStacks.indexOf(stack);
1251
1252 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1253 int taskNdx = tasks.indexOf(task);
1254
1255 ArrayList<ActivityRecord> activities = task.mActivities;
1256 int activityNdx = activities.indexOf(r) + 1;
1257
1258 final int numStacks = mStacks.size();
1259 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001260 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001261 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001262 final int numTasks = tasks.size();
1263 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001264 final TaskRecord currentTask = tasks.get(taskNdx);
1265 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001266 final int numActivities = activities.size();
1267 while (activityNdx < numActivities) {
1268 final ActivityRecord activity = activities.get(activityNdx);
1269 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001270 return historyStack.mFullscreen
1271 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001272 }
1273 ++activityNdx;
1274 }
1275 activityNdx = 0;
1276 ++taskNdx;
1277 }
1278 taskNdx = 0;
1279 ++stackNdx;
1280 }
1281
1282 return null;
1283 }
1284
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001285 private ActivityStack getNextVisibleStackLocked() {
1286 ArrayList<ActivityStack> stacks = mStacks;
1287 final ActivityRecord parent = mActivityContainer.mParentActivity;
1288 if (parent != null) {
1289 stacks = parent.task.stack.mStacks;
1290 }
1291 if (stacks != null) {
1292 for (int i = stacks.size() - 1; i >= 0; --i) {
1293 ActivityStack stack = stacks.get(i);
1294 if (stack != this && stack.isStackVisibleLocked()) {
1295 return stack;
1296 }
1297 }
1298 }
1299 return null;
1300 }
1301
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001302 /** Returns true if the stack contains a fullscreen task. */
1303 private boolean hasFullscreenTask() {
1304 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1305 final TaskRecord task = mTaskHistory.get(i);
1306 if (task.mFullscreen) {
1307 return true;
1308 }
1309 }
1310 return false;
1311 }
1312
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001313 private boolean hasTranslucentActivity(ActivityStack stack) {
1314 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1315 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1316 final TaskRecord task = tasks.get(taskNdx);
1317 final ArrayList<ActivityRecord> activities = task.mActivities;
1318 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1319 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001320
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001321 // Conditions for an activity to obscure the stack we're
1322 // examining:
1323 // 1. Not Finishing AND Visible AND:
1324 // 2. Either:
1325 // - Full Screen Activity OR
1326 // - On top of Home and our stack is NOT home
1327 if (!r.finishing && r.visible && (r.fullscreen ||
1328 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1329 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001330 }
1331 }
1332 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001333 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001334 }
1335
1336 /** Returns true if the stack is considered visible. */
Todd Kennedyaab56db2015-01-30 09:39:53 -08001337 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001338 if (!isAttached()) {
1339 return false;
1340 }
1341
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001342 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001343 return true;
1344 }
1345
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001346 final int stackIndex = mStacks.indexOf(this);
1347
1348 if (stackIndex == mStacks.size() - 1) {
1349 Slog.wtf(TAG,
1350 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1351 return false;
1352 }
1353
Chong Zhang75b37202015-12-04 14:16:36 -08001354 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1355 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
1356 return false;
1357 }
1358
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001359 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1360 final int focusedStackId = focusedStack.mStackId;
1361
1362 if (mStackId == DOCKED_STACK_ID) {
1363 // Docked stack is always visible, except in the case where the home activity
1364 // is the top running activity in the focused home stack.
1365 if (focusedStackId != HOME_STACK_ID) {
1366 return true;
1367 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001368 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked();
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001369 return topHomeActivity == null || !topHomeActivity.isHomeActivity();
1370 }
1371
Chong Zhangb16cf342015-11-12 17:22:40 -08001372 // Find the first stack below focused stack that actually got something visible.
1373 int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1374 while (belowFocusedIndex >= 0 &&
1375 mStacks.get(belowFocusedIndex).topRunningActivityLocked() == null) {
1376 belowFocusedIndex--;
1377 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001378 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1379 && stackIndex == belowFocusedIndex) {
1380 // Stacks directly behind the docked or pinned stack are always visible.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001381 return true;
1382 }
1383
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001384 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1385 && hasTranslucentActivity(focusedStack)) {
1386 // Stacks behind the fullscreen stack with a translucent activity are always
1387 // visible so they can act as a backdrop to the translucent activity.
1388 // For example, dialog activities
1389 if (stackIndex == belowFocusedIndex) {
1390 return true;
1391 }
1392 if (belowFocusedIndex >= 0) {
1393 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001394 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1395 && stackIndex == (belowFocusedIndex - 1)) {
1396 // The stack behind the docked or pinned stack is also visible so we can have a
1397 // complete backdrop to the translucent activity when the docked stack is up.
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001398 return true;
1399 }
1400 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001401 }
1402
Wale Ogunwale3797c222015-10-27 14:21:58 -07001403 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001404 // Visibility of any static stack should have been determined by the conditions above.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001405 return false;
1406 }
1407
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001408 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001409 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001410
1411 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1412 continue;
1413 }
1414
Wale Ogunwale3797c222015-10-27 14:21:58 -07001415 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001416 // These stacks can't have any dynamic stacks visible behind them.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001417 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001418 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001419
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001420 if (!hasTranslucentActivity(stack)) {
1421 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001422 }
1423 }
1424
1425 return true;
1426 }
1427
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001428 final int rankTaskLayers(int baseLayer) {
1429 int layer = 0;
1430 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1431 final TaskRecord task = mTaskHistory.get(taskNdx);
1432 ActivityRecord r = task.topRunningActivityLocked();
1433 if (r == null || r.finishing || !r.visible) {
1434 task.mLayerRank = -1;
1435 } else {
1436 task.mLayerRank = baseLayer + layer++;
1437 }
1438 }
1439 return layer;
1440 }
1441
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001442 /**
1443 * Make sure that all activities that need to be visible (that is, they
1444 * currently can be seen by the user) actually are.
1445 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001446 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1447 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001448 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001449 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001450 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001451 if (top != null) {
1452 checkTranslucentActivityWaiting(top);
1453 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001454
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001455 // If the top activity is not fullscreen, then we need to
1456 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001457 boolean aboveTop = top != null;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001458 final boolean stackInvisible = !isStackVisibleLocked();
1459 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001460 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001461
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001462 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001463 final TaskRecord task = mTaskHistory.get(taskNdx);
1464 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001465
Craig Mautnerd44711d2013-02-23 11:24:36 -08001466 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1467 final ActivityRecord r = activities.get(activityNdx);
1468 if (r.finishing) {
1469 continue;
1470 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001471 final boolean isTop = r == top;
1472 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001473 continue;
1474 }
1475 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001476 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1477 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001478 if ((!behindFullscreenActivity || r.mLaunchTaskBehind)
1479 && okToShowLocked(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001480 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1481 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001482 + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001483 // First: if this is not the current activity being started, make
1484 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001485 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001486 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001487 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001488
1489 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001490 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001491 noStackActivityResumed, r)) {
1492 if (activityNdx >= activities.size()) {
1493 // Record may be removed if its process needs to restart.
1494 activityNdx = activities.size() - 1;
1495 } else {
1496 noStackActivityResumed = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001497 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001498 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001499 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001500 // If this activity is already visible, then there is nothing to do here.
1501 if (handleAlreadyVisible(r)) {
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001502 noStackActivityResumed = false;
1503 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001504 } else {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001505 makeVisible(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001506 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001507 // Aggregate current change flags.
1508 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001509 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1510 behindFullscreenActivity, task, r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001511 } else {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001512 makeInvisible(stackInvisible, behindFullscreenActivity, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001513 }
1514 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001515 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1516 // The visibility of tasks and the activities they contain in freeform stack are
1517 // determined individually unlike other stacks where the visibility or fullscreen
1518 // status of an activity in a previous task affects other.
1519 behindFullscreenActivity = stackInvisible;
1520 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001521 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001522
1523 if (mTranslucentActivityWaiting != null &&
1524 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1525 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1526 notifyActivityDrawnLocked(null);
1527 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001528 }
Craig Mautner58547802013-03-05 08:23:53 -08001529
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001530 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1531 if (mTranslucentActivityWaiting != top) {
1532 mUndrawnActivitiesBelowTopTranslucent.clear();
1533 if (mTranslucentActivityWaiting != null) {
1534 // Call the callback with a timeout indication.
1535 notifyActivityDrawnLocked(null);
1536 mTranslucentActivityWaiting = null;
1537 }
1538 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1539 }
1540 }
1541
1542 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001543 boolean isTop, boolean noStackActivityResumed, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001544 // We need to make sure the app is running if it's the top, or it is just made visible from
1545 // invisible. If the app is already visible, it must have died while it was visible. In this
1546 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1547 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001548 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001549 // This activity needs to be visible, but isn't even running...
1550 // get it started and resume if no other stack in this stack is resumed.
1551 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1552 if (r != starting) {
1553 r.startFreezingScreenLocked(r.app, configChanges);
1554 }
1555 if (!r.visible || r.mLaunchTaskBehind) {
1556 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1557 setVisible(r, true);
1558 }
1559 if (r != starting) {
1560 mStackSupervisor.startSpecificActivityLocked(r, noStackActivityResumed, false);
1561 return true;
1562 }
1563 }
1564 return false;
1565 }
1566
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001567 private void makeInvisible(boolean stackInvisible, boolean behindFullscreenActivity,
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001568 ActivityRecord r) {
1569 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r + " finishing="
1570 + r.finishing + " state=" + r.state + " stackInvisible=" + stackInvisible
1571 + " behindFullscreenActivity=" + behindFullscreenActivity);
1572 if (!r.visible) {
1573 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1574 return;
1575 }
1576 // Now for any activities that aren't visible to the user, make sure they no longer are
1577 // keeping the screen frozen.
1578 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
1579 try {
1580 setVisible(r, false);
1581 switch (r.state) {
1582 case STOPPING:
1583 case STOPPED:
1584 if (r.app != null && r.app.thread != null) {
1585 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1586 "Scheduling invisibility: " + r);
1587 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1588 }
1589 break;
1590
1591 case INITIALIZING:
1592 case RESUMED:
1593 case PAUSING:
1594 case PAUSED:
1595 // This case created for transitioning activities from
1596 // translucent to opaque {@link Activity#convertToOpaque}.
1597 if (getVisibleBehindActivity() == r) {
1598 releaseBackgroundResources(r);
1599 } else {
1600 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1601 mStackSupervisor.mStoppingActivities.add(r);
1602 }
1603 mStackSupervisor.scheduleIdleLocked();
1604 }
1605 break;
1606
1607 default:
1608 break;
1609 }
1610 } catch (Exception e) {
1611 // Just skip on any failure; we'll make it visible when it next restarts.
1612 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1613 }
1614 }
1615
1616 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1617 TaskRecord task, ActivityRecord r) {
1618 if (r.fullscreen) {
1619 // At this point, nothing else needs to be shown in this task.
1620 behindFullscreenActivity = true;
1621 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
1622 + " stackInvisible=" + stackInvisible
1623 + " behindFullscreenActivity=" + behindFullscreenActivity);
1624 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
1625 behindFullscreenActivity = true;
1626 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1627 + " stackInvisible=" + stackInvisible
1628 + " behindFullscreenActivity=" + behindFullscreenActivity);
1629 }
1630 return behindFullscreenActivity;
1631 }
1632
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001633 private void makeVisible(ActivityRecord starting, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001634 // This activity is not currently visible, but is running. Tell it to become visible.
1635 r.visible = true;
1636 if (r.state != ActivityState.RESUMED && r != starting) {
1637 // If this activity is paused, tell it to now show its window.
1638 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1639 "Making visible and scheduling visibility: " + r);
1640 try {
1641 if (mTranslucentActivityWaiting != null) {
1642 r.updateOptionsLocked(r.returningOptions);
1643 mUndrawnActivitiesBelowTopTranslucent.add(r);
1644 }
1645 setVisible(r, true);
1646 r.sleeping = false;
1647 r.app.pendingUiClean = true;
1648 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1649 r.stopFreezingScreenLocked(false);
1650 } catch (Exception e) {
1651 // Just skip on any failure; we'll make it
1652 // visible when it next restarts.
1653 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
1654 }
1655 }
1656 }
1657
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001658 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001659 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1660 r.stopFreezingScreenLocked(false);
1661 try {
1662 if (r.returningOptions != null) {
1663 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1664 }
1665 } catch(RemoteException e) {
1666 }
1667 return r.state == ActivityState.RESUMED;
1668 }
1669
Todd Kennedyaab56db2015-01-30 09:39:53 -08001670 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001671 mTranslucentActivityWaiting = r;
1672 mUndrawnActivitiesBelowTopTranslucent.clear();
1673 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1674 }
1675
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001676 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1677 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1678 final TaskRecord task = mTaskHistory.get(taskNdx);
1679 final ArrayList<ActivityRecord> activities = task.mActivities;
1680 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1681 final ActivityRecord r = activities.get(activityNdx);
1682 if ( r.appTimeTracker != except) {
1683 r.appTimeTracker = null;
1684 }
1685 }
1686 }
1687 }
1688
Craig Mautner5eda9b32013-07-02 11:58:16 -07001689 /**
1690 * Called as activities below the top translucent activity are redrawn. When the last one is
1691 * redrawn notify the top activity by calling
1692 * {@link Activity#onTranslucentConversionComplete}.
1693 *
1694 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1695 * occurred and the activity will be notified immediately.
1696 */
1697 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001698 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001699 if ((r == null)
1700 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1701 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1702 // The last undrawn activity below the top has just been drawn. If there is an
1703 // opaque activity at the top, notify it that it can become translucent safely now.
1704 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1705 mTranslucentActivityWaiting = null;
1706 mUndrawnActivitiesBelowTopTranslucent.clear();
1707 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1708
Craig Mautner71dd1b62014-02-18 15:48:52 -08001709 if (waitingActivity != null) {
1710 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1711 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1712 try {
1713 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1714 waitingActivity.appToken, r != null);
1715 } catch (RemoteException e) {
1716 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001717 }
1718 }
1719 }
1720 }
1721
Craig Mautnera61bc652013-10-28 15:43:18 -07001722 /** If any activities below the top running one are in the INITIALIZING state and they have a
1723 * starting window displayed then remove that starting window. It is possible that the activity
1724 * in this state will never resumed in which case that starting window will be orphaned. */
1725 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001726 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001727 boolean aboveTop = true;
1728 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1729 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1730 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1731 final ActivityRecord r = activities.get(activityNdx);
1732 if (aboveTop) {
1733 if (r == topActivity) {
1734 aboveTop = false;
1735 }
1736 continue;
1737 }
1738
1739 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001740 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1741 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001742 r.mStartingWindowShown = false;
1743 mWindowManager.removeAppStartingWindow(r.appToken);
1744 }
1745 }
1746 }
1747 }
1748
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001749 /**
1750 * Ensure that the top activity in the stack is resumed.
1751 *
1752 * @param prev The previously resumed activity, for when in the process
1753 * of pausing; can be null to call from elsewhere.
1754 *
1755 * @return Returns true if something is being resumed, or false if
1756 * nothing happened.
1757 */
1758 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001759 return resumeTopActivityLocked(prev, null);
1760 }
1761
Chong Zhang280d3322015-11-03 17:27:26 -08001762 final boolean resumeTopActivityLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001763 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001764 // Don't even start recursing.
1765 return false;
1766 }
1767
1768 boolean result = false;
1769 try {
1770 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001771 mStackSupervisor.inResumeTopActivity = true;
1772 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1773 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001774 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001775 }
Craig Mautner544efa72014-09-04 16:41:20 -07001776 result = resumeTopActivityInnerLocked(prev, options);
1777 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001778 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001779 }
1780 return result;
1781 }
1782
Chong Zhang280d3322015-11-03 17:27:26 -08001783 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001784 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001785
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001786 if (!mService.mBooting && !mService.mBooted) {
1787 // Not ready yet!
1788 return false;
1789 }
1790
Craig Mautnerdf88d732014-01-27 09:21:32 -08001791 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001792 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001793 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001794 // Do not resume this stack if its parent is not resumed.
1795 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1796 return false;
1797 }
1798
Craig Mautnera61bc652013-10-28 15:43:18 -07001799 cancelInitializingActivities();
1800
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001801 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001802 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001803
1804 // Remember how we'll process this pause/resume situation, and ensure
1805 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001806 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1807 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001808
Craig Mautner84984fa2014-06-19 11:19:20 -07001809 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001810 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001811 // There are no more activities!
1812 final String reason = "noMoreActivities";
1813 if (!mFullscreen) {
1814 // Try to move focus to the next visible stack with a running activity if this
1815 // stack is not covering the entire screen.
1816 final ActivityStack stack = getNextVisibleStackLocked();
1817 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1818 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1819 }
1820 }
1821 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001822 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001823 if (DEBUG_STATES) Slog.d(TAG_STATES,
1824 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001825 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001826 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001827 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1828 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1829 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001830 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001831 }
1832
1833 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001834
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001836 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1837 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001838 // Make sure we have executed any pending transitions, since there
1839 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001840 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001841 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001842 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001843 if (DEBUG_STATES) Slog.d(TAG_STATES,
1844 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001845 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001846 return false;
1847 }
1848
Craig Mautner525f3d92013-05-07 14:01:50 -07001849 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001850 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001851 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001852 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001853 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001854 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001855 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001856 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001857 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001858 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001859 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001860 } else if (!isOnHomeDisplay()) {
1861 return false;
1862 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001863 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001864 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001865 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1866 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1867 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001868 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001869 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001870 }
1871
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001872 // If we are sleeping, and there is no resumed activity, and the top
1873 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001874 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001875 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001876 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001877 // Make sure we have executed any pending transitions, since there
1878 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001879 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001880 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001881 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001882 if (DEBUG_STATES) Slog.d(TAG_STATES,
1883 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001884 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001885 return false;
1886 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001887
1888 // Make sure that the user who owns this activity is started. If not,
1889 // we will just leave it as is because someone should be bringing
1890 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001891 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001892 Slog.w(TAG, "Skipping resume of top activity " + next
1893 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001894 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001895 return false;
1896 }
1897
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001898 // The activity may be waiting for stop, but that is no longer
1899 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001900 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001901 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001902 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001903 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001904
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001905 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001906
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001907 // If we are currently pausing an activity, then don't do anything
1908 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001909 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001910 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001911 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001912 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001913 return false;
1914 }
1915
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001916 // Okay we are now going to start a switch, to 'next'. We may first
1917 // have to pause the current activity, but this is an important point
1918 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001919 // XXX "App Redirected" dialog is getting too many false positives
1920 // at this point, so turn off for now.
1921 if (false) {
1922 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1923 long now = SystemClock.uptimeMillis();
1924 final boolean inTime = mLastStartedActivity.startTime != 0
1925 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1926 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1927 final int nextUid = next.info.applicationInfo.uid;
1928 if (inTime && lastUid != nextUid
1929 && lastUid != next.launchedFromUid
1930 && mService.checkPermission(
1931 android.Manifest.permission.STOP_APP_SWITCHES,
1932 -1, next.launchedFromUid)
1933 != PackageManager.PERMISSION_GRANTED) {
1934 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1935 } else {
1936 next.startTime = now;
1937 mLastStartedActivity = next;
1938 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001939 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001940 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001941 mLastStartedActivity = next;
1942 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001943 }
Craig Mautner58547802013-03-05 08:23:53 -08001944
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001945 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1946
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001947 // We need to start pausing the current activity so the top one
1948 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001949 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1950 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001951 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001952 if (DEBUG_STATES) Slog.d(TAG_STATES,
1953 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001954 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001955 }
1956 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001957 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001958 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001959 // At this point we want to put the upcoming activity's process
1960 // at the top of the LRU list, since we know we will be needing it
1961 // very soon and it would be a waste to let it get killed if it
1962 // happens to be sitting towards the end.
1963 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001964 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001965 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001966 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001967 return true;
1968 }
1969
Christopher Tated3f175c2012-06-14 14:16:54 -07001970 // If the most recent activity was noHistory but was only stopped rather
1971 // than stopped+finished because the device went to sleep, we need to make
1972 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001973 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001974 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001975 if (DEBUG_STATES) Slog.d(TAG_STATES,
1976 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001977 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001978 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001979 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001980 }
1981
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001982 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001983 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1984 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001985 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001986 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1987 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001988 } else {
1989 // The next activity is already visible, so hide the previous
1990 // activity's windows right now so we can show the new one ASAP.
1991 // We only do this if the previous is finishing, which should mean
1992 // it is on top of the one being resumed so hiding it quickly
1993 // is good. Otherwise, we want to do the normal route of allowing
1994 // the resumed activity to be shown so we can decide if the
1995 // previous should actually be hidden depending on whether the
1996 // new one is found to be full-screen or not.
1997 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001998 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001999 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2000 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002001 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002002 + ", nowVisible=" + next.nowVisible);
2003 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002004 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002005 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002006 + ", waitingVisible="
2007 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2008 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002009 }
2010 }
2011 }
2012
Dianne Hackborne7f97212011-02-24 14:40:20 -08002013 // Launching this app's activity, make sure the app is no longer
2014 // considered stopped.
2015 try {
2016 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002017 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002018 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002019 } catch (IllegalArgumentException e) {
2020 Slog.w(TAG, "Failed trying to unstop package "
2021 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002022 }
2023
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002024 // We are starting up the next activity, so tell the window manager
2025 // that the previous one will be hidden soon. This way it can know
2026 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002027 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002028 if (prev != null) {
2029 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002030 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002031 "Prepare close transition: prev=" + prev);
2032 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002033 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002034 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002035 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002036 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002037 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2038 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002039 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002040 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002041 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002042 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2043 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002044 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002045 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002046 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002047 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002048 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002049 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002050 : next.mLaunchTaskBehind
2051 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2052 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002053 }
2054 }
Craig Mautner967212c2013-04-13 21:10:58 -07002055 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002056 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002057 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002058 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002059 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002060 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002061 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002062 }
2063 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002064
2065 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002066 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002067 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2068 if (opts != null) {
2069 resumeAnimOptions = opts.toBundle();
2070 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002071 next.applyOptionsLocked();
2072 } else {
2073 next.clearOptionsLocked();
2074 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002075
Craig Mautnercf910b02013-04-23 11:23:27 -07002076 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002077 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002078 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002079
2080 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002081 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002082
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002083 // schedule launch ticks to collect information about slow apps.
2084 next.startLaunchTickingLocked();
2085
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002086 ActivityRecord lastResumedActivity =
2087 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002088 ActivityState lastState = next.state;
2089
2090 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002091
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002092 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002093 next.state = ActivityState.RESUMED;
2094 mResumedActivity = next;
2095 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002096 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002097 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002098 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002099 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002100
2101 // Have the window manager re-evaluate the orientation of
2102 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002103 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002104 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002105 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002106 mService.mConfiguration,
2107 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2108 if (config != null) {
2109 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002110 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002111 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002112 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002113
Craig Mautner525f3d92013-05-07 14:01:50 -07002114 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002115 // The configuration update wasn't able to keep the existing
2116 // instance of the activity, and instead started a new one.
2117 // We should be all done, but let's just make sure our activity
2118 // is still at the top and schedule another run if something
2119 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002120 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002121 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002122 "Activity config changed during resume: " + next
2123 + ", new next: " + nextNext);
2124 if (nextNext != next) {
2125 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002126 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002127 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002128 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002129 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002130 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002131 return true;
2132 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002133 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002134 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002135 }
Craig Mautner58547802013-03-05 08:23:53 -08002136
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002137 try {
2138 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002139 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002140 if (a != null) {
2141 final int N = a.size();
2142 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002143 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2144 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002145 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002146 }
2147 }
2148
2149 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002150 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002151 }
2152
Craig Mautner299f9602015-01-26 09:47:33 -08002153 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2154 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002155
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002156 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002157 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002158 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002159 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002160 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002161 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002162 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002163
Craig Mautner0eea92c2013-05-16 13:35:39 -07002164 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002165
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002166 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002167 } catch (Exception e) {
2168 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002169 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002170 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002171 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002172 if (lastStack != null) {
2173 lastStack.mResumedActivity = lastResumedActivity;
2174 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002175 Slog.i(TAG, "Restarting because process died: " + next);
2176 if (!next.hasBeenLaunched) {
2177 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002178 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2179 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002180 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002181 next.appToken, next.packageName, next.theme,
2182 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002183 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2184 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002185 }
George Mount2c92c972014-03-20 09:38:23 -07002186 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002187 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002188 return true;
2189 }
2190
2191 // From this point on, if something goes wrong there is no way
2192 // to recover the activity.
2193 try {
2194 next.visible = true;
2195 completeResumeLocked(next);
2196 } catch (Exception e) {
2197 // If any exception gets thrown, toss away this
2198 // activity and try the next one.
2199 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002200 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002201 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002202 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002203 return true;
2204 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002205 next.stopped = false;
2206
2207 } else {
2208 // Whoops, need to restart this activity!
2209 if (!next.hasBeenLaunched) {
2210 next.hasBeenLaunched = true;
2211 } else {
2212 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002213 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002214 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002215 mService.compatibilityInfoForPackageLocked(
2216 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002217 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002218 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002219 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002220 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002221 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002222 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002223 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002224 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002225 }
2226
Craig Mautnercf910b02013-04-23 11:23:27 -07002227 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002228 return true;
2229 }
2230
riddle_hsuc215a4f2014-12-27 12:10:45 +08002231 private TaskRecord getNextTask(TaskRecord targetTask) {
2232 final int index = mTaskHistory.indexOf(targetTask);
2233 if (index >= 0) {
2234 final int numTasks = mTaskHistory.size();
2235 for (int i = index + 1; i < numTasks; ++i) {
2236 TaskRecord task = mTaskHistory.get(i);
2237 if (task.userId == targetTask.userId) {
2238 return task;
2239 }
2240 }
2241 }
2242 return null;
2243 }
2244
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002245 private void insertTaskAtPosition(TaskRecord task, int position) {
2246 if (position >= mTaskHistory.size()) {
2247 insertTaskAtTop(task, null);
2248 return;
2249 }
2250 // Calculate maximum possible position for this task.
2251 int maxPosition = mTaskHistory.size();
2252 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002253 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002254 // Put non-current user tasks below current user tasks.
2255 while (maxPosition > 0) {
2256 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2257 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002258 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002259 break;
2260 }
2261 maxPosition--;
2262 }
2263 }
2264 position = Math.min(position, maxPosition);
2265 mTaskHistory.remove(task);
2266 mTaskHistory.add(position, task);
2267 updateTaskMovement(task, true);
2268 }
2269
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002270 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002271 // If the moving task is over home stack, transfer its return type to next task
2272 if (task.isOverHomeStack()) {
2273 final TaskRecord nextTask = getNextTask(task);
2274 if (nextTask != null) {
2275 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2276 }
2277 }
2278
Craig Mautner9c85c202013-10-04 20:11:26 -07002279 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002280 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002281 if (isOnHomeDisplay()) {
2282 ActivityStack lastStack = mStackSupervisor.getLastStack();
2283 final boolean fromHome = lastStack.isHomeStack();
2284 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002285 task.setTaskToReturnTo(fromHome
2286 ? lastStack.topTask() == null
2287 ? HOME_ACTIVITY_TYPE
2288 : lastStack.topTask().taskType
2289 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002290 }
2291 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002292 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002293 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002294
Craig Mautnerac6f8432013-07-17 13:24:59 -07002295 mTaskHistory.remove(task);
2296 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002297 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002298 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002299 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002300 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002301 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002302 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002303 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002304 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002305 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002306 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002307 break;
2308 }
2309 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002310 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002311 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002312 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002313 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002314 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002315
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002316 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2317 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002318 TaskRecord rTask = r.task;
2319 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002320 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2321 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002322 // Last activity in task had been removed or ActivityManagerService is reusing task.
2323 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002324 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002325 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002326 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002327 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002328 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002329 if (!newTask) {
2330 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002331 boolean startIt = true;
2332 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2333 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002334 if (task.getTopActivity() == null) {
2335 // All activities in task are finishing.
2336 continue;
2337 }
Craig Mautner70a86932013-02-28 22:37:44 -08002338 if (task == r.task) {
2339 // Here it is! Now, if this is not yet visible to the
2340 // user, then just add it without starting; it will
2341 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002342 if (!startIt) {
2343 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2344 + task, new RuntimeException("here").fillInStackTrace());
2345 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002346 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002347 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002349 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002350 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002351 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002352 return;
2353 }
2354 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002355 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002356 startIt = false;
2357 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002358 }
2359 }
2360
2361 // Place a new activity at top of stack, so it is next to interact
2362 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002363
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002364 // If we are not placing the new activity frontmost, we do not want
2365 // to deliver the onUserLeaving callback to the actual frontmost
2366 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002367 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002368 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002369 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002370 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002371 }
Craig Mautner70a86932013-02-28 22:37:44 -08002372
2373 task = r.task;
2374
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002375 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002376 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002377 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002378 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002379 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002380
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002381 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002382 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002383 // We want to show the starting preview window if we are
2384 // switching to a new task, or the next activity's process is
2385 // not currently running.
2386 boolean showStartingIcon = newTask;
2387 ProcessRecord proc = r.app;
2388 if (proc == null) {
2389 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2390 }
2391 if (proc == null || proc.thread == null) {
2392 showStartingIcon = true;
2393 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002394 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002395 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002396 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002397 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002398 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002399 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002400 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002401 ? r.mLaunchTaskBehind
2402 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2403 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002404 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002405 mNoAnimActivities.remove(r);
2406 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002407 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002408 boolean doShow = true;
2409 if (newTask) {
2410 // Even though this activity is starting fresh, we still need
2411 // to reset it to make sure we apply affinities to move any
2412 // existing activities from other tasks in to it.
2413 // If the caller has requested that the target task be
2414 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002415 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002416 resetTaskIfNeededLocked(r, r);
2417 doShow = topRunningNonDelayedActivityLocked(null) == r;
2418 }
Chong Zhang280d3322015-11-03 17:27:26 -08002419 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002420 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2421 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002422 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002423 if (r.mLaunchTaskBehind) {
2424 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2425 // tell WindowManager that r is visible even though it is at the back of the stack.
2426 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002427 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002428 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002429 // Figure out if we are transitioning from another activity that is
2430 // "has the same starting icon" as the next one. This allows the
2431 // window manager to keep the previous window it had previously
2432 // created, if it still had one.
2433 ActivityRecord prev = mResumedActivity;
2434 if (prev != null) {
2435 // We don't want to reuse the previous starting preview if:
2436 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002437 if (prev.task != r.task) {
2438 prev = null;
2439 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002440 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002441 else if (prev.nowVisible) {
2442 prev = null;
2443 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002444 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002445 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002446 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002447 mService.compatibilityInfoForPackageLocked(
2448 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002449 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002450 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002451 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002452 }
2453 } else {
2454 // If this is the first activity, don't do any fancy animations,
2455 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002456 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002457 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002458 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002459 }
2460 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002461 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002462 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002463 }
2464
Dianne Hackbornbe707852011-11-11 14:32:10 -08002465 final void validateAppTokensLocked() {
2466 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002467 mValidateAppTokens.ensureCapacity(numActivities());
2468 final int numTasks = mTaskHistory.size();
2469 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2470 TaskRecord task = mTaskHistory.get(taskNdx);
2471 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002472 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002473 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002474 }
Craig Mautner000f0022013-02-26 15:04:29 -08002475 TaskGroup group = new TaskGroup();
2476 group.taskId = task.taskId;
2477 mValidateAppTokens.add(group);
2478 final int numActivities = activities.size();
2479 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2480 final ActivityRecord r = activities.get(activityNdx);
2481 group.tokens.add(r.appToken);
2482 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002483 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002484 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002485 }
2486
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002487 /**
2488 * Perform a reset of the given task, if needed as part of launching it.
2489 * Returns the new HistoryRecord at the top of the task.
2490 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002491 /**
2492 * Helper method for #resetTaskIfNeededLocked.
2493 * We are inside of the task being reset... we'll either finish this activity, push it out
2494 * for another task, or leave it as-is.
2495 * @param task The task containing the Activity (taskTop) that might be reset.
2496 * @param forceReset
2497 * @return An ActivityOptions that needs to be processed.
2498 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002499 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002500 ActivityOptions topOptions = null;
2501
2502 int replyChainEnd = -1;
2503 boolean canMoveOptions = true;
2504
2505 // We only do this for activities that are not the root of the task (since if we finish
2506 // the root, we may no longer have the task!).
2507 final ArrayList<ActivityRecord> activities = task.mActivities;
2508 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002509 final int rootActivityNdx = task.findEffectiveRootIndex();
2510 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002511 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002512 if (target.frontOfTask)
2513 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002514
2515 final int flags = target.info.flags;
2516 final boolean finishOnTaskLaunch =
2517 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2518 final boolean allowTaskReparenting =
2519 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2520 final boolean clearWhenTaskReset =
2521 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2522
2523 if (!finishOnTaskLaunch
2524 && !clearWhenTaskReset
2525 && target.resultTo != null) {
2526 // If this activity is sending a reply to a previous
2527 // activity, we can't do anything with it now until
2528 // we reach the start of the reply chain.
2529 // XXX note that we are assuming the result is always
2530 // to the previous activity, which is almost always
2531 // the case but we really shouldn't count on.
2532 if (replyChainEnd < 0) {
2533 replyChainEnd = i;
2534 }
2535 } else if (!finishOnTaskLaunch
2536 && !clearWhenTaskReset
2537 && allowTaskReparenting
2538 && target.taskAffinity != null
2539 && !target.taskAffinity.equals(task.affinity)) {
2540 // If this activity has an affinity for another
2541 // task, then we need to move it out of here. We will
2542 // move it as far out of the way as possible, to the
2543 // bottom of the activity stack. This also keeps it
2544 // correctly ordered with any activities we previously
2545 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002546 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002547 final ActivityRecord bottom =
2548 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002549 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002550 if (bottom != null && target.taskAffinity != null
2551 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002552 // If the activity currently at the bottom has the
2553 // same task affinity as the one we are moving,
2554 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002555 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002556 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002557 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002558 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002559 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002560 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002561 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002562 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002563 + " out to new task " + target.task);
2564 }
2565
Wale Ogunwale706ed792015-08-02 10:29:44 -07002566 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002567
Craig Mautner525f3d92013-05-07 14:01:50 -07002568 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002569 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2570 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002571 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002572 if (p.finishing) {
2573 continue;
2574 }
2575
Craig Mautnere3a74d52013-02-22 14:14:58 -08002576 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002577 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002578 topOptions = p.takeOptionsLocked();
2579 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002580 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002581 }
2582 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002583 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2584 "Removing activity " + p + " from task=" + task + " adding to task="
2585 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002586 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2587 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002588 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002589 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002590
Wale Ogunwale706ed792015-08-02 10:29:44 -07002591 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002592 }
2593
Wale Ogunwale706ed792015-08-02 10:29:44 -07002594 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002595 if (VALIDATE_TOKENS) {
2596 validateAppTokensLocked();
2597 }
2598
2599 replyChainEnd = -1;
2600 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2601 // If the activity should just be removed -- either
2602 // because it asks for it, or the task should be
2603 // cleared -- then finish it and anything that is
2604 // part of its reply chain.
2605 int end;
2606 if (clearWhenTaskReset) {
2607 // In this case, we want to finish this activity
2608 // and everything above it, so be sneaky and pretend
2609 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002610 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002611 } else if (replyChainEnd < 0) {
2612 end = i;
2613 } else {
2614 end = replyChainEnd;
2615 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002616 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002617 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002618 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002619 if (p.finishing) {
2620 continue;
2621 }
2622 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002623 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002624 topOptions = p.takeOptionsLocked();
2625 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002626 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002627 }
2628 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002629 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002630 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002631 if (finishActivityLocked(
2632 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002633 end--;
2634 srcPos--;
2635 }
2636 }
2637 replyChainEnd = -1;
2638 } else {
2639 // If we were in the middle of a chain, well the
2640 // activity that started it all doesn't want anything
2641 // special, so leave it all as-is.
2642 replyChainEnd = -1;
2643 }
2644 }
2645
2646 return topOptions;
2647 }
2648
2649 /**
2650 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2651 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2652 * @param affinityTask The task we are looking for an affinity to.
2653 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2654 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2655 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2656 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002657 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002658 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002659 int replyChainEnd = -1;
2660 final int taskId = task.taskId;
2661 final String taskAffinity = task.affinity;
2662
2663 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2664 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002665 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2666
2667 // Do not operate on or below the effective root Activity.
2668 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002669 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002670 if (target.frontOfTask)
2671 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002672
2673 final int flags = target.info.flags;
2674 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2675 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2676
2677 if (target.resultTo != null) {
2678 // If this activity is sending a reply to a previous
2679 // activity, we can't do anything with it now until
2680 // we reach the start of the reply chain.
2681 // XXX note that we are assuming the result is always
2682 // to the previous activity, which is almost always
2683 // the case but we really shouldn't count on.
2684 if (replyChainEnd < 0) {
2685 replyChainEnd = i;
2686 }
2687 } else if (topTaskIsHigher
2688 && allowTaskReparenting
2689 && taskAffinity != null
2690 && taskAffinity.equals(target.taskAffinity)) {
2691 // This activity has an affinity for our task. Either remove it if we are
2692 // clearing or move it over to our task. Note that
2693 // we currently punt on the case where we are resetting a
2694 // task that is not at the top but who has activities above
2695 // with an affinity to it... this is really not a normal
2696 // case, and we will need to later pull that task to the front
2697 // and usually at that point we will do the reset and pick
2698 // up those remaining activities. (This only happens if
2699 // someone starts an activity in a new task from an activity
2700 // in a task that is not currently on top.)
2701 if (forceReset || finishOnTaskLaunch) {
2702 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002703 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2704 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002705 for (int srcPos = start; srcPos >= i; --srcPos) {
2706 final ActivityRecord p = activities.get(srcPos);
2707 if (p.finishing) {
2708 continue;
2709 }
Todd Kennedy539db512014-12-15 09:57:55 -08002710 finishActivityLocked(
2711 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002712 }
2713 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002714 if (taskInsertionPoint < 0) {
2715 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002716
Craig Mautner77878772013-03-04 19:46:24 -08002717 }
Craig Mautner77878772013-03-04 19:46:24 -08002718
2719 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002720 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2721 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2722 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002723 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002724 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002725 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002726 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002727
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002728 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2729 "Removing and adding activity " + p + " to stack at " + task
2730 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002731 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2732 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002733 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002734 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002735 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002736 if (VALIDATE_TOKENS) {
2737 validateAppTokensLocked();
2738 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002739
2740 // Now we've moved it in to place... but what if this is
2741 // a singleTop activity and we have put it on top of another
2742 // instance of the same activity? Then we drop the instance
2743 // below so it remains singleTop.
2744 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2745 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002746 int targetNdx = taskActivities.indexOf(target);
2747 if (targetNdx > 0) {
2748 ActivityRecord p = taskActivities.get(targetNdx - 1);
2749 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002750 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2751 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002752 }
2753 }
2754 }
2755 }
2756
2757 replyChainEnd = -1;
2758 }
2759 }
Craig Mautner77878772013-03-04 19:46:24 -08002760 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002761 }
2762
Craig Mautner8849a5e2013-04-02 16:41:03 -07002763 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002764 ActivityRecord newActivity) {
2765 boolean forceReset =
2766 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2767 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2768 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2769 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2770 forceReset = true;
2771 }
2772 }
2773
2774 final TaskRecord task = taskTop.task;
2775
2776 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2777 * for remaining tasks. Used for later tasks to reparent to task. */
2778 boolean taskFound = false;
2779
2780 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2781 ActivityOptions topOptions = null;
2782
Craig Mautner77878772013-03-04 19:46:24 -08002783 // Preserve the location for reparenting in the new task.
2784 int reparentInsertionPoint = -1;
2785
Craig Mautnere3a74d52013-02-22 14:14:58 -08002786 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2787 final TaskRecord targetTask = mTaskHistory.get(i);
2788
2789 if (targetTask == task) {
2790 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2791 taskFound = true;
2792 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002793 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2794 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002795 }
2796 }
2797
Craig Mautner70a86932013-02-28 22:37:44 -08002798 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002799 if (taskNdx >= 0) {
2800 do {
2801 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2802 } while (taskTop == null && taskNdx >= 0);
2803 }
Craig Mautner70a86932013-02-28 22:37:44 -08002804
Craig Mautnere3a74d52013-02-22 14:14:58 -08002805 if (topOptions != null) {
2806 // If we got some ActivityOptions from an activity on top that
2807 // was removed from the task, propagate them to the new real top.
2808 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002809 taskTop.updateOptionsLocked(topOptions);
2810 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002811 topOptions.abort();
2812 }
2813 }
2814
2815 return taskTop;
2816 }
2817
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002818 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2819 String resultWho, int requestCode, int resultCode, Intent data) {
2820
2821 if (callingUid > 0) {
2822 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002823 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002824 }
2825
2826 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2827 + " : who=" + resultWho + " req=" + requestCode
2828 + " res=" + resultCode + " data=" + data);
2829 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2830 try {
2831 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2832 list.add(new ResultInfo(resultWho, requestCode,
2833 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002834 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002835 return;
2836 } catch (Exception e) {
2837 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2838 }
2839 }
2840
2841 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2842 }
2843
Craig Mautner299f9602015-01-26 09:47:33 -08002844 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002845 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2846 return;
2847 }
2848
2849 final ActivityRecord next = topRunningActivityLocked();
2850 final String myReason = reason + " adjustFocus";
2851 if (next != r) {
2852 if (next != null && StackId.keepFocusInStackIfPossible(mStackId)) {
2853 // For freeform, docked, and pinned stacks we always keep the focus within the
2854 // stack as long as there is a running activity in the stack that we can adjust
2855 // focus to.
2856 mService.setFocusedActivityLocked(next, myReason);
2857 return;
2858 } else {
2859 final TaskRecord task = r.task;
2860 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2861 // For non-fullscreen stack, we want to move the focus to the next visible
2862 // stack to prevent the home screen from moving to the top and obscuring
2863 // other visible stacks.
2864 if (!mFullscreen
2865 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2866 return;
2867 }
2868 // Move the home stack to the top if this stack is fullscreen or there is no
2869 // other visible stack.
2870 if (mStackSupervisor.moveHomeStackTaskToTop(
2871 task.getTaskToReturnTo(), myReason)) {
2872 // Activity focus was already adjusted. Nothing else to do...
2873 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002874 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002875 }
2876 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08002877 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002878
Wale Ogunwaled045c822015-12-02 09:14:28 -08002879 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2880 if (top != null) {
2881 mService.setFocusedActivityLocked(top, myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002882 }
2883 }
2884
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002885 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2886 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2887 final String myReason = reason + " adjustFocusToNextVisibleStack";
2888 if (stack == null) {
2889 return false;
2890 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002891 final ActivityRecord top = stack.topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002892 if (top == null) {
2893 return false;
2894 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002895 mService.setFocusedActivityLocked(top, myReason);
2896 return true;
2897 }
2898
Craig Mautnerf3333272013-04-22 10:55:53 -07002899 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002900 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002901 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2902 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2903 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002904 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002905 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002906 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2907 "stop-no-history", false)) {
2908 // Activity was finished, no need to continue trying to schedule stop.
2909 adjustFocusedActivityLocked(r, "stopActivityFinished");
2910 r.resumeKeyDispatchingLocked();
2911 return;
2912 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002913 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002914 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002915 + " on stop because we're just sleeping");
2916 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002917 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002918 }
2919
2920 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002921 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002922 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002923 try {
2924 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002925 if (DEBUG_STATES) Slog.v(TAG_STATES,
2926 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002928 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2929 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002930 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002931 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002932 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002933 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002934 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002935 r.setSleeping(true);
2936 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002937 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002938 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002939 } catch (Exception e) {
2940 // Maybe just ignore exceptions here... if the process
2941 // has crashed, our death notification will clean things
2942 // up.
2943 Slog.w(TAG, "Exception thrown during pause", e);
2944 // Just in case, assume it to be stopped.
2945 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002946 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002947 r.state = ActivityState.STOPPED;
2948 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002949 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002950 }
2951 }
2952 }
2953 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002954
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002955 /**
2956 * @return Returns true if the activity is being finished, false if for
2957 * some reason it is being left as-is.
2958 */
2959 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002960 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002961 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002962 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2963 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002964 + ", result=" + resultCode + ", data=" + resultData
2965 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002966 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002967 return false;
2968 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002969
Craig Mautnerd44711d2013-02-23 11:24:36 -08002970 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002971 return true;
2972 }
2973
Craig Mautnerd2328952013-03-05 12:46:26 -08002974 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002975 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2976 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2977 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2978 ActivityRecord r = activities.get(activityNdx);
2979 if (r.resultTo == self && r.requestCode == requestCode) {
2980 if ((r.resultWho == null && resultWho == null) ||
2981 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2982 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2983 false);
2984 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002985 }
2986 }
2987 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002988 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002989 }
2990
Todd Kennedy539db512014-12-15 09:57:55 -08002991 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002992 ActivityRecord r = topRunningActivityLocked();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002993 if (r != null && r.app == app) {
2994 // If the top running activity is from this crashing
2995 // process, then terminate it to avoid getting in a loop.
2996 Slog.w(TAG, " Force finishing activity "
2997 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002998 int taskNdx = mTaskHistory.indexOf(r.task);
2999 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08003000 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003001 // Also terminate any activities below it that aren't yet
3002 // stopped, to avoid a situation where one will get
3003 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08003004 --activityNdx;
3005 if (activityNdx < 0) {
3006 do {
3007 --taskNdx;
3008 if (taskNdx < 0) {
3009 break;
3010 }
3011 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3012 } while (activityNdx < 0);
3013 }
3014 if (activityNdx >= 0) {
3015 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003016 if (r.state == ActivityState.RESUMED
3017 || r.state == ActivityState.PAUSING
3018 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07003019 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003020 Slog.w(TAG, " Force finishing activity "
3021 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08003022 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003023 }
3024 }
3025 }
3026 }
3027 }
3028
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003029 final void finishVoiceTask(IVoiceInteractionSession session) {
3030 IBinder sessionBinder = session.asBinder();
3031 boolean didOne = false;
3032 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3033 TaskRecord tr = mTaskHistory.get(taskNdx);
3034 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3035 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3036 ActivityRecord r = tr.mActivities.get(activityNdx);
3037 if (!r.finishing) {
3038 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3039 false);
3040 didOne = true;
3041 }
3042 }
3043 }
3044 }
3045 if (didOne) {
3046 mService.updateOomAdjLocked();
3047 }
3048 }
3049
Craig Mautnerd2328952013-03-05 12:46:26 -08003050 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003051 ArrayList<ActivityRecord> activities = r.task.mActivities;
3052 for (int index = activities.indexOf(r); index >= 0; --index) {
3053 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003054 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003055 break;
3056 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003057 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003058 }
3059 return true;
3060 }
3061
Dianne Hackborn5c607432012-02-28 14:44:19 -08003062 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3063 // send the result
3064 ActivityRecord resultTo = r.resultTo;
3065 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003066 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003067 + " who=" + r.resultWho + " req=" + r.requestCode
3068 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003069 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003070 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003071 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003072 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003073 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003074 if (r.info.applicationInfo.uid > 0) {
3075 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3076 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003077 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003078 }
3079 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3080 resultData);
3081 r.resultTo = null;
3082 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003083 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003084
3085 // Make sure this HistoryRecord is not holding on to other resources,
3086 // because clients have remote IPC references to this object so we
3087 // can't assume that will go away and want to avoid circular IPC refs.
3088 r.results = null;
3089 r.pendingResults = null;
3090 r.newIntents = null;
3091 r.icicle = null;
3092 }
3093
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003094 /**
3095 * @return Returns true if this activity has been removed from the history
3096 * list, or false if it is still in the list and will be removed later.
3097 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003098 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3099 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003100 if (r.finishing) {
3101 Slog.w(TAG, "Duplicate finish request for " + r);
3102 return false;
3103 }
3104
Wale Ogunwale7d701172015-03-11 15:36:30 -07003105 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003106 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003107 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003108 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003109 task.taskId, r.shortComponentName, reason);
3110 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003111 final int index = activities.indexOf(r);
3112 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003113 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003114 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003115 // If the caller asked that this activity (and all above it)
3116 // be cleared when the task is reset, don't lose that information,
3117 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003118 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003119 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003120 }
3121 }
3122
3123 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003124
Craig Mautner299f9602015-01-26 09:47:33 -08003125 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003126
Dianne Hackborn5c607432012-02-28 14:44:19 -08003127 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003128
Craig Mautnerde4ef022013-04-07 19:01:33 -07003129 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003130 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003131 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003132 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003133 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003134 ? AppTransition.TRANSIT_TASK_CLOSE
3135 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003136
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003137 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003138 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003139
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003140 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003141 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3142 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3143 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003144 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003145 }
3146
Craig Mautneraea74a52014-03-08 14:23:10 -08003147 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003148 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003149 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003150 } else if (r.state != ActivityState.PAUSING) {
3151 // If the activity is PAUSING, we will complete the finish once
3152 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003153 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003154 if (r.visible) {
3155 mWindowManager.setAppVisibility(r.appToken, false);
3156 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003157 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003158 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003159 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003160 }
3161
3162 return false;
3163 }
3164
Craig Mautnerf3333272013-04-22 10:55:53 -07003165 static final int FINISH_IMMEDIATELY = 0;
3166 static final int FINISH_AFTER_PAUSE = 1;
3167 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003168
Craig Mautnerf3333272013-04-22 10:55:53 -07003169 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003170 // First things first: if this activity is currently visible,
3171 // and the resumed activity is not yet visible, then hold off on
3172 // finishing until the resumed one becomes visible.
3173 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003174 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08003175 addToStopping(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003176 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003177 if (DEBUG_STATES) Slog.v(TAG_STATES,
3178 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003179 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003180 if (oomAdj) {
3181 mService.updateOomAdjLocked();
3182 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003183 return r;
3184 }
3185
3186 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003187 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003188 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003189 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003190 if (mResumedActivity == r) {
3191 mResumedActivity = null;
3192 }
3193 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003194 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003195 r.state = ActivityState.FINISHING;
3196
3197 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003198 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003199 || prevState == ActivityState.STOPPED
3200 || prevState == ActivityState.INITIALIZING) {
3201 // If this activity is already stopped, we can just finish
3202 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003203 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003204 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003205 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003206 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003207 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003208 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003209 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3210 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003211 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003212 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003213
3214 // Need to go through the full pause cycle to get this
3215 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003216 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003217 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003218 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003219 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003220 return r;
3221 }
3222
Craig Mautneree36c772014-07-16 14:56:05 -07003223 void finishAllActivitiesLocked(boolean immediately) {
3224 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003225 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3226 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3227 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3228 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003229 noActivitiesInStack = false;
3230 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003231 continue;
3232 }
Craig Mautneree36c772014-07-16 14:56:05 -07003233 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003234 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3235 }
3236 }
Craig Mautneree36c772014-07-16 14:56:05 -07003237 if (noActivitiesInStack) {
3238 mActivityContainer.onTaskListEmptyLocked();
3239 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003240 }
3241
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003242 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3243 // Basic case: for simple app-centric recents, we need to recreate
3244 // the task if the affinity has changed.
3245 if (srec == null || srec.task.affinity == null ||
3246 !srec.task.affinity.equals(destAffinity)) {
3247 return true;
3248 }
3249 // Document-centric case: an app may be split in to multiple documents;
3250 // they need to re-create their task if this current activity is the root
3251 // of a document, unless simply finishing it will return them to the the
3252 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003253 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3254 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003255 // Okay, this activity is at the root of its task. What to do, what to do...
3256 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3257 // Finishing won't return to an application, so we need to recreate.
3258 return true;
3259 }
3260 // We now need to get the task below it to determine what to do.
3261 int taskIdx = mTaskHistory.indexOf(srec.task);
3262 if (taskIdx <= 0) {
3263 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3264 return false;
3265 }
3266 if (taskIdx == 0) {
3267 // At the bottom of the stack, nothing to go back to.
3268 return true;
3269 }
3270 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3271 if (!srec.task.affinity.equals(prevTask.affinity)) {
3272 // These are different apps, so need to recreate.
3273 return true;
3274 }
3275 }
3276 return false;
3277 }
3278
Wale Ogunwale7d701172015-03-11 15:36:30 -07003279 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003280 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003281 final TaskRecord task = srec.task;
3282 final ArrayList<ActivityRecord> activities = task.mActivities;
3283 final int start = activities.indexOf(srec);
3284 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003285 return false;
3286 }
3287 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003288 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003289 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003290 final ComponentName dest = destIntent.getComponent();
3291 if (start > 0 && dest != null) {
3292 for (int i = finishTo; i >= 0; i--) {
3293 ActivityRecord r = activities.get(i);
3294 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003295 r.info.name.equals(dest.getClassName())) {
3296 finishTo = i;
3297 parent = r;
3298 foundParentInTask = true;
3299 break;
3300 }
3301 }
3302 }
3303
3304 IActivityController controller = mService.mController;
3305 if (controller != null) {
3306 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3307 if (next != null) {
3308 // ask watcher if this is allowed
3309 boolean resumeOK = true;
3310 try {
3311 resumeOK = controller.activityResuming(next.packageName);
3312 } catch (RemoteException e) {
3313 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003314 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003315 }
3316
3317 if (!resumeOK) {
3318 return false;
3319 }
3320 }
3321 }
3322 final long origId = Binder.clearCallingIdentity();
3323 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003324 ActivityRecord r = activities.get(i);
3325 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003326 // Only return the supplied result for the first activity finished
3327 resultCode = Activity.RESULT_CANCELED;
3328 resultData = null;
3329 }
3330
3331 if (parent != null && foundParentInTask) {
3332 final int parentLaunchMode = parent.info.launchMode;
3333 final int destIntentFlags = destIntent.getFlags();
3334 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3335 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3336 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3337 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003338 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3339 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003340 } else {
3341 try {
3342 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3343 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003344 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003345 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003346 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003347 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003348 foundParentInTask = res == ActivityManager.START_SUCCESS;
3349 } catch (RemoteException e) {
3350 foundParentInTask = false;
3351 }
3352 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003353 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003354 }
3355 }
3356 Binder.restoreCallingIdentity(origId);
3357 return foundParentInTask;
3358 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003359 /**
3360 * Perform the common clean-up of an activity record. This is called both
3361 * as part of destroyActivityLocked() (when destroying the client-side
3362 * representation) and cleaning things up as a result of its hosting
3363 * processing going away, in which case there is no remaining client-side
3364 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003365 *
3366 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003367 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003368 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3369 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003370 if (mResumedActivity == r) {
3371 mResumedActivity = null;
3372 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003373 if (mPausingActivity == r) {
3374 mPausingActivity = null;
3375 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003376 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003377
3378 r.configDestroy = false;
3379 r.frozenBeforeDestroy = false;
3380
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003381 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003382 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003383 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003384 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003385 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003386 }
3387
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003388 // Make sure this record is no longer in the pending finishes list.
3389 // This could happen, for example, if we are trimming activities
3390 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003391 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003392 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003393
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003394 // Remove any pending results.
3395 if (r.finishing && r.pendingResults != null) {
3396 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3397 PendingIntentRecord rec = apr.get();
3398 if (rec != null) {
3399 mService.cancelIntentSenderLocked(rec, false);
3400 }
3401 }
3402 r.pendingResults = null;
3403 }
3404
3405 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003406 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003407 }
3408
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003409 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003410 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003411 if (getVisibleBehindActivity() == r) {
3412 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003413 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003414 }
3415
3416 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003417 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003418 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003419 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003420 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003421 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003422 }
3423
Craig Mautner299f9602015-01-26 09:47:33 -08003424 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003425 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003426 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003427 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003428 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3429 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3430
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003431 r.takeFromHistory();
3432 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003433 if (DEBUG_STATES) Slog.v(TAG_STATES,
3434 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003435 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003436 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003437 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003438 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003439 if (VALIDATE_TOKENS) {
3440 validateAppTokensLocked();
3441 }
Craig Mautner312ba862014-02-10 17:55:01 -08003442 final TaskRecord task = r.task;
3443 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003444 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003445 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003446 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003447 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003448 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003449 }
Craig Mautner299f9602015-01-26 09:47:33 -08003450 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003451 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003452 cleanUpActivityServicesLocked(r);
3453 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003454 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003455
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003456 /**
3457 * Perform clean-up of service connections in an activity record.
3458 */
3459 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3460 // Throw away any services that have been bound by this activity.
3461 if (r.connections != null) {
3462 Iterator<ConnectionRecord> it = r.connections.iterator();
3463 while (it.hasNext()) {
3464 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003465 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003466 }
3467 r.connections = null;
3468 }
3469 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003470
Craig Mautneree2e45a2014-06-27 12:10:03 -07003471 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003472 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003473 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003474 mHandler.sendMessage(msg);
3475 }
3476
Craig Mautneree2e45a2014-06-27 12:10:03 -07003477 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003478 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003479 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003480 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3481 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3482 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3483 final ActivityRecord r = activities.get(activityNdx);
3484 if (r.finishing) {
3485 continue;
3486 }
3487 if (r.fullscreen) {
3488 lastIsOpaque = true;
3489 }
3490 if (owner != null && r.app != owner) {
3491 continue;
3492 }
3493 if (!lastIsOpaque) {
3494 continue;
3495 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003496 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003497 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003498 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003499 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003500 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003501 activityRemoved = true;
3502 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003503 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003504 }
3505 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003506 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003507 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003508 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003509 }
3510
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003511 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3512 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003513 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3514 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003515 + " pausing=" + mPausingActivity + " for reason " + reason);
3516 return destroyActivityLocked(r, true, reason);
3517 }
3518 return false;
3519 }
3520
3521 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3522 String reason) {
3523 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003524 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003525 int maxTasks = tasks.size() / 4;
3526 if (maxTasks < 1) {
3527 maxTasks = 1;
3528 }
3529 int numReleased = 0;
3530 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3531 final TaskRecord task = mTaskHistory.get(taskNdx);
3532 if (!tasks.contains(task)) {
3533 continue;
3534 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003535 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003536 int curNum = 0;
3537 final ArrayList<ActivityRecord> activities = task.mActivities;
3538 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3539 final ActivityRecord activity = activities.get(actNdx);
3540 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003541 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003542 + " in state " + activity.state + " resumed=" + mResumedActivity
3543 + " pausing=" + mPausingActivity + " for reason " + reason);
3544 destroyActivityLocked(activity, true, reason);
3545 if (activities.get(actNdx) != activity) {
3546 // Was removed from list, back up so we don't miss the next one.
3547 actNdx--;
3548 }
3549 curNum++;
3550 }
3551 }
3552 if (curNum > 0) {
3553 numReleased += curNum;
3554 maxTasks--;
3555 if (mTaskHistory.get(taskNdx) != task) {
3556 // The entire task got removed, back up so we don't miss the next one.
3557 taskNdx--;
3558 }
3559 }
3560 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003561 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3562 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003563 return numReleased;
3564 }
3565
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003566 /**
3567 * Destroy the current CLIENT SIDE instance of an activity. This may be
3568 * called both when actually finishing an activity, or when performing
3569 * a configuration switch where we destroy the current client-side object
3570 * but then create a new client-side object for this same HistoryRecord.
3571 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003572 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003573 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3574 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003575 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003576 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003577 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003578 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003579
3580 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003581
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003582 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003583
3584 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003585
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003586 if (hadApp) {
3587 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003588 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003589 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3590 mService.mHeavyWeightProcess = null;
3591 mService.mHandler.sendEmptyMessage(
3592 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3593 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003594 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003595 // Update any services we are bound to that might care about whether
3596 // their client may have activities.
3597 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003598 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003599 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003600 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003601 }
3602 }
3603
3604 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003605
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003606 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003607 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003608 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003609 r.configChangeFlags);
3610 } catch (Exception e) {
3611 // We can just ignore exceptions here... if the process
3612 // has crashed, our death notification will clean things
3613 // up.
3614 //Slog.w(TAG, "Exception thrown during finish", e);
3615 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003616 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003617 removedFromHistory = true;
3618 skipDestroy = true;
3619 }
3620 }
3621
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003622 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003623
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003624 // If the activity is finishing, we need to wait on removing it
3625 // from the list to give it a chance to do its cleanup. During
3626 // that time it may make calls back with its token so we need to
3627 // be able to find it on the list and so we don't want to remove
3628 // it from the list yet. Otherwise, we can just immediately put
3629 // it in the destroyed state since we are not removing it from the
3630 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003631 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003632 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003633 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003634 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003635 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003636 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3637 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003638 if (DEBUG_STATES) Slog.v(TAG_STATES,
3639 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003640 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003641 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003642 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003643 }
3644 } else {
3645 // remove this record from the history.
3646 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003647 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003648 removedFromHistory = true;
3649 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003650 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003651 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003652 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003653 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003654 }
3655 }
3656
3657 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003658
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003659 if (!mLRUActivities.remove(r) && hadApp) {
3660 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3661 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003662
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003663 return removedFromHistory;
3664 }
3665
Craig Mautner299f9602015-01-26 09:47:33 -08003666 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003667 final long origId = Binder.clearCallingIdentity();
3668 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003669 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003670 if (r != null) {
3671 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003672 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003673 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003674
Wale Ogunwale60454db2015-01-23 16:05:07 -08003675 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003676 if (r.state == ActivityState.DESTROYING) {
3677 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003678 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003679 }
3680 }
Craig Mautner05d29032013-05-03 13:40:13 -07003681 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003682 } finally {
3683 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003684 }
3685 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003686
Todd Kennedyaab56db2015-01-30 09:39:53 -08003687 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003688 if (hasVisibleBehindActivity() &&
3689 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003690 if (r == topRunningActivityLocked()) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003691 // Don't release the top activity if it has requested to run behind the next
3692 // activity.
3693 return;
3694 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003695 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003696 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003697 " thread=" + r.app.thread);
3698 if (r != null && r.app != null && r.app.thread != null) {
3699 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003700 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003701 } catch (RemoteException e) {
3702 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003703 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003704 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003705 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003706 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003707 }
3708 }
3709 }
3710
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003711 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003712 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3713 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003714 if (r != null) {
3715 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003716 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003717 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003718 }
3719 mStackSupervisor.resumeTopActivitiesLocked();
3720 }
3721
Jose Lima4b6c6692014-08-12 17:41:12 -07003722 boolean hasVisibleBehindActivity() {
3723 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003724 }
3725
Jose Lima4b6c6692014-08-12 17:41:12 -07003726 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003727 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003728 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003729 }
3730 }
3731
Jose Lima4b6c6692014-08-12 17:41:12 -07003732 ActivityRecord getVisibleBehindActivity() {
3733 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003734 }
3735
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003736 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3737 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003738 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003739 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3740 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003741 while (i > 0) {
3742 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003743 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003744 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003745 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003746 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003747 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003748 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003749 }
3750 }
3751 }
3752
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003753 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3754 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003755 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3756 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003757 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3758 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003759 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003760 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003761 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3762 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003763
3764 boolean hasVisibleActivities = false;
3765
3766 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003767 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003768 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3769 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003770 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3771 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3772 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3773 final ActivityRecord r = activities.get(activityNdx);
3774 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003775 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3776 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003777 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003778 if (r.visible) {
3779 hasVisibleActivities = true;
3780 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003781 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003782 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3783 // Don't currently have state for the activity, or
3784 // it is finishing -- always remove it.
3785 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003786 } else if (!r.visible && r.launchCount > 2 &&
3787 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003788 // We have launched this activity too many times since it was
3789 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003790 // (Note if the activity is visible, we don't remove the record.
3791 // We leave the dead window on the screen but the process will
3792 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003793 remove = true;
3794 } else {
3795 // The process may be gone, but the activity lives on!
3796 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003797 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003798 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003799 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3800 "Removing activity " + r + " from stack at " + i
3801 + ": haveState=" + r.haveState
3802 + " stateNotNeeded=" + r.stateNotNeeded
3803 + " finishing=" + r.finishing
3804 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003805 if (!r.finishing) {
3806 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3807 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3808 r.userId, System.identityHashCode(r),
3809 r.task.taskId, r.shortComponentName,
3810 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003811 if (r.state == ActivityState.RESUMED) {
3812 mService.updateUsageStats(r, false);
3813 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003814 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003815 } else {
3816 // We have the current state for this activity, so
3817 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003818 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3819 if (DEBUG_APP) Slog.v(TAG_APP,
3820 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003821 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003822 // Set nowVisible to previous visible state. If the app was visible while
3823 // it died, we leave the dead window on screen so it's basically visible.
3824 // This is needed when user later tap on the dead window, we need to stop
3825 // other apps when user transfers focus to the restarted activity.
3826 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003827 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003828 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003829 "App died, clearing saved state of " + r);
3830 r.icicle = null;
3831 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003832 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003833 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003834 if (remove) {
3835 removeActivityFromHistoryLocked(r, "appDied");
3836 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003837 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003838 }
3839 }
3840
3841 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003842 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003843
Chong Zhang280d3322015-11-03 17:27:26 -08003844 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003845 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003846 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003847 if (r != null && r.state != ActivityState.RESUMED) {
3848 r.updateOptionsLocked(options);
3849 } else {
3850 ActivityOptions.abort(options);
3851 }
3852 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003853 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003854 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003855
Craig Mautner21d24a22014-04-23 11:45:37 -07003856 void updateTaskMovement(TaskRecord task, boolean toFront) {
3857 if (task.isPersistable) {
3858 task.mLastTimeMoved = System.currentTimeMillis();
3859 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3860 // recently will be most negative, tasks sent to the bottom before that will be less
3861 // negative. Similarly for recent tasks moved to the top which will be most positive.
3862 if (!toFront) {
3863 task.mLastTimeMoved *= -1;
3864 }
3865 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003866 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003867 }
3868
Craig Mautner84984fa2014-06-19 11:19:20 -07003869 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003870 final int top = mTaskHistory.size() - 1;
3871 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3872 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003873 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003874 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3875 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003876 mTaskHistory.remove(taskNdx);
3877 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003878 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003879 return;
3880 }
3881 }
3882 }
3883
Chong Zhang280d3322015-11-03 17:27:26 -08003884 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003885 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003886 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003887
Craig Mautner11bf9a52013-02-19 14:08:51 -08003888 final int numTasks = mTaskHistory.size();
3889 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003890 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003891 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003892 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003893 ActivityOptions.abort(options);
3894 } else {
3895 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3896 }
3897 return;
3898 }
3899
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003900 if (timeTracker != null) {
3901 // The caller wants a time tracker associated with this task.
3902 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3903 tr.mActivities.get(i).appTimeTracker = timeTracker;
3904 }
3905 }
3906
Craig Mautner11bf9a52013-02-19 14:08:51 -08003907 // Shift all activities with this task up to the top
3908 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003909 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003910
Chong Zhang45c25ce2015-08-10 22:18:26 -07003911 // Don't refocus if invisible to current user
3912 ActivityRecord top = tr.getTopActivity();
3913 if (!okToShowLocked(top)) {
3914 addRecentActivityLocked(top);
3915 ActivityOptions.abort(options);
3916 return;
3917 }
3918
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003919 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003920 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003921 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003922
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003923 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003924 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003925 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003926 if (r != null) {
3927 mNoAnimActivities.add(r);
3928 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003929 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003930 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003931 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003932 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003933
Craig Mautner05d29032013-05-03 13:40:13 -07003934 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003935 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003936
3937 if (VALIDATE_TOKENS) {
3938 validateAppTokensLocked();
3939 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003940 }
3941
3942 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003943 * Worker method for rearranging history stack. Implements the function of moving all
3944 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003945 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003946 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003947 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3948 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003949 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003950 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003951 * @return Returns true if the move completed, false if not.
3952 */
Craig Mautner299f9602015-01-26 09:47:33 -08003953 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003954 final TaskRecord tr = taskForIdLocked(taskId);
3955 if (tr == null) {
3956 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3957 return false;
3958 }
3959
3960 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003961 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003962
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003963 // If we have a watcher, preflight the move before committing to it. First check
3964 // for *other* available tasks, but if none are available, then try again allowing the
3965 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003966 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003967 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003968 if (next == null) {
3969 next = topRunningActivityLocked(null, 0);
3970 }
3971 if (next != null) {
3972 // ask watcher if this is allowed
3973 boolean moveOK = true;
3974 try {
3975 moveOK = mService.mController.activityResuming(next.packageName);
3976 } catch (RemoteException e) {
3977 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003978 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003979 }
3980 if (!moveOK) {
3981 return false;
3982 }
3983 }
3984 }
3985
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003986 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003987
riddle_hsuc215a4f2014-12-27 12:10:45 +08003988 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07003989
3990 // If true, we should resume the home activity next if the task we are moving to the
3991 // back is over the home stack. We force to false if the task we are moving to back
3992 // is the home task and we don't want it resumed after moving to the back.
3993 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
3994 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08003995 final TaskRecord nextTask = getNextTask(tr);
3996 if (nextTask != null) {
3997 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3998 } else {
3999 prevIsHome = true;
4000 }
4001 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004002 mTaskHistory.remove(tr);
4003 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004004 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004005
Craig Mautnerc8143c62013-09-03 12:15:57 -07004006 // There is an assumption that moving a task to the back moves it behind the home activity.
4007 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004008 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004009 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4010 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004011 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004012 break;
4013 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004014 if (taskNdx == 1) {
4015 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004016 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004017 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004018 }
4019
Craig Mautner299f9602015-01-26 09:47:33 -08004020 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004021 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004022
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004023 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004024 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004025 }
4026
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004027 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004028 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004029 if (!mService.mBooting && !mService.mBooted) {
4030 // Not ready yet!
4031 return false;
4032 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004033 final int taskToReturnTo = tr.getTaskToReturnTo();
4034 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004035 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004036 }
4037
Craig Mautner05d29032013-05-03 13:40:13 -07004038 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004039 return true;
4040 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004041
Craig Mautner8849a5e2013-04-02 16:41:03 -07004042 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004043 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004044 final Uri data = r.intent.getData();
4045 final String strData = data != null ? data.toSafeString() : null;
4046
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004047 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004048 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004049 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004050 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004051 }
4052
4053 /**
4054 * Make sure the given activity matches the current configuration. Returns
4055 * false if the activity had to be destroyed. Returns true if the
4056 * configuration is the same, or the activity will remain running as-is
4057 * for whatever reason. Ensures the HistoryRecord is updated with the
4058 * correct configuration and all other bookkeeping is handled.
4059 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004060 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4061 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004062 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004063 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004064 "Skipping config check (will change): " + r);
4065 return true;
4066 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004067
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004068 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004069 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004070
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004071 // Short circuit: if the two configurations are the exact same
4072 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004073 final Configuration newConfig = mService.mConfiguration;
4074 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004075 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004076 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004077 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004078 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004079 "Configuration unchanged in " + r);
4080 return true;
4081 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004082
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004083 // We don't worry about activities that are finishing.
4084 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004085 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004086 "Configuration doesn't matter in finishing " + r);
4087 r.stopFreezingScreenLocked(false);
4088 return true;
4089 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004090
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004091 // Okay we now are going to make this activity have the new config.
4092 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004093 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004094 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004095 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004096 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004097
Filip Gruszczynski23493322015-07-29 17:02:59 -07004098 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004099 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004100 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004101 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004102 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004103 // There are no significant differences, so we won't relaunch but should still deliver
4104 // the new configuration to the client process.
4105 r.scheduleConfigurationChanged(taskConfig, false);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004106 return true;
4107 }
4108
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004109 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4110 "Configuration changes for " + r + " ; taskChanges="
4111 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4112 + Configuration.configurationDiffToString(changes));
4113
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004114 // If the activity isn't currently running, just leave the new
4115 // configuration and it will pick that up next time it starts.
4116 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004117 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004118 "Configuration doesn't matter not running " + r);
4119 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004120 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004121 return true;
4122 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004123
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004124 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004125 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4126 "Checking to restart " + r.info.name + ": changed=0x"
4127 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004128 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4129 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004130
Dianne Hackborne6676352011-06-01 16:51:20 -07004131 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004132 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4133 r.configChangeFlags |= changes;
4134 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004135 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004136 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004137 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004138 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004139 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004140 } else if (r.state == ActivityState.PAUSING) {
4141 // A little annoying: we are waiting for this activity to
4142 // finish pausing. Let's not do anything now, but just
4143 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004144 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004145 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004146 r.configDestroy = true;
4147 return true;
4148 } else if (r.state == ActivityState.RESUMED) {
4149 // Try to optimize this case: the configuration is changing
4150 // and we need to restart the top, resumed activity.
4151 // Instead of doing the normal handshaking, just say
4152 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004153 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004154 "Config is relaunching resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004155 relaunchActivityLocked(r, r.configChangeFlags, true,
4156 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004157 r.configChangeFlags = 0;
4158 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004159 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004160 "Config is relaunching non-resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004161 relaunchActivityLocked(r, r.configChangeFlags, false,
4162 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004163 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004164 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004165
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004166 // All done... tell the caller we weren't able to keep this
4167 // activity around.
4168 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004169 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004170
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004171 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004172 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004173 // changes is always sent to all processes when they happen so it can just use whatever
4174 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004175 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004176 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004177
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004178 return true;
4179 }
4180
Filip Gruszczynski23493322015-07-29 17:02:59 -07004181 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4182 Configuration oldTaskOverride) {
4183 // Determine what has changed. May be nothing, if this is a config
4184 // that has come back from the app after going idle. In that case
4185 // we just want to leave the official config object now in the
4186 // activity and do nothing else.
4187 int taskChanges = oldTaskOverride.diff(taskConfig);
4188 // We don't want to use size changes if they don't cross boundaries that are important to
4189 // the app.
4190 if ((taskChanges & ActivityInfo.CONFIG_SCREEN_SIZE) != 0) {
4191 final boolean crosses = record.crossesHorizontalSizeThreshold(
4192 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4193 || record.crossesVerticalSizeThreshold(
4194 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4195 if (!crosses) {
4196 taskChanges &= ~ActivityInfo.CONFIG_SCREEN_SIZE;
4197 }
4198 }
4199 if ((taskChanges & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4200 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4201 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004202 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004203 taskChanges &= ~ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4204 }
4205 }
4206 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4207 }
4208
4209 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4210 Configuration oldTaskOverride, int taskChanges) {
4211 if (taskChanges == 0) {
4212 // {@link Configuration#diff} doesn't catch changes from unset values.
4213 // Check for changes we care about.
4214 if (oldTaskOverride.orientation != taskConfig.orientation) {
4215 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
4216 }
4217 // We want to explicitly track situations where the size configuration goes from
4218 // undefined to defined. We don't care about crossing the threshold in that case,
4219 // because there is no threshold.
4220 final int oldHeight = oldTaskOverride.screenHeightDp;
4221 final int newHeight = taskConfig.screenHeightDp;
4222 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4223 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4224 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4225 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4226 }
4227 final int oldWidth = oldTaskOverride.screenWidthDp;
4228 final int newWidth = taskConfig.screenWidthDp;
4229 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4230 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4231 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4232 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4233 }
4234 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4235 final int newSmallest = taskConfig.smallestScreenWidthDp;
4236 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4237 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4238 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4239 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4240 }
4241 }
4242 return taskChanges;
4243 }
4244
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004245 private static boolean isResizeOnlyChange(int change) {
4246 return (change & ~(ActivityInfo.CONFIG_SCREEN_SIZE
4247 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_ORIENTATION)) == 0;
4248 }
4249
Wale Ogunwale83301a92015-09-24 15:54:08 -07004250 private void relaunchActivityLocked(
4251 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4252 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
4253 return;
4254 }
4255
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004256 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004257 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004258 if (andResume) {
4259 results = r.results;
4260 newIntents = r.newIntents;
4261 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004262 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4263 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004264 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004265 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004266 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004267 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004268
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004269 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004270
Craig Mautner34b73df2014-01-12 21:11:08 -08004271 mStackSupervisor.removeChildActivityContainers(r);
4272
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004273 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004274 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4275 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004276 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004277 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4278 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004279 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004280 // Note: don't need to call pauseIfSleepingLocked() here, because
4281 // the caller will only pass in 'andResume' if this activity is
4282 // currently resumed, which implies we aren't sleeping.
4283 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004284 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004285 }
4286
4287 if (andResume) {
4288 r.results = null;
4289 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004290 r.state = ActivityState.RESUMED;
4291 } else {
4292 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4293 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004294 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004295 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004296
4297 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004298 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4299 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4300 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4301 final ActivityRecord r = activities.get(activityNdx);
4302 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004303 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004304 }
4305 if (r.fullscreen && !r.finishing) {
4306 return false;
4307 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004308 }
4309 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004310 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004311 if (r == null) {
4312 return false;
4313 }
4314 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4315 + " would have returned true for r=" + r);
4316 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004317 }
4318
4319 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004320 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4321 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4322 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4323 final ActivityRecord r = activities.get(activityNdx);
4324 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004325 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004326 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004327 }
4328 }
4329 }
4330
Wale Ogunwale540e1232015-05-01 15:35:39 -07004331 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4332 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004333 boolean didSomething = false;
4334 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004335 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004336 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4337 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4338 int numActivities = activities.size();
4339 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4340 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004341 final boolean sameComponent =
4342 (r.packageName.equals(packageName) && (filterByClasses == null
4343 || filterByClasses.contains(r.realActivity.getClassName())))
4344 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004345 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004346 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004347 && (r.app == null || evenPersistent || !r.app.persistent)) {
4348 if (!doit) {
4349 if (r.finishing) {
4350 // If this activity is just finishing, then it is not
4351 // interesting as far as something to stop.
4352 continue;
4353 }
4354 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004355 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004356 if (r.isHomeActivity()) {
4357 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4358 Slog.i(TAG, "Skip force-stop again " + r);
4359 continue;
4360 } else {
4361 homeActivity = r.realActivity;
4362 }
4363 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004364 didSomething = true;
4365 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004366 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004367 if (r.app != null) {
4368 r.app.removed = true;
4369 }
4370 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004371 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004372 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004373 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4374 true)) {
4375 // r has been deleted from mActivities, accommodate.
4376 --numActivities;
4377 --activityNdx;
4378 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004379 }
4380 }
4381 }
4382 return didSomething;
4383 }
4384
Dianne Hackborn09233282014-04-30 11:33:59 -07004385 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004386 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4387 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004388 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004389 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004390 if (task.getTopActivity() == null) {
4391 continue;
4392 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004393 ActivityRecord r = null;
4394 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004395 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004396 int numActivities = 0;
4397 int numRunning = 0;
4398 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004399 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004400 continue;
4401 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004402 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004403 tmp = activities.get(activityNdx);
4404 if (tmp.finishing) {
4405 continue;
4406 }
4407 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004408
Craig Mautneraab647e2013-02-28 16:31:36 -08004409 // Initialize state for next task if needed.
4410 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4411 top = r;
4412 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004413 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004414
4415 // Add 'r' into the current task.
4416 numActivities++;
4417 if (r.app != null && r.app.thread != null) {
4418 numRunning++;
4419 }
4420
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004421 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004422 TAG, r.intent.getComponent().flattenToShortString()
4423 + ": task=" + r.task);
4424 }
4425
4426 RunningTaskInfo ci = new RunningTaskInfo();
4427 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004428 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004429 ci.baseActivity = r.intent.getComponent();
4430 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004431 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004432 if (focusedStack && topTask) {
4433 // Give the latest time to ensure foreground task can be sorted
4434 // at the first, because lastActiveTime of creating task is 0.
4435 ci.lastActiveTime = System.currentTimeMillis();
4436 topTask = false;
4437 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004438
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004439 if (top.task != null) {
4440 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004441 }
4442 ci.numActivities = numActivities;
4443 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004444 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004445 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004446 }
4447
4448 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004449 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004450 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004451 if (top >= 0) {
4452 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4453 int activityTop = activities.size() - 1;
4454 if (activityTop > 0) {
4455 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4456 "unhandled-back", true);
4457 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004458 }
4459 }
4460
Craig Mautner6b74cb52013-09-27 17:02:21 -07004461 /**
4462 * Reset local parameters because an app's activity died.
4463 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004464 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004465 */
4466 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004467 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004468 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004469 "App died while pausing: " + mPausingActivity);
4470 mPausingActivity = null;
4471 }
4472 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4473 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004474 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004475 }
4476
Craig Mautner19091252013-10-05 00:03:53 -07004477 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004478 }
4479
Craig Mautnercae015f2013-02-08 14:31:27 -08004480 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004481 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4482 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4483 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4484 final ActivityRecord r = activities.get(activityNdx);
4485 if (r.app == app) {
4486 Slog.w(TAG, " Force finishing activity "
4487 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004488 // Force the destroy to skip right to removal.
4489 r.app = null;
4490 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004491 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004492 }
4493 }
4494 }
4495
Dianne Hackborn390517b2013-05-30 15:03:32 -07004496 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004497 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004498 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004499 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4500 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004501 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4502 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004503 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004504 " Task id #" + task.taskId + "\n" +
4505 " mFullscreen=" + task.mFullscreen + "\n" +
4506 " mBounds=" + task.mBounds + "\n" +
4507 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004508 if (printed) {
4509 header = null;
4510 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004511 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004512 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004513 }
4514
4515 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4516 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4517
4518 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004519 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4520 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004521 }
4522 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004523 final int top = mTaskHistory.size() - 1;
4524 if (top >= 0) {
4525 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4526 int listTop = list.size() - 1;
4527 if (listTop >= 0) {
4528 activities.add(list.get(listTop));
4529 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004530 }
4531 } else {
4532 ItemMatcher matcher = new ItemMatcher();
4533 matcher.build(name);
4534
Craig Mautneraab647e2013-02-28 16:31:36 -08004535 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4536 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4537 if (matcher.match(r1, r1.intent.getComponent())) {
4538 activities.add(r1);
4539 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004540 }
4541 }
4542 }
4543
4544 return activities;
4545 }
4546
4547 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004548 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004549
4550 // All activities that came from the package must be
4551 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004552 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4553 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4554 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4555 final ActivityRecord a = activities.get(activityNdx);
4556 if (a.info.packageName.equals(packageName)) {
4557 a.forceNewConfig = true;
4558 if (starting != null && a == starting && a.visible) {
4559 a.startFreezingScreenLocked(starting.app,
4560 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4561 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004562 }
4563 }
4564 }
4565
4566 return starting;
4567 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004568
Craig Mautner299f9602015-01-26 09:47:33 -08004569 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004570 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004571 }
4572
Wale Ogunwale000957c2015-04-03 08:19:12 -07004573 /**
4574 * Removes the input task from this stack.
4575 * @param task to remove.
4576 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004577 * @param moving task to another stack. In the case we are moving we don't want to perform
4578 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004579 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004580 void removeTask(TaskRecord task, String reason, boolean moving) {
4581 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004582 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004583 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004584 if (!StackId.persistTaskBounds(mStackId)) {
4585 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4586 // default configuration the next time it launches.
4587 task.updateOverrideConfiguration(null);
4588 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004589 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004590
Craig Mautner04a0ea62014-01-13 12:51:26 -08004591 final ActivityRecord r = mResumedActivity;
4592 if (r != null && r.task == task) {
4593 mResumedActivity = null;
4594 }
4595
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004596 final int taskNdx = mTaskHistory.indexOf(task);
4597 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004598 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4599 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4600 if (!nextTask.isOverHomeStack()) {
4601 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4602 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004603 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004604 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004605 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004606
Wale Ogunwale040b4702015-08-06 18:10:50 -07004607 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004608 final boolean isVoiceSession = task.voiceSession != null;
4609 if (isVoiceSession) {
4610 try {
4611 task.voiceSession.taskFinished(task.intent, task.taskId);
4612 } catch (RemoteException e) {
4613 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004614 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004615 if (task.autoRemoveFromRecents() || isVoiceSession) {
4616 // Task creator asked to remove this when done, or this task was a voice
4617 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004618 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004619 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004620 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004621 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004622
4623 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004624 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004625 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004626 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004627 String myReason = reason + " leftTaskHistoryEmpty";
4628 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004629 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004630 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004631 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004632 if (mStacks != null) {
4633 mStacks.remove(this);
4634 mStacks.add(0, this);
4635 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004636 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004637 mActivityContainer.onTaskListEmptyLocked();
4638 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004639 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004640
4641 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004642 }
4643
Dianne Hackborn91097de2014-04-04 18:02:06 -07004644 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4645 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4646 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004647 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4648 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004649 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08004650 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08004651 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
4652 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.mResizeable
4653 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004654 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004655 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004656 return task;
4657 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004658
Wale Ogunwale935e5022015-11-10 12:36:10 -08004659 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4660 if (mTaskPositioner == null) {
4661 return false;
4662 }
4663 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4664 return true;
4665 }
4666
Craig Mautnerc00204b2013-03-05 15:02:14 -08004667 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004668 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004669 }
4670
Wale Ogunwale5f986092015-12-04 15:35:38 -08004671 void addTask(final TaskRecord task, final boolean toTop, String reason) {
4672 final ActivityStack prevStack = preAddTask(task, reason);
4673
Craig Mautnerc00204b2013-03-05 15:02:14 -08004674 task.stack = this;
4675 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004676 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004677 } else {
4678 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004679 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004680 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08004681 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004682 }
4683
Wale Ogunwale5f986092015-12-04 15:35:38 -08004684 void positionTask(final TaskRecord task, int position) {
4685 final ActivityStack prevStack = preAddTask(task, "positionTask");
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004686 task.stack = this;
4687 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004688 postAddTask(task, prevStack);
4689 }
4690
4691 private ActivityStack preAddTask(TaskRecord task, String reason) {
4692 final ActivityStack prevStack = task.stack;
4693 if (prevStack != null && prevStack != this) {
4694 prevStack.removeTask(task, reason, MOVING);
4695 }
4696 return prevStack;
4697 }
4698
4699 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
4700 if (prevStack != null) {
4701 if (prevStack != this
4702 && (prevStack.mStackId == PINNED_STACK_ID || mStackId == PINNED_STACK_ID)) {
4703 task.reportPictureInPictureModeChange();
4704 }
4705 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004706 try {
4707 task.voiceSession.taskStarted(task.intent, task.taskId);
4708 } catch (RemoteException e) {
4709 }
4710 }
4711 }
4712
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004713 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07004714 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004715 task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004716 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4717 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
4718 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
4719 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
Wale Ogunwale61b009e2015-09-16 15:43:05 -07004720 bounds, task.mOverrideConfig, !r.isHomeActivity());
Chong Zhangb15758a2015-11-17 12:12:03 -08004721 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004722 r.taskConfigOverride = task.mOverrideConfig;
4723 }
4724
Wale Ogunwale079a0042015-10-24 11:44:07 -07004725 void setFocusAndResumeStateIfNeeded(
4726 ActivityRecord r, boolean setFocus, boolean setResume, String reason) {
4727 // If the activity had focus before move focus to this stack.
4728 if (setFocus) {
4729 // If the activity owns the last resumed activity, transfer that together,
4730 // so that we don't resume the same activity again in the new stack.
4731 // Apps may depend on onResume()/onPause() being called in pairs.
4732 if (setResume) {
4733 mResumedActivity = r;
4734 // Move the stack in which we are placing the activity to the front. We don't use
4735 // ActivityManagerService.setFocusedActivityLocked, because if the activity is
4736 // already focused, the call will short-circuit and do nothing.
4737 moveToFront(reason);
4738 } else {
4739 // We need to not only move the stack to the front, but also have the activity
4740 // focused. This will achieve both goals.
4741 mService.setFocusedActivityLocked(r, reason);
4742 }
4743 }
4744 }
4745
4746 /**
4747 * Moves the input activity from its current stack to this one.
4748 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4749 * created on this stack which the activity is added to.
4750 * */
4751 void moveActivityToStack(ActivityRecord r) {
4752 final ActivityStack prevStack = r.task.stack;
4753 if (prevStack.mStackId == mStackId) {
4754 // You are already in the right stack silly...
4755 return;
4756 }
4757
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004758 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004759 && (mStackSupervisor.topRunningActivityLocked() == r);
4760 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4761
4762 final TaskRecord task = createTaskRecord(
4763 mStackSupervisor.getNextTaskId(), r.info, r.intent, null, null, true);
4764 r.setTask(task, null);
4765 task.addActivityToTop(r);
4766 setAppTask(r, task);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004767 task.reportPictureInPictureModeChange();
Wale Ogunwale079a0042015-10-24 11:44:07 -07004768 setFocusAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4769 }
4770
Wale Ogunwale706ed792015-08-02 10:29:44 -07004771 private void setAppTask(ActivityRecord r, TaskRecord task) {
4772 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004773 task.updateOverrideConfiguration(bounds);
4774 mWindowManager.setAppTask(
Wale Ogunwale2998eef2015-12-02 19:46:29 -08004775 r.appToken, task.taskId, mStackId, task.getLaunchBounds(), task.mOverrideConfig);
Chong Zhangb15758a2015-11-17 12:12:03 -08004776 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004777 r.taskConfigOverride = task.mOverrideConfig;
4778 }
4779
Craig Mautnerc00204b2013-03-05 15:02:14 -08004780 public int getStackId() {
4781 return mStackId;
4782 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004783
4784 @Override
4785 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004786 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4787 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004788 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004789
Craig Mautner15df08a2015-04-01 12:17:18 -07004790 void onLockTaskPackagesUpdatedLocked() {
4791 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4792 mTaskHistory.get(taskNdx).setLockTaskAuth();
4793 }
4794 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004795}