blob: fcd596f28c5a7c42d70e80f1a4d311f0955d8644 [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -070019import static android.app.ActivityManager.DOCKED_STACK_ID;
Wale Ogunwale61803ee2015-08-07 19:59:47 -070020import static android.app.ActivityManager.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -070021import static android.app.ActivityManager.FULLSCREEN_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.HOME_STACK_ID;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070023import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070024
Wale Ogunwalee23149f2015-03-06 15:39:44 -080025import static com.android.server.am.ActivityManagerDebugConfig.*;
Craig Mautner0eea92c2013-05-16 13:35:39 -070026
Craig Mautner84984fa2014-06-19 11:19:20 -070027import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
28import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070029
Wale Ogunwale040b4702015-08-06 18:10:50 -070030import static com.android.server.am.ActivityStackSupervisor.MOVING;
31
Wale Ogunwale706ed792015-08-02 10:29:44 -070032import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070033import android.util.ArraySet;
Wale Ogunwale706ed792015-08-02 10:29:44 -070034import android.view.IApplicationToken;
Dianne Hackborn91097de2014-04-04 18:02:06 -070035import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080036import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070037import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070038import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080039import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080040import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080041import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080042import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070043import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070044
45import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070046import android.app.ActivityManager;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070047import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070048import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080049import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070050import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080051import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070055import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080057import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080058import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070060import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080061import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062import android.os.Handler;
63import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090064import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070066import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.os.RemoteException;
68import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080069import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070070import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070071import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070072import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070074import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075
Craig Mautnercae015f2013-02-08 14:31:27 -080076import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080077import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078import java.lang.ref.WeakReference;
79import java.util.ArrayList;
80import java.util.Iterator;
81import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080082import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070083import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070084
85/**
86 * State and management of a single stack of activities.
87 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070088final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080089
Wale Ogunwalee23149f2015-03-06 15:39:44 -080090 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070091 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
92 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080093 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070094 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070095 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070096 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070097 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -070098 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070099 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
100 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700101 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700102 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700103 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
104 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
105 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
106 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
107 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700108
109 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800110
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700111 // Ticks during which we check progress while waiting for an app to launch.
112 static final int LAUNCH_TICK = 500;
113
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700114 // How long we wait until giving up on the last activity to pause. This
115 // is short because it directly impacts the responsiveness of starting the
116 // next activity.
117 static final int PAUSE_TIMEOUT = 500;
118
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700119 // How long we wait for the activity to tell us it has stopped before
120 // giving up. This is a good amount of time because we really need this
121 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700122 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700123
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700124 // How long we wait until giving up on an activity telling us it has
125 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700126 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800127
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700128 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800129 // disabled.
130 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800131
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700132 // How long between activity launches that we consider safe to not warn
133 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700134 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800135
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700136 // Set to false to disable the preview that is shown while a new activity
137 // is being started.
138 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800139
Craig Mautner5eda9b32013-07-02 11:58:16 -0700140 // How long to wait for all background Activities to redraw following a call to
141 // convertToTranslucent().
142 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
143
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700144 enum ActivityState {
145 INITIALIZING,
146 RESUMED,
147 PAUSING,
148 PAUSED,
149 STOPPING,
150 STOPPED,
151 FINISHING,
152 DESTROYING,
153 DESTROYED
154 }
155
156 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700157 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800158 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800159
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700160 /**
161 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800162 * running) activities. It contains #TaskRecord objects.
163 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800164 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800165
166 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800167 * Used for validating app tokens with window manager.
168 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800169 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800170
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700171 /**
172 * List of running activities, sorted by recent usage.
173 * The first entry in the list is the least recently used.
174 * It contains HistoryRecord objects.
175 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800176 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700177
178 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700179 * Animations that for the current transition have requested not to
180 * be considered for the transition animation.
181 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800182 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700183
184 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700185 * When we are in the process of pausing an activity, before starting the
186 * next one, this variable holds the activity that is currently being paused.
187 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800188 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700189
190 /**
191 * This is the last activity that we put into the paused state. This is
192 * used to determine if we need to do an activity transition while sleeping,
193 * when we normally hold the top activity paused.
194 */
195 ActivityRecord mLastPausedActivity = null;
196
197 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700198 * Activities that specify No History must be removed once the user navigates away from them.
199 * If the device goes to sleep with such an activity in the paused state then we save it here
200 * and finish it later if another activity replaces it on wakeup.
201 */
202 ActivityRecord mLastNoHistoryActivity = null;
203
204 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700205 * Current activity that is resumed, or null if there is none.
206 */
207 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800208
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700209 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700210 * This is the last activity that has been started. It is only used to
211 * identify when multiple activities are started at once so that the user
212 * can be warned they may not be in the activity they think they are.
213 */
214 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800215
Craig Mautner5eda9b32013-07-02 11:58:16 -0700216 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
217 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
218 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
219 // Activity in mTranslucentActivityWaiting is notified via
220 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
221 // background activity being drawn then the same call will be made with a true value.
222 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700223 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700224
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700225 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700226 * Set when we know we are going to be calling updateConfiguration()
227 * soon, so want to skip intermediate config checks.
228 */
229 boolean mConfigWillChange;
230
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700231 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800232 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700233 // Current bounds of the stack or null if fullscreen.
234 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800235
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700236 long mLaunchStartTime = 0;
237 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800238
Craig Mautner858d8a62013-04-23 17:08:34 -0700239 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700240
Craig Mautnerc00204b2013-03-05 15:02:14 -0800241 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800242 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800243 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
244 ArrayList<ActivityStack> mStacks;
245 /** The attached Display's unique identifier, or -1 if detached */
246 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800247
Craig Mautner27084302013-03-25 08:05:25 -0700248 /** Run all ActivityStacks through this */
249 final ActivityStackSupervisor mStackSupervisor;
250
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700251 private final LaunchingTaskPositioner mTaskPositioner;
252
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700253 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700254 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
255 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
256 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
257 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700258 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700259 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700260 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700261
262 static class ScheduleDestroyArgs {
263 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700264 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700265 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700266 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700267 mReason = reason;
268 }
269 }
270
Zoran Marcetaf958b322012-08-09 20:27:12 +0900271 final Handler mHandler;
272
273 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800274
Craig Mautnerc8143c62013-09-03 12:15:57 -0700275 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900276 super(looper);
277 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700278
Zoran Marcetaf958b322012-08-09 20:27:12 +0900279 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700280 public void handleMessage(Message msg) {
281 switch (msg.what) {
282 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800283 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700284 // We don't at this point know if the activity is fullscreen,
285 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800286 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700287 synchronized (mService) {
288 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700289 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700290 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700291 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800292 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700293 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700294 case LAUNCH_TICK_MSG: {
295 ActivityRecord r = (ActivityRecord)msg.obj;
296 synchronized (mService) {
297 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700298 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700299 }
300 }
301 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700302 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800303 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700304 // We don't at this point know if the activity is fullscreen,
305 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800306 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800307 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800308 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800309 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700310 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700311 case STOP_TIMEOUT_MSG: {
312 ActivityRecord r = (ActivityRecord)msg.obj;
313 // We don't at this point know if the activity is fullscreen,
314 // so we need to be conservative and assume it isn't.
315 Slog.w(TAG, "Activity stop timeout for " + r);
316 synchronized (mService) {
317 if (r.isInHistory()) {
318 activityStoppedLocked(r, null, null, null);
319 }
320 }
321 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700322 case DESTROY_ACTIVITIES_MSG: {
323 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
324 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700325 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700326 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700327 } break;
328 case TRANSLUCENT_TIMEOUT_MSG: {
329 synchronized (mService) {
330 notifyActivityDrawnLocked(null);
331 }
332 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700333 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700334 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700335 final ActivityRecord r = getVisibleBehindActivity();
336 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700337 if (r != null) {
338 mService.killAppAtUsersRequest(r.app, null);
339 }
340 }
341 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700342 }
343 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800344 }
345
Craig Mautner34b73df2014-01-12 21:11:08 -0800346 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800347 int count = 0;
348 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
349 count += mTaskHistory.get(taskNdx).mActivities.size();
350 }
351 return count;
352 }
353
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800354 int numTasks() {
355 return mTaskHistory.size();
356 }
357
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800358 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
359 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800360 mActivityContainer = activityContainer;
361 mStackSupervisor = activityContainer.getOuter();
362 mService = mStackSupervisor.mService;
363 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
364 mWindowManager = mService.mWindowManager;
365 mStackId = activityContainer.mStackId;
366 mCurrentUser = mService.mCurrentUserId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800367 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700368 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
369 ? new LaunchingTaskPositioner() : null;
370 }
371
372 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
373 mDisplayId = activityDisplay.mDisplayId;
374 mStacks = activityDisplay.mStacks;
375 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
376 mFullscreen = mBounds == null;
377 if (mTaskPositioner != null) {
378 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
379 mTaskPositioner.configure(mBounds);
380 }
381 }
382
383 void detachDisplay() {
384 mDisplayId = Display.INVALID_DISPLAY;
385 mStacks = null;
386 if (mTaskPositioner != null) {
387 mTaskPositioner.reset();
388 }
389 mWindowManager.detachStack(mStackId);
390 }
391
392 void setBounds(Rect bounds) {
393 mBounds = mFullscreen ? null : new Rect(bounds);
394 mTaskPositioner.configure(bounds);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700395 }
Craig Mautner5962b122012-10-05 14:45:52 -0700396
Amith Yamasani734983f2014-03-04 16:48:05 -0800397 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700398 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700399 }
400
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700401 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800402 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700403 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
404 if (r != null) {
405 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800406 }
407 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700408 return null;
409 }
410
411 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800412 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
413 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800414 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800415 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
416 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800417 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800418 return r;
419 }
420 }
421 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700422 return null;
423 }
424
425 /**
426 * This is a simplified version of topRunningActivityLocked that provides a number of
427 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800428 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700429 * @param token If non-null, any history records matching this token will be skipped.
430 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800431 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700432 * @return Returns the HistoryRecord of the next activity on the stack.
433 */
434 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800435 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
436 TaskRecord task = mTaskHistory.get(taskNdx);
437 if (task.taskId == taskId) {
438 continue;
439 }
440 ArrayList<ActivityRecord> activities = task.mActivities;
441 for (int i = activities.size() - 1; i >= 0; --i) {
442 final ActivityRecord r = activities.get(i);
443 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800444 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800445 return r;
446 }
447 }
448 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700449 return null;
450 }
451
Craig Mautner8849a5e2013-04-02 16:41:03 -0700452 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700453 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
454 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700455 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
456 final ActivityRecord r = activities.get(activityNdx);
457 if (!r.finishing) {
458 return r;
459 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700460 }
461 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700462 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700463 }
464
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700465 final TaskRecord topTask() {
466 final int size = mTaskHistory.size();
467 if (size > 0) {
468 return mTaskHistory.get(size - 1);
469 }
470 return null;
471 }
472
Craig Mautnerd2328952013-03-05 12:46:26 -0800473 TaskRecord taskForIdLocked(int id) {
474 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
475 final TaskRecord task = mTaskHistory.get(taskNdx);
476 if (task.taskId == id) {
477 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800478 }
479 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700480 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700481 }
482
Craig Mautnerd2328952013-03-05 12:46:26 -0800483 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700484 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800485 return isInStackLocked(r);
486 }
487
488 ActivityRecord isInStackLocked(ActivityRecord r) {
489 if (r == null) {
490 return null;
491 }
492 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700493 if (task != null && task.stack != null
494 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800495 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800496 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800497 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800498 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800499 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800500 }
501
Craig Mautner2420ead2013-04-01 17:13:20 -0700502 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700503 final boolean hadit = mLRUActivities.remove(r);
504 mLRUActivities.add(r);
505 return hadit;
506 }
507
Craig Mautnerde4ef022013-04-07 19:01:33 -0700508 final boolean isHomeStack() {
509 return mStackId == HOME_STACK_ID;
510 }
511
Craig Mautnere0a38842013-12-16 16:14:02 -0800512 final boolean isOnHomeDisplay() {
513 return isAttached() &&
514 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
515 }
516
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700517 void moveToFront(String reason) {
518 moveToFront(reason, null);
519 }
520
521 /**
522 * @param reason The reason for moving the stack to the front.
523 * @param task If non-null, the task will be moved to the top of the stack.
524 * */
525 void moveToFront(String reason, TaskRecord task) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800526 if (isAttached()) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800527 final boolean homeStack = isHomeStack()
528 || (mActivityContainer.mParentActivity != null
529 && mActivityContainer.mParentActivity.isHomeActivity());
530 ActivityStack lastFocusStack = null;
531 if (!homeStack) {
532 // Need to move this stack to the front before calling
533 // {@link ActivityStackSupervisor#moveHomeStack} below.
534 lastFocusStack = mStacks.get(mStacks.size() - 1);
535 mStacks.remove(this);
536 mStacks.add(this);
Wale Ogunwaleaf0e4482015-02-24 12:27:31 -0800537 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700538 // TODO(multi-display): Focus stack currently adjusted in call to move home stack.
539 // Needs to also work if focus is moving to the non-home display.
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800540 if (isOnHomeDisplay()) {
541 mStackSupervisor.moveHomeStack(homeStack, reason, lastFocusStack);
542 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700543 if (task != null) {
544 insertTaskAtTop(task, null);
545 } else {
546 task = topTask();
547 }
Craig Mautner6b904ef2014-12-17 15:45:03 -0800548 if (task != null) {
549 mWindowManager.moveTaskToTop(task.taskId);
550 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800551 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800552 }
553
554 final boolean isAttached() {
555 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800556 }
557
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700558 /**
559 * Returns the top activity in any existing task matching the given
560 * Intent. Returns null if no such task is found.
561 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700562 ActivityRecord findTaskLocked(ActivityRecord target) {
563 Intent intent = target.intent;
564 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700565 ComponentName cls = intent.getComponent();
566 if (info.targetActivity != null) {
567 cls = new ComponentName(info.packageName, info.targetActivity);
568 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700569 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700570 boolean isDocument = intent != null & intent.isDocument();
571 // If documentData is non-null then it must match the existing task data.
572 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800573
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700574 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800575 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
576 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700577 if (task.voiceSession != null) {
578 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700579 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700580 continue;
581 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700582 if (task.userId != userId) {
583 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700584 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700585 continue;
586 }
Craig Mautner000f0022013-02-26 15:04:29 -0800587 final ActivityRecord r = task.getTopActivity();
588 if (r == null || r.finishing || r.userId != userId ||
589 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700590 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800591 continue;
592 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700593 if (r.mActivityType != target.mActivityType) {
594 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
595 continue;
596 }
Craig Mautner000f0022013-02-26 15:04:29 -0800597
Craig Mautnerd00f4742014-03-12 14:17:26 -0700598 final Intent taskIntent = task.intent;
599 final Intent affinityIntent = task.affinityIntent;
600 final boolean taskIsDocument;
601 final Uri taskDocumentData;
602 if (taskIntent != null && taskIntent.isDocument()) {
603 taskIsDocument = true;
604 taskDocumentData = taskIntent.getData();
605 } else if (affinityIntent != null && affinityIntent.isDocument()) {
606 taskIsDocument = true;
607 taskDocumentData = affinityIntent.getData();
608 } else {
609 taskIsDocument = false;
610 taskDocumentData = null;
611 }
612
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700613 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700614 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700615 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700616 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700617 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
618 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700619 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700620 return r;
621 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700622 } else if (taskIntent != null && taskIntent.getComponent() != null &&
623 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700624 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700625 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800626 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700627 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
628 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800629 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700630 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
631 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700632 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700633 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800634 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700635 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
636 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800637 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700638 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700639 }
640
641 return null;
642 }
643
644 /**
645 * Returns the first activity (starting from the top of the stack) that
646 * is the same as the given activity. Returns null if no such activity
647 * is found.
648 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700649 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700650 ComponentName cls = intent.getComponent();
651 if (info.targetActivity != null) {
652 cls = new ComponentName(info.packageName, info.targetActivity);
653 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700654 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700655
Craig Mautner000f0022013-02-26 15:04:29 -0800656 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700657 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700658 final boolean notCurrentUserTask =
659 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700660 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700661
Craig Mautner000f0022013-02-26 15:04:29 -0800662 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
663 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700664 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700665 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700666 }
Craig Mautner000f0022013-02-26 15:04:29 -0800667 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700668 return r;
669 }
670 }
671 }
672
673 return null;
674 }
675
Amith Yamasani742a6712011-05-04 14:49:28 -0700676 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700677 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700678 */
Craig Mautner93529a42013-10-04 15:03:13 -0700679 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800680 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700681 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800682 }
683 mCurrentUser = userId;
684
685 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800686 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800687 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700688 final TaskRecord task = mTaskHistory.get(i);
689
690 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
691 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700692 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700693 || task.topRunningActivityLocked(null) != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700694 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700695 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800696 mTaskHistory.remove(i);
697 mTaskHistory.add(task);
698 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800699 // Use same value for i.
700 } else {
701 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800702 }
703 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700704 if (VALIDATE_TOKENS) {
705 validateAppTokensLocked();
706 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700707 }
708
Craig Mautner2420ead2013-04-01 17:13:20 -0700709 void minimalResumeActivityLocked(ActivityRecord r) {
710 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700711 if (DEBUG_STATES) Slog.v(TAG_STATES,
712 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700713 r.stopped = false;
714 mResumedActivity = r;
715 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800716 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700717 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700718 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700719 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700720 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
721 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700722 }
723
Chong Zhang45c25ce2015-08-10 22:18:26 -0700724 private void addRecentActivityLocked(ActivityRecord r) {
725 if (r != null) {
726 mRecentTasks.addLocked(r.task);
727 r.task.touchActiveTime();
728 }
729 }
730
Narayan Kamath7829c812015-06-08 17:39:43 +0100731 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700732 if (mFullyDrawnStartTime != 0) {
733 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
734 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100735 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700736 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
737 }
738
739 private void stopFullyDrawnTraceIfNeeded() {
740 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
741 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
742 mFullyDrawnStartTime = 0;
743 }
744 }
745
Craig Mautnere79d42682013-04-01 19:01:53 -0700746 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700747 if (r.displayStartTime == 0) {
748 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
749 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100750 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700751 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700752 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700753 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100754 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700755 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700756 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700757 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800758
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700759 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700760 // Make sure that there is no activity waiting for this to launch.
761 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
762 r.displayStartTime = r.fullyDrawnStartTime = 0;
763 } else {
764 mStackSupervisor.removeTimeoutsForActivityLocked(r);
765 mStackSupervisor.scheduleIdleTimeoutLocked(r);
766 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700767 }
768
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800769 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800770 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800771 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
772 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
773 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
774 activities.get(activityNdx).setSleeping(false);
775 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800776 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800777 if (mPausingActivity != null) {
778 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
779 activityPausedLocked(mPausingActivity.appToken, true);
780 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800781 }
782
Craig Mautner0eea92c2013-05-16 13:35:39 -0700783 /**
784 * @return true if something must be done before going to sleep.
785 */
786 boolean checkReadyForSleepLocked() {
787 if (mResumedActivity != null) {
788 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700789 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
790 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
791 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700792 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700793 return true;
794 }
795 if (mPausingActivity != null) {
796 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700797 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700798 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800799 }
800
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700801 if (hasVisibleBehindActivity()) {
802 // Stop visible behind activity before going to sleep.
803 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
804 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700805 if (DEBUG_STATES) Slog.v(TAG_STATES,
806 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700807 return true;
808 }
809
Craig Mautner0eea92c2013-05-16 13:35:39 -0700810 return false;
811 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800812
Craig Mautner0eea92c2013-05-16 13:35:39 -0700813 void goToSleep() {
814 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800815
Craig Mautner0eea92c2013-05-16 13:35:39 -0700816 // Make sure any stopped but visible activities are now sleeping.
817 // This ensures that the activity's onStop() is called.
818 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
819 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
820 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
821 final ActivityRecord r = activities.get(activityNdx);
822 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
823 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800824 }
825 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800826 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700827 }
Craig Mautner59c00972012-07-30 12:10:24 -0700828
Dianne Hackbornd2835932010-12-13 16:28:46 -0800829 public final Bitmap screenshotActivities(ActivityRecord who) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700830 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800831 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700832 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800833 return null;
834 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800835
Winson Chung083baf92014-07-11 10:32:42 -0700836 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700837 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800838 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700839 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700840 return null;
841 }
842
Winson Chung48a10a52014-08-27 14:36:51 -0700843 int w = mService.mThumbnailWidth;
844 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800845 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700846 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800847 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Dianne Hackborn27eac1d2015-03-16 17:15:53 -0700848 w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800849 }
Winson Chung376543b2014-05-21 17:43:28 -0700850 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800851 return null;
852 }
853
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700854 /**
855 * Start pausing the currently resumed activity. It is an error to call this if there
856 * is already an activity being paused or there is no resumed activity.
857 *
858 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
859 * @param uiSleeping True if this is happening with the user interface going to sleep (the
860 * screen turning off).
861 * @param resuming True if this is being called as part of resuming the top activity, so
862 * we shouldn't try to instigate a resume here.
863 * @param dontWait True if the caller does not want to wait for the pause to complete. If
864 * set to true, we will immediately complete the pause here before returning.
865 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
866 * it to tell us when it is done.
867 */
868 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
869 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800870 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800871 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
872 + " state=" + mPausingActivity.state);
873 if (!mService.isSleeping()) {
874 // Avoid recursion among check for sleep and complete pause during sleeping.
875 // Because activity will be paused immediately after resume, just let pause
876 // be completed by the order of activity paused from clients.
877 completePauseLocked(false);
878 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800879 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700880 ActivityRecord prev = mResumedActivity;
881 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700882 if (!resuming) {
883 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
884 mStackSupervisor.resumeTopActivitiesLocked();
885 }
886 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700887 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800888
889 if (mActivityContainer.mParentActivity == null) {
890 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700891 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800892 }
893
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700894 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700895 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700896 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800897 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700898 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700899 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
900 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700901 prev.state = ActivityState.PAUSING;
902 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700903 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700904 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800905 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800906 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700907 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700908 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700909
910 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800911
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700912 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700913 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700914 try {
915 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700916 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700917 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700918 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800919 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700920 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700921 } catch (Exception e) {
922 // Ignore exception, if process died other code will cleanup.
923 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800924 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700925 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700926 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700927 }
928 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800929 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700930 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700931 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700932 }
933
934 // If we are not going to sleep, we want to ensure the device is
935 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700936 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700937 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700938 }
939
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800940 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700941 // Have the window manager pause its key dispatching until the new
942 // activity has started. If we're pausing the activity just because
943 // the screen is being turned off and the UI is sleeping, don't interrupt
944 // key dispatch; the same activity will pick it up again on wakeup.
945 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800946 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700947 } else if (DEBUG_PAUSE) {
948 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700949 }
950
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700951 if (dontWait) {
952 // If the caller said they don't want to wait for the pause, then complete
953 // the pause now.
954 completePauseLocked(false);
955 return false;
956
957 } else {
958 // Schedule a pause timeout in case the app doesn't respond.
959 // We don't give it much time because this directly impacts the
960 // responsiveness seen by the user.
961 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
962 msg.obj = prev;
963 prev.pauseTime = SystemClock.uptimeMillis();
964 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700965 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700966 return true;
967 }
968
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700969 } else {
970 // This activity failed to schedule the
971 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700972 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700973 if (!resuming) {
974 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
975 }
976 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700977 }
978 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700979
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700980 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700981 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
982 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700983
Craig Mautnerd2328952013-03-05 12:46:26 -0800984 final ActivityRecord r = isInStackLocked(token);
985 if (r != null) {
986 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
987 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700988 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -0800989 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700990 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800991 } else {
992 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
993 r.userId, System.identityHashCode(r), r.shortComponentName,
994 mPausingActivity != null
995 ? mPausingActivity.shortComponentName : "(none)");
louis_chang047dfd42015-04-08 16:35:55 +0800996 if (r.finishing && r.state == ActivityState.PAUSING) {
997 if (DEBUG_PAUSE) Slog.v(TAG,
998 "Executing finish of failed to pause activity: " + r);
999 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1000 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001001 }
1002 }
1003 }
1004
Craig Mautnera0026042014-04-23 11:45:37 -07001005 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1006 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001007 if (r.state != ActivityState.STOPPING) {
1008 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1009 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1010 return;
1011 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001012 if (persistentState != null) {
1013 r.persistentState = persistentState;
1014 mService.notifyTaskPersisterLocked(r.task, false);
1015 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001016 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001017 if (icicle != null) {
1018 // If icicle is null, this is happening due to a timeout, so we
1019 // haven't really saved the state.
1020 r.icicle = icicle;
1021 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001022 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001023 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001024 }
1025 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001026 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001027 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1028 r.stopped = true;
1029 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -07001030 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
1031 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001032 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001033 if (r.finishing) {
1034 r.clearOptionsLocked();
1035 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001036 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001037 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -07001038 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001039 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001040 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001041 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001042 }
1043 }
1044 }
1045
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001046 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001047 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001048 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001049
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001050 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001051 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001052 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001053 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001054 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001055 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001056 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001057 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001058 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1059 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001060 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001061 if (prev.configDestroy) {
1062 // The previous is being paused because the configuration
1063 // is changing, which means it is actually stopping...
1064 // To juggle the fact that we are also starting a new
1065 // instance right now, we need to first completely stop
1066 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001067 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001068 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001069 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001070 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001071 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001072 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001073 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1074 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001075 // If we already have a few activities waiting to stop,
1076 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001077 // them out. Or if r is the last of activity of the last task the stack
1078 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001079 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001080 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001081 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001082 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001083 }
1084 }
1085 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001086 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001087 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001088 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001089 // It is possible the activity was freezing the screen before it was paused.
1090 // In that case go ahead and remove the freeze this activity has on the screen
1091 // since it is no longer visible.
1092 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001093 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001094 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001095
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001096 if (resumeNext) {
1097 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1098 if (!mService.isSleepingOrShuttingDown()) {
1099 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1100 } else {
1101 mStackSupervisor.checkReadyForSleepLocked();
1102 ActivityRecord top = topStack.topRunningActivityLocked(null);
1103 if (top == null || (prev != null && top != prev)) {
1104 // If there are no more activities available to run,
1105 // do resume anyway to start something. Also if the top
1106 // activity on the stack is not the just paused activity,
1107 // we need to go ahead and resume it to ensure we complete
1108 // an in-flight app switch.
1109 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1110 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001111 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001112 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001113
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001114 if (prev != null) {
1115 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001116
Craig Mautner525f3d92013-05-07 14:01:50 -07001117 if (prev.app != null && prev.cpuTimeAtResume > 0
1118 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001119 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1120 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001121 if (diff > 0) {
1122 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1123 synchronized (bsi) {
1124 BatteryStatsImpl.Uid.Proc ps =
1125 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001126 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001127 if (ps != null) {
1128 ps.addForegroundTimeLocked(diff);
1129 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001130 }
1131 }
1132 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001133 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001134 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001135
1136 // Notfiy when the task stack has changed
1137 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001138 }
1139
1140 /**
1141 * Once we know that we have asked an application to put an activity in
1142 * the resumed state (either by launching it or explicitly telling it),
1143 * this function updates the rest of our state to match that fact.
1144 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001145 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001146 next.idle = false;
1147 next.results = null;
1148 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001149
1150 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1151 ProcessRecord app = next.task.mActivities.get(0).app;
1152 if (app != null && app != mService.mHomeProcess) {
1153 mService.mHomeProcess = app;
1154 }
1155 }
1156
Craig Mautner07566322013-09-26 16:42:55 -07001157 if (next.nowVisible) {
1158 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001159 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001160 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001161
1162 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001163 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001164
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001165 mStackSupervisor.reportResumedActivityLocked(next);
1166
1167 next.resumeKeyDispatchingLocked();
1168 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001169
1170 // Mark the point when the activity is resuming
1171 // TODO: To be more accurate, the mark should be before the onCreate,
1172 // not after the onResume. But for subsequent starts, onResume is fine.
1173 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001174 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001175 } else {
1176 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1177 }
Winson Chung376543b2014-05-21 17:43:28 -07001178
George Mount6ba042b2014-07-28 11:12:28 -07001179 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001180
1181 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1182 // When resuming an activity, require it to call requestVisibleBehind() again.
1183 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1184 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001185 }
1186
Craig Mautner2568c3a2015-03-26 14:22:34 -07001187 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001188 r.visible = visible;
1189 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001190 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001191 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001192 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001193 container.setVisible(visible);
1194 }
1195 }
1196
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001197 // Find the first visible activity above the passed activity and if it is translucent return it
1198 // otherwise return null;
1199 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1200 TaskRecord task = r.task;
1201 if (task == null) {
1202 return null;
1203 }
1204
1205 ActivityStack stack = task.stack;
1206 if (stack == null) {
1207 return null;
1208 }
1209
1210 int stackNdx = mStacks.indexOf(stack);
1211
1212 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1213 int taskNdx = tasks.indexOf(task);
1214
1215 ArrayList<ActivityRecord> activities = task.mActivities;
1216 int activityNdx = activities.indexOf(r) + 1;
1217
1218 final int numStacks = mStacks.size();
1219 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001220 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001221 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001222 final int numTasks = tasks.size();
1223 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001224 final TaskRecord currentTask = tasks.get(taskNdx);
1225 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001226 final int numActivities = activities.size();
1227 while (activityNdx < numActivities) {
1228 final ActivityRecord activity = activities.get(activityNdx);
1229 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001230 return historyStack.mFullscreen
1231 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001232 }
1233 ++activityNdx;
1234 }
1235 activityNdx = 0;
1236 ++taskNdx;
1237 }
1238 taskNdx = 0;
1239 ++stackNdx;
1240 }
1241
1242 return null;
1243 }
1244
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001245 private ActivityStack getNextVisibleStackLocked() {
1246 ArrayList<ActivityStack> stacks = mStacks;
1247 final ActivityRecord parent = mActivityContainer.mParentActivity;
1248 if (parent != null) {
1249 stacks = parent.task.stack.mStacks;
1250 }
1251 if (stacks != null) {
1252 for (int i = stacks.size() - 1; i >= 0; --i) {
1253 ActivityStack stack = stacks.get(i);
1254 if (stack != this && stack.isStackVisibleLocked()) {
1255 return stack;
1256 }
1257 }
1258 }
1259 return null;
1260 }
1261
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001262 /** Returns true if the stack contains a fullscreen task. */
1263 private boolean hasFullscreenTask() {
1264 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1265 final TaskRecord task = mTaskHistory.get(i);
1266 if (task.mFullscreen) {
1267 return true;
1268 }
1269 }
1270 return false;
1271 }
1272
1273 /** Return true if this stack is hidden by the presence of a docked stack. */
1274 private boolean isHiddenByDockedStack() {
1275 final ActivityStack dockedStack = mStackSupervisor.getStack(DOCKED_STACK_ID);
1276 if (dockedStack != null) {
1277 final int dockedStackIndex = mStacks.indexOf(dockedStack);
1278 final int stackIndex = mStacks.indexOf(this);
1279 if (dockedStackIndex > stackIndex) {
1280 // Fullscreen stacks or stacks with fullscreen task below the docked stack are not
1281 // visible. We do this so we don't have the 2 stacks and their tasks overlap.
1282 if (mFullscreen) {
1283 return true;
1284 }
1285
1286 // We need to also check the tasks in the stack because they can be fullscreen
1287 // even though their stack isn't due to their root activity not been resizeable
1288 // (i.e. doesn't support multi-window mode).
1289 if (hasFullscreenTask()) {
1290 return true;
1291 }
1292 }
1293 }
1294 return false;
1295 }
1296
1297 /** Returns true if the stack is considered visible. */
Todd Kennedyaab56db2015-01-30 09:39:53 -08001298 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001299 if (!isAttached()) {
1300 return false;
1301 }
1302
1303 if (mStackSupervisor.isFrontStack(this)) {
1304 return true;
1305 }
1306
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001307 final int stackIndex = mStacks.indexOf(this);
1308
1309 if (stackIndex == mStacks.size() - 1) {
1310 Slog.wtf(TAG,
1311 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1312 return false;
1313 }
1314
1315 if (isHiddenByDockedStack()) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001316 return false;
1317 }
1318
Jose Lima729cb232014-05-05 15:59:40 -07001319 /**
1320 * Start at the task above this one and go up, looking for a visible
1321 * fullscreen activity, or a translucent activity that requested the
1322 * wallpaper to be shown behind it.
1323 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001324 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001325 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001326 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1327
1328 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1329 continue;
1330 }
1331
1332 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID
1333 || stack.mStackId == HOME_STACK_ID) {
1334 // The freeform and home stacks can't have any other stack visible behind them
1335 // when they are fullscreen since they act as base/cut-off points for visibility.
1336 // NOTE: we don't cut-off at the FULLSCREEN_WORKSPACE_STACK_ID because the home
1337 // stack sometimes needs to be visible behind it when it is displaying a dialog
1338 // activity. We let it fall through to the logic below to determine visibility.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001339 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001340 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001341
Todd Kennedyaab56db2015-01-30 09:39:53 -08001342 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001343 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001344 // task above isn't fullscreen, so, we assume we're still visible.
1345 if (!task.mFullscreen) {
1346 continue;
1347 }
Craig Mautner84984fa2014-06-19 11:19:20 -07001348 final ArrayList<ActivityRecord> activities = task.mActivities;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001349 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Jose Lima7ba71252014-04-30 12:59:27 -07001350 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001351
1352 // Conditions for an activity to obscure the stack we're
1353 // examining:
1354 // 1. Not Finishing AND Visible AND:
1355 // 2. Either:
1356 // - Full Screen Activity OR
1357 // - On top of Home and our stack is NOT home
1358 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001359 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001360 return false;
1361 }
1362 }
1363 }
1364 }
1365
1366 return true;
1367 }
1368
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001369 /**
1370 * Make sure that all activities that need to be visible (that is, they
1371 * currently can be seen by the user) actually are.
1372 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001373 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1374 ActivityRecord top = topRunningActivityLocked(null);
1375 if (top == null) {
1376 return;
1377 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001378 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1379 "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001380 + " configChanges=0x" + Integer.toHexString(configChanges));
1381
Craig Mautner5eda9b32013-07-02 11:58:16 -07001382 if (mTranslucentActivityWaiting != top) {
1383 mUndrawnActivitiesBelowTopTranslucent.clear();
1384 if (mTranslucentActivityWaiting != null) {
1385 // Call the callback with a timeout indication.
1386 notifyActivityDrawnLocked(null);
1387 mTranslucentActivityWaiting = null;
1388 }
1389 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1390 }
1391
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001392 // If the top activity is not fullscreen, then we need to
1393 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001394 boolean aboveTop = true;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001395 boolean behindFullscreen = !isStackVisibleLocked();
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001396 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001397
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001398 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001399 final TaskRecord task = mTaskHistory.get(taskNdx);
1400 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001401 // Set to true if an activity in this task is fullscreen thereby hiding other
1402 // activities in the same task. Initialized to the same value as behindFullscreen
1403 // which represent if the entire task/stack is behind another fullscreen task/stack.
1404 boolean behindFullscreenActivity = behindFullscreen;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001405 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1406 final ActivityRecord r = activities.get(activityNdx);
1407 if (r.finishing) {
1408 continue;
1409 }
1410 if (aboveTop && r != top) {
1411 continue;
1412 }
1413 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001414 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1415 // but must be drawn initially for the animation as though they were visible.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001416 if (!behindFullscreenActivity || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001417 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1418 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001419 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001420
Craig Mautnerd44711d2013-02-23 11:24:36 -08001421 // First: if this is not the current activity being started, make
1422 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001423 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001424 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001425 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001426
1427 if (r.app == null || r.app.thread == null) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001428 // This activity needs to be visible, but isn't even running...
1429 // get it started and resume if no other stack in this stack is resumed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001430 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1431 "Start and freeze screen for " + r);
Craig Mautnerbb742462014-07-07 15:28:55 -07001432 if (r != starting) {
1433 r.startFreezingScreenLocked(r.app, configChanges);
1434 }
1435 if (!r.visible || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001436 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1437 "Starting and making visible: " + r);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001438 setVisible(r, true);
Craig Mautnerbb742462014-07-07 15:28:55 -07001439 }
1440 if (r != starting) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001441 mStackSupervisor.startSpecificActivityLocked(
1442 r, noStackActivityResumed, false);
riddle_hsu36ee73d2015-06-05 16:38:38 +08001443 if (activityNdx >= activities.size()) {
1444 // Record may be removed if its process needs to restart.
1445 activityNdx = activities.size() - 1;
1446 } else {
1447 noStackActivityResumed = false;
1448 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001449 }
1450
1451 } else if (r.visible) {
1452 // If this activity is already visible, then there is nothing
1453 // else to do here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001454 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1455 "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001456 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001457 try {
George Mount6ba042b2014-07-28 11:12:28 -07001458 if (r.returningOptions != null) {
1459 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1460 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001461 }
1462 } catch(RemoteException e) {
1463 }
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001464 if (r.state == ActivityState.RESUMED) {
1465 noStackActivityResumed = false;
1466 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001467 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001468 // This activity is not currently visible, but is running.
1469 // Tell it to become visible.
1470 r.visible = true;
1471 if (r.state != ActivityState.RESUMED && r != starting) {
1472 // If this activity is paused, tell it
1473 // to now show its window.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001474 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1475 "Making visible and scheduling visibility: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001476 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001477 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001478 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001479 mUndrawnActivitiesBelowTopTranslucent.add(r);
1480 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001481 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001482 r.sleeping = false;
1483 r.app.pendingUiClean = true;
1484 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1485 r.stopFreezingScreenLocked(false);
1486 } catch (Exception e) {
1487 // Just skip on any failure; we'll make it
1488 // visible when it next restarts.
1489 Slog.w(TAG, "Exception thrown making visibile: "
1490 + r.intent.getComponent(), e);
1491 }
1492 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001493 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001494
Craig Mautnerd44711d2013-02-23 11:24:36 -08001495 // Aggregate current change flags.
1496 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001497
Craig Mautnerd44711d2013-02-23 11:24:36 -08001498 if (r.fullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001499 // At this point, nothing else needs to be shown in this task.
1500 behindFullscreenActivity = true;
1501 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
1502 + " behindFullscreen=" + behindFullscreen
1503 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautner84984fa2014-06-19 11:19:20 -07001504 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001505 behindFullscreenActivity = true;
1506 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1507 + " behindFullscreen=" + behindFullscreen
1508 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001509 }
1510 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001511 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001512 "Make invisible? " + r + " finishing=" + r.finishing
1513 + " state=" + r.state + " behindFullscreen=" + behindFullscreen
1514 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001515 // Now for any activities that aren't visible to the user, make
1516 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001517 if (r.visible) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001518 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001519 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001520 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001521 switch (r.state) {
1522 case STOPPING:
1523 case STOPPED:
1524 if (r.app != null && r.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001525 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1526 "Scheduling invisibility: " + r);
Craig Mautner4addfc52013-06-25 08:05:45 -07001527 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1528 }
1529 break;
1530
1531 case INITIALIZING:
1532 case RESUMED:
1533 case PAUSING:
1534 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001535 // This case created for transitioning activities from
1536 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001537 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001538 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001539 } else {
1540 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1541 mStackSupervisor.mStoppingActivities.add(r);
1542 }
1543 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001544 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001545 break;
1546
1547 default:
1548 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001549 }
1550 } catch (Exception e) {
1551 // Just skip on any failure; we'll make it
1552 // visible when it next restarts.
1553 Slog.w(TAG, "Exception thrown making hidden: "
1554 + r.intent.getComponent(), e);
1555 }
1556 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001557 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001558 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001559 }
1560 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001561 // Factoring if the previous task is fullscreen there by affecting the visibility of
1562 // task behind it.
1563 behindFullscreen |= task.mFullscreen;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001564 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001565
1566 if (mTranslucentActivityWaiting != null &&
1567 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1568 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1569 notifyActivityDrawnLocked(null);
1570 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001571 }
Craig Mautner58547802013-03-05 08:23:53 -08001572
Todd Kennedyaab56db2015-01-30 09:39:53 -08001573 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001574 mTranslucentActivityWaiting = r;
1575 mUndrawnActivitiesBelowTopTranslucent.clear();
1576 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1577 }
1578
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001579 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1580 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1581 final TaskRecord task = mTaskHistory.get(taskNdx);
1582 final ArrayList<ActivityRecord> activities = task.mActivities;
1583 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1584 final ActivityRecord r = activities.get(activityNdx);
1585 if ( r.appTimeTracker != except) {
1586 r.appTimeTracker = null;
1587 }
1588 }
1589 }
1590 }
1591
Craig Mautner5eda9b32013-07-02 11:58:16 -07001592 /**
1593 * Called as activities below the top translucent activity are redrawn. When the last one is
1594 * redrawn notify the top activity by calling
1595 * {@link Activity#onTranslucentConversionComplete}.
1596 *
1597 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1598 * occurred and the activity will be notified immediately.
1599 */
1600 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001601 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001602 if ((r == null)
1603 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1604 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1605 // The last undrawn activity below the top has just been drawn. If there is an
1606 // opaque activity at the top, notify it that it can become translucent safely now.
1607 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1608 mTranslucentActivityWaiting = null;
1609 mUndrawnActivitiesBelowTopTranslucent.clear();
1610 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1611
Craig Mautner71dd1b62014-02-18 15:48:52 -08001612 if (waitingActivity != null) {
1613 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1614 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1615 try {
1616 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1617 waitingActivity.appToken, r != null);
1618 } catch (RemoteException e) {
1619 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001620 }
1621 }
1622 }
1623 }
1624
Craig Mautnera61bc652013-10-28 15:43:18 -07001625 /** If any activities below the top running one are in the INITIALIZING state and they have a
1626 * starting window displayed then remove that starting window. It is possible that the activity
1627 * in this state will never resumed in which case that starting window will be orphaned. */
1628 void cancelInitializingActivities() {
1629 final ActivityRecord topActivity = topRunningActivityLocked(null);
1630 boolean aboveTop = true;
1631 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1632 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1633 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1634 final ActivityRecord r = activities.get(activityNdx);
1635 if (aboveTop) {
1636 if (r == topActivity) {
1637 aboveTop = false;
1638 }
1639 continue;
1640 }
1641
1642 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001643 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1644 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001645 r.mStartingWindowShown = false;
1646 mWindowManager.removeAppStartingWindow(r.appToken);
1647 }
1648 }
1649 }
1650 }
1651
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001652 /**
1653 * Ensure that the top activity in the stack is resumed.
1654 *
1655 * @param prev The previously resumed activity, for when in the process
1656 * of pausing; can be null to call from elsewhere.
1657 *
1658 * @return Returns true if something is being resumed, or false if
1659 * nothing happened.
1660 */
1661 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001662 return resumeTopActivityLocked(prev, null);
1663 }
1664
1665 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001666 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001667 // Don't even start recursing.
1668 return false;
1669 }
1670
1671 boolean result = false;
1672 try {
1673 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001674 mStackSupervisor.inResumeTopActivity = true;
1675 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1676 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001677 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001678 }
Craig Mautner544efa72014-09-04 16:41:20 -07001679 result = resumeTopActivityInnerLocked(prev, options);
1680 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001681 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001682 }
1683 return result;
1684 }
1685
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001686 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001687 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001688
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001689 if (!mService.mBooting && !mService.mBooted) {
1690 // Not ready yet!
1691 return false;
1692 }
1693
Craig Mautnerdf88d732014-01-27 09:21:32 -08001694 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001695 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001696 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001697 // Do not resume this stack if its parent is not resumed.
1698 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1699 return false;
1700 }
1701
Craig Mautnera61bc652013-10-28 15:43:18 -07001702 cancelInitializingActivities();
1703
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001704 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001705 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001706
1707 // Remember how we'll process this pause/resume situation, and ensure
1708 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001709 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1710 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001711
Craig Mautner84984fa2014-06-19 11:19:20 -07001712 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001713 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001714 // There are no more activities!
1715 final String reason = "noMoreActivities";
1716 if (!mFullscreen) {
1717 // Try to move focus to the next visible stack with a running activity if this
1718 // stack is not covering the entire screen.
1719 final ActivityStack stack = getNextVisibleStackLocked();
1720 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1721 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1722 }
1723 }
1724 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001725 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001726 if (DEBUG_STATES) Slog.d(TAG_STATES,
1727 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001728 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001729 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001730 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1731 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1732 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001733 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001734 }
1735
1736 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001737
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001738 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001739 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1740 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001741 // Make sure we have executed any pending transitions, since there
1742 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001743 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001744 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001745 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001746 if (DEBUG_STATES) Slog.d(TAG_STATES,
1747 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001748 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001749 return false;
1750 }
1751
Craig Mautner525f3d92013-05-07 14:01:50 -07001752 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001753 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001754 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001755 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001756 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001757 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001758 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001759 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001760 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001761 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001762 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001763 } else if (!isOnHomeDisplay()) {
1764 return false;
1765 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001766 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001767 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001768 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1769 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1770 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001771 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001772 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001773 }
1774
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001775 // If we are sleeping, and there is no resumed activity, and the top
1776 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001777 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001778 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001779 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001780 // Make sure we have executed any pending transitions, since there
1781 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001782 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001783 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001784 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001785 if (DEBUG_STATES) Slog.d(TAG_STATES,
1786 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001787 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001788 return false;
1789 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001790
1791 // Make sure that the user who owns this activity is started. If not,
1792 // we will just leave it as is because someone should be bringing
1793 // another user's activities to the top of the stack.
1794 if (mService.mStartedUsers.get(next.userId) == null) {
1795 Slog.w(TAG, "Skipping resume of top activity " + next
1796 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001797 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001798 return false;
1799 }
1800
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001801 // The activity may be waiting for stop, but that is no longer
1802 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001803 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001804 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001805 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001806 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001807
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001808 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001809
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001810 // If we are currently pausing an activity, then don't do anything
1811 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001812 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001813 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001814 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001815 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001816 return false;
1817 }
1818
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001819 // Okay we are now going to start a switch, to 'next'. We may first
1820 // have to pause the current activity, but this is an important point
1821 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001822 // XXX "App Redirected" dialog is getting too many false positives
1823 // at this point, so turn off for now.
1824 if (false) {
1825 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1826 long now = SystemClock.uptimeMillis();
1827 final boolean inTime = mLastStartedActivity.startTime != 0
1828 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1829 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1830 final int nextUid = next.info.applicationInfo.uid;
1831 if (inTime && lastUid != nextUid
1832 && lastUid != next.launchedFromUid
1833 && mService.checkPermission(
1834 android.Manifest.permission.STOP_APP_SWITCHES,
1835 -1, next.launchedFromUid)
1836 != PackageManager.PERMISSION_GRANTED) {
1837 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1838 } else {
1839 next.startTime = now;
1840 mLastStartedActivity = next;
1841 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001842 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001843 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001844 mLastStartedActivity = next;
1845 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001846 }
Craig Mautner58547802013-03-05 08:23:53 -08001847
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001848 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1849
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001850 // We need to start pausing the current activity so the top one
1851 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001852 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1853 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001854 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001855 if (DEBUG_STATES) Slog.d(TAG_STATES,
1856 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001857 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001858 }
1859 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001860 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001861 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001862 // At this point we want to put the upcoming activity's process
1863 // at the top of the LRU list, since we know we will be needing it
1864 // very soon and it would be a waste to let it get killed if it
1865 // happens to be sitting towards the end.
1866 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001867 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001868 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001869 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001870 return true;
1871 }
1872
Christopher Tated3f175c2012-06-14 14:16:54 -07001873 // If the most recent activity was noHistory but was only stopped rather
1874 // than stopped+finished because the device went to sleep, we need to make
1875 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001876 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001877 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001878 if (DEBUG_STATES) Slog.d(TAG_STATES,
1879 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001880 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001881 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001882 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001883 }
1884
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001885 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001886 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1887 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001888 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001889 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1890 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001891 } else {
1892 // The next activity is already visible, so hide the previous
1893 // activity's windows right now so we can show the new one ASAP.
1894 // We only do this if the previous is finishing, which should mean
1895 // it is on top of the one being resumed so hiding it quickly
1896 // is good. Otherwise, we want to do the normal route of allowing
1897 // the resumed activity to be shown so we can decide if the
1898 // previous should actually be hidden depending on whether the
1899 // new one is found to be full-screen or not.
1900 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001901 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001902 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1903 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001904 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001905 + ", nowVisible=" + next.nowVisible);
1906 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001907 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001908 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001909 + ", waitingVisible="
1910 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1911 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001912 }
1913 }
1914 }
1915
Dianne Hackborne7f97212011-02-24 14:40:20 -08001916 // Launching this app's activity, make sure the app is no longer
1917 // considered stopped.
1918 try {
1919 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001920 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001921 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001922 } catch (IllegalArgumentException e) {
1923 Slog.w(TAG, "Failed trying to unstop package "
1924 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001925 }
1926
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001927 // We are starting up the next activity, so tell the window manager
1928 // that the previous one will be hidden soon. This way it can know
1929 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001930 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001931 if (prev != null) {
1932 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001933 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001934 "Prepare close transition: prev=" + prev);
1935 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001936 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001937 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001938 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001939 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001940 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1941 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001942 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001943 mWindowManager.setAppWillBeHidden(prev.appToken);
1944 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001945 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001946 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
1947 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001948 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001949 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001950 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001951 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001952 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001953 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001954 : next.mLaunchTaskBehind
1955 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1956 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001957 }
1958 }
1959 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001960 mWindowManager.setAppWillBeHidden(prev.appToken);
1961 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001962 }
Craig Mautner967212c2013-04-13 21:10:58 -07001963 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001964 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001965 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001966 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001967 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001968 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001969 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001970 }
1971 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001972
1973 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001974 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001975 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1976 if (opts != null) {
1977 resumeAnimOptions = opts.toBundle();
1978 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001979 next.applyOptionsLocked();
1980 } else {
1981 next.clearOptionsLocked();
1982 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001983
Craig Mautnercf910b02013-04-23 11:23:27 -07001984 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001985 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001986 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001987
1988 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001989 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001990
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001991 // schedule launch ticks to collect information about slow apps.
1992 next.startLaunchTickingLocked();
1993
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001994 ActivityRecord lastResumedActivity =
1995 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001996 ActivityState lastState = next.state;
1997
1998 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001999
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002000 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002001 next.state = ActivityState.RESUMED;
2002 mResumedActivity = next;
2003 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002004 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002005 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002006 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002007 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002008
2009 // Have the window manager re-evaluate the orientation of
2010 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002011 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002012 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002013 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002014 mService.mConfiguration,
2015 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2016 if (config != null) {
2017 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002018 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002019 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002020 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002021
Craig Mautner525f3d92013-05-07 14:01:50 -07002022 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002023 // The configuration update wasn't able to keep the existing
2024 // instance of the activity, and instead started a new one.
2025 // We should be all done, but let's just make sure our activity
2026 // is still at the top and schedule another run if something
2027 // weird happened.
2028 ActivityRecord nextNext = topRunningActivityLocked(null);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002029 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002030 "Activity config changed during resume: " + next
2031 + ", new next: " + nextNext);
2032 if (nextNext != next) {
2033 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002034 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002035 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002036 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002037 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002038 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002039 return true;
2040 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002041 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002042 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002043 }
Craig Mautner58547802013-03-05 08:23:53 -08002044
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002045 try {
2046 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002047 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002048 if (a != null) {
2049 final int N = a.size();
2050 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002051 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2052 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002053 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002054 }
2055 }
2056
2057 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002058 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002059 }
2060
Craig Mautner299f9602015-01-26 09:47:33 -08002061 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2062 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002063
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002064 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002065 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002066 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002067 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002068 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002069 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002070 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002071
Craig Mautner0eea92c2013-05-16 13:35:39 -07002072 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002073
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002074 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002075 } catch (Exception e) {
2076 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002077 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002078 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002079 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002080 if (lastStack != null) {
2081 lastStack.mResumedActivity = lastResumedActivity;
2082 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002083 Slog.i(TAG, "Restarting because process died: " + next);
2084 if (!next.hasBeenLaunched) {
2085 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002086 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2087 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002088 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002089 next.appToken, next.packageName, next.theme,
2090 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002091 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2092 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002093 }
George Mount2c92c972014-03-20 09:38:23 -07002094 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002095 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002096 return true;
2097 }
2098
2099 // From this point on, if something goes wrong there is no way
2100 // to recover the activity.
2101 try {
2102 next.visible = true;
2103 completeResumeLocked(next);
2104 } catch (Exception e) {
2105 // If any exception gets thrown, toss away this
2106 // activity and try the next one.
2107 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002108 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002109 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002110 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002111 return true;
2112 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002113 next.stopped = false;
2114
2115 } else {
2116 // Whoops, need to restart this activity!
2117 if (!next.hasBeenLaunched) {
2118 next.hasBeenLaunched = true;
2119 } else {
2120 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002121 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002122 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002123 mService.compatibilityInfoForPackageLocked(
2124 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002125 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002126 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002127 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002128 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002129 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002130 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002131 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002132 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002133 }
2134
Craig Mautnercf910b02013-04-23 11:23:27 -07002135 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002136 return true;
2137 }
2138
riddle_hsuc215a4f2014-12-27 12:10:45 +08002139 private TaskRecord getNextTask(TaskRecord targetTask) {
2140 final int index = mTaskHistory.indexOf(targetTask);
2141 if (index >= 0) {
2142 final int numTasks = mTaskHistory.size();
2143 for (int i = index + 1; i < numTasks; ++i) {
2144 TaskRecord task = mTaskHistory.get(i);
2145 if (task.userId == targetTask.userId) {
2146 return task;
2147 }
2148 }
2149 }
2150 return null;
2151 }
2152
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002153 private void insertTaskAtPosition(TaskRecord task, int position) {
2154 if (position >= mTaskHistory.size()) {
2155 insertTaskAtTop(task, null);
2156 return;
2157 }
2158 // Calculate maximum possible position for this task.
2159 int maxPosition = mTaskHistory.size();
2160 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
2161 && task.topRunningActivityLocked(null) == null) {
2162 // Put non-current user tasks below current user tasks.
2163 while (maxPosition > 0) {
2164 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2165 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
2166 || tmpTask.topRunningActivityLocked(null) == null) {
2167 break;
2168 }
2169 maxPosition--;
2170 }
2171 }
2172 position = Math.min(position, maxPosition);
2173 mTaskHistory.remove(task);
2174 mTaskHistory.add(position, task);
2175 updateTaskMovement(task, true);
2176 }
2177
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002178 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002179 // If the moving task is over home stack, transfer its return type to next task
2180 if (task.isOverHomeStack()) {
2181 final TaskRecord nextTask = getNextTask(task);
2182 if (nextTask != null) {
2183 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2184 }
2185 }
2186
Craig Mautner9c85c202013-10-04 20:11:26 -07002187 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002188 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002189 if (isOnHomeDisplay()) {
2190 ActivityStack lastStack = mStackSupervisor.getLastStack();
2191 final boolean fromHome = lastStack.isHomeStack();
2192 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002193 task.setTaskToReturnTo(fromHome
2194 ? lastStack.topTask() == null
2195 ? HOME_ACTIVITY_TYPE
2196 : lastStack.topTask().taskType
2197 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002198 }
2199 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002200 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002201 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002202
Craig Mautnerac6f8432013-07-17 13:24:59 -07002203 mTaskHistory.remove(task);
2204 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002205 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002206 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002207 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002208 || (newActivity == null && task.topRunningActivityLocked(null) == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002209 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002210 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002211 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002212 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002213 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002214 || tmpTask.topRunningActivityLocked(null) == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002215 break;
2216 }
2217 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002218 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002219 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002220 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002221 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002222 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002223
Craig Mautner8849a5e2013-04-02 16:41:03 -07002224 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002225 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002226 TaskRecord rTask = r.task;
2227 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002228 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2229 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002230 // Last activity in task had been removed or ActivityManagerService is reusing task.
2231 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002232 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002233 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002234 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002235 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002236 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002237 if (!newTask) {
2238 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002239 boolean startIt = true;
2240 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2241 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002242 if (task.getTopActivity() == null) {
2243 // All activities in task are finishing.
2244 continue;
2245 }
Craig Mautner70a86932013-02-28 22:37:44 -08002246 if (task == r.task) {
2247 // Here it is! Now, if this is not yet visible to the
2248 // user, then just add it without starting; it will
2249 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002250 if (!startIt) {
2251 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2252 + task, new RuntimeException("here").fillInStackTrace());
2253 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002254 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002255 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002256 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002257 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002258 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002259 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002260 return;
2261 }
2262 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002263 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002264 startIt = false;
2265 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002266 }
2267 }
2268
2269 // Place a new activity at top of stack, so it is next to interact
2270 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002271
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002272 // If we are not placing the new activity frontmost, we do not want
2273 // to deliver the onUserLeaving callback to the actual frontmost
2274 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002275 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002276 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002277 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002278 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002279 }
Craig Mautner70a86932013-02-28 22:37:44 -08002280
2281 task = r.task;
2282
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002283 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002284 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002285 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002286 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002287 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002288
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002289 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002290 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002291 // We want to show the starting preview window if we are
2292 // switching to a new task, or the next activity's process is
2293 // not currently running.
2294 boolean showStartingIcon = newTask;
2295 ProcessRecord proc = r.app;
2296 if (proc == null) {
2297 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2298 }
2299 if (proc == null || proc.thread == null) {
2300 showStartingIcon = true;
2301 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002302 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002303 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002304 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002305 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002306 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002307 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002308 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002309 ? r.mLaunchTaskBehind
2310 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2311 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002312 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002313 mNoAnimActivities.remove(r);
2314 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002315 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002316 boolean doShow = true;
2317 if (newTask) {
2318 // Even though this activity is starting fresh, we still need
2319 // to reset it to make sure we apply affinities to move any
2320 // existing activities from other tasks in to it.
2321 // If the caller has requested that the target task be
2322 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002323 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002324 resetTaskIfNeededLocked(r, r);
2325 doShow = topRunningNonDelayedActivityLocked(null) == r;
2326 }
George Mount70778d72014-07-01 16:33:45 -07002327 } else if (options != null && new ActivityOptions(options).getAnimationType()
2328 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2329 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002330 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002331 if (r.mLaunchTaskBehind) {
2332 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2333 // tell WindowManager that r is visible even though it is at the back of the stack.
2334 mWindowManager.setAppVisibility(r.appToken, true);
2335 ensureActivitiesVisibleLocked(null, 0);
2336 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002337 // Figure out if we are transitioning from another activity that is
2338 // "has the same starting icon" as the next one. This allows the
2339 // window manager to keep the previous window it had previously
2340 // created, if it still had one.
2341 ActivityRecord prev = mResumedActivity;
2342 if (prev != null) {
2343 // We don't want to reuse the previous starting preview if:
2344 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002345 if (prev.task != r.task) {
2346 prev = null;
2347 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002349 else if (prev.nowVisible) {
2350 prev = null;
2351 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002352 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002353 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002354 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002355 mService.compatibilityInfoForPackageLocked(
2356 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002357 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002358 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002359 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 }
2361 } else {
2362 // If this is the first activity, don't do any fancy animations,
2363 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002364 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002365 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002366 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002367 }
2368 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002369 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002370 }
2371
2372 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002373 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002374 } else {
2375 addRecentActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002376 }
2377 }
2378
Dianne Hackbornbe707852011-11-11 14:32:10 -08002379 final void validateAppTokensLocked() {
2380 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002381 mValidateAppTokens.ensureCapacity(numActivities());
2382 final int numTasks = mTaskHistory.size();
2383 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2384 TaskRecord task = mTaskHistory.get(taskNdx);
2385 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002386 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002387 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002388 }
Craig Mautner000f0022013-02-26 15:04:29 -08002389 TaskGroup group = new TaskGroup();
2390 group.taskId = task.taskId;
2391 mValidateAppTokens.add(group);
2392 final int numActivities = activities.size();
2393 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2394 final ActivityRecord r = activities.get(activityNdx);
2395 group.tokens.add(r.appToken);
2396 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002397 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002398 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002399 }
2400
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002401 /**
2402 * Perform a reset of the given task, if needed as part of launching it.
2403 * Returns the new HistoryRecord at the top of the task.
2404 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002405 /**
2406 * Helper method for #resetTaskIfNeededLocked.
2407 * We are inside of the task being reset... we'll either finish this activity, push it out
2408 * for another task, or leave it as-is.
2409 * @param task The task containing the Activity (taskTop) that might be reset.
2410 * @param forceReset
2411 * @return An ActivityOptions that needs to be processed.
2412 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002413 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002414 ActivityOptions topOptions = null;
2415
2416 int replyChainEnd = -1;
2417 boolean canMoveOptions = true;
2418
2419 // We only do this for activities that are not the root of the task (since if we finish
2420 // the root, we may no longer have the task!).
2421 final ArrayList<ActivityRecord> activities = task.mActivities;
2422 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002423 final int rootActivityNdx = task.findEffectiveRootIndex();
2424 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002425 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002426 if (target.frontOfTask)
2427 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002428
2429 final int flags = target.info.flags;
2430 final boolean finishOnTaskLaunch =
2431 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2432 final boolean allowTaskReparenting =
2433 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2434 final boolean clearWhenTaskReset =
2435 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2436
2437 if (!finishOnTaskLaunch
2438 && !clearWhenTaskReset
2439 && target.resultTo != null) {
2440 // If this activity is sending a reply to a previous
2441 // activity, we can't do anything with it now until
2442 // we reach the start of the reply chain.
2443 // XXX note that we are assuming the result is always
2444 // to the previous activity, which is almost always
2445 // the case but we really shouldn't count on.
2446 if (replyChainEnd < 0) {
2447 replyChainEnd = i;
2448 }
2449 } else if (!finishOnTaskLaunch
2450 && !clearWhenTaskReset
2451 && allowTaskReparenting
2452 && target.taskAffinity != null
2453 && !target.taskAffinity.equals(task.affinity)) {
2454 // If this activity has an affinity for another
2455 // task, then we need to move it out of here. We will
2456 // move it as far out of the way as possible, to the
2457 // bottom of the activity stack. This also keeps it
2458 // correctly ordered with any activities we previously
2459 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002460 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002461 final ActivityRecord bottom =
2462 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002463 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002464 if (bottom != null && target.taskAffinity != null
2465 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002466 // If the activity currently at the bottom has the
2467 // same task affinity as the one we are moving,
2468 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002469 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002470 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002471 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002472 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002473 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002474 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002475 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002476 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002477 + " out to new task " + target.task);
2478 }
2479
Wale Ogunwale706ed792015-08-02 10:29:44 -07002480 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002481
Craig Mautner525f3d92013-05-07 14:01:50 -07002482 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002483 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2484 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002485 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002486 if (p.finishing) {
2487 continue;
2488 }
2489
Craig Mautnere3a74d52013-02-22 14:14:58 -08002490 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002491 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002492 topOptions = p.takeOptionsLocked();
2493 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002494 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002495 }
2496 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002497 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2498 "Removing activity " + p + " from task=" + task + " adding to task="
2499 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002500 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2501 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002502 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002503 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002504
Wale Ogunwale706ed792015-08-02 10:29:44 -07002505 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002506 }
2507
Wale Ogunwale706ed792015-08-02 10:29:44 -07002508 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002509 if (VALIDATE_TOKENS) {
2510 validateAppTokensLocked();
2511 }
2512
2513 replyChainEnd = -1;
2514 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2515 // If the activity should just be removed -- either
2516 // because it asks for it, or the task should be
2517 // cleared -- then finish it and anything that is
2518 // part of its reply chain.
2519 int end;
2520 if (clearWhenTaskReset) {
2521 // In this case, we want to finish this activity
2522 // and everything above it, so be sneaky and pretend
2523 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002524 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002525 } else if (replyChainEnd < 0) {
2526 end = i;
2527 } else {
2528 end = replyChainEnd;
2529 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002530 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002531 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002532 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002533 if (p.finishing) {
2534 continue;
2535 }
2536 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002537 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002538 topOptions = p.takeOptionsLocked();
2539 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002540 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002541 }
2542 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002543 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002544 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002545 if (finishActivityLocked(
2546 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002547 end--;
2548 srcPos--;
2549 }
2550 }
2551 replyChainEnd = -1;
2552 } else {
2553 // If we were in the middle of a chain, well the
2554 // activity that started it all doesn't want anything
2555 // special, so leave it all as-is.
2556 replyChainEnd = -1;
2557 }
2558 }
2559
2560 return topOptions;
2561 }
2562
2563 /**
2564 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2565 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2566 * @param affinityTask The task we are looking for an affinity to.
2567 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2568 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2569 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2570 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002571 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002572 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002573 int replyChainEnd = -1;
2574 final int taskId = task.taskId;
2575 final String taskAffinity = task.affinity;
2576
2577 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2578 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002579 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2580
2581 // Do not operate on or below the effective root Activity.
2582 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002583 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002584 if (target.frontOfTask)
2585 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002586
2587 final int flags = target.info.flags;
2588 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2589 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2590
2591 if (target.resultTo != null) {
2592 // If this activity is sending a reply to a previous
2593 // activity, we can't do anything with it now until
2594 // we reach the start of the reply chain.
2595 // XXX note that we are assuming the result is always
2596 // to the previous activity, which is almost always
2597 // the case but we really shouldn't count on.
2598 if (replyChainEnd < 0) {
2599 replyChainEnd = i;
2600 }
2601 } else if (topTaskIsHigher
2602 && allowTaskReparenting
2603 && taskAffinity != null
2604 && taskAffinity.equals(target.taskAffinity)) {
2605 // This activity has an affinity for our task. Either remove it if we are
2606 // clearing or move it over to our task. Note that
2607 // we currently punt on the case where we are resetting a
2608 // task that is not at the top but who has activities above
2609 // with an affinity to it... this is really not a normal
2610 // case, and we will need to later pull that task to the front
2611 // and usually at that point we will do the reset and pick
2612 // up those remaining activities. (This only happens if
2613 // someone starts an activity in a new task from an activity
2614 // in a task that is not currently on top.)
2615 if (forceReset || finishOnTaskLaunch) {
2616 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002617 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2618 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002619 for (int srcPos = start; srcPos >= i; --srcPos) {
2620 final ActivityRecord p = activities.get(srcPos);
2621 if (p.finishing) {
2622 continue;
2623 }
Todd Kennedy539db512014-12-15 09:57:55 -08002624 finishActivityLocked(
2625 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002626 }
2627 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002628 if (taskInsertionPoint < 0) {
2629 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002630
Craig Mautner77878772013-03-04 19:46:24 -08002631 }
Craig Mautner77878772013-03-04 19:46:24 -08002632
2633 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002634 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2635 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2636 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002637 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002638 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002639 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002640 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002641
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002642 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2643 "Removing and adding activity " + p + " to stack at " + task
2644 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002645 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2646 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002647 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002648 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002649 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002650 if (VALIDATE_TOKENS) {
2651 validateAppTokensLocked();
2652 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002653
2654 // Now we've moved it in to place... but what if this is
2655 // a singleTop activity and we have put it on top of another
2656 // instance of the same activity? Then we drop the instance
2657 // below so it remains singleTop.
2658 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2659 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002660 int targetNdx = taskActivities.indexOf(target);
2661 if (targetNdx > 0) {
2662 ActivityRecord p = taskActivities.get(targetNdx - 1);
2663 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002664 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2665 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002666 }
2667 }
2668 }
2669 }
2670
2671 replyChainEnd = -1;
2672 }
2673 }
Craig Mautner77878772013-03-04 19:46:24 -08002674 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002675 }
2676
Craig Mautner8849a5e2013-04-02 16:41:03 -07002677 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002678 ActivityRecord newActivity) {
2679 boolean forceReset =
2680 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2681 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2682 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2683 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2684 forceReset = true;
2685 }
2686 }
2687
2688 final TaskRecord task = taskTop.task;
2689
2690 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2691 * for remaining tasks. Used for later tasks to reparent to task. */
2692 boolean taskFound = false;
2693
2694 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2695 ActivityOptions topOptions = null;
2696
Craig Mautner77878772013-03-04 19:46:24 -08002697 // Preserve the location for reparenting in the new task.
2698 int reparentInsertionPoint = -1;
2699
Craig Mautnere3a74d52013-02-22 14:14:58 -08002700 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2701 final TaskRecord targetTask = mTaskHistory.get(i);
2702
2703 if (targetTask == task) {
2704 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2705 taskFound = true;
2706 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002707 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2708 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002709 }
2710 }
2711
Craig Mautner70a86932013-02-28 22:37:44 -08002712 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002713 if (taskNdx >= 0) {
2714 do {
2715 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2716 } while (taskTop == null && taskNdx >= 0);
2717 }
Craig Mautner70a86932013-02-28 22:37:44 -08002718
Craig Mautnere3a74d52013-02-22 14:14:58 -08002719 if (topOptions != null) {
2720 // If we got some ActivityOptions from an activity on top that
2721 // was removed from the task, propagate them to the new real top.
2722 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002723 taskTop.updateOptionsLocked(topOptions);
2724 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002725 topOptions.abort();
2726 }
2727 }
2728
2729 return taskTop;
2730 }
2731
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002732 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2733 String resultWho, int requestCode, int resultCode, Intent data) {
2734
2735 if (callingUid > 0) {
2736 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002737 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002738 }
2739
2740 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2741 + " : who=" + resultWho + " req=" + requestCode
2742 + " res=" + resultCode + " data=" + data);
2743 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2744 try {
2745 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2746 list.add(new ResultInfo(resultWho, requestCode,
2747 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002748 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002749 return;
2750 } catch (Exception e) {
2751 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2752 }
2753 }
2754
2755 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2756 }
2757
Craig Mautner299f9602015-01-26 09:47:33 -08002758 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002759 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2760 ActivityRecord next = topRunningActivityLocked(null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002761 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002762 if (next != r) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002763 if (next != null && (mStackId == FREEFORM_WORKSPACE_STACK_ID
2764 || mStackId == DOCKED_STACK_ID)) {
2765 // For freeform and docked stacks we always keep the focus within the stack as
2766 // long as there is a running activity in the stack that we can adjust focus to.
Wale Ogunwale61803ee2015-08-07 19:59:47 -07002767 mService.setFocusedActivityLocked(next, myReason);
2768 return;
2769 } else {
2770 final TaskRecord task = r.task;
2771 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2772 // For non-fullscreen stack, we want to move the focus to the next visible
2773 // stack to prevent the home screen from moving to the top and obscuring
2774 // other visible stacks.
2775 if (!mFullscreen
2776 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2777 return;
2778 }
2779 // Move the home stack to the top if this stack is fullscreen or there is no
2780 // other visible stack.
2781 if (mStackSupervisor.moveHomeStackTaskToTop(
2782 task.getTaskToReturnTo(), myReason)) {
2783 // Activity focus was already adjusted. Nothing else to do...
2784 return;
2785 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002786 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002787 }
2788 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002789
2790 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002791 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002792 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002793 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002794 }
2795 }
2796
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002797 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2798 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2799 final String myReason = reason + " adjustFocusToNextVisibleStack";
2800 if (stack == null) {
2801 return false;
2802 }
2803 final ActivityRecord top = stack.topRunningActivityLocked(null);
2804 if (top == null) {
2805 return false;
2806 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002807 mService.setFocusedActivityLocked(top, myReason);
2808 return true;
2809 }
2810
Craig Mautnerf3333272013-04-22 10:55:53 -07002811 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002812 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002813 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2814 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2815 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002816 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002817 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002818 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2819 "stop-no-history", false)) {
2820 // Activity was finished, no need to continue trying to schedule stop.
2821 adjustFocusedActivityLocked(r, "stopActivityFinished");
2822 r.resumeKeyDispatchingLocked();
2823 return;
2824 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002825 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002826 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002827 + " on stop because we're just sleeping");
2828 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002829 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002830 }
2831
2832 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002833 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002834 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002835 try {
2836 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002837 if (DEBUG_STATES) Slog.v(TAG_STATES,
2838 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002839 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002840 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2841 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002842 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002843 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002844 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002845 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002846 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002847 r.setSleeping(true);
2848 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002849 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002850 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002851 } catch (Exception e) {
2852 // Maybe just ignore exceptions here... if the process
2853 // has crashed, our death notification will clean things
2854 // up.
2855 Slog.w(TAG, "Exception thrown during pause", e);
2856 // Just in case, assume it to be stopped.
2857 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002858 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002859 r.state = ActivityState.STOPPED;
2860 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002861 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002862 }
2863 }
2864 }
2865 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002866
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002867 /**
2868 * @return Returns true if the activity is being finished, false if for
2869 * some reason it is being left as-is.
2870 */
2871 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002872 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002873 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002874 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2875 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002876 + ", result=" + resultCode + ", data=" + resultData
2877 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002878 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002879 return false;
2880 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002881
Craig Mautnerd44711d2013-02-23 11:24:36 -08002882 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002883 return true;
2884 }
2885
Craig Mautnerd2328952013-03-05 12:46:26 -08002886 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002887 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2888 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2889 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2890 ActivityRecord r = activities.get(activityNdx);
2891 if (r.resultTo == self && r.requestCode == requestCode) {
2892 if ((r.resultWho == null && resultWho == null) ||
2893 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2894 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2895 false);
2896 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002897 }
2898 }
2899 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002900 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002901 }
2902
Todd Kennedy539db512014-12-15 09:57:55 -08002903 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002904 ActivityRecord r = topRunningActivityLocked(null);
2905 if (r != null && r.app == app) {
2906 // If the top running activity is from this crashing
2907 // process, then terminate it to avoid getting in a loop.
2908 Slog.w(TAG, " Force finishing activity "
2909 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002910 int taskNdx = mTaskHistory.indexOf(r.task);
2911 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002912 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002913 // Also terminate any activities below it that aren't yet
2914 // stopped, to avoid a situation where one will get
2915 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002916 --activityNdx;
2917 if (activityNdx < 0) {
2918 do {
2919 --taskNdx;
2920 if (taskNdx < 0) {
2921 break;
2922 }
2923 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2924 } while (activityNdx < 0);
2925 }
2926 if (activityNdx >= 0) {
2927 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002928 if (r.state == ActivityState.RESUMED
2929 || r.state == ActivityState.PAUSING
2930 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002931 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002932 Slog.w(TAG, " Force finishing activity "
2933 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002934 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002935 }
2936 }
2937 }
2938 }
2939 }
2940
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002941 final void finishVoiceTask(IVoiceInteractionSession session) {
2942 IBinder sessionBinder = session.asBinder();
2943 boolean didOne = false;
2944 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2945 TaskRecord tr = mTaskHistory.get(taskNdx);
2946 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2947 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2948 ActivityRecord r = tr.mActivities.get(activityNdx);
2949 if (!r.finishing) {
2950 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2951 false);
2952 didOne = true;
2953 }
2954 }
2955 }
2956 }
2957 if (didOne) {
2958 mService.updateOomAdjLocked();
2959 }
2960 }
2961
Craig Mautnerd2328952013-03-05 12:46:26 -08002962 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002963 ArrayList<ActivityRecord> activities = r.task.mActivities;
2964 for (int index = activities.indexOf(r); index >= 0; --index) {
2965 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002966 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002967 break;
2968 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002969 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002970 }
2971 return true;
2972 }
2973
Dianne Hackborn5c607432012-02-28 14:44:19 -08002974 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2975 // send the result
2976 ActivityRecord resultTo = r.resultTo;
2977 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002978 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08002979 + " who=" + r.resultWho + " req=" + r.requestCode
2980 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002981 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002982 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01002983 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002984 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002985 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002986 if (r.info.applicationInfo.uid > 0) {
2987 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2988 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002989 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002990 }
2991 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2992 resultData);
2993 r.resultTo = null;
2994 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002995 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002996
2997 // Make sure this HistoryRecord is not holding on to other resources,
2998 // because clients have remote IPC references to this object so we
2999 // can't assume that will go away and want to avoid circular IPC refs.
3000 r.results = null;
3001 r.pendingResults = null;
3002 r.newIntents = null;
3003 r.icicle = null;
3004 }
3005
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003006 /**
3007 * @return Returns true if this activity has been removed from the history
3008 * list, or false if it is still in the list and will be removed later.
3009 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003010 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3011 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003012 if (r.finishing) {
3013 Slog.w(TAG, "Duplicate finish request for " + r);
3014 return false;
3015 }
3016
Wale Ogunwale7d701172015-03-11 15:36:30 -07003017 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003018 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003019 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003020 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003021 task.taskId, r.shortComponentName, reason);
3022 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003023 final int index = activities.indexOf(r);
3024 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003025 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003026 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003027 // If the caller asked that this activity (and all above it)
3028 // be cleared when the task is reset, don't lose that information,
3029 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003030 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003031 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003032 }
3033 }
3034
3035 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003036
Craig Mautner299f9602015-01-26 09:47:33 -08003037 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003038
Dianne Hackborn5c607432012-02-28 14:44:19 -08003039 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003040
Craig Mautnerde4ef022013-04-07 19:01:33 -07003041 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003042 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003043 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003044 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003045 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003046 ? AppTransition.TRANSIT_TASK_CLOSE
3047 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003048
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003049 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003050 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003051
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003052 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003053 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3054 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3055 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003056 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003057 }
3058
Craig Mautneraea74a52014-03-08 14:23:10 -08003059 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003060 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003061 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003062 } else if (r.state != ActivityState.PAUSING) {
3063 // If the activity is PAUSING, we will complete the finish once
3064 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003065 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003066 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003067 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003068 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003069 }
3070
3071 return false;
3072 }
3073
Craig Mautnerf3333272013-04-22 10:55:53 -07003074 static final int FINISH_IMMEDIATELY = 0;
3075 static final int FINISH_AFTER_PAUSE = 1;
3076 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003077
Craig Mautnerf3333272013-04-22 10:55:53 -07003078 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003079 // First things first: if this activity is currently visible,
3080 // and the resumed activity is not yet visible, then hold off on
3081 // finishing until the resumed one becomes visible.
3082 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003083 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
3084 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07003085 if (mStackSupervisor.mStoppingActivities.size() > 3
3086 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003087 // If we already have a few activities waiting to stop,
3088 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07003089 // them out. Or if r is the last of activity of the last task the stack
3090 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07003091 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003092 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003093 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003094 }
3095 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003096 if (DEBUG_STATES) Slog.v(TAG_STATES,
3097 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003098 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003099 if (oomAdj) {
3100 mService.updateOomAdjLocked();
3101 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003102 return r;
3103 }
3104
3105 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003106 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003107 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003108 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003109 if (mResumedActivity == r) {
3110 mResumedActivity = null;
3111 }
3112 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003113 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003114 r.state = ActivityState.FINISHING;
3115
3116 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003117 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003118 || prevState == ActivityState.STOPPED
3119 || prevState == ActivityState.INITIALIZING) {
3120 // If this activity is already stopped, we can just finish
3121 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003122 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003123 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003124 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003125 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003126 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003127 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003128 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3129 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003130 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003131 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003132
3133 // Need to go through the full pause cycle to get this
3134 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003135 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003136 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003137 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003138 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003139 return r;
3140 }
3141
Craig Mautneree36c772014-07-16 14:56:05 -07003142 void finishAllActivitiesLocked(boolean immediately) {
3143 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003144 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3145 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3146 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3147 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003148 noActivitiesInStack = false;
3149 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003150 continue;
3151 }
Craig Mautneree36c772014-07-16 14:56:05 -07003152 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003153 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3154 }
3155 }
Craig Mautneree36c772014-07-16 14:56:05 -07003156 if (noActivitiesInStack) {
3157 mActivityContainer.onTaskListEmptyLocked();
3158 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003159 }
3160
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003161 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3162 // Basic case: for simple app-centric recents, we need to recreate
3163 // the task if the affinity has changed.
3164 if (srec == null || srec.task.affinity == null ||
3165 !srec.task.affinity.equals(destAffinity)) {
3166 return true;
3167 }
3168 // Document-centric case: an app may be split in to multiple documents;
3169 // they need to re-create their task if this current activity is the root
3170 // of a document, unless simply finishing it will return them to the the
3171 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003172 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3173 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003174 // Okay, this activity is at the root of its task. What to do, what to do...
3175 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3176 // Finishing won't return to an application, so we need to recreate.
3177 return true;
3178 }
3179 // We now need to get the task below it to determine what to do.
3180 int taskIdx = mTaskHistory.indexOf(srec.task);
3181 if (taskIdx <= 0) {
3182 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3183 return false;
3184 }
3185 if (taskIdx == 0) {
3186 // At the bottom of the stack, nothing to go back to.
3187 return true;
3188 }
3189 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3190 if (!srec.task.affinity.equals(prevTask.affinity)) {
3191 // These are different apps, so need to recreate.
3192 return true;
3193 }
3194 }
3195 return false;
3196 }
3197
Wale Ogunwale7d701172015-03-11 15:36:30 -07003198 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003199 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003200 final TaskRecord task = srec.task;
3201 final ArrayList<ActivityRecord> activities = task.mActivities;
3202 final int start = activities.indexOf(srec);
3203 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003204 return false;
3205 }
3206 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003207 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003208 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003209 final ComponentName dest = destIntent.getComponent();
3210 if (start > 0 && dest != null) {
3211 for (int i = finishTo; i >= 0; i--) {
3212 ActivityRecord r = activities.get(i);
3213 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003214 r.info.name.equals(dest.getClassName())) {
3215 finishTo = i;
3216 parent = r;
3217 foundParentInTask = true;
3218 break;
3219 }
3220 }
3221 }
3222
3223 IActivityController controller = mService.mController;
3224 if (controller != null) {
3225 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3226 if (next != null) {
3227 // ask watcher if this is allowed
3228 boolean resumeOK = true;
3229 try {
3230 resumeOK = controller.activityResuming(next.packageName);
3231 } catch (RemoteException e) {
3232 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003233 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003234 }
3235
3236 if (!resumeOK) {
3237 return false;
3238 }
3239 }
3240 }
3241 final long origId = Binder.clearCallingIdentity();
3242 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003243 ActivityRecord r = activities.get(i);
3244 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003245 // Only return the supplied result for the first activity finished
3246 resultCode = Activity.RESULT_CANCELED;
3247 resultData = null;
3248 }
3249
3250 if (parent != null && foundParentInTask) {
3251 final int parentLaunchMode = parent.info.launchMode;
3252 final int destIntentFlags = destIntent.getFlags();
3253 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3254 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3255 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3256 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003257 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3258 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003259 } else {
3260 try {
3261 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3262 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003263 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003264 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003265 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003266 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003267 foundParentInTask = res == ActivityManager.START_SUCCESS;
3268 } catch (RemoteException e) {
3269 foundParentInTask = false;
3270 }
3271 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003272 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003273 }
3274 }
3275 Binder.restoreCallingIdentity(origId);
3276 return foundParentInTask;
3277 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003278 /**
3279 * Perform the common clean-up of an activity record. This is called both
3280 * as part of destroyActivityLocked() (when destroying the client-side
3281 * representation) and cleaning things up as a result of its hosting
3282 * processing going away, in which case there is no remaining client-side
3283 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003284 *
3285 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003286 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003287 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3288 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003289 if (mResumedActivity == r) {
3290 mResumedActivity = null;
3291 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003292 if (mPausingActivity == r) {
3293 mPausingActivity = null;
3294 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003295 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003296
3297 r.configDestroy = false;
3298 r.frozenBeforeDestroy = false;
3299
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003300 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003301 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003302 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003303 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003304 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003305 }
3306
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003307 // Make sure this record is no longer in the pending finishes list.
3308 // This could happen, for example, if we are trimming activities
3309 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003310 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003311 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003312
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003313 // Remove any pending results.
3314 if (r.finishing && r.pendingResults != null) {
3315 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3316 PendingIntentRecord rec = apr.get();
3317 if (rec != null) {
3318 mService.cancelIntentSenderLocked(rec, false);
3319 }
3320 }
3321 r.pendingResults = null;
3322 }
3323
3324 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003325 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003326 }
3327
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003328 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003329 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003330 if (getVisibleBehindActivity() == r) {
3331 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003332 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003333 }
3334
3335 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003336 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003337 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003338 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003339 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003340 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003341 }
3342
Craig Mautner299f9602015-01-26 09:47:33 -08003343 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003344 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003345 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003346 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003347 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3348 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3349
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003350 r.takeFromHistory();
3351 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003352 if (DEBUG_STATES) Slog.v(TAG_STATES,
3353 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003354 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003355 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003356 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003357 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003358 if (VALIDATE_TOKENS) {
3359 validateAppTokensLocked();
3360 }
Craig Mautner312ba862014-02-10 17:55:01 -08003361 final TaskRecord task = r.task;
3362 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003363 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003364 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003365 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3366 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003367 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003368 }
Craig Mautner299f9602015-01-26 09:47:33 -08003369 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003370 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003371 cleanUpActivityServicesLocked(r);
3372 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003373 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003374
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003375 /**
3376 * Perform clean-up of service connections in an activity record.
3377 */
3378 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3379 // Throw away any services that have been bound by this activity.
3380 if (r.connections != null) {
3381 Iterator<ConnectionRecord> it = r.connections.iterator();
3382 while (it.hasNext()) {
3383 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003384 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003385 }
3386 r.connections = null;
3387 }
3388 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003389
Craig Mautneree2e45a2014-06-27 12:10:03 -07003390 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003391 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003392 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003393 mHandler.sendMessage(msg);
3394 }
3395
Craig Mautneree2e45a2014-06-27 12:10:03 -07003396 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003397 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003398 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003399 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3400 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3401 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3402 final ActivityRecord r = activities.get(activityNdx);
3403 if (r.finishing) {
3404 continue;
3405 }
3406 if (r.fullscreen) {
3407 lastIsOpaque = true;
3408 }
3409 if (owner != null && r.app != owner) {
3410 continue;
3411 }
3412 if (!lastIsOpaque) {
3413 continue;
3414 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003415 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003416 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003417 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003418 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003419 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003420 activityRemoved = true;
3421 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003422 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003423 }
3424 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003425 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003426 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003427 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003428 }
3429
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003430 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3431 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003432 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3433 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003434 + " pausing=" + mPausingActivity + " for reason " + reason);
3435 return destroyActivityLocked(r, true, reason);
3436 }
3437 return false;
3438 }
3439
3440 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3441 String reason) {
3442 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003443 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003444 int maxTasks = tasks.size() / 4;
3445 if (maxTasks < 1) {
3446 maxTasks = 1;
3447 }
3448 int numReleased = 0;
3449 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3450 final TaskRecord task = mTaskHistory.get(taskNdx);
3451 if (!tasks.contains(task)) {
3452 continue;
3453 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003454 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003455 int curNum = 0;
3456 final ArrayList<ActivityRecord> activities = task.mActivities;
3457 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3458 final ActivityRecord activity = activities.get(actNdx);
3459 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003460 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003461 + " in state " + activity.state + " resumed=" + mResumedActivity
3462 + " pausing=" + mPausingActivity + " for reason " + reason);
3463 destroyActivityLocked(activity, true, reason);
3464 if (activities.get(actNdx) != activity) {
3465 // Was removed from list, back up so we don't miss the next one.
3466 actNdx--;
3467 }
3468 curNum++;
3469 }
3470 }
3471 if (curNum > 0) {
3472 numReleased += curNum;
3473 maxTasks--;
3474 if (mTaskHistory.get(taskNdx) != task) {
3475 // The entire task got removed, back up so we don't miss the next one.
3476 taskNdx--;
3477 }
3478 }
3479 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003480 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3481 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003482 return numReleased;
3483 }
3484
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003485 /**
3486 * Destroy the current CLIENT SIDE instance of an activity. This may be
3487 * called both when actually finishing an activity, or when performing
3488 * a configuration switch where we destroy the current client-side object
3489 * but then create a new client-side object for this same HistoryRecord.
3490 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003491 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003492 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3493 "Removing activity from " + reason + ": token=" + r
3494 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003495 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003496 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003497 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003498
3499 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003500
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003501 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003502
3503 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003504
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003505 if (hadApp) {
3506 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003507 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003508 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3509 mService.mHeavyWeightProcess = null;
3510 mService.mHandler.sendEmptyMessage(
3511 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3512 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003513 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003514 // Update any services we are bound to that might care about whether
3515 // their client may have activities.
3516 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003517 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003518 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003519 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003520 }
3521 }
3522
3523 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003524
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003525 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003526 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003527 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003528 r.configChangeFlags);
3529 } catch (Exception e) {
3530 // We can just ignore exceptions here... if the process
3531 // has crashed, our death notification will clean things
3532 // up.
3533 //Slog.w(TAG, "Exception thrown during finish", e);
3534 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003535 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003536 removedFromHistory = true;
3537 skipDestroy = true;
3538 }
3539 }
3540
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003541 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003542
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003543 // If the activity is finishing, we need to wait on removing it
3544 // from the list to give it a chance to do its cleanup. During
3545 // that time it may make calls back with its token so we need to
3546 // be able to find it on the list and so we don't want to remove
3547 // it from the list yet. Otherwise, we can just immediately put
3548 // it in the destroyed state since we are not removing it from the
3549 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003550 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003551 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003552 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003553 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003554 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003555 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3556 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003557 if (DEBUG_STATES) Slog.v(TAG_STATES,
3558 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003559 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003560 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003561 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003562 }
3563 } else {
3564 // remove this record from the history.
3565 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003566 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003567 removedFromHistory = true;
3568 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003569 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003570 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003571 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003572 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003573 }
3574 }
3575
3576 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003577
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003578 if (!mLRUActivities.remove(r) && hadApp) {
3579 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3580 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003581
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003582 return removedFromHistory;
3583 }
3584
Craig Mautner299f9602015-01-26 09:47:33 -08003585 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003586 final long origId = Binder.clearCallingIdentity();
3587 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003588 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003589 if (r != null) {
3590 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003591 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003592 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003593
Wale Ogunwale60454db2015-01-23 16:05:07 -08003594 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003595 if (r.state == ActivityState.DESTROYING) {
3596 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003597 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003598 }
3599 }
Craig Mautner05d29032013-05-03 13:40:13 -07003600 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003601 } finally {
3602 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003603 }
3604 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003605
Todd Kennedyaab56db2015-01-30 09:39:53 -08003606 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003607 if (hasVisibleBehindActivity() &&
3608 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003609 if (r == topRunningActivityLocked(null)) {
3610 // Don't release the top activity if it has requested to run behind the next
3611 // activity.
3612 return;
3613 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003614 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003615 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003616 " thread=" + r.app.thread);
3617 if (r != null && r.app != null && r.app.thread != null) {
3618 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003619 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003620 } catch (RemoteException e) {
3621 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003622 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003623 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003624 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003625 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003626 }
3627 }
3628 }
3629
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003630 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003631 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3632 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003633 if (r != null) {
3634 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003635 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003636 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003637 }
3638 mStackSupervisor.resumeTopActivitiesLocked();
3639 }
3640
Jose Lima4b6c6692014-08-12 17:41:12 -07003641 boolean hasVisibleBehindActivity() {
3642 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003643 }
3644
Jose Lima4b6c6692014-08-12 17:41:12 -07003645 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003646 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003647 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003648 }
3649 }
3650
Jose Lima4b6c6692014-08-12 17:41:12 -07003651 ActivityRecord getVisibleBehindActivity() {
3652 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003653 }
3654
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003655 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3656 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003657 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003658 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3659 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003660 while (i > 0) {
3661 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003662 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003663 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003664 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003665 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003666 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003667 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003668 }
3669 }
3670 }
3671
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003672 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3673 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003674 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3675 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003676 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3677 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003678 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003679 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003680 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3681 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003682
3683 boolean hasVisibleActivities = false;
3684
3685 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003686 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003687 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3688 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003689 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3690 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3691 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3692 final ActivityRecord r = activities.get(activityNdx);
3693 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003694 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3695 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003696 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003697 if (r.visible) {
3698 hasVisibleActivities = true;
3699 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003700 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003701 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3702 // Don't currently have state for the activity, or
3703 // it is finishing -- always remove it.
3704 remove = true;
3705 } else if (r.launchCount > 2 &&
3706 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3707 // We have launched this activity too many times since it was
3708 // able to run, so give up and remove it.
3709 remove = true;
3710 } else {
3711 // The process may be gone, but the activity lives on!
3712 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003713 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003714 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003715 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3716 "Removing activity " + r + " from stack at " + i
3717 + ": haveState=" + r.haveState
3718 + " stateNotNeeded=" + r.stateNotNeeded
3719 + " finishing=" + r.finishing
3720 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003721 if (!r.finishing) {
3722 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3723 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3724 r.userId, System.identityHashCode(r),
3725 r.task.taskId, r.shortComponentName,
3726 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003727 if (r.state == ActivityState.RESUMED) {
3728 mService.updateUsageStats(r, false);
3729 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003730 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003731 } else {
3732 // We have the current state for this activity, so
3733 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003734 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3735 if (DEBUG_APP) Slog.v(TAG_APP,
3736 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003737 r.app = null;
3738 r.nowVisible = false;
3739 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003740 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003741 "App died, clearing saved state of " + r);
3742 r.icicle = null;
3743 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003744 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003745 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003746 if (remove) {
3747 removeActivityFromHistoryLocked(r, "appDied");
3748 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003749 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003750 }
3751 }
3752
3753 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003754 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003755
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003756 final void updateTransitLocked(int transit, Bundle options) {
3757 if (options != null) {
3758 ActivityRecord r = topRunningActivityLocked(null);
3759 if (r != null && r.state != ActivityState.RESUMED) {
3760 r.updateOptionsLocked(options);
3761 } else {
3762 ActivityOptions.abort(options);
3763 }
3764 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003765 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003766 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003767
Craig Mautner21d24a22014-04-23 11:45:37 -07003768 void updateTaskMovement(TaskRecord task, boolean toFront) {
3769 if (task.isPersistable) {
3770 task.mLastTimeMoved = System.currentTimeMillis();
3771 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3772 // recently will be most negative, tasks sent to the bottom before that will be less
3773 // negative. Similarly for recent tasks moved to the top which will be most positive.
3774 if (!toFront) {
3775 task.mLastTimeMoved *= -1;
3776 }
3777 }
3778 }
3779
Craig Mautner84984fa2014-06-19 11:19:20 -07003780 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003781 final int top = mTaskHistory.size() - 1;
3782 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3783 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003784 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003785 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3786 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003787 mTaskHistory.remove(taskNdx);
3788 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003789 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003790 return;
3791 }
3792 }
3793 }
3794
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003795 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003796 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003797 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003798
Craig Mautner11bf9a52013-02-19 14:08:51 -08003799 final int numTasks = mTaskHistory.size();
3800 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003801 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003802 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003803 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003804 ActivityOptions.abort(options);
3805 } else {
3806 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3807 }
3808 return;
3809 }
3810
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003811 if (timeTracker != null) {
3812 // The caller wants a time tracker associated with this task.
3813 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3814 tr.mActivities.get(i).appTimeTracker = timeTracker;
3815 }
3816 }
3817
Craig Mautner11bf9a52013-02-19 14:08:51 -08003818 // Shift all activities with this task up to the top
3819 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003820 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003821
Chong Zhang45c25ce2015-08-10 22:18:26 -07003822 // Don't refocus if invisible to current user
3823 ActivityRecord top = tr.getTopActivity();
3824 if (!okToShowLocked(top)) {
3825 addRecentActivityLocked(top);
3826 ActivityOptions.abort(options);
3827 return;
3828 }
3829
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003830 // Set focus to the top running activity of this stack.
3831 ActivityRecord r = topRunningActivityLocked(null);
3832 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003833
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003834 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003835 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003836 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003837 if (r != null) {
3838 mNoAnimActivities.add(r);
3839 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003840 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003841 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003842 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003843 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003844
Craig Mautner05d29032013-05-03 13:40:13 -07003845 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003846 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003847
3848 if (VALIDATE_TOKENS) {
3849 validateAppTokensLocked();
3850 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003851 }
3852
3853 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003854 * Worker method for rearranging history stack. Implements the function of moving all
3855 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003856 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003857 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003858 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3859 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003860 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003861 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003862 * @return Returns true if the move completed, false if not.
3863 */
Craig Mautner299f9602015-01-26 09:47:33 -08003864 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003865 final TaskRecord tr = taskForIdLocked(taskId);
3866 if (tr == null) {
3867 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3868 return false;
3869 }
3870
3871 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003872 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003873
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003874 // If we have a watcher, preflight the move before committing to it. First check
3875 // for *other* available tasks, but if none are available, then try again allowing the
3876 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003877 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003878 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003879 if (next == null) {
3880 next = topRunningActivityLocked(null, 0);
3881 }
3882 if (next != null) {
3883 // ask watcher if this is allowed
3884 boolean moveOK = true;
3885 try {
3886 moveOK = mService.mController.activityResuming(next.packageName);
3887 } catch (RemoteException e) {
3888 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003889 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003890 }
3891 if (!moveOK) {
3892 return false;
3893 }
3894 }
3895 }
3896
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003897 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003898
riddle_hsuc215a4f2014-12-27 12:10:45 +08003899 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07003900
3901 // If true, we should resume the home activity next if the task we are moving to the
3902 // back is over the home stack. We force to false if the task we are moving to back
3903 // is the home task and we don't want it resumed after moving to the back.
3904 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
3905 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08003906 final TaskRecord nextTask = getNextTask(tr);
3907 if (nextTask != null) {
3908 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3909 } else {
3910 prevIsHome = true;
3911 }
3912 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003913 mTaskHistory.remove(tr);
3914 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003915 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003916
Craig Mautnerc8143c62013-09-03 12:15:57 -07003917 // There is an assumption that moving a task to the back moves it behind the home activity.
3918 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003919 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003920 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3921 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003922 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003923 break;
3924 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003925 if (taskNdx == 1) {
3926 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003927 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003928 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003929 }
3930
Craig Mautner299f9602015-01-26 09:47:33 -08003931 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003932 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003933
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003934 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003935 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003936 }
3937
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003938 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07003939 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003940 if (!mService.mBooting && !mService.mBooted) {
3941 // Not ready yet!
3942 return false;
3943 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003944 final int taskToReturnTo = tr.getTaskToReturnTo();
3945 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003946 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003947 }
3948
Craig Mautner05d29032013-05-03 13:40:13 -07003949 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003950 return true;
3951 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003952
Craig Mautner8849a5e2013-04-02 16:41:03 -07003953 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003954 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003955 final Uri data = r.intent.getData();
3956 final String strData = data != null ? data.toSafeString() : null;
3957
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003958 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003959 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003960 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003961 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003962 }
3963
3964 /**
3965 * Make sure the given activity matches the current configuration. Returns
3966 * false if the activity had to be destroyed. Returns true if the
3967 * configuration is the same, or the activity will remain running as-is
3968 * for whatever reason. Ensures the HistoryRecord is updated with the
3969 * correct configuration and all other bookkeeping is handled.
3970 */
3971 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3972 int globalChanges) {
3973 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003974 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003975 "Skipping config check (will change): " + r);
3976 return true;
3977 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003978
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003979 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003980 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003981
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003982 // Short circuit: if the two configurations are the exact same
3983 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003984 final Configuration newConfig = mService.mConfiguration;
3985 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003986 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003987 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08003988 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003989 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003990 "Configuration unchanged in " + r);
3991 return true;
3992 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003993
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003994 // We don't worry about activities that are finishing.
3995 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003996 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003997 "Configuration doesn't matter in finishing " + r);
3998 r.stopFreezingScreenLocked(false);
3999 return true;
4000 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004001
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004002 // Okay we now are going to make this activity have the new config.
4003 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004004 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004005 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004006 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004007 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004008
Filip Gruszczynski23493322015-07-29 17:02:59 -07004009 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004010 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004011 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004012 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004013 "Configuration no differences in " + r);
4014 return true;
4015 }
4016
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004017 // If the activity isn't currently running, just leave the new
4018 // configuration and it will pick that up next time it starts.
4019 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004020 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004021 "Configuration doesn't matter not running " + r);
4022 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004023 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004024 return true;
4025 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004026
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004027 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004028 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4029 "Checking to restart " + r.info.name + ": changed=0x"
4030 + Integer.toHexString(changes) + ", handles=0x"
4031 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
4032
Dianne Hackborne6676352011-06-01 16:51:20 -07004033 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004034 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4035 r.configChangeFlags |= changes;
4036 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004037 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004038 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004039 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004040 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004041 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004042 } else if (r.state == ActivityState.PAUSING) {
4043 // A little annoying: we are waiting for this activity to
4044 // finish pausing. Let's not do anything now, but just
4045 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004046 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004047 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004048 r.configDestroy = true;
4049 return true;
4050 } else if (r.state == ActivityState.RESUMED) {
4051 // Try to optimize this case: the configuration is changing
4052 // and we need to restart the top, resumed activity.
4053 // Instead of doing the normal handshaking, just say
4054 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004055 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004056 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004057 relaunchActivityLocked(r, r.configChangeFlags, true);
4058 r.configChangeFlags = 0;
4059 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004060 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004061 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004062 relaunchActivityLocked(r, r.configChangeFlags, false);
4063 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004064 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004065
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004066 // All done... tell the caller we weren't able to keep this
4067 // activity around.
4068 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004069 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004070
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004071 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004072 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004073 // changes is always sent to all processes when they happen so it can just use whatever
4074 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004075 if (r.app != null && r.app.thread != null) {
4076 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004077 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004078 r.app.thread.scheduleActivityConfigurationChanged(
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004079 r.appToken, new Configuration(taskConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004080 } catch (RemoteException e) {
4081 // If process died, whatever.
4082 }
4083 }
4084 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004085
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004086 return true;
4087 }
4088
Filip Gruszczynski23493322015-07-29 17:02:59 -07004089 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4090 Configuration oldTaskOverride) {
4091 // Determine what has changed. May be nothing, if this is a config
4092 // that has come back from the app after going idle. In that case
4093 // we just want to leave the official config object now in the
4094 // activity and do nothing else.
4095 int taskChanges = oldTaskOverride.diff(taskConfig);
4096 // We don't want to use size changes if they don't cross boundaries that are important to
4097 // the app.
4098 if ((taskChanges & ActivityInfo.CONFIG_SCREEN_SIZE) != 0) {
4099 final boolean crosses = record.crossesHorizontalSizeThreshold(
4100 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4101 || record.crossesVerticalSizeThreshold(
4102 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4103 if (!crosses) {
4104 taskChanges &= ~ActivityInfo.CONFIG_SCREEN_SIZE;
4105 }
4106 }
4107 if ((taskChanges & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4108 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4109 final int newSmallest = taskConfig.smallestScreenWidthDp;
4110 final boolean crosses = record.crossesHorizontalSizeThreshold(oldSmallest, newSmallest)
4111 || record.crossesVerticalSizeThreshold(oldSmallest, newSmallest);
4112 if (!crosses) {
4113 taskChanges &= ~ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4114 }
4115 }
4116 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4117 }
4118
4119 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4120 Configuration oldTaskOverride, int taskChanges) {
4121 if (taskChanges == 0) {
4122 // {@link Configuration#diff} doesn't catch changes from unset values.
4123 // Check for changes we care about.
4124 if (oldTaskOverride.orientation != taskConfig.orientation) {
4125 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
4126 }
4127 // We want to explicitly track situations where the size configuration goes from
4128 // undefined to defined. We don't care about crossing the threshold in that case,
4129 // because there is no threshold.
4130 final int oldHeight = oldTaskOverride.screenHeightDp;
4131 final int newHeight = taskConfig.screenHeightDp;
4132 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4133 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4134 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4135 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4136 }
4137 final int oldWidth = oldTaskOverride.screenWidthDp;
4138 final int newWidth = taskConfig.screenWidthDp;
4139 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4140 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4141 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4142 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4143 }
4144 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4145 final int newSmallest = taskConfig.smallestScreenWidthDp;
4146 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4147 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4148 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4149 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4150 }
4151 }
4152 return taskChanges;
4153 }
4154
Craig Mautner2568c3a2015-03-26 14:22:34 -07004155 private boolean relaunchActivityLocked(ActivityRecord r, int changes, boolean andResume) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004156 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004157 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004158 if (andResume) {
4159 results = r.results;
4160 newIntents = r.newIntents;
4161 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004162 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4163 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004164 + " andResume=" + andResume);
4165 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004166 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004167 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004168
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004169 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004170
Craig Mautner34b73df2014-01-12 21:11:08 -08004171 mStackSupervisor.removeChildActivityContainers(r);
4172
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004173 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004174 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4175 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004176 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004177 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4178 !andResume, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004179 new Configuration(r.task.mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004180 // Note: don't need to call pauseIfSleepingLocked() here, because
4181 // the caller will only pass in 'andResume' if this activity is
4182 // currently resumed, which implies we aren't sleeping.
4183 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004184 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004185 }
4186
4187 if (andResume) {
4188 r.results = null;
4189 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004190 r.state = ActivityState.RESUMED;
4191 } else {
4192 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4193 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004194 }
4195
4196 return true;
4197 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004198
4199 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004200 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4201 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4202 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4203 final ActivityRecord r = activities.get(activityNdx);
4204 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004205 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004206 }
4207 if (r.fullscreen && !r.finishing) {
4208 return false;
4209 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004210 }
4211 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004212 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004213 if (r == null) {
4214 return false;
4215 }
4216 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4217 + " would have returned true for r=" + r);
4218 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004219 }
4220
4221 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004222 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4223 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4224 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4225 final ActivityRecord r = activities.get(activityNdx);
4226 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004227 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004228 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004229 }
4230 }
4231 }
4232
Wale Ogunwale540e1232015-05-01 15:35:39 -07004233 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4234 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004235 boolean didSomething = false;
4236 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004237 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004238 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4239 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4240 int numActivities = activities.size();
4241 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4242 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004243 final boolean sameComponent =
4244 (r.packageName.equals(packageName) && (filterByClasses == null
4245 || filterByClasses.contains(r.realActivity.getClassName())))
4246 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004247 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004248 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004249 && (r.app == null || evenPersistent || !r.app.persistent)) {
4250 if (!doit) {
4251 if (r.finishing) {
4252 // If this activity is just finishing, then it is not
4253 // interesting as far as something to stop.
4254 continue;
4255 }
4256 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004257 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004258 if (r.isHomeActivity()) {
4259 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4260 Slog.i(TAG, "Skip force-stop again " + r);
4261 continue;
4262 } else {
4263 homeActivity = r.realActivity;
4264 }
4265 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004266 didSomething = true;
4267 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004268 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004269 if (r.app != null) {
4270 r.app.removed = true;
4271 }
4272 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004273 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004274 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004275 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4276 true)) {
4277 // r has been deleted from mActivities, accommodate.
4278 --numActivities;
4279 --activityNdx;
4280 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004281 }
4282 }
4283 }
4284 return didSomething;
4285 }
4286
Dianne Hackborn09233282014-04-30 11:33:59 -07004287 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004288 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4289 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004290 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004291 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004292 if (task.getTopActivity() == null) {
4293 continue;
4294 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004295 ActivityRecord r = null;
4296 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004297 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004298 int numActivities = 0;
4299 int numRunning = 0;
4300 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004301 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004302 continue;
4303 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004304 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004305 tmp = activities.get(activityNdx);
4306 if (tmp.finishing) {
4307 continue;
4308 }
4309 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004310
Craig Mautneraab647e2013-02-28 16:31:36 -08004311 // Initialize state for next task if needed.
4312 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4313 top = r;
4314 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004315 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004316
4317 // Add 'r' into the current task.
4318 numActivities++;
4319 if (r.app != null && r.app.thread != null) {
4320 numRunning++;
4321 }
4322
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004323 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004324 TAG, r.intent.getComponent().flattenToShortString()
4325 + ": task=" + r.task);
4326 }
4327
4328 RunningTaskInfo ci = new RunningTaskInfo();
4329 ci.id = task.taskId;
4330 ci.baseActivity = r.intent.getComponent();
4331 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004332 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004333 if (focusedStack && topTask) {
4334 // Give the latest time to ensure foreground task can be sorted
4335 // at the first, because lastActiveTime of creating task is 0.
4336 ci.lastActiveTime = System.currentTimeMillis();
4337 topTask = false;
4338 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004339
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004340 if (top.task != null) {
4341 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004342 }
4343 ci.numActivities = numActivities;
4344 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004345 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004346 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004347 }
4348
4349 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004350 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004351 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004352 if (top >= 0) {
4353 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4354 int activityTop = activities.size() - 1;
4355 if (activityTop > 0) {
4356 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4357 "unhandled-back", true);
4358 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004359 }
4360 }
4361
Craig Mautner6b74cb52013-09-27 17:02:21 -07004362 /**
4363 * Reset local parameters because an app's activity died.
4364 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004365 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004366 */
4367 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004368 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004369 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004370 "App died while pausing: " + mPausingActivity);
4371 mPausingActivity = null;
4372 }
4373 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4374 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004375 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004376 }
4377
Craig Mautner19091252013-10-05 00:03:53 -07004378 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004379 }
4380
Craig Mautnercae015f2013-02-08 14:31:27 -08004381 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004382 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4383 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4384 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4385 final ActivityRecord r = activities.get(activityNdx);
4386 if (r.app == app) {
4387 Slog.w(TAG, " Force finishing activity "
4388 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004389 // Force the destroy to skip right to removal.
4390 r.app = null;
4391 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004392 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004393 }
4394 }
4395 }
4396
Dianne Hackborn390517b2013-05-30 15:03:32 -07004397 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004398 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004399 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004400 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4401 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004402 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4403 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004404 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004405 " Task id #" + task.taskId + "\n" +
4406 " mFullscreen=" + task.mFullscreen + "\n" +
4407 " mBounds=" + task.mBounds + "\n" +
4408 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004409 if (printed) {
4410 header = null;
4411 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004412 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004413 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004414 }
4415
4416 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4417 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4418
4419 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004420 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4421 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004422 }
4423 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004424 final int top = mTaskHistory.size() - 1;
4425 if (top >= 0) {
4426 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4427 int listTop = list.size() - 1;
4428 if (listTop >= 0) {
4429 activities.add(list.get(listTop));
4430 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004431 }
4432 } else {
4433 ItemMatcher matcher = new ItemMatcher();
4434 matcher.build(name);
4435
Craig Mautneraab647e2013-02-28 16:31:36 -08004436 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4437 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4438 if (matcher.match(r1, r1.intent.getComponent())) {
4439 activities.add(r1);
4440 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004441 }
4442 }
4443 }
4444
4445 return activities;
4446 }
4447
4448 ActivityRecord restartPackage(String packageName) {
4449 ActivityRecord starting = topRunningActivityLocked(null);
4450
4451 // All activities that came from the package must be
4452 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004453 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4454 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4455 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4456 final ActivityRecord a = activities.get(activityNdx);
4457 if (a.info.packageName.equals(packageName)) {
4458 a.forceNewConfig = true;
4459 if (starting != null && a == starting && a.visible) {
4460 a.startFreezingScreenLocked(starting.app,
4461 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4462 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004463 }
4464 }
4465 }
4466
4467 return starting;
4468 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004469
Craig Mautner299f9602015-01-26 09:47:33 -08004470 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004471 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004472 }
4473
Wale Ogunwale000957c2015-04-03 08:19:12 -07004474 /**
4475 * Removes the input task from this stack.
4476 * @param task to remove.
4477 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004478 * @param moving task to another stack. In the case we are moving we don't want to perform
4479 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004480 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004481 void removeTask(TaskRecord task, String reason, boolean moving) {
4482 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004483 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004484 mWindowManager.removeTask(task.taskId);
4485 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004486
Craig Mautner04a0ea62014-01-13 12:51:26 -08004487 final ActivityRecord r = mResumedActivity;
4488 if (r != null && r.task == task) {
4489 mResumedActivity = null;
4490 }
4491
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004492 final int taskNdx = mTaskHistory.indexOf(task);
4493 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004494 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4495 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4496 if (!nextTask.isOverHomeStack()) {
4497 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4498 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004499 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004500 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004501 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004502
Wale Ogunwale040b4702015-08-06 18:10:50 -07004503 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004504 final boolean isVoiceSession = task.voiceSession != null;
4505 if (isVoiceSession) {
4506 try {
4507 task.voiceSession.taskFinished(task.intent, task.taskId);
4508 } catch (RemoteException e) {
4509 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004510 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004511 if (task.autoRemoveFromRecents() || isVoiceSession) {
4512 // Task creator asked to remove this when done, or this task was a voice
4513 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004514 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004515 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004516 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004517 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004518
4519 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004520 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004521 final boolean notHomeStack = !isHomeStack();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004522 if (isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004523 String myReason = reason + " leftTaskHistoryEmpty";
4524 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
4525 mStackSupervisor.moveHomeStack(notHomeStack, myReason);
4526 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004527 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004528 if (mStacks != null) {
4529 mStacks.remove(this);
4530 mStacks.add(0, this);
4531 }
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004532 if (notHomeStack) {
4533 mActivityContainer.onTaskListEmptyLocked();
4534 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004535 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004536
4537 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004538 }
4539
Dianne Hackborn91097de2014-04-04 18:02:06 -07004540 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4541 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4542 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004543 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4544 voiceInteractor);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004545 if (mTaskPositioner != null) {
4546 mTaskPositioner.updateDefaultBounds(task, mTaskHistory);
4547 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004548 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004549 return task;
4550 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004551
4552 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004553 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004554 }
4555
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004556 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004557 task.stack = this;
4558 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004559 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004560 } else {
4561 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004562 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004563 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004564 if (!moving && task.voiceSession != null) {
4565 try {
4566 task.voiceSession.taskStarted(task.intent, task.taskId);
4567 } catch (RemoteException e) {
4568 }
4569 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004570 }
4571
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004572 void positionTask(final TaskRecord task, int position, boolean moving) {
4573 task.stack = this;
4574 insertTaskAtPosition(task, position);
4575 if (!moving && task.voiceSession != null) {
4576 try {
4577 task.voiceSession.taskStarted(task.intent, task.taskId);
4578 } catch (RemoteException e) {
4579 }
4580 }
4581 }
4582
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004583 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07004584 final Rect bounds = task.getLaunchBounds();
4585 final Configuration config =
4586 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4587 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
4588 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
4589 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
4590 bounds);
4591 if (config != null) {
4592 task.updateOverrideConfiguration(config, bounds);
4593 }
4594 r.taskConfigOverride = task.mOverrideConfig;
4595 }
4596
4597 private void setAppTask(ActivityRecord r, TaskRecord task) {
4598 final Rect bounds = task.getLaunchBounds();
4599 final Configuration config =
4600 mWindowManager.setAppTask(r.appToken, task.taskId, task.getLaunchBounds());
4601 if (config != null) {
4602 task.updateOverrideConfiguration(config, bounds);
4603 }
4604 r.taskConfigOverride = task.mOverrideConfig;
4605 }
4606
Craig Mautnerc00204b2013-03-05 15:02:14 -08004607 public int getStackId() {
4608 return mStackId;
4609 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004610
4611 @Override
4612 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004613 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4614 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004615 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004616
Craig Mautner15df08a2015-04-01 12:17:18 -07004617 void onLockTaskPackagesUpdatedLocked() {
4618 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4619 mTaskHistory.get(taskNdx).setLockTaskAuth();
4620 }
4621 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004622}