blob: 356565fc802f16cdd3d94884ac0977bf2639213f [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
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700251 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700252 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
253 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
254 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
255 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700256 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700257 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700258 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700259
260 static class ScheduleDestroyArgs {
261 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700262 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700263 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700264 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700265 mReason = reason;
266 }
267 }
268
Zoran Marcetaf958b322012-08-09 20:27:12 +0900269 final Handler mHandler;
270
271 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800272
Craig Mautnerc8143c62013-09-03 12:15:57 -0700273 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900274 super(looper);
275 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700276
Zoran Marcetaf958b322012-08-09 20:27:12 +0900277 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700278 public void handleMessage(Message msg) {
279 switch (msg.what) {
280 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800281 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700282 // We don't at this point know if the activity is fullscreen,
283 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800284 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700285 synchronized (mService) {
286 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700287 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700288 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700289 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800290 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700291 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700292 case LAUNCH_TICK_MSG: {
293 ActivityRecord r = (ActivityRecord)msg.obj;
294 synchronized (mService) {
295 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700296 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700297 }
298 }
299 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700300 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800301 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700302 // We don't at this point know if the activity is fullscreen,
303 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800304 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800305 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800306 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800307 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700308 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700309 case STOP_TIMEOUT_MSG: {
310 ActivityRecord r = (ActivityRecord)msg.obj;
311 // We don't at this point know if the activity is fullscreen,
312 // so we need to be conservative and assume it isn't.
313 Slog.w(TAG, "Activity stop timeout for " + r);
314 synchronized (mService) {
315 if (r.isInHistory()) {
316 activityStoppedLocked(r, null, null, null);
317 }
318 }
319 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700320 case DESTROY_ACTIVITIES_MSG: {
321 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
322 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700323 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700324 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700325 } break;
326 case TRANSLUCENT_TIMEOUT_MSG: {
327 synchronized (mService) {
328 notifyActivityDrawnLocked(null);
329 }
330 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700331 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700332 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700333 final ActivityRecord r = getVisibleBehindActivity();
334 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700335 if (r != null) {
336 mService.killAppAtUsersRequest(r.app, null);
337 }
338 }
339 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700340 }
341 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800342 }
343
Craig Mautner34b73df2014-01-12 21:11:08 -0800344 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800345 int count = 0;
346 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
347 count += mTaskHistory.get(taskNdx).mActivities.size();
348 }
349 return count;
350 }
351
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800352 int numTasks() {
353 return mTaskHistory.size();
354 }
355
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800356 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
357 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800358 mActivityContainer = activityContainer;
359 mStackSupervisor = activityContainer.getOuter();
360 mService = mStackSupervisor.mService;
361 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
362 mWindowManager = mService.mWindowManager;
363 mStackId = activityContainer.mStackId;
364 mCurrentUser = mService.mCurrentUserId;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800365 mRecentTasks = recentTasks;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700366 }
Craig Mautner5962b122012-10-05 14:45:52 -0700367
Amith Yamasani734983f2014-03-04 16:48:05 -0800368 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700369 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700370 }
371
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700372 final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800373 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner6b74cb52013-09-27 17:02:21 -0700374 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked(notTop);
375 if (r != null) {
376 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800377 }
378 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700379 return null;
380 }
381
382 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800383 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
384 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800385 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800386 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
387 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800388 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800389 return r;
390 }
391 }
392 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700393 return null;
394 }
395
396 /**
397 * This is a simplified version of topRunningActivityLocked that provides a number of
398 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800399 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700400 * @param token If non-null, any history records matching this token will be skipped.
401 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800402 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700403 * @return Returns the HistoryRecord of the next activity on the stack.
404 */
405 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800406 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
407 TaskRecord task = mTaskHistory.get(taskNdx);
408 if (task.taskId == taskId) {
409 continue;
410 }
411 ArrayList<ActivityRecord> activities = task.mActivities;
412 for (int i = activities.size() - 1; i >= 0; --i) {
413 final ActivityRecord r = activities.get(i);
414 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800415 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800416 return r;
417 }
418 }
419 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700420 return null;
421 }
422
Craig Mautner8849a5e2013-04-02 16:41:03 -0700423 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700424 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
425 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700426 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
427 final ActivityRecord r = activities.get(activityNdx);
428 if (!r.finishing) {
429 return r;
430 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700431 }
432 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700433 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700434 }
435
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700436 final TaskRecord topTask() {
437 final int size = mTaskHistory.size();
438 if (size > 0) {
439 return mTaskHistory.get(size - 1);
440 }
441 return null;
442 }
443
Craig Mautnerd2328952013-03-05 12:46:26 -0800444 TaskRecord taskForIdLocked(int id) {
445 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
446 final TaskRecord task = mTaskHistory.get(taskNdx);
447 if (task.taskId == id) {
448 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800449 }
450 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700451 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700452 }
453
Craig Mautnerd2328952013-03-05 12:46:26 -0800454 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700455 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800456 return isInStackLocked(r);
457 }
458
459 ActivityRecord isInStackLocked(ActivityRecord r) {
460 if (r == null) {
461 return null;
462 }
463 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700464 if (task != null && task.stack != null
465 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800466 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800467 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800468 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800469 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800470 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800471 }
472
Craig Mautner2420ead2013-04-01 17:13:20 -0700473 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700474 final boolean hadit = mLRUActivities.remove(r);
475 mLRUActivities.add(r);
476 return hadit;
477 }
478
Craig Mautnerde4ef022013-04-07 19:01:33 -0700479 final boolean isHomeStack() {
480 return mStackId == HOME_STACK_ID;
481 }
482
Craig Mautnere0a38842013-12-16 16:14:02 -0800483 final boolean isOnHomeDisplay() {
484 return isAttached() &&
485 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
486 }
487
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700488 void moveToFront(String reason) {
489 moveToFront(reason, null);
490 }
491
492 /**
493 * @param reason The reason for moving the stack to the front.
494 * @param task If non-null, the task will be moved to the top of the stack.
495 * */
496 void moveToFront(String reason, TaskRecord task) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800497 if (isAttached()) {
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800498 final boolean homeStack = isHomeStack()
499 || (mActivityContainer.mParentActivity != null
500 && mActivityContainer.mParentActivity.isHomeActivity());
501 ActivityStack lastFocusStack = null;
502 if (!homeStack) {
503 // Need to move this stack to the front before calling
504 // {@link ActivityStackSupervisor#moveHomeStack} below.
505 lastFocusStack = mStacks.get(mStacks.size() - 1);
506 mStacks.remove(this);
507 mStacks.add(this);
Wale Ogunwaleaf0e4482015-02-24 12:27:31 -0800508 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -0700509 // TODO(multi-display): Focus stack currently adjusted in call to move home stack.
510 // Needs to also work if focus is moving to the non-home display.
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800511 if (isOnHomeDisplay()) {
512 mStackSupervisor.moveHomeStack(homeStack, reason, lastFocusStack);
513 }
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700514 if (task != null) {
515 insertTaskAtTop(task, null);
516 } else {
517 task = topTask();
518 }
Craig Mautner6b904ef2014-12-17 15:45:03 -0800519 if (task != null) {
520 mWindowManager.moveTaskToTop(task.taskId);
521 }
Craig Mautner4a1cb222013-12-04 16:14:06 -0800522 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800523 }
524
525 final boolean isAttached() {
526 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800527 }
528
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700529 /**
530 * Returns the top activity in any existing task matching the given
531 * Intent. Returns null if no such task is found.
532 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700533 ActivityRecord findTaskLocked(ActivityRecord target) {
534 Intent intent = target.intent;
535 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700536 ComponentName cls = intent.getComponent();
537 if (info.targetActivity != null) {
538 cls = new ComponentName(info.packageName, info.targetActivity);
539 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700540 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700541 boolean isDocument = intent != null & intent.isDocument();
542 // If documentData is non-null then it must match the existing task data.
543 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800544
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700545 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800546 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
547 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700548 if (task.voiceSession != null) {
549 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700550 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700551 continue;
552 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700553 if (task.userId != userId) {
554 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700555 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700556 continue;
557 }
Craig Mautner000f0022013-02-26 15:04:29 -0800558 final ActivityRecord r = task.getTopActivity();
559 if (r == null || r.finishing || r.userId != userId ||
560 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700561 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800562 continue;
563 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700564 if (r.mActivityType != target.mActivityType) {
565 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
566 continue;
567 }
Craig Mautner000f0022013-02-26 15:04:29 -0800568
Craig Mautnerd00f4742014-03-12 14:17:26 -0700569 final Intent taskIntent = task.intent;
570 final Intent affinityIntent = task.affinityIntent;
571 final boolean taskIsDocument;
572 final Uri taskDocumentData;
573 if (taskIntent != null && taskIntent.isDocument()) {
574 taskIsDocument = true;
575 taskDocumentData = taskIntent.getData();
576 } else if (affinityIntent != null && affinityIntent.isDocument()) {
577 taskIsDocument = true;
578 taskDocumentData = affinityIntent.getData();
579 } else {
580 taskIsDocument = false;
581 taskDocumentData = null;
582 }
583
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700584 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700585 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700586 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700587 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700588 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
589 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700590 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700591 return r;
592 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700593 } else if (taskIntent != null && taskIntent.getComponent() != null &&
594 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700595 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700596 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800597 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700598 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
599 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800600 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700601 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
602 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700603 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700604 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800605 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700606 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
607 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800608 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700609 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700610 }
611
612 return null;
613 }
614
615 /**
616 * Returns the first activity (starting from the top of the stack) that
617 * is the same as the given activity. Returns null if no such activity
618 * is found.
619 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700620 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700621 ComponentName cls = intent.getComponent();
622 if (info.targetActivity != null) {
623 cls = new ComponentName(info.packageName, info.targetActivity);
624 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700625 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700626
Craig Mautner000f0022013-02-26 15:04:29 -0800627 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700628 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700629 final boolean notCurrentUserTask =
630 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700631 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700632
Craig Mautner000f0022013-02-26 15:04:29 -0800633 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
634 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700635 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700636 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700637 }
Craig Mautner000f0022013-02-26 15:04:29 -0800638 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700639 return r;
640 }
641 }
642 }
643
644 return null;
645 }
646
Amith Yamasani742a6712011-05-04 14:49:28 -0700647 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700648 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700649 */
Craig Mautner93529a42013-10-04 15:03:13 -0700650 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800651 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700652 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800653 }
654 mCurrentUser = userId;
655
656 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800657 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800658 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700659 final TaskRecord task = mTaskHistory.get(i);
660
661 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
662 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700663 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700664 || task.topRunningActivityLocked(null) != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700665 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700666 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800667 mTaskHistory.remove(i);
668 mTaskHistory.add(task);
669 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800670 // Use same value for i.
671 } else {
672 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800673 }
674 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700675 if (VALIDATE_TOKENS) {
676 validateAppTokensLocked();
677 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700678 }
679
Craig Mautner2420ead2013-04-01 17:13:20 -0700680 void minimalResumeActivityLocked(ActivityRecord r) {
681 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700682 if (DEBUG_STATES) Slog.v(TAG_STATES,
683 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700684 r.stopped = false;
685 mResumedActivity = r;
686 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800687 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700688 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700689 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700690 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700691 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
692 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700693 }
694
Chong Zhang45c25ce2015-08-10 22:18:26 -0700695 private void addRecentActivityLocked(ActivityRecord r) {
696 if (r != null) {
697 mRecentTasks.addLocked(r.task);
698 r.task.touchActiveTime();
699 }
700 }
701
Narayan Kamath7829c812015-06-08 17:39:43 +0100702 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700703 if (mFullyDrawnStartTime != 0) {
704 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
705 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100706 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700707 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
708 }
709
710 private void stopFullyDrawnTraceIfNeeded() {
711 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
712 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
713 mFullyDrawnStartTime = 0;
714 }
715 }
716
Craig Mautnere79d42682013-04-01 19:01:53 -0700717 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700718 if (r.displayStartTime == 0) {
719 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
720 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100721 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700722 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700723 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700724 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100725 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700726 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700727 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700728 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800729
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700730 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700731 // Make sure that there is no activity waiting for this to launch.
732 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
733 r.displayStartTime = r.fullyDrawnStartTime = 0;
734 } else {
735 mStackSupervisor.removeTimeoutsForActivityLocked(r);
736 mStackSupervisor.scheduleIdleTimeoutLocked(r);
737 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700738 }
739
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800740 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800741 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800742 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
743 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
744 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
745 activities.get(activityNdx).setSleeping(false);
746 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800747 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800748 if (mPausingActivity != null) {
749 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
750 activityPausedLocked(mPausingActivity.appToken, true);
751 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800752 }
753
Craig Mautner0eea92c2013-05-16 13:35:39 -0700754 /**
755 * @return true if something must be done before going to sleep.
756 */
757 boolean checkReadyForSleepLocked() {
758 if (mResumedActivity != null) {
759 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700760 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
761 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
762 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700763 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700764 return true;
765 }
766 if (mPausingActivity != null) {
767 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700768 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700769 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800770 }
771
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700772 if (hasVisibleBehindActivity()) {
773 // Stop visible behind activity before going to sleep.
774 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
775 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700776 if (DEBUG_STATES) Slog.v(TAG_STATES,
777 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700778 return true;
779 }
780
Craig Mautner0eea92c2013-05-16 13:35:39 -0700781 return false;
782 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800783
Craig Mautner0eea92c2013-05-16 13:35:39 -0700784 void goToSleep() {
785 ensureActivitiesVisibleLocked(null, 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800786
Craig Mautner0eea92c2013-05-16 13:35:39 -0700787 // Make sure any stopped but visible activities are now sleeping.
788 // This ensures that the activity's onStop() is called.
789 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
790 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
791 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
792 final ActivityRecord r = activities.get(activityNdx);
793 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
794 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800795 }
796 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800797 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700798 }
Craig Mautner59c00972012-07-30 12:10:24 -0700799
Dianne Hackbornd2835932010-12-13 16:28:46 -0800800 public final Bitmap screenshotActivities(ActivityRecord who) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700801 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivities: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800802 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700803 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800804 return null;
805 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800806
Winson Chung083baf92014-07-11 10:32:42 -0700807 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700808 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800809 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700810 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700811 return null;
812 }
813
Winson Chung48a10a52014-08-27 14:36:51 -0700814 int w = mService.mThumbnailWidth;
815 int h = mService.mThumbnailHeight;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800816 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700817 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800818 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Dianne Hackborn27eac1d2015-03-16 17:15:53 -0700819 w, h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800820 }
Winson Chung376543b2014-05-21 17:43:28 -0700821 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800822 return null;
823 }
824
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700825 /**
826 * Start pausing the currently resumed activity. It is an error to call this if there
827 * is already an activity being paused or there is no resumed activity.
828 *
829 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
830 * @param uiSleeping True if this is happening with the user interface going to sleep (the
831 * screen turning off).
832 * @param resuming True if this is being called as part of resuming the top activity, so
833 * we shouldn't try to instigate a resume here.
834 * @param dontWait True if the caller does not want to wait for the pause to complete. If
835 * set to true, we will immediately complete the pause here before returning.
836 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
837 * it to tell us when it is done.
838 */
839 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
840 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800841 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800842 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
843 + " state=" + mPausingActivity.state);
844 if (!mService.isSleeping()) {
845 // Avoid recursion among check for sleep and complete pause during sleeping.
846 // Because activity will be paused immediately after resume, just let pause
847 // be completed by the order of activity paused from clients.
848 completePauseLocked(false);
849 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800850 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700851 ActivityRecord prev = mResumedActivity;
852 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700853 if (!resuming) {
854 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
855 mStackSupervisor.resumeTopActivitiesLocked();
856 }
857 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700858 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800859
860 if (mActivityContainer.mParentActivity == null) {
861 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700862 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800863 }
864
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700865 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700866 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700867 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800868 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700869 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700870 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
871 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700872 prev.state = ActivityState.PAUSING;
873 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700874 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700875 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwaled3e2a082014-11-04 16:09:19 -0800876 if (mService.mHasRecents && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800877 prev.updateThumbnailLocked(screenshotActivities(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700878 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700879 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700880
881 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800882
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700883 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700884 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700885 try {
886 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700887 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700888 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700889 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800890 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700891 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700892 } catch (Exception e) {
893 // Ignore exception, if process died other code will cleanup.
894 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800895 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700896 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700897 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700898 }
899 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800900 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700901 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700902 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700903 }
904
905 // If we are not going to sleep, we want to ensure the device is
906 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700907 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700908 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700909 }
910
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800911 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700912 // Have the window manager pause its key dispatching until the new
913 // activity has started. If we're pausing the activity just because
914 // the screen is being turned off and the UI is sleeping, don't interrupt
915 // key dispatch; the same activity will pick it up again on wakeup.
916 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800917 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700918 } else if (DEBUG_PAUSE) {
919 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700920 }
921
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700922 if (dontWait) {
923 // If the caller said they don't want to wait for the pause, then complete
924 // the pause now.
925 completePauseLocked(false);
926 return false;
927
928 } else {
929 // Schedule a pause timeout in case the app doesn't respond.
930 // We don't give it much time because this directly impacts the
931 // responsiveness seen by the user.
932 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
933 msg.obj = prev;
934 prev.pauseTime = SystemClock.uptimeMillis();
935 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700936 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700937 return true;
938 }
939
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700940 } else {
941 // This activity failed to schedule the
942 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700943 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700944 if (!resuming) {
945 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
946 }
947 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700948 }
949 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700950
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700951 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700952 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
953 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700954
Craig Mautnerd2328952013-03-05 12:46:26 -0800955 final ActivityRecord r = isInStackLocked(token);
956 if (r != null) {
957 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
958 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700959 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -0800960 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700961 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800962 } else {
963 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
964 r.userId, System.identityHashCode(r), r.shortComponentName,
965 mPausingActivity != null
966 ? mPausingActivity.shortComponentName : "(none)");
louis_chang047dfd42015-04-08 16:35:55 +0800967 if (r.finishing && r.state == ActivityState.PAUSING) {
968 if (DEBUG_PAUSE) Slog.v(TAG,
969 "Executing finish of failed to pause activity: " + r);
970 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
971 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700972 }
973 }
974 }
975
Craig Mautnera0026042014-04-23 11:45:37 -0700976 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
977 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700978 if (r.state != ActivityState.STOPPING) {
979 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
980 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
981 return;
982 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700983 if (persistentState != null) {
984 r.persistentState = persistentState;
985 mService.notifyTaskPersisterLocked(r.task, false);
986 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700987 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700988 if (icicle != null) {
989 // If icicle is null, this is happening due to a timeout, so we
990 // haven't really saved the state.
991 r.icicle = icicle;
992 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -0800993 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -0800994 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700995 }
996 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700997 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700998 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
999 r.stopped = true;
1000 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -07001001 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
1002 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001003 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001004 if (r.finishing) {
1005 r.clearOptionsLocked();
1006 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001007 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001008 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -07001009 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001010 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001011 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001012 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001013 }
1014 }
1015 }
1016
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001017 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001018 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001019 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001020
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001021 if (prev != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001022 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001023 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001024 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001025 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001026 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001027 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001028 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001029 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1030 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001031 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001032 if (prev.configDestroy) {
1033 // The previous is being paused because the configuration
1034 // is changing, which means it is actually stopping...
1035 // To juggle the fact that we are also starting a new
1036 // instance right now, we need to first completely stop
1037 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001038 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001039 destroyActivityLocked(prev, true, "pause-config");
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001040 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001041 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001042 // stopping.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001043 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001044 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1045 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001046 // If we already have a few activities waiting to stop,
1047 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001048 // them out. Or if r is the last of activity of the last task the stack
1049 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001050 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001051 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001052 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001053 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001054 }
1055 }
1056 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001057 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001058 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001059 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001060 // It is possible the activity was freezing the screen before it was paused.
1061 // In that case go ahead and remove the freeze this activity has on the screen
1062 // since it is no longer visible.
1063 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001064 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001065 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001066
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001067 if (resumeNext) {
1068 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1069 if (!mService.isSleepingOrShuttingDown()) {
1070 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1071 } else {
1072 mStackSupervisor.checkReadyForSleepLocked();
1073 ActivityRecord top = topStack.topRunningActivityLocked(null);
1074 if (top == null || (prev != null && top != prev)) {
1075 // If there are no more activities available to run,
1076 // do resume anyway to start something. Also if the top
1077 // activity on the stack is not the just paused activity,
1078 // we need to go ahead and resume it to ensure we complete
1079 // an in-flight app switch.
1080 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1081 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001082 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001083 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001084
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001085 if (prev != null) {
1086 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001087
Craig Mautner525f3d92013-05-07 14:01:50 -07001088 if (prev.app != null && prev.cpuTimeAtResume > 0
1089 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001090 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1091 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001092 if (diff > 0) {
1093 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1094 synchronized (bsi) {
1095 BatteryStatsImpl.Uid.Proc ps =
1096 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001097 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001098 if (ps != null) {
1099 ps.addForegroundTimeLocked(diff);
1100 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001101 }
1102 }
1103 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001104 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001105 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001106
1107 // Notfiy when the task stack has changed
1108 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001109 }
1110
1111 /**
1112 * Once we know that we have asked an application to put an activity in
1113 * the resumed state (either by launching it or explicitly telling it),
1114 * this function updates the rest of our state to match that fact.
1115 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001116 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001117 next.idle = false;
1118 next.results = null;
1119 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001120
1121 if (next.isHomeActivity() && next.isNotResolverActivity()) {
1122 ProcessRecord app = next.task.mActivities.get(0).app;
1123 if (app != null && app != mService.mHomeProcess) {
1124 mService.mHomeProcess = app;
1125 }
1126 }
1127
Craig Mautner07566322013-09-26 16:42:55 -07001128 if (next.nowVisible) {
1129 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001130 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001131 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001132
1133 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001134 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001135
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001136 mStackSupervisor.reportResumedActivityLocked(next);
1137
1138 next.resumeKeyDispatchingLocked();
1139 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001140
1141 // Mark the point when the activity is resuming
1142 // TODO: To be more accurate, the mark should be before the onCreate,
1143 // not after the onResume. But for subsequent starts, onResume is fine.
1144 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001145 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001146 } else {
1147 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1148 }
Winson Chung376543b2014-05-21 17:43:28 -07001149
George Mount6ba042b2014-07-28 11:12:28 -07001150 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001151
1152 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1153 // When resuming an activity, require it to call requestVisibleBehind() again.
1154 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1155 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001156 }
1157
Craig Mautner2568c3a2015-03-26 14:22:34 -07001158 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001159 r.visible = visible;
1160 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001161 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001162 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001163 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001164 container.setVisible(visible);
1165 }
1166 }
1167
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001168 // Find the first visible activity above the passed activity and if it is translucent return it
1169 // otherwise return null;
1170 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1171 TaskRecord task = r.task;
1172 if (task == null) {
1173 return null;
1174 }
1175
1176 ActivityStack stack = task.stack;
1177 if (stack == null) {
1178 return null;
1179 }
1180
1181 int stackNdx = mStacks.indexOf(stack);
1182
1183 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1184 int taskNdx = tasks.indexOf(task);
1185
1186 ArrayList<ActivityRecord> activities = task.mActivities;
1187 int activityNdx = activities.indexOf(r) + 1;
1188
1189 final int numStacks = mStacks.size();
1190 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001191 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001192 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001193 final int numTasks = tasks.size();
1194 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001195 final TaskRecord currentTask = tasks.get(taskNdx);
1196 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001197 final int numActivities = activities.size();
1198 while (activityNdx < numActivities) {
1199 final ActivityRecord activity = activities.get(activityNdx);
1200 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001201 return historyStack.mFullscreen
1202 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001203 }
1204 ++activityNdx;
1205 }
1206 activityNdx = 0;
1207 ++taskNdx;
1208 }
1209 taskNdx = 0;
1210 ++stackNdx;
1211 }
1212
1213 return null;
1214 }
1215
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001216 private ActivityStack getNextVisibleStackLocked() {
1217 ArrayList<ActivityStack> stacks = mStacks;
1218 final ActivityRecord parent = mActivityContainer.mParentActivity;
1219 if (parent != null) {
1220 stacks = parent.task.stack.mStacks;
1221 }
1222 if (stacks != null) {
1223 for (int i = stacks.size() - 1; i >= 0; --i) {
1224 ActivityStack stack = stacks.get(i);
1225 if (stack != this && stack.isStackVisibleLocked()) {
1226 return stack;
1227 }
1228 }
1229 }
1230 return null;
1231 }
1232
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001233 /** Returns true if the stack contains a fullscreen task. */
1234 private boolean hasFullscreenTask() {
1235 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1236 final TaskRecord task = mTaskHistory.get(i);
1237 if (task.mFullscreen) {
1238 return true;
1239 }
1240 }
1241 return false;
1242 }
1243
1244 /** Return true if this stack is hidden by the presence of a docked stack. */
1245 private boolean isHiddenByDockedStack() {
1246 final ActivityStack dockedStack = mStackSupervisor.getStack(DOCKED_STACK_ID);
1247 if (dockedStack != null) {
1248 final int dockedStackIndex = mStacks.indexOf(dockedStack);
1249 final int stackIndex = mStacks.indexOf(this);
1250 if (dockedStackIndex > stackIndex) {
1251 // Fullscreen stacks or stacks with fullscreen task below the docked stack are not
1252 // visible. We do this so we don't have the 2 stacks and their tasks overlap.
1253 if (mFullscreen) {
1254 return true;
1255 }
1256
1257 // We need to also check the tasks in the stack because they can be fullscreen
1258 // even though their stack isn't due to their root activity not been resizeable
1259 // (i.e. doesn't support multi-window mode).
1260 if (hasFullscreenTask()) {
1261 return true;
1262 }
1263 }
1264 }
1265 return false;
1266 }
1267
1268 /** Returns true if the stack is considered visible. */
Todd Kennedyaab56db2015-01-30 09:39:53 -08001269 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001270 if (!isAttached()) {
1271 return false;
1272 }
1273
1274 if (mStackSupervisor.isFrontStack(this)) {
1275 return true;
1276 }
1277
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001278 final int stackIndex = mStacks.indexOf(this);
1279
1280 if (stackIndex == mStacks.size() - 1) {
1281 Slog.wtf(TAG,
1282 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1283 return false;
1284 }
1285
1286 if (isHiddenByDockedStack()) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001287 return false;
1288 }
1289
Jose Lima729cb232014-05-05 15:59:40 -07001290 /**
1291 * Start at the task above this one and go up, looking for a visible
1292 * fullscreen activity, or a translucent activity that requested the
1293 * wallpaper to be shown behind it.
1294 */
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001295 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001296 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001297 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1298
1299 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1300 continue;
1301 }
1302
1303 if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID
1304 || stack.mStackId == HOME_STACK_ID) {
1305 // The freeform and home stacks can't have any other stack visible behind them
1306 // when they are fullscreen since they act as base/cut-off points for visibility.
1307 // NOTE: we don't cut-off at the FULLSCREEN_WORKSPACE_STACK_ID because the home
1308 // stack sometimes needs to be visible behind it when it is displaying a dialog
1309 // activity. We let it fall through to the logic below to determine visibility.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001310 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001311 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001312
Todd Kennedyaab56db2015-01-30 09:39:53 -08001313 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001314 final TaskRecord task = tasks.get(taskNdx);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001315 // task above isn't fullscreen, so, we assume we're still visible.
1316 if (!task.mFullscreen) {
1317 continue;
1318 }
Craig Mautner84984fa2014-06-19 11:19:20 -07001319 final ArrayList<ActivityRecord> activities = task.mActivities;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001320 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Jose Lima7ba71252014-04-30 12:59:27 -07001321 final ActivityRecord r = activities.get(activityNdx);
Jose Lima729cb232014-05-05 15:59:40 -07001322
1323 // Conditions for an activity to obscure the stack we're
1324 // examining:
1325 // 1. Not Finishing AND Visible AND:
1326 // 2. Either:
1327 // - Full Screen Activity OR
1328 // - On top of Home and our stack is NOT home
1329 if (!r.finishing && r.visible && (r.fullscreen ||
Craig Mautner84984fa2014-06-19 11:19:20 -07001330 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
Jose Lima7ba71252014-04-30 12:59:27 -07001331 return false;
1332 }
1333 }
1334 }
1335 }
1336
1337 return true;
1338 }
1339
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001340 /**
1341 * Make sure that all activities that need to be visible (that is, they
1342 * currently can be seen by the user) actually are.
1343 */
Craig Mautnerbb742462014-07-07 15:28:55 -07001344 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
1345 ActivityRecord top = topRunningActivityLocked(null);
1346 if (top == null) {
1347 return;
1348 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001349 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1350 "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001351 + " configChanges=0x" + Integer.toHexString(configChanges));
1352
Craig Mautner5eda9b32013-07-02 11:58:16 -07001353 if (mTranslucentActivityWaiting != top) {
1354 mUndrawnActivitiesBelowTopTranslucent.clear();
1355 if (mTranslucentActivityWaiting != null) {
1356 // Call the callback with a timeout indication.
1357 notifyActivityDrawnLocked(null);
1358 mTranslucentActivityWaiting = null;
1359 }
1360 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1361 }
1362
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001363 // If the top activity is not fullscreen, then we need to
1364 // make sure any activities under it are now visible.
Craig Mautnerd44711d2013-02-23 11:24:36 -08001365 boolean aboveTop = true;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001366 boolean behindFullscreen = !isStackVisibleLocked();
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001367 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001368
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001369 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001370 final TaskRecord task = mTaskHistory.get(taskNdx);
1371 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001372 // Set to true if an activity in this task is fullscreen thereby hiding other
1373 // activities in the same task. Initialized to the same value as behindFullscreen
1374 // which represent if the entire task/stack is behind another fullscreen task/stack.
1375 boolean behindFullscreenActivity = behindFullscreen;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001376 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1377 final ActivityRecord r = activities.get(activityNdx);
1378 if (r.finishing) {
1379 continue;
1380 }
1381 if (aboveTop && r != top) {
1382 continue;
1383 }
1384 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001385 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1386 // but must be drawn initially for the animation as though they were visible.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001387 if (!behindFullscreenActivity || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001388 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1389 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001390 + " state=" + r.state);
Craig Mautner58547802013-03-05 08:23:53 -08001391
Craig Mautnerd44711d2013-02-23 11:24:36 -08001392 // First: if this is not the current activity being started, make
1393 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001394 if (r != starting) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001395 ensureActivityConfigurationLocked(r, 0);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001396 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001397
1398 if (r.app == null || r.app.thread == null) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001399 // This activity needs to be visible, but isn't even running...
1400 // get it started and resume if no other stack in this stack is resumed.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001401 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1402 "Start and freeze screen for " + r);
Craig Mautnerbb742462014-07-07 15:28:55 -07001403 if (r != starting) {
1404 r.startFreezingScreenLocked(r.app, configChanges);
1405 }
1406 if (!r.visible || r.mLaunchTaskBehind) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001407 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1408 "Starting and making visible: " + r);
Craig Mautner2568c3a2015-03-26 14:22:34 -07001409 setVisible(r, true);
Craig Mautnerbb742462014-07-07 15:28:55 -07001410 }
1411 if (r != starting) {
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001412 mStackSupervisor.startSpecificActivityLocked(
1413 r, noStackActivityResumed, false);
riddle_hsu36ee73d2015-06-05 16:38:38 +08001414 if (activityNdx >= activities.size()) {
1415 // Record may be removed if its process needs to restart.
1416 activityNdx = activities.size() - 1;
1417 } else {
1418 noStackActivityResumed = false;
1419 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001420 }
1421
1422 } else if (r.visible) {
1423 // If this activity is already visible, then there is nothing
1424 // else to do here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001425 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1426 "Skipping: already visible at " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001427 r.stopFreezingScreenLocked(false);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001428 try {
George Mount6ba042b2014-07-28 11:12:28 -07001429 if (r.returningOptions != null) {
1430 r.app.thread.scheduleOnNewActivityOptions(r.appToken,
1431 r.returningOptions);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001432 }
1433 } catch(RemoteException e) {
1434 }
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001435 if (r.state == ActivityState.RESUMED) {
1436 noStackActivityResumed = false;
1437 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001438 } else {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001439 // This activity is not currently visible, but is running.
1440 // Tell it to become visible.
1441 r.visible = true;
1442 if (r.state != ActivityState.RESUMED && r != starting) {
1443 // If this activity is paused, tell it
1444 // to now show its window.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001445 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1446 "Making visible and scheduling visibility: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001447 try {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001448 if (mTranslucentActivityWaiting != null) {
George Mount6ba042b2014-07-28 11:12:28 -07001449 r.updateOptionsLocked(r.returningOptions);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001450 mUndrawnActivitiesBelowTopTranslucent.add(r);
1451 }
Craig Mautner2568c3a2015-03-26 14:22:34 -07001452 setVisible(r, true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001453 r.sleeping = false;
1454 r.app.pendingUiClean = true;
1455 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1456 r.stopFreezingScreenLocked(false);
1457 } catch (Exception e) {
1458 // Just skip on any failure; we'll make it
1459 // visible when it next restarts.
1460 Slog.w(TAG, "Exception thrown making visibile: "
1461 + r.intent.getComponent(), e);
1462 }
1463 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001464 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001465
Craig Mautnerd44711d2013-02-23 11:24:36 -08001466 // Aggregate current change flags.
1467 configChanges |= r.configChangeFlags;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001468
Craig Mautnerd44711d2013-02-23 11:24:36 -08001469 if (r.fullscreen) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001470 // At this point, nothing else needs to be shown in this task.
1471 behindFullscreenActivity = true;
1472 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
1473 + " behindFullscreen=" + behindFullscreen
1474 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautner84984fa2014-06-19 11:19:20 -07001475 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001476 behindFullscreenActivity = true;
1477 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1478 + " behindFullscreen=" + behindFullscreen
1479 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001480 }
1481 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001482 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001483 "Make invisible? " + r + " finishing=" + r.finishing
1484 + " state=" + r.state + " behindFullscreen=" + behindFullscreen
1485 + " behindFullscreenActivity=" + behindFullscreenActivity);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001486 // Now for any activities that aren't visible to the user, make
1487 // sure they no longer are keeping the screen frozen.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001488 if (r.visible) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001489 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001490 try {
Craig Mautner2568c3a2015-03-26 14:22:34 -07001491 setVisible(r, false);
Craig Mautner4addfc52013-06-25 08:05:45 -07001492 switch (r.state) {
1493 case STOPPING:
1494 case STOPPED:
1495 if (r.app != null && r.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001496 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1497 "Scheduling invisibility: " + r);
Craig Mautner4addfc52013-06-25 08:05:45 -07001498 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1499 }
1500 break;
1501
1502 case INITIALIZING:
1503 case RESUMED:
1504 case PAUSING:
1505 case PAUSED:
Craig Mautner88176102013-07-22 12:57:51 -07001506 // This case created for transitioning activities from
1507 // translucent to opaque {@link Activity#convertToOpaque}.
Jose Lima4b6c6692014-08-12 17:41:12 -07001508 if (getVisibleBehindActivity() == r) {
Todd Kennedyaab56db2015-01-30 09:39:53 -08001509 releaseBackgroundResources(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001510 } else {
1511 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1512 mStackSupervisor.mStoppingActivities.add(r);
1513 }
1514 mStackSupervisor.scheduleIdleLocked();
Craig Mautnere5273b42013-09-09 12:57:47 -07001515 }
Craig Mautner4addfc52013-06-25 08:05:45 -07001516 break;
1517
1518 default:
1519 break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001520 }
1521 } catch (Exception e) {
1522 // Just skip on any failure; we'll make it
1523 // visible when it next restarts.
1524 Slog.w(TAG, "Exception thrown making hidden: "
1525 + r.intent.getComponent(), e);
1526 }
1527 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001528 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001529 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001530 }
1531 }
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001532 // Factoring if the previous task is fullscreen there by affecting the visibility of
1533 // task behind it.
1534 behindFullscreen |= task.mFullscreen;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001535 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001536
1537 if (mTranslucentActivityWaiting != null &&
1538 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1539 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1540 notifyActivityDrawnLocked(null);
1541 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001542 }
Craig Mautner58547802013-03-05 08:23:53 -08001543
Todd Kennedyaab56db2015-01-30 09:39:53 -08001544 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001545 mTranslucentActivityWaiting = r;
1546 mUndrawnActivitiesBelowTopTranslucent.clear();
1547 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1548 }
1549
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001550 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1551 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1552 final TaskRecord task = mTaskHistory.get(taskNdx);
1553 final ArrayList<ActivityRecord> activities = task.mActivities;
1554 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1555 final ActivityRecord r = activities.get(activityNdx);
1556 if ( r.appTimeTracker != except) {
1557 r.appTimeTracker = null;
1558 }
1559 }
1560 }
1561 }
1562
Craig Mautner5eda9b32013-07-02 11:58:16 -07001563 /**
1564 * Called as activities below the top translucent activity are redrawn. When the last one is
1565 * redrawn notify the top activity by calling
1566 * {@link Activity#onTranslucentConversionComplete}.
1567 *
1568 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1569 * occurred and the activity will be notified immediately.
1570 */
1571 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001572 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001573 if ((r == null)
1574 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1575 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1576 // The last undrawn activity below the top has just been drawn. If there is an
1577 // opaque activity at the top, notify it that it can become translucent safely now.
1578 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1579 mTranslucentActivityWaiting = null;
1580 mUndrawnActivitiesBelowTopTranslucent.clear();
1581 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1582
Craig Mautner71dd1b62014-02-18 15:48:52 -08001583 if (waitingActivity != null) {
1584 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1585 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1586 try {
1587 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1588 waitingActivity.appToken, r != null);
1589 } catch (RemoteException e) {
1590 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001591 }
1592 }
1593 }
1594 }
1595
Craig Mautnera61bc652013-10-28 15:43:18 -07001596 /** If any activities below the top running one are in the INITIALIZING state and they have a
1597 * starting window displayed then remove that starting window. It is possible that the activity
1598 * in this state will never resumed in which case that starting window will be orphaned. */
1599 void cancelInitializingActivities() {
1600 final ActivityRecord topActivity = topRunningActivityLocked(null);
1601 boolean aboveTop = true;
1602 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1603 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1604 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1605 final ActivityRecord r = activities.get(activityNdx);
1606 if (aboveTop) {
1607 if (r == topActivity) {
1608 aboveTop = false;
1609 }
1610 continue;
1611 }
1612
1613 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001614 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1615 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001616 r.mStartingWindowShown = false;
1617 mWindowManager.removeAppStartingWindow(r.appToken);
1618 }
1619 }
1620 }
1621 }
1622
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001623 /**
1624 * Ensure that the top activity in the stack is resumed.
1625 *
1626 * @param prev The previously resumed activity, for when in the process
1627 * of pausing; can be null to call from elsewhere.
1628 *
1629 * @return Returns true if something is being resumed, or false if
1630 * nothing happened.
1631 */
1632 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001633 return resumeTopActivityLocked(prev, null);
1634 }
1635
1636 final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001637 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001638 // Don't even start recursing.
1639 return false;
1640 }
1641
1642 boolean result = false;
1643 try {
1644 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001645 mStackSupervisor.inResumeTopActivity = true;
1646 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1647 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001648 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001649 }
Craig Mautner544efa72014-09-04 16:41:20 -07001650 result = resumeTopActivityInnerLocked(prev, options);
1651 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001652 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001653 }
1654 return result;
1655 }
1656
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001657 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001658 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001659
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001660 if (!mService.mBooting && !mService.mBooted) {
1661 // Not ready yet!
1662 return false;
1663 }
1664
Craig Mautnerdf88d732014-01-27 09:21:32 -08001665 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001666 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001667 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001668 // Do not resume this stack if its parent is not resumed.
1669 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1670 return false;
1671 }
1672
Craig Mautnera61bc652013-10-28 15:43:18 -07001673 cancelInitializingActivities();
1674
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001675 // Find the first activity that is not finishing.
Craig Mautner94ab4662015-01-20 10:49:22 -08001676 final ActivityRecord next = topRunningActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001677
1678 // Remember how we'll process this pause/resume situation, and ensure
1679 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001680 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1681 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001682
Craig Mautner84984fa2014-06-19 11:19:20 -07001683 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001684 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001685 // There are no more activities!
1686 final String reason = "noMoreActivities";
1687 if (!mFullscreen) {
1688 // Try to move focus to the next visible stack with a running activity if this
1689 // stack is not covering the entire screen.
1690 final ActivityStack stack = getNextVisibleStackLocked();
1691 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1692 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1693 }
1694 }
1695 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001696 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001697 if (DEBUG_STATES) Slog.d(TAG_STATES,
1698 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001699 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001700 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001701 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1702 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1703 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001704 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001705 }
1706
1707 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001708
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001709 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001710 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1711 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001712 // Make sure we have executed any pending transitions, since there
1713 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001714 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001715 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001716 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001717 if (DEBUG_STATES) Slog.d(TAG_STATES,
1718 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001719 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001720 return false;
1721 }
1722
Craig Mautner525f3d92013-05-07 14:01:50 -07001723 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001724 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001725 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001726 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001727 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001728 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001729 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001730 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001731 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001732 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001733 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001734 } else if (!isOnHomeDisplay()) {
1735 return false;
1736 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001737 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001738 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001739 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1740 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1741 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001742 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001743 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001744 }
1745
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001746 // If we are sleeping, and there is no resumed activity, and the top
1747 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001748 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001749 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001750 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001751 // Make sure we have executed any pending transitions, since there
1752 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001753 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001754 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001755 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001756 if (DEBUG_STATES) Slog.d(TAG_STATES,
1757 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001758 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001759 return false;
1760 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001761
1762 // Make sure that the user who owns this activity is started. If not,
1763 // we will just leave it as is because someone should be bringing
1764 // another user's activities to the top of the stack.
1765 if (mService.mStartedUsers.get(next.userId) == null) {
1766 Slog.w(TAG, "Skipping resume of top activity " + next
1767 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001768 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001769 return false;
1770 }
1771
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001772 // The activity may be waiting for stop, but that is no longer
1773 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001774 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001775 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001776 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001777 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001778
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001779 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001780
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001781 // If we are currently pausing an activity, then don't do anything
1782 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001783 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001784 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001785 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001786 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001787 return false;
1788 }
1789
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001790 // Okay we are now going to start a switch, to 'next'. We may first
1791 // have to pause the current activity, but this is an important point
1792 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001793 // XXX "App Redirected" dialog is getting too many false positives
1794 // at this point, so turn off for now.
1795 if (false) {
1796 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1797 long now = SystemClock.uptimeMillis();
1798 final boolean inTime = mLastStartedActivity.startTime != 0
1799 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1800 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1801 final int nextUid = next.info.applicationInfo.uid;
1802 if (inTime && lastUid != nextUid
1803 && lastUid != next.launchedFromUid
1804 && mService.checkPermission(
1805 android.Manifest.permission.STOP_APP_SWITCHES,
1806 -1, next.launchedFromUid)
1807 != PackageManager.PERMISSION_GRANTED) {
1808 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1809 } else {
1810 next.startTime = now;
1811 mLastStartedActivity = next;
1812 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001813 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001814 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001815 mLastStartedActivity = next;
1816 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001817 }
Craig Mautner58547802013-03-05 08:23:53 -08001818
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001819 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1820
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001821 // We need to start pausing the current activity so the top one
1822 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001823 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1824 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001825 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001826 if (DEBUG_STATES) Slog.d(TAG_STATES,
1827 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001828 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001829 }
1830 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001831 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001832 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001833 // At this point we want to put the upcoming activity's process
1834 // at the top of the LRU list, since we know we will be needing it
1835 // very soon and it would be a waste to let it get killed if it
1836 // happens to be sitting towards the end.
1837 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001838 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001839 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001840 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001841 return true;
1842 }
1843
Christopher Tated3f175c2012-06-14 14:16:54 -07001844 // If the most recent activity was noHistory but was only stopped rather
1845 // than stopped+finished because the device went to sleep, we need to make
1846 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001847 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001848 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001849 if (DEBUG_STATES) Slog.d(TAG_STATES,
1850 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001851 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001852 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001853 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001854 }
1855
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001856 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001857 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1858 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001859 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001860 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1861 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001862 } else {
1863 // The next activity is already visible, so hide the previous
1864 // activity's windows right now so we can show the new one ASAP.
1865 // We only do this if the previous is finishing, which should mean
1866 // it is on top of the one being resumed so hiding it quickly
1867 // is good. Otherwise, we want to do the normal route of allowing
1868 // the resumed activity to be shown so we can decide if the
1869 // previous should actually be hidden depending on whether the
1870 // new one is found to be full-screen or not.
1871 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001872 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001873 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1874 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001875 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001876 + ", nowVisible=" + next.nowVisible);
1877 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001878 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001879 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001880 + ", waitingVisible="
1881 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1882 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001883 }
1884 }
1885 }
1886
Dianne Hackborne7f97212011-02-24 14:40:20 -08001887 // Launching this app's activity, make sure the app is no longer
1888 // considered stopped.
1889 try {
1890 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001891 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08001892 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08001893 } catch (IllegalArgumentException e) {
1894 Slog.w(TAG, "Failed trying to unstop package "
1895 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08001896 }
1897
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001898 // We are starting up the next activity, so tell the window manager
1899 // that the previous one will be hidden soon. This way it can know
1900 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07001901 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001902 if (prev != null) {
1903 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001904 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001905 "Prepare close transition: prev=" + prev);
1906 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001907 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001908 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001909 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001910 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001911 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
1912 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001913 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001914 mWindowManager.setAppWillBeHidden(prev.appToken);
1915 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001916 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001917 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
1918 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001919 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001920 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001921 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001922 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001923 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08001924 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07001925 : next.mLaunchTaskBehind
1926 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
1927 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001928 }
1929 }
1930 if (false) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001931 mWindowManager.setAppWillBeHidden(prev.appToken);
1932 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001933 }
Craig Mautner967212c2013-04-13 21:10:58 -07001934 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001935 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001936 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07001937 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001938 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001939 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001940 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001941 }
1942 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001943
1944 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07001945 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08001946 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
1947 if (opts != null) {
1948 resumeAnimOptions = opts.toBundle();
1949 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001950 next.applyOptionsLocked();
1951 } else {
1952 next.clearOptionsLocked();
1953 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001954
Craig Mautnercf910b02013-04-23 11:23:27 -07001955 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001956 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001957 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001958
1959 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001960 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001961
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07001962 // schedule launch ticks to collect information about slow apps.
1963 next.startLaunchTickingLocked();
1964
Craig Mautnerf88c50f2013-04-18 19:25:12 -07001965 ActivityRecord lastResumedActivity =
1966 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001967 ActivityState lastState = next.state;
1968
1969 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08001970
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001971 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001972 next.state = ActivityState.RESUMED;
1973 mResumedActivity = next;
1974 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08001975 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07001976 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001977 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07001978 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001979
1980 // Have the window manager re-evaluate the orientation of
1981 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07001982 boolean notUpdated = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001983 if (mStackSupervisor.isFrontStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001984 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07001985 mService.mConfiguration,
1986 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
1987 if (config != null) {
1988 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001989 }
Maxim Bogatov05075302015-05-19 18:33:08 -07001990 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001991 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07001992
Craig Mautner525f3d92013-05-07 14:01:50 -07001993 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001994 // The configuration update wasn't able to keep the existing
1995 // instance of the activity, and instead started a new one.
1996 // We should be all done, but let's just make sure our activity
1997 // is still at the top and schedule another run if something
1998 // weird happened.
1999 ActivityRecord nextNext = topRunningActivityLocked(null);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002000 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002001 "Activity config changed during resume: " + next
2002 + ", new next: " + nextNext);
2003 if (nextNext != next) {
2004 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002005 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002006 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002007 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002008 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002009 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002010 return true;
2011 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002012 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002013 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002014 }
Craig Mautner58547802013-03-05 08:23:53 -08002015
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002016 try {
2017 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002018 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002019 if (a != null) {
2020 final int N = a.size();
2021 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002022 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2023 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002024 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002025 }
2026 }
2027
2028 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002029 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002030 }
2031
Craig Mautner299f9602015-01-26 09:47:33 -08002032 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2033 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002034
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002035 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002036 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002037 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002038 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002039 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002040 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002041 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002042
Craig Mautner0eea92c2013-05-16 13:35:39 -07002043 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002044
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002045 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002046 } catch (Exception e) {
2047 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002048 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002049 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002050 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002051 if (lastStack != null) {
2052 lastStack.mResumedActivity = lastResumedActivity;
2053 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002054 Slog.i(TAG, "Restarting because process died: " + next);
2055 if (!next.hasBeenLaunched) {
2056 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002057 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2058 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002059 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002060 next.appToken, next.packageName, next.theme,
2061 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002062 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2063 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002064 }
George Mount2c92c972014-03-20 09:38:23 -07002065 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002066 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002067 return true;
2068 }
2069
2070 // From this point on, if something goes wrong there is no way
2071 // to recover the activity.
2072 try {
2073 next.visible = true;
2074 completeResumeLocked(next);
2075 } catch (Exception e) {
2076 // If any exception gets thrown, toss away this
2077 // activity and try the next one.
2078 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002079 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002080 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002081 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002082 return true;
2083 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002084 next.stopped = false;
2085
2086 } else {
2087 // Whoops, need to restart this activity!
2088 if (!next.hasBeenLaunched) {
2089 next.hasBeenLaunched = true;
2090 } else {
2091 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002092 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002093 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002094 mService.compatibilityInfoForPackageLocked(
2095 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002096 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002097 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002098 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002099 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002100 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002101 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002102 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002103 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 }
2105
Craig Mautnercf910b02013-04-23 11:23:27 -07002106 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002107 return true;
2108 }
2109
riddle_hsuc215a4f2014-12-27 12:10:45 +08002110 private TaskRecord getNextTask(TaskRecord targetTask) {
2111 final int index = mTaskHistory.indexOf(targetTask);
2112 if (index >= 0) {
2113 final int numTasks = mTaskHistory.size();
2114 for (int i = index + 1; i < numTasks; ++i) {
2115 TaskRecord task = mTaskHistory.get(i);
2116 if (task.userId == targetTask.userId) {
2117 return task;
2118 }
2119 }
2120 }
2121 return null;
2122 }
2123
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002124 private void insertTaskAtPosition(TaskRecord task, int position) {
2125 if (position >= mTaskHistory.size()) {
2126 insertTaskAtTop(task, null);
2127 return;
2128 }
2129 // Calculate maximum possible position for this task.
2130 int maxPosition = mTaskHistory.size();
2131 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
2132 && task.topRunningActivityLocked(null) == null) {
2133 // Put non-current user tasks below current user tasks.
2134 while (maxPosition > 0) {
2135 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2136 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
2137 || tmpTask.topRunningActivityLocked(null) == null) {
2138 break;
2139 }
2140 maxPosition--;
2141 }
2142 }
2143 position = Math.min(position, maxPosition);
2144 mTaskHistory.remove(task);
2145 mTaskHistory.add(position, task);
2146 updateTaskMovement(task, true);
2147 }
2148
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002149 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002150 // If the moving task is over home stack, transfer its return type to next task
2151 if (task.isOverHomeStack()) {
2152 final TaskRecord nextTask = getNextTask(task);
2153 if (nextTask != null) {
2154 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2155 }
2156 }
2157
Craig Mautner9c85c202013-10-04 20:11:26 -07002158 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002159 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002160 if (isOnHomeDisplay()) {
2161 ActivityStack lastStack = mStackSupervisor.getLastStack();
2162 final boolean fromHome = lastStack.isHomeStack();
2163 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002164 task.setTaskToReturnTo(fromHome
2165 ? lastStack.topTask() == null
2166 ? HOME_ACTIVITY_TYPE
2167 : lastStack.topTask().taskType
2168 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002169 }
2170 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002171 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002172 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002173
Craig Mautnerac6f8432013-07-17 13:24:59 -07002174 mTaskHistory.remove(task);
2175 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002176 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002177 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002178 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002179 || (newActivity == null && task.topRunningActivityLocked(null) == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002180 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002181 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002182 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002183 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002184 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002185 || tmpTask.topRunningActivityLocked(null) == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002186 break;
2187 }
2188 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002189 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002190 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002191 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002192 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002193 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002194
Craig Mautner8849a5e2013-04-02 16:41:03 -07002195 final void startActivityLocked(ActivityRecord r, boolean newTask,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002196 boolean doResume, boolean keepCurTransition, Bundle options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002197 TaskRecord rTask = r.task;
2198 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002199 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2200 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002201 // Last activity in task had been removed or ActivityManagerService is reusing task.
2202 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002203 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002204 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002205 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002206 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002207 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002208 if (!newTask) {
2209 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002210 boolean startIt = true;
2211 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2212 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002213 if (task.getTopActivity() == null) {
2214 // All activities in task are finishing.
2215 continue;
2216 }
Craig Mautner70a86932013-02-28 22:37:44 -08002217 if (task == r.task) {
2218 // Here it is! Now, if this is not yet visible to the
2219 // user, then just add it without starting; it will
2220 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002221 if (!startIt) {
2222 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2223 + task, new RuntimeException("here").fillInStackTrace());
2224 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002225 r.putInHistory();
Wale Ogunwale706ed792015-08-02 10:29:44 -07002226 addAppToken(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002227 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002228 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002229 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002230 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002231 return;
2232 }
2233 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002234 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002235 startIt = false;
2236 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002237 }
2238 }
2239
2240 // Place a new activity at top of stack, so it is next to interact
2241 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002242
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002243 // If we are not placing the new activity frontmost, we do not want
2244 // to deliver the onUserLeaving callback to the actual frontmost
2245 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002246 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002247 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002248 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002249 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002250 }
Craig Mautner70a86932013-02-28 22:37:44 -08002251
2252 task = r.task;
2253
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002254 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002255 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002256 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002257 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002258 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002259
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002260 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002261 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002262 // We want to show the starting preview window if we are
2263 // switching to a new task, or the next activity's process is
2264 // not currently running.
2265 boolean showStartingIcon = newTask;
2266 ProcessRecord proc = r.app;
2267 if (proc == null) {
2268 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2269 }
2270 if (proc == null || proc.thread == null) {
2271 showStartingIcon = true;
2272 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002273 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002274 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002275 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002276 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002277 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002278 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002279 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002280 ? r.mLaunchTaskBehind
2281 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2282 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002283 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002284 mNoAnimActivities.remove(r);
2285 }
Wale Ogunwale706ed792015-08-02 10:29:44 -07002286 addAppToken(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002287 boolean doShow = true;
2288 if (newTask) {
2289 // Even though this activity is starting fresh, we still need
2290 // to reset it to make sure we apply affinities to move any
2291 // existing activities from other tasks in to it.
2292 // If the caller has requested that the target task be
2293 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002294 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002295 resetTaskIfNeededLocked(r, r);
2296 doShow = topRunningNonDelayedActivityLocked(null) == r;
2297 }
George Mount70778d72014-07-01 16:33:45 -07002298 } else if (options != null && new ActivityOptions(options).getAnimationType()
2299 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2300 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002301 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002302 if (r.mLaunchTaskBehind) {
2303 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2304 // tell WindowManager that r is visible even though it is at the back of the stack.
2305 mWindowManager.setAppVisibility(r.appToken, true);
2306 ensureActivitiesVisibleLocked(null, 0);
2307 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002308 // Figure out if we are transitioning from another activity that is
2309 // "has the same starting icon" as the next one. This allows the
2310 // window manager to keep the previous window it had previously
2311 // created, if it still had one.
2312 ActivityRecord prev = mResumedActivity;
2313 if (prev != null) {
2314 // We don't want to reuse the previous starting preview if:
2315 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002316 if (prev.task != r.task) {
2317 prev = null;
2318 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002319 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002320 else if (prev.nowVisible) {
2321 prev = null;
2322 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002323 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002324 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002325 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002326 mService.compatibilityInfoForPackageLocked(
2327 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002328 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002329 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002330 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002331 }
2332 } else {
2333 // If this is the first activity, don't do any fancy animations,
2334 // because there is nothing for it to animate on top of.
Wale Ogunwale706ed792015-08-02 10:29:44 -07002335 addAppToken(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002336 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002337 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002338 }
2339 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002340 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002341 }
2342
2343 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002344 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002345 } else {
2346 addRecentActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002347 }
2348 }
2349
Dianne Hackbornbe707852011-11-11 14:32:10 -08002350 final void validateAppTokensLocked() {
2351 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002352 mValidateAppTokens.ensureCapacity(numActivities());
2353 final int numTasks = mTaskHistory.size();
2354 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2355 TaskRecord task = mTaskHistory.get(taskNdx);
2356 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002357 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002358 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002359 }
Craig Mautner000f0022013-02-26 15:04:29 -08002360 TaskGroup group = new TaskGroup();
2361 group.taskId = task.taskId;
2362 mValidateAppTokens.add(group);
2363 final int numActivities = activities.size();
2364 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2365 final ActivityRecord r = activities.get(activityNdx);
2366 group.tokens.add(r.appToken);
2367 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002368 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002369 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002370 }
2371
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002372 /**
2373 * Perform a reset of the given task, if needed as part of launching it.
2374 * Returns the new HistoryRecord at the top of the task.
2375 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002376 /**
2377 * Helper method for #resetTaskIfNeededLocked.
2378 * We are inside of the task being reset... we'll either finish this activity, push it out
2379 * for another task, or leave it as-is.
2380 * @param task The task containing the Activity (taskTop) that might be reset.
2381 * @param forceReset
2382 * @return An ActivityOptions that needs to be processed.
2383 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002384 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002385 ActivityOptions topOptions = null;
2386
2387 int replyChainEnd = -1;
2388 boolean canMoveOptions = true;
2389
2390 // We only do this for activities that are not the root of the task (since if we finish
2391 // the root, we may no longer have the task!).
2392 final ArrayList<ActivityRecord> activities = task.mActivities;
2393 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002394 final int rootActivityNdx = task.findEffectiveRootIndex();
2395 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002396 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002397 if (target.frontOfTask)
2398 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002399
2400 final int flags = target.info.flags;
2401 final boolean finishOnTaskLaunch =
2402 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2403 final boolean allowTaskReparenting =
2404 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2405 final boolean clearWhenTaskReset =
2406 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2407
2408 if (!finishOnTaskLaunch
2409 && !clearWhenTaskReset
2410 && target.resultTo != null) {
2411 // If this activity is sending a reply to a previous
2412 // activity, we can't do anything with it now until
2413 // we reach the start of the reply chain.
2414 // XXX note that we are assuming the result is always
2415 // to the previous activity, which is almost always
2416 // the case but we really shouldn't count on.
2417 if (replyChainEnd < 0) {
2418 replyChainEnd = i;
2419 }
2420 } else if (!finishOnTaskLaunch
2421 && !clearWhenTaskReset
2422 && allowTaskReparenting
2423 && target.taskAffinity != null
2424 && !target.taskAffinity.equals(task.affinity)) {
2425 // If this activity has an affinity for another
2426 // task, then we need to move it out of here. We will
2427 // move it as far out of the way as possible, to the
2428 // bottom of the activity stack. This also keeps it
2429 // correctly ordered with any activities we previously
2430 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002431 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002432 final ActivityRecord bottom =
2433 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002434 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002435 if (bottom != null && target.taskAffinity != null
2436 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002437 // If the activity currently at the bottom has the
2438 // same task affinity as the one we are moving,
2439 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002440 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002441 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002442 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002443 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002444 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002445 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002446 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002447 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002448 + " out to new task " + target.task);
2449 }
2450
Wale Ogunwale706ed792015-08-02 10:29:44 -07002451 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002452
Craig Mautner525f3d92013-05-07 14:01:50 -07002453 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002454 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2455 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002456 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002457 if (p.finishing) {
2458 continue;
2459 }
2460
Craig Mautnere3a74d52013-02-22 14:14:58 -08002461 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002462 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002463 topOptions = p.takeOptionsLocked();
2464 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002465 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002466 }
2467 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002468 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2469 "Removing activity " + p + " from task=" + task + " adding to task="
2470 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002471 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2472 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002473 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002474 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002475
Wale Ogunwale706ed792015-08-02 10:29:44 -07002476 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002477 }
2478
Wale Ogunwale706ed792015-08-02 10:29:44 -07002479 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002480 if (VALIDATE_TOKENS) {
2481 validateAppTokensLocked();
2482 }
2483
2484 replyChainEnd = -1;
2485 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2486 // If the activity should just be removed -- either
2487 // because it asks for it, or the task should be
2488 // cleared -- then finish it and anything that is
2489 // part of its reply chain.
2490 int end;
2491 if (clearWhenTaskReset) {
2492 // In this case, we want to finish this activity
2493 // and everything above it, so be sneaky and pretend
2494 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002495 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002496 } else if (replyChainEnd < 0) {
2497 end = i;
2498 } else {
2499 end = replyChainEnd;
2500 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002501 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002502 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002503 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002504 if (p.finishing) {
2505 continue;
2506 }
2507 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002508 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002509 topOptions = p.takeOptionsLocked();
2510 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002511 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002512 }
2513 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002514 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002515 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002516 if (finishActivityLocked(
2517 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002518 end--;
2519 srcPos--;
2520 }
2521 }
2522 replyChainEnd = -1;
2523 } else {
2524 // If we were in the middle of a chain, well the
2525 // activity that started it all doesn't want anything
2526 // special, so leave it all as-is.
2527 replyChainEnd = -1;
2528 }
2529 }
2530
2531 return topOptions;
2532 }
2533
2534 /**
2535 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2536 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2537 * @param affinityTask The task we are looking for an affinity to.
2538 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2539 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2540 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2541 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002542 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002543 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002544 int replyChainEnd = -1;
2545 final int taskId = task.taskId;
2546 final String taskAffinity = task.affinity;
2547
2548 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2549 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002550 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2551
2552 // Do not operate on or below the effective root Activity.
2553 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002554 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002555 if (target.frontOfTask)
2556 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002557
2558 final int flags = target.info.flags;
2559 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2560 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2561
2562 if (target.resultTo != null) {
2563 // If this activity is sending a reply to a previous
2564 // activity, we can't do anything with it now until
2565 // we reach the start of the reply chain.
2566 // XXX note that we are assuming the result is always
2567 // to the previous activity, which is almost always
2568 // the case but we really shouldn't count on.
2569 if (replyChainEnd < 0) {
2570 replyChainEnd = i;
2571 }
2572 } else if (topTaskIsHigher
2573 && allowTaskReparenting
2574 && taskAffinity != null
2575 && taskAffinity.equals(target.taskAffinity)) {
2576 // This activity has an affinity for our task. Either remove it if we are
2577 // clearing or move it over to our task. Note that
2578 // we currently punt on the case where we are resetting a
2579 // task that is not at the top but who has activities above
2580 // with an affinity to it... this is really not a normal
2581 // case, and we will need to later pull that task to the front
2582 // and usually at that point we will do the reset and pick
2583 // up those remaining activities. (This only happens if
2584 // someone starts an activity in a new task from an activity
2585 // in a task that is not currently on top.)
2586 if (forceReset || finishOnTaskLaunch) {
2587 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002588 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2589 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002590 for (int srcPos = start; srcPos >= i; --srcPos) {
2591 final ActivityRecord p = activities.get(srcPos);
2592 if (p.finishing) {
2593 continue;
2594 }
Todd Kennedy539db512014-12-15 09:57:55 -08002595 finishActivityLocked(
2596 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002597 }
2598 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002599 if (taskInsertionPoint < 0) {
2600 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002601
Craig Mautner77878772013-03-04 19:46:24 -08002602 }
Craig Mautner77878772013-03-04 19:46:24 -08002603
2604 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002605 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2606 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2607 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002608 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002609 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002610 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002611 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002612
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002613 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2614 "Removing and adding activity " + p + " to stack at " + task
2615 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002616 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2617 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002618 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002619 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002620 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002621 if (VALIDATE_TOKENS) {
2622 validateAppTokensLocked();
2623 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002624
2625 // Now we've moved it in to place... but what if this is
2626 // a singleTop activity and we have put it on top of another
2627 // instance of the same activity? Then we drop the instance
2628 // below so it remains singleTop.
2629 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2630 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002631 int targetNdx = taskActivities.indexOf(target);
2632 if (targetNdx > 0) {
2633 ActivityRecord p = taskActivities.get(targetNdx - 1);
2634 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002635 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2636 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002637 }
2638 }
2639 }
2640 }
2641
2642 replyChainEnd = -1;
2643 }
2644 }
Craig Mautner77878772013-03-04 19:46:24 -08002645 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002646 }
2647
Craig Mautner8849a5e2013-04-02 16:41:03 -07002648 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002649 ActivityRecord newActivity) {
2650 boolean forceReset =
2651 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2652 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2653 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2654 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2655 forceReset = true;
2656 }
2657 }
2658
2659 final TaskRecord task = taskTop.task;
2660
2661 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2662 * for remaining tasks. Used for later tasks to reparent to task. */
2663 boolean taskFound = false;
2664
2665 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2666 ActivityOptions topOptions = null;
2667
Craig Mautner77878772013-03-04 19:46:24 -08002668 // Preserve the location for reparenting in the new task.
2669 int reparentInsertionPoint = -1;
2670
Craig Mautnere3a74d52013-02-22 14:14:58 -08002671 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2672 final TaskRecord targetTask = mTaskHistory.get(i);
2673
2674 if (targetTask == task) {
2675 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2676 taskFound = true;
2677 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002678 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2679 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002680 }
2681 }
2682
Craig Mautner70a86932013-02-28 22:37:44 -08002683 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002684 if (taskNdx >= 0) {
2685 do {
2686 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2687 } while (taskTop == null && taskNdx >= 0);
2688 }
Craig Mautner70a86932013-02-28 22:37:44 -08002689
Craig Mautnere3a74d52013-02-22 14:14:58 -08002690 if (topOptions != null) {
2691 // If we got some ActivityOptions from an activity on top that
2692 // was removed from the task, propagate them to the new real top.
2693 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002694 taskTop.updateOptionsLocked(topOptions);
2695 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002696 topOptions.abort();
2697 }
2698 }
2699
2700 return taskTop;
2701 }
2702
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002703 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2704 String resultWho, int requestCode, int resultCode, Intent data) {
2705
2706 if (callingUid > 0) {
2707 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002708 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002709 }
2710
2711 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2712 + " : who=" + resultWho + " req=" + requestCode
2713 + " res=" + resultCode + " data=" + data);
2714 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2715 try {
2716 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2717 list.add(new ResultInfo(resultWho, requestCode,
2718 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002719 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002720 return;
2721 } catch (Exception e) {
2722 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2723 }
2724 }
2725
2726 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2727 }
2728
Craig Mautner299f9602015-01-26 09:47:33 -08002729 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Craig Mautner04f0b702013-10-22 12:31:01 -07002730 if (mStackSupervisor.isFrontStack(this) && mService.mFocusedActivity == r) {
2731 ActivityRecord next = topRunningActivityLocked(null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002732 final String myReason = reason + " adjustFocus";
Craig Mautner04f0b702013-10-22 12:31:01 -07002733 if (next != r) {
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07002734 if (next != null && (mStackId == FREEFORM_WORKSPACE_STACK_ID
2735 || mStackId == DOCKED_STACK_ID)) {
2736 // For freeform and docked stacks we always keep the focus within the stack as
2737 // long as there is a running activity in the stack that we can adjust focus to.
Wale Ogunwale61803ee2015-08-07 19:59:47 -07002738 mService.setFocusedActivityLocked(next, myReason);
2739 return;
2740 } else {
2741 final TaskRecord task = r.task;
2742 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2743 // For non-fullscreen stack, we want to move the focus to the next visible
2744 // stack to prevent the home screen from moving to the top and obscuring
2745 // other visible stacks.
2746 if (!mFullscreen
2747 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2748 return;
2749 }
2750 // Move the home stack to the top if this stack is fullscreen or there is no
2751 // other visible stack.
2752 if (mStackSupervisor.moveHomeStackTaskToTop(
2753 task.getTaskToReturnTo(), myReason)) {
2754 // Activity focus was already adjusted. Nothing else to do...
2755 return;
2756 }
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002757 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002758 }
2759 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002760
2761 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
Winson Chung648c83b2014-04-28 15:11:56 -07002762 if (top != null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002763 mService.setFocusedActivityLocked(top, myReason);
Winson Chung648c83b2014-04-28 15:11:56 -07002764 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002765 }
2766 }
2767
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002768 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2769 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2770 final String myReason = reason + " adjustFocusToNextVisibleStack";
2771 if (stack == null) {
2772 return false;
2773 }
2774 final ActivityRecord top = stack.topRunningActivityLocked(null);
2775 if (top == null) {
2776 return false;
2777 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002778 mService.setFocusedActivityLocked(top, myReason);
2779 return true;
2780 }
2781
Craig Mautnerf3333272013-04-22 10:55:53 -07002782 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002783 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002784 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2785 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2786 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002787 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002788 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002789 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2790 "stop-no-history", false)) {
2791 // Activity was finished, no need to continue trying to schedule stop.
2792 adjustFocusedActivityLocked(r, "stopActivityFinished");
2793 r.resumeKeyDispatchingLocked();
2794 return;
2795 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002796 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002797 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002798 + " on stop because we're just sleeping");
2799 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002800 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002801 }
2802
2803 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002804 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002805 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002806 try {
2807 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002808 if (DEBUG_STATES) Slog.v(TAG_STATES,
2809 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002810 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002811 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2812 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002813 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002814 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002815 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002816 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002817 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002818 r.setSleeping(true);
2819 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002820 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002821 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002822 } catch (Exception e) {
2823 // Maybe just ignore exceptions here... if the process
2824 // has crashed, our death notification will clean things
2825 // up.
2826 Slog.w(TAG, "Exception thrown during pause", e);
2827 // Just in case, assume it to be stopped.
2828 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002829 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002830 r.state = ActivityState.STOPPED;
2831 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002832 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002833 }
2834 }
2835 }
2836 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002837
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002838 /**
2839 * @return Returns true if the activity is being finished, false if for
2840 * some reason it is being left as-is.
2841 */
2842 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002843 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002844 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002845 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2846 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002847 + ", result=" + resultCode + ", data=" + resultData
2848 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002849 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002850 return false;
2851 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002852
Craig Mautnerd44711d2013-02-23 11:24:36 -08002853 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002854 return true;
2855 }
2856
Craig Mautnerd2328952013-03-05 12:46:26 -08002857 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002858 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2859 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2860 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2861 ActivityRecord r = activities.get(activityNdx);
2862 if (r.resultTo == self && r.requestCode == requestCode) {
2863 if ((r.resultWho == null && resultWho == null) ||
2864 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2865 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2866 false);
2867 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002868 }
2869 }
2870 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002871 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002872 }
2873
Todd Kennedy539db512014-12-15 09:57:55 -08002874 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002875 ActivityRecord r = topRunningActivityLocked(null);
2876 if (r != null && r.app == app) {
2877 // If the top running activity is from this crashing
2878 // process, then terminate it to avoid getting in a loop.
2879 Slog.w(TAG, " Force finishing activity "
2880 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002881 int taskNdx = mTaskHistory.indexOf(r.task);
2882 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002883 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002884 // Also terminate any activities below it that aren't yet
2885 // stopped, to avoid a situation where one will get
2886 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002887 --activityNdx;
2888 if (activityNdx < 0) {
2889 do {
2890 --taskNdx;
2891 if (taskNdx < 0) {
2892 break;
2893 }
2894 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
2895 } while (activityNdx < 0);
2896 }
2897 if (activityNdx >= 0) {
2898 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002899 if (r.state == ActivityState.RESUMED
2900 || r.state == ActivityState.PAUSING
2901 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07002902 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002903 Slog.w(TAG, " Force finishing activity "
2904 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08002905 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002906 }
2907 }
2908 }
2909 }
2910 }
2911
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002912 final void finishVoiceTask(IVoiceInteractionSession session) {
2913 IBinder sessionBinder = session.asBinder();
2914 boolean didOne = false;
2915 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2916 TaskRecord tr = mTaskHistory.get(taskNdx);
2917 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
2918 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
2919 ActivityRecord r = tr.mActivities.get(activityNdx);
2920 if (!r.finishing) {
2921 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
2922 false);
2923 didOne = true;
2924 }
2925 }
2926 }
2927 }
2928 if (didOne) {
2929 mService.updateOomAdjLocked();
2930 }
2931 }
2932
Craig Mautnerd2328952013-03-05 12:46:26 -08002933 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002934 ArrayList<ActivityRecord> activities = r.task.mActivities;
2935 for (int index = activities.indexOf(r); index >= 0; --index) {
2936 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08002937 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002938 break;
2939 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002940 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002941 }
2942 return true;
2943 }
2944
Dianne Hackborn5c607432012-02-28 14:44:19 -08002945 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
2946 // send the result
2947 ActivityRecord resultTo = r.resultTo;
2948 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002949 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08002950 + " who=" + r.resultWho + " req=" + r.requestCode
2951 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002952 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002953 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01002954 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01002955 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002956 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002957 if (r.info.applicationInfo.uid > 0) {
2958 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
2959 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002960 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002961 }
2962 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
2963 resultData);
2964 r.resultTo = null;
2965 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002966 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08002967
2968 // Make sure this HistoryRecord is not holding on to other resources,
2969 // because clients have remote IPC references to this object so we
2970 // can't assume that will go away and want to avoid circular IPC refs.
2971 r.results = null;
2972 r.pendingResults = null;
2973 r.newIntents = null;
2974 r.icicle = null;
2975 }
2976
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002977 /**
2978 * @return Returns true if this activity has been removed from the history
2979 * list, or false if it is still in the list and will be removed later.
2980 */
Craig Mautnerf3333272013-04-22 10:55:53 -07002981 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
2982 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002983 if (r.finishing) {
2984 Slog.w(TAG, "Duplicate finish request for " + r);
2985 return false;
2986 }
2987
Wale Ogunwale7d701172015-03-11 15:36:30 -07002988 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08002989 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002990 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002991 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08002992 task.taskId, r.shortComponentName, reason);
2993 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08002994 final int index = activities.indexOf(r);
2995 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002996 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07002997 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08002998 // If the caller asked that this activity (and all above it)
2999 // be cleared when the task is reset, don't lose that information,
3000 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003001 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003002 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003003 }
3004 }
3005
3006 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003007
Craig Mautner299f9602015-01-26 09:47:33 -08003008 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003009
Dianne Hackborn5c607432012-02-28 14:44:19 -08003010 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003011
Craig Mautnerde4ef022013-04-07 19:01:33 -07003012 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003013 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003014 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003015 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003016 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003017 ? AppTransition.TRANSIT_TASK_CLOSE
3018 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003019
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003020 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003021 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003022
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003023 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003024 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3025 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3026 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003027 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003028 }
3029
Craig Mautneraea74a52014-03-08 14:23:10 -08003030 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003031 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003032 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003033 } else if (r.state != ActivityState.PAUSING) {
3034 // If the activity is PAUSING, we will complete the finish once
3035 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003036 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003037 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003038 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003039 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003040 }
3041
3042 return false;
3043 }
3044
Craig Mautnerf3333272013-04-22 10:55:53 -07003045 static final int FINISH_IMMEDIATELY = 0;
3046 static final int FINISH_AFTER_PAUSE = 1;
3047 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003048
Craig Mautnerf3333272013-04-22 10:55:53 -07003049 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003050 // First things first: if this activity is currently visible,
3051 // and the resumed activity is not yet visible, then hold off on
3052 // finishing until the resumed one becomes visible.
3053 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003054 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
3055 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07003056 if (mStackSupervisor.mStoppingActivities.size() > 3
3057 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003058 // If we already have a few activities waiting to stop,
3059 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07003060 // them out. Or if r is the last of activity of the last task the stack
3061 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07003062 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003063 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003064 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003065 }
3066 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003067 if (DEBUG_STATES) Slog.v(TAG_STATES,
3068 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003069 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003070 if (oomAdj) {
3071 mService.updateOomAdjLocked();
3072 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003073 return r;
3074 }
3075
3076 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003077 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003078 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003079 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003080 if (mResumedActivity == r) {
3081 mResumedActivity = null;
3082 }
3083 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003084 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003085 r.state = ActivityState.FINISHING;
3086
3087 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003088 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003089 || prevState == ActivityState.STOPPED
3090 || prevState == ActivityState.INITIALIZING) {
3091 // If this activity is already stopped, we can just finish
3092 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003093 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003094 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003095 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003096 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003097 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003098 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003099 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3100 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003101 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003102 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003103
3104 // Need to go through the full pause cycle to get this
3105 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003106 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003107 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003108 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003109 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003110 return r;
3111 }
3112
Craig Mautneree36c772014-07-16 14:56:05 -07003113 void finishAllActivitiesLocked(boolean immediately) {
3114 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003115 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3116 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3117 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3118 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003119 noActivitiesInStack = false;
3120 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003121 continue;
3122 }
Craig Mautneree36c772014-07-16 14:56:05 -07003123 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003124 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3125 }
3126 }
Craig Mautneree36c772014-07-16 14:56:05 -07003127 if (noActivitiesInStack) {
3128 mActivityContainer.onTaskListEmptyLocked();
3129 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003130 }
3131
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003132 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3133 // Basic case: for simple app-centric recents, we need to recreate
3134 // the task if the affinity has changed.
3135 if (srec == null || srec.task.affinity == null ||
3136 !srec.task.affinity.equals(destAffinity)) {
3137 return true;
3138 }
3139 // Document-centric case: an app may be split in to multiple documents;
3140 // they need to re-create their task if this current activity is the root
3141 // of a document, unless simply finishing it will return them to the the
3142 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003143 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3144 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003145 // Okay, this activity is at the root of its task. What to do, what to do...
3146 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3147 // Finishing won't return to an application, so we need to recreate.
3148 return true;
3149 }
3150 // We now need to get the task below it to determine what to do.
3151 int taskIdx = mTaskHistory.indexOf(srec.task);
3152 if (taskIdx <= 0) {
3153 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3154 return false;
3155 }
3156 if (taskIdx == 0) {
3157 // At the bottom of the stack, nothing to go back to.
3158 return true;
3159 }
3160 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3161 if (!srec.task.affinity.equals(prevTask.affinity)) {
3162 // These are different apps, so need to recreate.
3163 return true;
3164 }
3165 }
3166 return false;
3167 }
3168
Wale Ogunwale7d701172015-03-11 15:36:30 -07003169 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003170 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003171 final TaskRecord task = srec.task;
3172 final ArrayList<ActivityRecord> activities = task.mActivities;
3173 final int start = activities.indexOf(srec);
3174 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003175 return false;
3176 }
3177 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003178 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003179 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003180 final ComponentName dest = destIntent.getComponent();
3181 if (start > 0 && dest != null) {
3182 for (int i = finishTo; i >= 0; i--) {
3183 ActivityRecord r = activities.get(i);
3184 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003185 r.info.name.equals(dest.getClassName())) {
3186 finishTo = i;
3187 parent = r;
3188 foundParentInTask = true;
3189 break;
3190 }
3191 }
3192 }
3193
3194 IActivityController controller = mService.mController;
3195 if (controller != null) {
3196 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3197 if (next != null) {
3198 // ask watcher if this is allowed
3199 boolean resumeOK = true;
3200 try {
3201 resumeOK = controller.activityResuming(next.packageName);
3202 } catch (RemoteException e) {
3203 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003204 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003205 }
3206
3207 if (!resumeOK) {
3208 return false;
3209 }
3210 }
3211 }
3212 final long origId = Binder.clearCallingIdentity();
3213 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003214 ActivityRecord r = activities.get(i);
3215 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003216 // Only return the supplied result for the first activity finished
3217 resultCode = Activity.RESULT_CANCELED;
3218 resultData = null;
3219 }
3220
3221 if (parent != null && foundParentInTask) {
3222 final int parentLaunchMode = parent.info.launchMode;
3223 final int destIntentFlags = destIntent.getFlags();
3224 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3225 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3226 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3227 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003228 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3229 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003230 } else {
3231 try {
3232 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3233 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003234 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003235 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003236 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003237 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003238 foundParentInTask = res == ActivityManager.START_SUCCESS;
3239 } catch (RemoteException e) {
3240 foundParentInTask = false;
3241 }
3242 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003243 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003244 }
3245 }
3246 Binder.restoreCallingIdentity(origId);
3247 return foundParentInTask;
3248 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003249 /**
3250 * Perform the common clean-up of an activity record. This is called both
3251 * as part of destroyActivityLocked() (when destroying the client-side
3252 * representation) and cleaning things up as a result of its hosting
3253 * processing going away, in which case there is no remaining client-side
3254 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003255 *
3256 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003257 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003258 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3259 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003260 if (mResumedActivity == r) {
3261 mResumedActivity = null;
3262 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003263 if (mPausingActivity == r) {
3264 mPausingActivity = null;
3265 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003266 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003267
3268 r.configDestroy = false;
3269 r.frozenBeforeDestroy = false;
3270
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003271 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003272 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003273 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003274 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003275 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003276 }
3277
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003278 // Make sure this record is no longer in the pending finishes list.
3279 // This could happen, for example, if we are trimming activities
3280 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003281 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003282 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003283
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003284 // Remove any pending results.
3285 if (r.finishing && r.pendingResults != null) {
3286 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3287 PendingIntentRecord rec = apr.get();
3288 if (rec != null) {
3289 mService.cancelIntentSenderLocked(rec, false);
3290 }
3291 }
3292 r.pendingResults = null;
3293 }
3294
3295 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003296 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003297 }
3298
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003299 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003300 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003301 if (getVisibleBehindActivity() == r) {
3302 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003303 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003304 }
3305
3306 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003307 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003308 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003309 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003310 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003311 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003312 }
3313
Craig Mautner299f9602015-01-26 09:47:33 -08003314 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003315 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003316 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003317 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003318 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3319 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3320
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003321 r.takeFromHistory();
3322 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003323 if (DEBUG_STATES) Slog.v(TAG_STATES,
3324 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003325 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003326 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003327 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003328 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003329 if (VALIDATE_TOKENS) {
3330 validateAppTokensLocked();
3331 }
Craig Mautner312ba862014-02-10 17:55:01 -08003332 final TaskRecord task = r.task;
3333 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003334 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003335 "removeActivityFromHistoryLocked: last activity removed from " + this);
Craig Mautner84984fa2014-06-19 11:19:20 -07003336 if (mStackSupervisor.isFrontStack(this) && task == topTask() &&
3337 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003338 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003339 }
Craig Mautner299f9602015-01-26 09:47:33 -08003340 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003341 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003342 cleanUpActivityServicesLocked(r);
3343 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003344 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003345
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003346 /**
3347 * Perform clean-up of service connections in an activity record.
3348 */
3349 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3350 // Throw away any services that have been bound by this activity.
3351 if (r.connections != null) {
3352 Iterator<ConnectionRecord> it = r.connections.iterator();
3353 while (it.hasNext()) {
3354 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003355 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003356 }
3357 r.connections = null;
3358 }
3359 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003360
Craig Mautneree2e45a2014-06-27 12:10:03 -07003361 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003362 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003363 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003364 mHandler.sendMessage(msg);
3365 }
3366
Craig Mautneree2e45a2014-06-27 12:10:03 -07003367 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003368 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003369 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003370 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3371 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3372 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3373 final ActivityRecord r = activities.get(activityNdx);
3374 if (r.finishing) {
3375 continue;
3376 }
3377 if (r.fullscreen) {
3378 lastIsOpaque = true;
3379 }
3380 if (owner != null && r.app != owner) {
3381 continue;
3382 }
3383 if (!lastIsOpaque) {
3384 continue;
3385 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003386 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003387 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003388 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003389 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003390 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003391 activityRemoved = true;
3392 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003393 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003394 }
3395 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003396 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003397 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003398 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003399 }
3400
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003401 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3402 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003403 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3404 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003405 + " pausing=" + mPausingActivity + " for reason " + reason);
3406 return destroyActivityLocked(r, true, reason);
3407 }
3408 return false;
3409 }
3410
3411 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3412 String reason) {
3413 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003414 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003415 int maxTasks = tasks.size() / 4;
3416 if (maxTasks < 1) {
3417 maxTasks = 1;
3418 }
3419 int numReleased = 0;
3420 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3421 final TaskRecord task = mTaskHistory.get(taskNdx);
3422 if (!tasks.contains(task)) {
3423 continue;
3424 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003425 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003426 int curNum = 0;
3427 final ArrayList<ActivityRecord> activities = task.mActivities;
3428 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3429 final ActivityRecord activity = activities.get(actNdx);
3430 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003431 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003432 + " in state " + activity.state + " resumed=" + mResumedActivity
3433 + " pausing=" + mPausingActivity + " for reason " + reason);
3434 destroyActivityLocked(activity, true, reason);
3435 if (activities.get(actNdx) != activity) {
3436 // Was removed from list, back up so we don't miss the next one.
3437 actNdx--;
3438 }
3439 curNum++;
3440 }
3441 }
3442 if (curNum > 0) {
3443 numReleased += curNum;
3444 maxTasks--;
3445 if (mTaskHistory.get(taskNdx) != task) {
3446 // The entire task got removed, back up so we don't miss the next one.
3447 taskNdx--;
3448 }
3449 }
3450 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003451 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3452 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003453 return numReleased;
3454 }
3455
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003456 /**
3457 * Destroy the current CLIENT SIDE instance of an activity. This may be
3458 * called both when actually finishing an activity, or when performing
3459 * a configuration switch where we destroy the current client-side object
3460 * but then create a new client-side object for this same HistoryRecord.
3461 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003462 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003463 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3464 "Removing activity from " + reason + ": token=" + r
3465 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003466 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003467 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003468 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003469
3470 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003471
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003472 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003473
3474 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003475
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003476 if (hadApp) {
3477 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003478 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003479 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3480 mService.mHeavyWeightProcess = null;
3481 mService.mHandler.sendEmptyMessage(
3482 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3483 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003484 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003485 // Update any services we are bound to that might care about whether
3486 // their client may have activities.
3487 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003488 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003489 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003490 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003491 }
3492 }
3493
3494 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003495
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003496 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003497 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003498 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003499 r.configChangeFlags);
3500 } catch (Exception e) {
3501 // We can just ignore exceptions here... if the process
3502 // has crashed, our death notification will clean things
3503 // up.
3504 //Slog.w(TAG, "Exception thrown during finish", e);
3505 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003506 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003507 removedFromHistory = true;
3508 skipDestroy = true;
3509 }
3510 }
3511
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003512 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003513
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003514 // If the activity is finishing, we need to wait on removing it
3515 // from the list to give it a chance to do its cleanup. During
3516 // that time it may make calls back with its token so we need to
3517 // be able to find it on the list and so we don't want to remove
3518 // it from the list yet. Otherwise, we can just immediately put
3519 // it in the destroyed state since we are not removing it from the
3520 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003521 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003522 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003523 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003524 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003525 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003526 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3527 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003528 if (DEBUG_STATES) Slog.v(TAG_STATES,
3529 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003530 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003531 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003532 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003533 }
3534 } else {
3535 // remove this record from the history.
3536 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003537 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003538 removedFromHistory = true;
3539 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003540 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003541 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003542 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003543 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003544 }
3545 }
3546
3547 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003548
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003549 if (!mLRUActivities.remove(r) && hadApp) {
3550 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3551 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003552
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003553 return removedFromHistory;
3554 }
3555
Craig Mautner299f9602015-01-26 09:47:33 -08003556 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003557 final long origId = Binder.clearCallingIdentity();
3558 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003559 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003560 if (r != null) {
3561 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003562 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003563 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003564
Wale Ogunwale60454db2015-01-23 16:05:07 -08003565 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003566 if (r.state == ActivityState.DESTROYING) {
3567 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003568 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003569 }
3570 }
Craig Mautner05d29032013-05-03 13:40:13 -07003571 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003572 } finally {
3573 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003574 }
3575 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003576
Todd Kennedyaab56db2015-01-30 09:39:53 -08003577 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003578 if (hasVisibleBehindActivity() &&
3579 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003580 if (r == topRunningActivityLocked(null)) {
3581 // Don't release the top activity if it has requested to run behind the next
3582 // activity.
3583 return;
3584 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003585 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003586 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003587 " thread=" + r.app.thread);
3588 if (r != null && r.app != null && r.app.thread != null) {
3589 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003590 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003591 } catch (RemoteException e) {
3592 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003593 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003594 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003595 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003596 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003597 }
3598 }
3599 }
3600
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003601 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003602 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3603 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003604 if (r != null) {
3605 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003606 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003607 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003608 }
3609 mStackSupervisor.resumeTopActivitiesLocked();
3610 }
3611
Jose Lima4b6c6692014-08-12 17:41:12 -07003612 boolean hasVisibleBehindActivity() {
3613 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003614 }
3615
Jose Lima4b6c6692014-08-12 17:41:12 -07003616 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003617 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003618 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003619 }
3620 }
3621
Jose Lima4b6c6692014-08-12 17:41:12 -07003622 ActivityRecord getVisibleBehindActivity() {
3623 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003624 }
3625
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003626 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3627 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003628 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003629 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3630 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003631 while (i > 0) {
3632 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003633 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003634 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003635 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003636 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003637 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003638 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003639 }
3640 }
3641 }
3642
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003643 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3644 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003645 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3646 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003647 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3648 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003649 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003650 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003651 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3652 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003653
3654 boolean hasVisibleActivities = false;
3655
3656 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003657 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003658 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3659 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003660 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3661 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3662 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3663 final ActivityRecord r = activities.get(activityNdx);
3664 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003665 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3666 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003667 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003668 if (r.visible) {
3669 hasVisibleActivities = true;
3670 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003671 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003672 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3673 // Don't currently have state for the activity, or
3674 // it is finishing -- always remove it.
3675 remove = true;
3676 } else if (r.launchCount > 2 &&
3677 r.lastLaunchTime > (SystemClock.uptimeMillis()-60000)) {
3678 // We have launched this activity too many times since it was
3679 // able to run, so give up and remove it.
3680 remove = true;
3681 } else {
3682 // The process may be gone, but the activity lives on!
3683 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003684 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003685 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003686 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3687 "Removing activity " + r + " from stack at " + i
3688 + ": haveState=" + r.haveState
3689 + " stateNotNeeded=" + r.stateNotNeeded
3690 + " finishing=" + r.finishing
3691 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003692 if (!r.finishing) {
3693 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3694 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3695 r.userId, System.identityHashCode(r),
3696 r.task.taskId, r.shortComponentName,
3697 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003698 if (r.state == ActivityState.RESUMED) {
3699 mService.updateUsageStats(r, false);
3700 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003701 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003702 } else {
3703 // We have the current state for this activity, so
3704 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003705 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3706 if (DEBUG_APP) Slog.v(TAG_APP,
3707 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003708 r.app = null;
3709 r.nowVisible = false;
3710 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003711 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003712 "App died, clearing saved state of " + r);
3713 r.icicle = null;
3714 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003715 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003716 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003717 if (remove) {
3718 removeActivityFromHistoryLocked(r, "appDied");
3719 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003720 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003721 }
3722 }
3723
3724 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003725 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003726
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003727 final void updateTransitLocked(int transit, Bundle options) {
3728 if (options != null) {
3729 ActivityRecord r = topRunningActivityLocked(null);
3730 if (r != null && r.state != ActivityState.RESUMED) {
3731 r.updateOptionsLocked(options);
3732 } else {
3733 ActivityOptions.abort(options);
3734 }
3735 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003736 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003737 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003738
Craig Mautner21d24a22014-04-23 11:45:37 -07003739 void updateTaskMovement(TaskRecord task, boolean toFront) {
3740 if (task.isPersistable) {
3741 task.mLastTimeMoved = System.currentTimeMillis();
3742 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3743 // recently will be most negative, tasks sent to the bottom before that will be less
3744 // negative. Similarly for recent tasks moved to the top which will be most positive.
3745 if (!toFront) {
3746 task.mLastTimeMoved *= -1;
3747 }
3748 }
3749 }
3750
Craig Mautner84984fa2014-06-19 11:19:20 -07003751 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003752 final int top = mTaskHistory.size() - 1;
3753 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3754 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003755 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003756 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3757 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003758 mTaskHistory.remove(taskNdx);
3759 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003760 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003761 return;
3762 }
3763 }
3764 }
3765
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003766 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, Bundle options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003767 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003768 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003769
Craig Mautner11bf9a52013-02-19 14:08:51 -08003770 final int numTasks = mTaskHistory.size();
3771 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003772 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003773 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003774 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003775 ActivityOptions.abort(options);
3776 } else {
3777 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3778 }
3779 return;
3780 }
3781
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003782 if (timeTracker != null) {
3783 // The caller wants a time tracker associated with this task.
3784 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3785 tr.mActivities.get(i).appTimeTracker = timeTracker;
3786 }
3787 }
3788
Craig Mautner11bf9a52013-02-19 14:08:51 -08003789 // Shift all activities with this task up to the top
3790 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003791 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003792
Chong Zhang45c25ce2015-08-10 22:18:26 -07003793 // Don't refocus if invisible to current user
3794 ActivityRecord top = tr.getTopActivity();
3795 if (!okToShowLocked(top)) {
3796 addRecentActivityLocked(top);
3797 ActivityOptions.abort(options);
3798 return;
3799 }
3800
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003801 // Set focus to the top running activity of this stack.
3802 ActivityRecord r = topRunningActivityLocked(null);
3803 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003804
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003805 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003806 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003807 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003808 if (r != null) {
3809 mNoAnimActivities.add(r);
3810 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003811 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003812 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003813 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003814 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003815
Craig Mautner05d29032013-05-03 13:40:13 -07003816 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003817 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003818
3819 if (VALIDATE_TOKENS) {
3820 validateAppTokensLocked();
3821 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003822 }
3823
3824 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003825 * Worker method for rearranging history stack. Implements the function of moving all
3826 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003827 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003828 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003829 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3830 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003831 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003832 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003833 * @return Returns true if the move completed, false if not.
3834 */
Craig Mautner299f9602015-01-26 09:47:33 -08003835 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003836 final TaskRecord tr = taskForIdLocked(taskId);
3837 if (tr == null) {
3838 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3839 return false;
3840 }
3841
3842 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003843 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003844
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003845 // If we have a watcher, preflight the move before committing to it. First check
3846 // for *other* available tasks, but if none are available, then try again allowing the
3847 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003848 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003849 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003850 if (next == null) {
3851 next = topRunningActivityLocked(null, 0);
3852 }
3853 if (next != null) {
3854 // ask watcher if this is allowed
3855 boolean moveOK = true;
3856 try {
3857 moveOK = mService.mController.activityResuming(next.packageName);
3858 } catch (RemoteException e) {
3859 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003860 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003861 }
3862 if (!moveOK) {
3863 return false;
3864 }
3865 }
3866 }
3867
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003868 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003869
riddle_hsuc215a4f2014-12-27 12:10:45 +08003870 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07003871
3872 // If true, we should resume the home activity next if the task we are moving to the
3873 // back is over the home stack. We force to false if the task we are moving to back
3874 // is the home task and we don't want it resumed after moving to the back.
3875 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
3876 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08003877 final TaskRecord nextTask = getNextTask(tr);
3878 if (nextTask != null) {
3879 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
3880 } else {
3881 prevIsHome = true;
3882 }
3883 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08003884 mTaskHistory.remove(tr);
3885 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07003886 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003887
Craig Mautnerc8143c62013-09-03 12:15:57 -07003888 // There is an assumption that moving a task to the back moves it behind the home activity.
3889 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07003890 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003891 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
3892 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003893 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07003894 break;
3895 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003896 if (taskNdx == 1) {
3897 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07003898 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003899 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003900 }
3901
Craig Mautner299f9602015-01-26 09:47:33 -08003902 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003903 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003904
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003905 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08003906 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003907 }
3908
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003909 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07003910 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07003911 if (!mService.mBooting && !mService.mBooted) {
3912 // Not ready yet!
3913 return false;
3914 }
Craig Mautner84984fa2014-06-19 11:19:20 -07003915 final int taskToReturnTo = tr.getTaskToReturnTo();
3916 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08003917 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003918 }
3919
Craig Mautner05d29032013-05-03 13:40:13 -07003920 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003921 return true;
3922 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07003923
Craig Mautner8849a5e2013-04-02 16:41:03 -07003924 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003925 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08003926 final Uri data = r.intent.getData();
3927 final String strData = data != null ? data.toSafeString() : null;
3928
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003929 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003930 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003931 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08003932 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003933 }
3934
3935 /**
3936 * Make sure the given activity matches the current configuration. Returns
3937 * false if the activity had to be destroyed. Returns true if the
3938 * configuration is the same, or the activity will remain running as-is
3939 * for whatever reason. Ensures the HistoryRecord is updated with the
3940 * correct configuration and all other bookkeeping is handled.
3941 */
3942 final boolean ensureActivityConfigurationLocked(ActivityRecord r,
3943 int globalChanges) {
3944 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003945 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003946 "Skipping config check (will change): " + r);
3947 return true;
3948 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003949
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003950 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003951 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07003952
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003953 // Short circuit: if the two configurations are the exact same
3954 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003955 final Configuration newConfig = mService.mConfiguration;
3956 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08003957 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003958 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08003959 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003960 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003961 "Configuration unchanged in " + r);
3962 return true;
3963 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003964
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003965 // We don't worry about activities that are finishing.
3966 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003967 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003968 "Configuration doesn't matter in finishing " + r);
3969 r.stopFreezingScreenLocked(false);
3970 return true;
3971 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003972
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003973 // Okay we now are going to make this activity have the new config.
3974 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08003975 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003976 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003977 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003978 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003979
Filip Gruszczynski23493322015-07-29 17:02:59 -07003980 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07003981 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003982 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003983 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003984 "Configuration no differences in " + r);
3985 return true;
3986 }
3987
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003988 // If the activity isn't currently running, just leave the new
3989 // configuration and it will pick that up next time it starts.
3990 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003991 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003992 "Configuration doesn't matter not running " + r);
3993 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003994 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003995 return true;
3996 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003997
Dianne Hackborn58f42a52011-10-10 13:46:34 -07003998 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07003999 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4000 "Checking to restart " + r.info.name + ": changed=0x"
4001 + Integer.toHexString(changes) + ", handles=0x"
4002 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig);
4003
Dianne Hackborne6676352011-06-01 16:51:20 -07004004 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004005 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4006 r.configChangeFlags |= changes;
4007 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004008 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004009 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004010 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004011 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004012 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004013 } else if (r.state == ActivityState.PAUSING) {
4014 // A little annoying: we are waiting for this activity to
4015 // finish pausing. Let's not do anything now, but just
4016 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004017 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004018 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004019 r.configDestroy = true;
4020 return true;
4021 } else if (r.state == ActivityState.RESUMED) {
4022 // Try to optimize this case: the configuration is changing
4023 // and we need to restart the top, resumed activity.
4024 // Instead of doing the normal handshaking, just say
4025 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004026 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004027 "Config is relaunching resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004028 relaunchActivityLocked(r, r.configChangeFlags, true);
4029 r.configChangeFlags = 0;
4030 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004031 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004032 "Config is relaunching non-resumed " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004033 relaunchActivityLocked(r, r.configChangeFlags, false);
4034 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004035 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004036
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004037 // All done... tell the caller we weren't able to keep this
4038 // activity around.
4039 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004040 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004041
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004042 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004043 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004044 // changes is always sent to all processes when they happen so it can just use whatever
4045 // system level configuration it last got.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004046 if (r.app != null && r.app.thread != null) {
4047 try {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004048 if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION, "Sending new config to " + r);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004049 r.app.thread.scheduleActivityConfigurationChanged(
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004050 r.appToken, new Configuration(taskConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004051 } catch (RemoteException e) {
4052 // If process died, whatever.
4053 }
4054 }
4055 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004056
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004057 return true;
4058 }
4059
Filip Gruszczynski23493322015-07-29 17:02:59 -07004060 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4061 Configuration oldTaskOverride) {
4062 // Determine what has changed. May be nothing, if this is a config
4063 // that has come back from the app after going idle. In that case
4064 // we just want to leave the official config object now in the
4065 // activity and do nothing else.
4066 int taskChanges = oldTaskOverride.diff(taskConfig);
4067 // We don't want to use size changes if they don't cross boundaries that are important to
4068 // the app.
4069 if ((taskChanges & ActivityInfo.CONFIG_SCREEN_SIZE) != 0) {
4070 final boolean crosses = record.crossesHorizontalSizeThreshold(
4071 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4072 || record.crossesVerticalSizeThreshold(
4073 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4074 if (!crosses) {
4075 taskChanges &= ~ActivityInfo.CONFIG_SCREEN_SIZE;
4076 }
4077 }
4078 if ((taskChanges & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4079 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4080 final int newSmallest = taskConfig.smallestScreenWidthDp;
4081 final boolean crosses = record.crossesHorizontalSizeThreshold(oldSmallest, newSmallest)
4082 || record.crossesVerticalSizeThreshold(oldSmallest, newSmallest);
4083 if (!crosses) {
4084 taskChanges &= ~ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4085 }
4086 }
4087 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4088 }
4089
4090 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4091 Configuration oldTaskOverride, int taskChanges) {
4092 if (taskChanges == 0) {
4093 // {@link Configuration#diff} doesn't catch changes from unset values.
4094 // Check for changes we care about.
4095 if (oldTaskOverride.orientation != taskConfig.orientation) {
4096 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
4097 }
4098 // We want to explicitly track situations where the size configuration goes from
4099 // undefined to defined. We don't care about crossing the threshold in that case,
4100 // because there is no threshold.
4101 final int oldHeight = oldTaskOverride.screenHeightDp;
4102 final int newHeight = taskConfig.screenHeightDp;
4103 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4104 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4105 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4106 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4107 }
4108 final int oldWidth = oldTaskOverride.screenWidthDp;
4109 final int newWidth = taskConfig.screenWidthDp;
4110 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4111 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4112 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4113 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4114 }
4115 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4116 final int newSmallest = taskConfig.smallestScreenWidthDp;
4117 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4118 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4119 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4120 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4121 }
4122 }
4123 return taskChanges;
4124 }
4125
Craig Mautner2568c3a2015-03-26 14:22:34 -07004126 private boolean relaunchActivityLocked(ActivityRecord r, int changes, boolean andResume) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004127 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004128 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004129 if (andResume) {
4130 results = r.results;
4131 newIntents = r.newIntents;
4132 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004133 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4134 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004135 + " andResume=" + andResume);
4136 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004137 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004138 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004139
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004140 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004141
Craig Mautner34b73df2014-01-12 21:11:08 -08004142 mStackSupervisor.removeChildActivityContainers(r);
4143
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004144 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004145 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4146 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004147 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004148 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4149 !andResume, new Configuration(mService.mConfiguration),
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004150 new Configuration(r.task.mOverrideConfig));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004151 // Note: don't need to call pauseIfSleepingLocked() here, because
4152 // the caller will only pass in 'andResume' if this activity is
4153 // currently resumed, which implies we aren't sleeping.
4154 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004155 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004156 }
4157
4158 if (andResume) {
4159 r.results = null;
4160 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004161 r.state = ActivityState.RESUMED;
4162 } else {
4163 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4164 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004165 }
4166
4167 return true;
4168 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004169
4170 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004171 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4172 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4173 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4174 final ActivityRecord r = activities.get(activityNdx);
4175 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004176 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004177 }
4178 if (r.fullscreen && !r.finishing) {
4179 return false;
4180 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004181 }
4182 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004183 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004184 if (r == null) {
4185 return false;
4186 }
4187 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4188 + " would have returned true for r=" + r);
4189 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004190 }
4191
4192 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004193 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4194 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4195 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4196 final ActivityRecord r = activities.get(activityNdx);
4197 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004198 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004199 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004200 }
4201 }
4202 }
4203
Wale Ogunwale540e1232015-05-01 15:35:39 -07004204 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4205 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004206 boolean didSomething = false;
4207 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004208 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004209 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4210 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4211 int numActivities = activities.size();
4212 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4213 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004214 final boolean sameComponent =
4215 (r.packageName.equals(packageName) && (filterByClasses == null
4216 || filterByClasses.contains(r.realActivity.getClassName())))
4217 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004218 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004219 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004220 && (r.app == null || evenPersistent || !r.app.persistent)) {
4221 if (!doit) {
4222 if (r.finishing) {
4223 // If this activity is just finishing, then it is not
4224 // interesting as far as something to stop.
4225 continue;
4226 }
4227 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004228 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004229 if (r.isHomeActivity()) {
4230 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4231 Slog.i(TAG, "Skip force-stop again " + r);
4232 continue;
4233 } else {
4234 homeActivity = r.realActivity;
4235 }
4236 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004237 didSomething = true;
4238 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004239 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004240 if (r.app != null) {
4241 r.app.removed = true;
4242 }
4243 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004244 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004245 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004246 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4247 true)) {
4248 // r has been deleted from mActivities, accommodate.
4249 --numActivities;
4250 --activityNdx;
4251 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004252 }
4253 }
4254 }
4255 return didSomething;
4256 }
4257
Dianne Hackborn09233282014-04-30 11:33:59 -07004258 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004259 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4260 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004261 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004262 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004263 if (task.getTopActivity() == null) {
4264 continue;
4265 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004266 ActivityRecord r = null;
4267 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004268 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004269 int numActivities = 0;
4270 int numRunning = 0;
4271 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004272 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004273 continue;
4274 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004275 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004276 tmp = activities.get(activityNdx);
4277 if (tmp.finishing) {
4278 continue;
4279 }
4280 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004281
Craig Mautneraab647e2013-02-28 16:31:36 -08004282 // Initialize state for next task if needed.
4283 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4284 top = r;
4285 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004286 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004287
4288 // Add 'r' into the current task.
4289 numActivities++;
4290 if (r.app != null && r.app.thread != null) {
4291 numRunning++;
4292 }
4293
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004294 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004295 TAG, r.intent.getComponent().flattenToShortString()
4296 + ": task=" + r.task);
4297 }
4298
4299 RunningTaskInfo ci = new RunningTaskInfo();
4300 ci.id = task.taskId;
4301 ci.baseActivity = r.intent.getComponent();
4302 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004303 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004304 if (focusedStack && topTask) {
4305 // Give the latest time to ensure foreground task can be sorted
4306 // at the first, because lastActiveTime of creating task is 0.
4307 ci.lastActiveTime = System.currentTimeMillis();
4308 topTask = false;
4309 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004310
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004311 if (top.task != null) {
4312 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004313 }
4314 ci.numActivities = numActivities;
4315 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004316 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004317 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004318 }
4319
4320 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004321 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004322 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004323 if (top >= 0) {
4324 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4325 int activityTop = activities.size() - 1;
4326 if (activityTop > 0) {
4327 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4328 "unhandled-back", true);
4329 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004330 }
4331 }
4332
Craig Mautner6b74cb52013-09-27 17:02:21 -07004333 /**
4334 * Reset local parameters because an app's activity died.
4335 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004336 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004337 */
4338 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004339 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004340 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004341 "App died while pausing: " + mPausingActivity);
4342 mPausingActivity = null;
4343 }
4344 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4345 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004346 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004347 }
4348
Craig Mautner19091252013-10-05 00:03:53 -07004349 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004350 }
4351
Craig Mautnercae015f2013-02-08 14:31:27 -08004352 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004353 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4354 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4355 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4356 final ActivityRecord r = activities.get(activityNdx);
4357 if (r.app == app) {
4358 Slog.w(TAG, " Force finishing activity "
4359 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004360 // Force the destroy to skip right to removal.
4361 r.app = null;
4362 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004363 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004364 }
4365 }
4366 }
4367
Dianne Hackborn390517b2013-05-30 15:03:32 -07004368 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004369 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004370 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004371 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4372 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004373 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4374 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004375 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004376 " Task id #" + task.taskId + "\n" +
4377 " mFullscreen=" + task.mFullscreen + "\n" +
4378 " mBounds=" + task.mBounds + "\n" +
4379 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004380 if (printed) {
4381 header = null;
4382 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004383 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004384 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004385 }
4386
4387 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4388 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4389
4390 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004391 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4392 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004393 }
4394 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004395 final int top = mTaskHistory.size() - 1;
4396 if (top >= 0) {
4397 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4398 int listTop = list.size() - 1;
4399 if (listTop >= 0) {
4400 activities.add(list.get(listTop));
4401 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004402 }
4403 } else {
4404 ItemMatcher matcher = new ItemMatcher();
4405 matcher.build(name);
4406
Craig Mautneraab647e2013-02-28 16:31:36 -08004407 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4408 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4409 if (matcher.match(r1, r1.intent.getComponent())) {
4410 activities.add(r1);
4411 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004412 }
4413 }
4414 }
4415
4416 return activities;
4417 }
4418
4419 ActivityRecord restartPackage(String packageName) {
4420 ActivityRecord starting = topRunningActivityLocked(null);
4421
4422 // All activities that came from the package must be
4423 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004424 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4425 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4426 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4427 final ActivityRecord a = activities.get(activityNdx);
4428 if (a.info.packageName.equals(packageName)) {
4429 a.forceNewConfig = true;
4430 if (starting != null && a == starting && a.visible) {
4431 a.startFreezingScreenLocked(starting.app,
4432 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4433 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004434 }
4435 }
4436 }
4437
4438 return starting;
4439 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004440
Craig Mautner299f9602015-01-26 09:47:33 -08004441 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004442 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004443 }
4444
Wale Ogunwale000957c2015-04-03 08:19:12 -07004445 /**
4446 * Removes the input task from this stack.
4447 * @param task to remove.
4448 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004449 * @param moving task to another stack. In the case we are moving we don't want to perform
4450 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004451 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004452 void removeTask(TaskRecord task, String reason, boolean moving) {
4453 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004454 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004455 mWindowManager.removeTask(task.taskId);
4456 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004457
Craig Mautner04a0ea62014-01-13 12:51:26 -08004458 final ActivityRecord r = mResumedActivity;
4459 if (r != null && r.task == task) {
4460 mResumedActivity = null;
4461 }
4462
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004463 final int taskNdx = mTaskHistory.indexOf(task);
4464 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004465 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4466 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4467 if (!nextTask.isOverHomeStack()) {
4468 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4469 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004470 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004471 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004472 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004473
Wale Ogunwale040b4702015-08-06 18:10:50 -07004474 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004475 final boolean isVoiceSession = task.voiceSession != null;
4476 if (isVoiceSession) {
4477 try {
4478 task.voiceSession.taskFinished(task.intent, task.taskId);
4479 } catch (RemoteException e) {
4480 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004481 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004482 if (task.autoRemoveFromRecents() || isVoiceSession) {
4483 // Task creator asked to remove this when done, or this task was a voice
4484 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004485 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004486 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004487 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004488 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004489
4490 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004491 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004492 final boolean notHomeStack = !isHomeStack();
Craig Mautner04a0ea62014-01-13 12:51:26 -08004493 if (isOnHomeDisplay()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004494 String myReason = reason + " leftTaskHistoryEmpty";
4495 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
4496 mStackSupervisor.moveHomeStack(notHomeStack, myReason);
4497 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004498 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004499 if (mStacks != null) {
4500 mStacks.remove(this);
4501 mStacks.add(0, this);
4502 }
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004503 if (notHomeStack) {
4504 mActivityContainer.onTaskListEmptyLocked();
4505 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004506 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004507
4508 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004509 }
4510
Dianne Hackborn91097de2014-04-04 18:02:06 -07004511 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4512 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4513 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004514 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4515 voiceInteractor);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004516 addTask(task, toTop, false);
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004517 return task;
4518 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004519
4520 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004521 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004522 }
4523
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004524 void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
Craig Mautnerc00204b2013-03-05 15:02:14 -08004525 task.stack = this;
4526 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004527 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004528 } else {
4529 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004530 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004531 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004532 if (!moving && task.voiceSession != null) {
4533 try {
4534 task.voiceSession.taskStarted(task.intent, task.taskId);
4535 } catch (RemoteException e) {
4536 }
4537 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004538 }
4539
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004540 void positionTask(final TaskRecord task, int position, boolean moving) {
4541 task.stack = this;
4542 insertTaskAtPosition(task, position);
4543 if (!moving && task.voiceSession != null) {
4544 try {
4545 task.voiceSession.taskStarted(task.intent, task.taskId);
4546 } catch (RemoteException e) {
4547 }
4548 }
4549 }
4550
Wale Ogunwale706ed792015-08-02 10:29:44 -07004551 void addAppToken(ActivityRecord r, TaskRecord task) {
4552 final Rect bounds = task.getLaunchBounds();
4553 final Configuration config =
4554 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4555 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
4556 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
4557 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
4558 bounds);
4559 if (config != null) {
4560 task.updateOverrideConfiguration(config, bounds);
4561 }
4562 r.taskConfigOverride = task.mOverrideConfig;
4563 }
4564
4565 private void setAppTask(ActivityRecord r, TaskRecord task) {
4566 final Rect bounds = task.getLaunchBounds();
4567 final Configuration config =
4568 mWindowManager.setAppTask(r.appToken, task.taskId, task.getLaunchBounds());
4569 if (config != null) {
4570 task.updateOverrideConfiguration(config, bounds);
4571 }
4572 r.taskConfigOverride = task.mOverrideConfig;
4573 }
4574
Craig Mautnerc00204b2013-03-05 15:02:14 -08004575 public int getStackId() {
4576 return mStackId;
4577 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004578
4579 @Override
4580 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004581 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4582 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004583 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004584
Craig Mautner15df08a2015-04-01 12:17:18 -07004585 void onLockTaskPackagesUpdatedLocked() {
4586 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4587 mTaskHistory.get(taskNdx).setLockTaskAuth();
4588 }
4589 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004590}