blob: bad71b23a55310243be83330cf0280a326762f58 [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
23import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070024import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070025
Wale Ogunwalee23149f2015-03-06 15:39:44 -080026import static com.android.server.am.ActivityManagerDebugConfig.*;
Chong Zhang75b37202015-12-04 14:16:36 -080027import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
Craig Mautner84984fa2014-06-19 11:19:20 -070028import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
29import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070030
Wale Ogunwale040b4702015-08-06 18:10:50 -070031import static com.android.server.am.ActivityStackSupervisor.MOVING;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070032import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwale040b4702015-08-06 18:10:50 -070033
Winsonc809cbb2015-11-02 12:06:15 -080034import android.graphics.Point;
Wale Ogunwale706ed792015-08-02 10:29:44 -070035import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070036import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070037
Dianne Hackborn91097de2014-04-04 18:02:06 -070038import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080039import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070040import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070041import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080042import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080043import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080044import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080045import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070046import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070047
48import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070049import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070050import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070051import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070052import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080053import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070054import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080055import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070059import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080061import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080062import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070064import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080065import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.os.Handler;
67import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090068import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070070import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.RemoteException;
72import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080073import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070074import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070075import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070078import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079
Craig Mautnercae015f2013-02-08 14:31:27 -080080import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080081import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082import java.lang.ref.WeakReference;
83import java.util.ArrayList;
84import java.util.Iterator;
85import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080086import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070087import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070088
89/**
90 * State and management of a single stack of activities.
91 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070092final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080093
Wale Ogunwalee23149f2015-03-06 15:39:44 -080094 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070095 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
96 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080097 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -070098 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -070099 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700100 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700101 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700102 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700103 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
104 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700105 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700106 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700107 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
108 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
109 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
110 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
111 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700112
113 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800114
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700115 // Ticks during which we check progress while waiting for an app to launch.
116 static final int LAUNCH_TICK = 500;
117
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700118 // How long we wait until giving up on the last activity to pause. This
119 // is short because it directly impacts the responsiveness of starting the
120 // next activity.
121 static final int PAUSE_TIMEOUT = 500;
122
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700123 // How long we wait for the activity to tell us it has stopped before
124 // giving up. This is a good amount of time because we really need this
125 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700126 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700127
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700128 // How long we wait until giving up on an activity telling us it has
129 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700130 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800131
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700132 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800133 // disabled.
134 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800135
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700136 // How long between activity launches that we consider safe to not warn
137 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700138 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800139
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700140 // Set to false to disable the preview that is shown while a new activity
141 // is being started.
142 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800143
Craig Mautner5eda9b32013-07-02 11:58:16 -0700144 // How long to wait for all background Activities to redraw following a call to
145 // convertToTranslucent().
146 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
147
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700148 enum ActivityState {
149 INITIALIZING,
150 RESUMED,
151 PAUSING,
152 PAUSED,
153 STOPPING,
154 STOPPED,
155 FINISHING,
156 DESTROYING,
157 DESTROYED
158 }
159
160 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700161 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800162 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800163
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700164 /**
165 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800166 * running) activities. It contains #TaskRecord objects.
167 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800168 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800169
170 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800171 * Used for validating app tokens with window manager.
172 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800173 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800174
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700175 /**
176 * List of running activities, sorted by recent usage.
177 * The first entry in the list is the least recently used.
178 * It contains HistoryRecord objects.
179 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800180 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700181
182 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700183 * Animations that for the current transition have requested not to
184 * be considered for the transition animation.
185 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800186 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700187
188 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700189 * When we are in the process of pausing an activity, before starting the
190 * next one, this variable holds the activity that is currently being paused.
191 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800192 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700193
194 /**
195 * This is the last activity that we put into the paused state. This is
196 * used to determine if we need to do an activity transition while sleeping,
197 * when we normally hold the top activity paused.
198 */
199 ActivityRecord mLastPausedActivity = null;
200
201 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700202 * Activities that specify No History must be removed once the user navigates away from them.
203 * If the device goes to sleep with such an activity in the paused state then we save it here
204 * and finish it later if another activity replaces it on wakeup.
205 */
206 ActivityRecord mLastNoHistoryActivity = null;
207
208 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700209 * Current activity that is resumed, or null if there is none.
210 */
211 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800212
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700213 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700214 * This is the last activity that has been started. It is only used to
215 * identify when multiple activities are started at once so that the user
216 * can be warned they may not be in the activity they think they are.
217 */
218 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800219
Craig Mautner5eda9b32013-07-02 11:58:16 -0700220 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
221 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
222 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
223 // Activity in mTranslucentActivityWaiting is notified via
224 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
225 // background activity being drawn then the same call will be made with a true value.
226 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700227 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700228
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700229 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700230 * Set when we know we are going to be calling updateConfiguration()
231 * soon, so want to skip intermediate config checks.
232 */
233 boolean mConfigWillChange;
234
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700235 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800236 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700237 // Current bounds of the stack or null if fullscreen.
238 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800239
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700240 long mLaunchStartTime = 0;
241 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800242
Craig Mautner858d8a62013-04-23 17:08:34 -0700243 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700244
Craig Mautnerc00204b2013-03-05 15:02:14 -0800245 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800246 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800247 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
248 ArrayList<ActivityStack> mStacks;
249 /** The attached Display's unique identifier, or -1 if detached */
250 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800251
Craig Mautner27084302013-03-25 08:05:25 -0700252 /** Run all ActivityStacks through this */
253 final ActivityStackSupervisor mStackSupervisor;
254
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700255 private final LaunchingTaskPositioner mTaskPositioner;
256
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700257 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700258 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
259 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
260 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
261 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700262 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700263 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700264 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700265
266 static class ScheduleDestroyArgs {
267 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700268 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700269 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700270 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700271 mReason = reason;
272 }
273 }
274
Zoran Marcetaf958b322012-08-09 20:27:12 +0900275 final Handler mHandler;
276
277 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800278
Craig Mautnerc8143c62013-09-03 12:15:57 -0700279 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900280 super(looper);
281 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700282
Zoran Marcetaf958b322012-08-09 20:27:12 +0900283 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700284 public void handleMessage(Message msg) {
285 switch (msg.what) {
286 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800287 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700288 // We don't at this point know if the activity is fullscreen,
289 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800290 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700291 synchronized (mService) {
292 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700293 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700294 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700295 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800296 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700297 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700298 case LAUNCH_TICK_MSG: {
299 ActivityRecord r = (ActivityRecord)msg.obj;
300 synchronized (mService) {
301 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700302 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700303 }
304 }
305 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700306 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800307 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700308 // We don't at this point know if the activity is fullscreen,
309 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800310 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800311 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800312 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800313 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700314 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700315 case STOP_TIMEOUT_MSG: {
316 ActivityRecord r = (ActivityRecord)msg.obj;
317 // We don't at this point know if the activity is fullscreen,
318 // so we need to be conservative and assume it isn't.
319 Slog.w(TAG, "Activity stop timeout for " + r);
320 synchronized (mService) {
321 if (r.isInHistory()) {
322 activityStoppedLocked(r, null, null, null);
323 }
324 }
325 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700326 case DESTROY_ACTIVITIES_MSG: {
327 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
328 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700329 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700330 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700331 } break;
332 case TRANSLUCENT_TIMEOUT_MSG: {
333 synchronized (mService) {
334 notifyActivityDrawnLocked(null);
335 }
336 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700337 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700338 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700339 final ActivityRecord r = getVisibleBehindActivity();
340 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700341 if (r != null) {
342 mService.killAppAtUsersRequest(r.app, null);
343 }
344 }
345 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700346 }
347 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800348 }
349
Craig Mautner34b73df2014-01-12 21:11:08 -0800350 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800351 int count = 0;
352 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
353 count += mTaskHistory.get(taskNdx).mActivities.size();
354 }
355 return count;
356 }
357
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800358 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
359 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800360 mActivityContainer = activityContainer;
361 mStackSupervisor = activityContainer.getOuter();
362 mService = mStackSupervisor.mService;
363 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
364 mWindowManager = mService.mWindowManager;
365 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700366 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800367 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700368 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
369 ? new LaunchingTaskPositioner() : null;
370 }
371
372 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
373 mDisplayId = activityDisplay.mDisplayId;
374 mStacks = activityDisplay.mStacks;
375 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
376 mFullscreen = mBounds == null;
377 if (mTaskPositioner != null) {
378 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
379 mTaskPositioner.configure(mBounds);
380 }
381 }
382
383 void detachDisplay() {
384 mDisplayId = Display.INVALID_DISPLAY;
385 mStacks = null;
386 if (mTaskPositioner != null) {
387 mTaskPositioner.reset();
388 }
389 mWindowManager.detachStack(mStackId);
390 }
391
Winsonc809cbb2015-11-02 12:06:15 -0800392 public void getDisplaySize(Point out) {
393 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
394 }
395
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700396 void setBounds(Rect bounds) {
397 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700398 if (mTaskPositioner != null) {
399 mTaskPositioner.configure(bounds);
400 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700401 }
Craig Mautner5962b122012-10-05 14:45:52 -0700402
Amith Yamasani734983f2014-03-04 16:48:05 -0800403 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700404 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700405 }
406
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700407 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800408 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700409 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700410 if (r != null) {
411 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800412 }
413 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700414 return null;
415 }
416
417 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800418 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
419 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800420 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800421 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
422 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800423 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800424 return r;
425 }
426 }
427 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700428 return null;
429 }
430
431 /**
432 * This is a simplified version of topRunningActivityLocked that provides a number of
433 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800434 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700435 * @param token If non-null, any history records matching this token will be skipped.
436 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800437 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700438 * @return Returns the HistoryRecord of the next activity on the stack.
439 */
440 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800441 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
442 TaskRecord task = mTaskHistory.get(taskNdx);
443 if (task.taskId == taskId) {
444 continue;
445 }
446 ArrayList<ActivityRecord> activities = task.mActivities;
447 for (int i = activities.size() - 1; i >= 0; --i) {
448 final ActivityRecord r = activities.get(i);
449 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800450 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800451 return r;
452 }
453 }
454 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700455 return null;
456 }
457
Craig Mautner8849a5e2013-04-02 16:41:03 -0700458 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700459 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
460 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700461 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
462 final ActivityRecord r = activities.get(activityNdx);
463 if (!r.finishing) {
464 return r;
465 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700466 }
467 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700468 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700469 }
470
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700471 final TaskRecord topTask() {
472 final int size = mTaskHistory.size();
473 if (size > 0) {
474 return mTaskHistory.get(size - 1);
475 }
476 return null;
477 }
478
Craig Mautnerd2328952013-03-05 12:46:26 -0800479 TaskRecord taskForIdLocked(int id) {
480 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
481 final TaskRecord task = mTaskHistory.get(taskNdx);
482 if (task.taskId == id) {
483 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800484 }
485 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700486 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700487 }
488
Craig Mautnerd2328952013-03-05 12:46:26 -0800489 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700490 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800491 return isInStackLocked(r);
492 }
493
494 ActivityRecord isInStackLocked(ActivityRecord r) {
495 if (r == null) {
496 return null;
497 }
498 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700499 if (task != null && task.stack != null
500 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800501 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800502 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800503 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800504 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800505 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800506 }
507
Craig Mautner2420ead2013-04-01 17:13:20 -0700508 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700509 final boolean hadit = mLRUActivities.remove(r);
510 mLRUActivities.add(r);
511 return hadit;
512 }
513
Craig Mautnerde4ef022013-04-07 19:01:33 -0700514 final boolean isHomeStack() {
515 return mStackId == HOME_STACK_ID;
516 }
517
Craig Mautnere0a38842013-12-16 16:14:02 -0800518 final boolean isOnHomeDisplay() {
519 return isAttached() &&
520 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
521 }
522
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700523 void moveToFront(String reason) {
524 moveToFront(reason, null);
525 }
526
527 /**
528 * @param reason The reason for moving the stack to the front.
529 * @param task If non-null, the task will be moved to the top of the stack.
530 * */
531 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700532 if (!isAttached()) {
533 return;
534 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700535
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700536 mStacks.remove(this);
537 int addIndex = mStacks.size();
538
539 if (addIndex > 0) {
540 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800541 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
542 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700543 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800544 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700545 }
546
547 mStacks.add(addIndex, this);
548
549 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
550 if (isOnHomeDisplay()) {
551 mStackSupervisor.setFocusStack(reason, this);
552 }
553 if (task != null) {
554 insertTaskAtTop(task, null);
555 } else {
556 task = topTask();
557 }
558 if (task != null) {
559 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800560 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800561 }
562
563 final boolean isAttached() {
564 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800565 }
566
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700567 /**
568 * Returns the top activity in any existing task matching the given
569 * Intent. Returns null if no such task is found.
570 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700571 ActivityRecord findTaskLocked(ActivityRecord target) {
572 Intent intent = target.intent;
573 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700574 ComponentName cls = intent.getComponent();
575 if (info.targetActivity != null) {
576 cls = new ComponentName(info.packageName, info.targetActivity);
577 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700578 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700579 boolean isDocument = intent != null & intent.isDocument();
580 // If documentData is non-null then it must match the existing task data.
581 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800582
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700583 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800584 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
585 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700586 if (task.voiceSession != null) {
587 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700588 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700589 continue;
590 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700591 if (task.userId != userId) {
592 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700593 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700594 continue;
595 }
Craig Mautner000f0022013-02-26 15:04:29 -0800596 final ActivityRecord r = task.getTopActivity();
597 if (r == null || r.finishing || r.userId != userId ||
598 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700599 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800600 continue;
601 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700602 if (r.mActivityType != target.mActivityType) {
603 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
604 continue;
605 }
Craig Mautner000f0022013-02-26 15:04:29 -0800606
Craig Mautnerd00f4742014-03-12 14:17:26 -0700607 final Intent taskIntent = task.intent;
608 final Intent affinityIntent = task.affinityIntent;
609 final boolean taskIsDocument;
610 final Uri taskDocumentData;
611 if (taskIntent != null && taskIntent.isDocument()) {
612 taskIsDocument = true;
613 taskDocumentData = taskIntent.getData();
614 } else if (affinityIntent != null && affinityIntent.isDocument()) {
615 taskIsDocument = true;
616 taskDocumentData = affinityIntent.getData();
617 } else {
618 taskIsDocument = false;
619 taskDocumentData = null;
620 }
621
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700622 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700623 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700624 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700625 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700626 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
627 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700628 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700629 return r;
630 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700631 } else if (taskIntent != null && taskIntent.getComponent() != null &&
632 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700633 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700634 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800635 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700636 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
637 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800638 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700639 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
640 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700641 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700642 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800643 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700644 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
645 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800646 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700647 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700648 }
649
650 return null;
651 }
652
653 /**
654 * Returns the first activity (starting from the top of the stack) that
655 * is the same as the given activity. Returns null if no such activity
656 * is found.
657 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700658 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700659 ComponentName cls = intent.getComponent();
660 if (info.targetActivity != null) {
661 cls = new ComponentName(info.packageName, info.targetActivity);
662 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700663 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700664
Craig Mautner000f0022013-02-26 15:04:29 -0800665 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700666 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700667 final boolean notCurrentUserTask =
668 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700669 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700670
Craig Mautner000f0022013-02-26 15:04:29 -0800671 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
672 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700673 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700674 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700675 }
Craig Mautner000f0022013-02-26 15:04:29 -0800676 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700677 return r;
678 }
679 }
680 }
681
682 return null;
683 }
684
Amith Yamasani742a6712011-05-04 14:49:28 -0700685 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700686 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700687 */
Craig Mautner93529a42013-10-04 15:03:13 -0700688 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800689 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700690 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800691 }
692 mCurrentUser = userId;
693
694 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800695 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800696 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700697 final TaskRecord task = mTaskHistory.get(i);
698
699 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
700 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700701 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700702 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700703 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700704 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800705 mTaskHistory.remove(i);
706 mTaskHistory.add(task);
707 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800708 // Use same value for i.
709 } else {
710 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800711 }
712 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700713 if (VALIDATE_TOKENS) {
714 validateAppTokensLocked();
715 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700716 }
717
Craig Mautner2420ead2013-04-01 17:13:20 -0700718 void minimalResumeActivityLocked(ActivityRecord r) {
719 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700720 if (DEBUG_STATES) Slog.v(TAG_STATES,
721 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700722 r.stopped = false;
723 mResumedActivity = r;
724 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800725 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700726 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700727 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700728 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700729 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
730 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700731 }
732
Chong Zhang45c25ce2015-08-10 22:18:26 -0700733 private void addRecentActivityLocked(ActivityRecord r) {
734 if (r != null) {
735 mRecentTasks.addLocked(r.task);
736 r.task.touchActiveTime();
737 }
738 }
739
Narayan Kamath7829c812015-06-08 17:39:43 +0100740 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700741 if (mFullyDrawnStartTime != 0) {
742 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
743 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100744 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700745 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
746 }
747
748 private void stopFullyDrawnTraceIfNeeded() {
749 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
750 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
751 mFullyDrawnStartTime = 0;
752 }
753 }
754
Craig Mautnere79d42682013-04-01 19:01:53 -0700755 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700756 if (r.displayStartTime == 0) {
757 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
758 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100759 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700760 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700761 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700762 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100763 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700764 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700765 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700766 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800767
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700768 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700769 // Make sure that there is no activity waiting for this to launch.
770 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
771 r.displayStartTime = r.fullyDrawnStartTime = 0;
772 } else {
773 mStackSupervisor.removeTimeoutsForActivityLocked(r);
774 mStackSupervisor.scheduleIdleTimeoutLocked(r);
775 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700776 }
777
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800778 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800779 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800780 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
781 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
782 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
783 activities.get(activityNdx).setSleeping(false);
784 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800785 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800786 if (mPausingActivity != null) {
787 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
788 activityPausedLocked(mPausingActivity.appToken, true);
789 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800790 }
791
Craig Mautner0eea92c2013-05-16 13:35:39 -0700792 /**
793 * @return true if something must be done before going to sleep.
794 */
795 boolean checkReadyForSleepLocked() {
796 if (mResumedActivity != null) {
797 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700798 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
799 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
800 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700801 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700802 return true;
803 }
804 if (mPausingActivity != null) {
805 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700806 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700807 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800808 }
809
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700810 if (hasVisibleBehindActivity()) {
811 // Stop visible behind activity before going to sleep.
812 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
813 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700814 if (DEBUG_STATES) Slog.v(TAG_STATES,
815 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700816 return true;
817 }
818
Craig Mautner0eea92c2013-05-16 13:35:39 -0700819 return false;
820 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800821
Craig Mautner0eea92c2013-05-16 13:35:39 -0700822 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700823 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800824
Craig Mautner0eea92c2013-05-16 13:35:39 -0700825 // Make sure any stopped but visible activities are now sleeping.
826 // This ensures that the activity's onStop() is called.
827 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
828 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
829 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
830 final ActivityRecord r = activities.get(activityNdx);
831 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
832 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800833 }
834 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800835 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700836 }
Craig Mautner59c00972012-07-30 12:10:24 -0700837
Winson8b1871d2015-11-20 09:56:20 -0800838 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
839 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800840 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700841 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800842 return null;
843 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800844
Winson Chung083baf92014-07-11 10:32:42 -0700845 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700846 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800847 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700848 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700849 return null;
850 }
851
Winson Chung48a10a52014-08-27 14:36:51 -0700852 int w = mService.mThumbnailWidth;
853 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800854 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800855 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700856 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800857
858 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800859 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
860 // use within SystemUI while keeping memory usage low.
Winson8b1871d2015-11-20 09:56:20 -0800861 if (mService.mTakeFullscreenScreenshots) {
Winson8b1871d2015-11-20 09:56:20 -0800862 w = h = -1;
Winson387aac62015-11-25 11:18:56 -0800863 scale = 0.5f;
Winson8b1871d2015-11-20 09:56:20 -0800864 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800865 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800866 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800867 }
Winson Chung376543b2014-05-21 17:43:28 -0700868 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800869 return null;
870 }
871
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700872 /**
873 * Start pausing the currently resumed activity. It is an error to call this if there
874 * is already an activity being paused or there is no resumed activity.
875 *
876 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
877 * @param uiSleeping True if this is happening with the user interface going to sleep (the
878 * screen turning off).
879 * @param resuming True if this is being called as part of resuming the top activity, so
880 * we shouldn't try to instigate a resume here.
881 * @param dontWait True if the caller does not want to wait for the pause to complete. If
882 * set to true, we will immediately complete the pause here before returning.
883 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
884 * it to tell us when it is done.
885 */
886 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
887 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800888 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800889 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
890 + " state=" + mPausingActivity.state);
891 if (!mService.isSleeping()) {
892 // Avoid recursion among check for sleep and complete pause during sleeping.
893 // Because activity will be paused immediately after resume, just let pause
894 // be completed by the order of activity paused from clients.
895 completePauseLocked(false);
896 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800897 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700898 ActivityRecord prev = mResumedActivity;
899 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700900 if (!resuming) {
901 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
902 mStackSupervisor.resumeTopActivitiesLocked();
903 }
904 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700905 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800906
907 if (mActivityContainer.mParentActivity == null) {
908 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700909 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800910 }
911
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700912 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700913 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700914 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800915 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700916 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700917 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
918 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700919 prev.state = ActivityState.PAUSING;
920 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700921 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700922 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700923 if (mService.mHasRecents
924 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Winson8b1871d2015-11-20 09:56:20 -0800925 prev.updateThumbnailLocked(screenshotActivitiesLocked(prev), null);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700926 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700927 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700928
929 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800930
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700931 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700932 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700933 try {
934 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700935 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700936 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700937 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800938 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700939 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700940 } catch (Exception e) {
941 // Ignore exception, if process died other code will cleanup.
942 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800943 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700944 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700945 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700946 }
947 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800948 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700949 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700950 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700951 }
952
953 // If we are not going to sleep, we want to ensure the device is
954 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700955 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700956 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700957 }
958
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800959 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700960 // Have the window manager pause its key dispatching until the new
961 // activity has started. If we're pausing the activity just because
962 // the screen is being turned off and the UI is sleeping, don't interrupt
963 // key dispatch; the same activity will pick it up again on wakeup.
964 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800965 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700966 } else if (DEBUG_PAUSE) {
967 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700968 }
969
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700970 if (dontWait) {
971 // If the caller said they don't want to wait for the pause, then complete
972 // the pause now.
973 completePauseLocked(false);
974 return false;
975
976 } else {
977 // Schedule a pause timeout in case the app doesn't respond.
978 // We don't give it much time because this directly impacts the
979 // responsiveness seen by the user.
980 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
981 msg.obj = prev;
982 prev.pauseTime = SystemClock.uptimeMillis();
983 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700984 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700985 return true;
986 }
987
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700988 } else {
989 // This activity failed to schedule the
990 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700991 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700992 if (!resuming) {
993 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
994 }
995 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700996 }
997 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -0700998
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700999 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001000 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1001 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001002
Craig Mautnerd2328952013-03-05 12:46:26 -08001003 final ActivityRecord r = isInStackLocked(token);
1004 if (r != null) {
1005 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1006 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001007 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001008 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001009 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -08001010 } else {
1011 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1012 r.userId, System.identityHashCode(r), r.shortComponentName,
1013 mPausingActivity != null
1014 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001015 if (r.state == ActivityState.PAUSING) {
1016 r.state = ActivityState.PAUSED;
1017 if (r.finishing) {
1018 if (DEBUG_PAUSE) Slog.v(TAG,
1019 "Executing finish of failed to pause activity: " + r);
1020 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1021 }
louis_chang047dfd42015-04-08 16:35:55 +08001022 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001023 }
1024 }
1025 }
1026
Craig Mautnera0026042014-04-23 11:45:37 -07001027 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1028 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001029 if (r.state != ActivityState.STOPPING) {
1030 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1031 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1032 return;
1033 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001034 if (persistentState != null) {
1035 r.persistentState = persistentState;
1036 mService.notifyTaskPersisterLocked(r.task, false);
1037 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001038 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001039 if (icicle != null) {
1040 // If icicle is null, this is happening due to a timeout, so we
1041 // haven't really saved the state.
1042 r.icicle = icicle;
1043 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001044 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001045 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001046 }
1047 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001048 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001049 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1050 r.stopped = true;
1051 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -07001052 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
1053 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001054 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001055 if (r.finishing) {
1056 r.clearOptionsLocked();
1057 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001058 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001059 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -07001060 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001061 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001062 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001063 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001064 }
1065 }
1066 }
1067
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001068 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001069 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001070 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001071
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001072 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001073 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001074 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001075 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001076 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001077 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001078 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001079 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001080 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001081 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1082 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001083 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001084 if (prev.configDestroy) {
1085 // The previous is being paused because the configuration
1086 // is changing, which means it is actually stopping...
1087 // To juggle the fact that we are also starting a new
1088 // instance right now, we need to first completely stop
1089 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001090 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001091 destroyActivityLocked(prev, true, "pause-config");
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001092 } else if (wasStopping) {
1093 // We are also stopping, the stop request must have gone soon after the pause.
1094 // We can't clobber it, because the stop confirmation will not be handled.
1095 // We don't need to schedule another stop, we only need to let it happen.
1096 prev.state = ActivityState.STOPPING;
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001097 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001098 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001099 // stopping.
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001100
Craig Mautnerde4ef022013-04-07 19:01:33 -07001101 mStackSupervisor.mStoppingActivities.add(prev);
Craig Mautner29219d92013-04-16 20:19:12 -07001102 if (mStackSupervisor.mStoppingActivities.size() > 3 ||
1103 prev.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001104 // If we already have a few activities waiting to stop,
1105 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07001106 // them out. Or if r is the last of activity of the last task the stack
1107 // will be empty and must be cleared immediately.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001108 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07001109 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001110 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07001111 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001112 }
1113 }
1114 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001115 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001116 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001117 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001118 // It is possible the activity was freezing the screen before it was paused.
1119 // In that case go ahead and remove the freeze this activity has on the screen
1120 // since it is no longer visible.
1121 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001122 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001123 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001124
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001125 if (resumeNext) {
1126 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1127 if (!mService.isSleepingOrShuttingDown()) {
1128 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1129 } else {
1130 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001131 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001132 if (top == null || (prev != null && top != prev)) {
1133 // If there are no more activities available to run,
1134 // do resume anyway to start something. Also if the top
1135 // activity on the stack is not the just paused activity,
1136 // we need to go ahead and resume it to ensure we complete
1137 // an in-flight app switch.
1138 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1139 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001140 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001141 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001142
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001143 if (prev != null) {
1144 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001145
Craig Mautner525f3d92013-05-07 14:01:50 -07001146 if (prev.app != null && prev.cpuTimeAtResume > 0
1147 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001148 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1149 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001150 if (diff > 0) {
1151 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1152 synchronized (bsi) {
1153 BatteryStatsImpl.Uid.Proc ps =
1154 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001155 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001156 if (ps != null) {
1157 ps.addForegroundTimeLocked(diff);
1158 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001159 }
1160 }
1161 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001162 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001163 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001164
1165 // Notfiy when the task stack has changed
1166 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001167 }
1168
1169 /**
1170 * Once we know that we have asked an application to put an activity in
1171 * the resumed state (either by launching it or explicitly telling it),
1172 * this function updates the rest of our state to match that fact.
1173 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001174 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001175 next.idle = false;
1176 next.results = null;
1177 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001178
Chong Zhang85ee6542015-10-02 13:36:38 -07001179 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001180 ProcessRecord app = next.task.mActivities.get(0).app;
1181 if (app != null && app != mService.mHomeProcess) {
1182 mService.mHomeProcess = app;
1183 }
1184 }
1185
Craig Mautner07566322013-09-26 16:42:55 -07001186 if (next.nowVisible) {
1187 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001188 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001189 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001190
1191 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001192 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001193
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001194 mStackSupervisor.reportResumedActivityLocked(next);
1195
1196 next.resumeKeyDispatchingLocked();
1197 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001198
1199 // Mark the point when the activity is resuming
1200 // TODO: To be more accurate, the mark should be before the onCreate,
1201 // not after the onResume. But for subsequent starts, onResume is fine.
1202 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001203 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001204 } else {
1205 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1206 }
Winson Chung376543b2014-05-21 17:43:28 -07001207
George Mount6ba042b2014-07-28 11:12:28 -07001208 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001209
1210 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1211 // When resuming an activity, require it to call requestVisibleBehind() again.
1212 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1213 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001214 }
1215
Craig Mautner2568c3a2015-03-26 14:22:34 -07001216 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001217 r.visible = visible;
1218 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001219 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001220 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001221 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001222 container.setVisible(visible);
1223 }
1224 }
1225
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001226 // Find the first visible activity above the passed activity and if it is translucent return it
1227 // otherwise return null;
1228 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1229 TaskRecord task = r.task;
1230 if (task == null) {
1231 return null;
1232 }
1233
1234 ActivityStack stack = task.stack;
1235 if (stack == null) {
1236 return null;
1237 }
1238
1239 int stackNdx = mStacks.indexOf(stack);
1240
1241 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1242 int taskNdx = tasks.indexOf(task);
1243
1244 ArrayList<ActivityRecord> activities = task.mActivities;
1245 int activityNdx = activities.indexOf(r) + 1;
1246
1247 final int numStacks = mStacks.size();
1248 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001249 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001250 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001251 final int numTasks = tasks.size();
1252 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001253 final TaskRecord currentTask = tasks.get(taskNdx);
1254 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001255 final int numActivities = activities.size();
1256 while (activityNdx < numActivities) {
1257 final ActivityRecord activity = activities.get(activityNdx);
1258 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001259 return historyStack.mFullscreen
1260 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001261 }
1262 ++activityNdx;
1263 }
1264 activityNdx = 0;
1265 ++taskNdx;
1266 }
1267 taskNdx = 0;
1268 ++stackNdx;
1269 }
1270
1271 return null;
1272 }
1273
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001274 private ActivityStack getNextVisibleStackLocked() {
1275 ArrayList<ActivityStack> stacks = mStacks;
1276 final ActivityRecord parent = mActivityContainer.mParentActivity;
1277 if (parent != null) {
1278 stacks = parent.task.stack.mStacks;
1279 }
1280 if (stacks != null) {
1281 for (int i = stacks.size() - 1; i >= 0; --i) {
1282 ActivityStack stack = stacks.get(i);
1283 if (stack != this && stack.isStackVisibleLocked()) {
1284 return stack;
1285 }
1286 }
1287 }
1288 return null;
1289 }
1290
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001291 /** Returns true if the stack contains a fullscreen task. */
1292 private boolean hasFullscreenTask() {
1293 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1294 final TaskRecord task = mTaskHistory.get(i);
1295 if (task.mFullscreen) {
1296 return true;
1297 }
1298 }
1299 return false;
1300 }
1301
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001302 private boolean hasTranslucentActivity(ActivityStack stack) {
1303 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1304 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1305 final TaskRecord task = tasks.get(taskNdx);
1306 final ArrayList<ActivityRecord> activities = task.mActivities;
1307 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1308 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001309
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001310 // Conditions for an activity to obscure the stack we're
1311 // examining:
1312 // 1. Not Finishing AND Visible AND:
1313 // 2. Either:
1314 // - Full Screen Activity OR
1315 // - On top of Home and our stack is NOT home
1316 if (!r.finishing && r.visible && (r.fullscreen ||
1317 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1318 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001319 }
1320 }
1321 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001322 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001323 }
1324
1325 /** Returns true if the stack is considered visible. */
Todd Kennedyaab56db2015-01-30 09:39:53 -08001326 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001327 if (!isAttached()) {
1328 return false;
1329 }
1330
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001331 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001332 return true;
1333 }
1334
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001335 final int stackIndex = mStacks.indexOf(this);
1336
1337 if (stackIndex == mStacks.size() - 1) {
1338 Slog.wtf(TAG,
1339 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1340 return false;
1341 }
1342
Chong Zhang75b37202015-12-04 14:16:36 -08001343 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1344 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
1345 return false;
1346 }
1347
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001348 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1349 final int focusedStackId = focusedStack.mStackId;
1350
1351 if (mStackId == DOCKED_STACK_ID) {
1352 // Docked stack is always visible, except in the case where the home activity
1353 // is the top running activity in the focused home stack.
1354 if (focusedStackId != HOME_STACK_ID) {
1355 return true;
1356 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001357 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked();
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001358 return topHomeActivity == null || !topHomeActivity.isHomeActivity();
1359 }
1360
Chong Zhangb16cf342015-11-12 17:22:40 -08001361 // Find the first stack below focused stack that actually got something visible.
1362 int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1363 while (belowFocusedIndex >= 0 &&
1364 mStacks.get(belowFocusedIndex).topRunningActivityLocked() == null) {
1365 belowFocusedIndex--;
1366 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001367 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1368 && stackIndex == belowFocusedIndex) {
1369 // Stacks directly behind the docked or pinned stack are always visible.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001370 return true;
1371 }
1372
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001373 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1374 && hasTranslucentActivity(focusedStack)) {
1375 // Stacks behind the fullscreen stack with a translucent activity are always
1376 // visible so they can act as a backdrop to the translucent activity.
1377 // For example, dialog activities
1378 if (stackIndex == belowFocusedIndex) {
1379 return true;
1380 }
1381 if (belowFocusedIndex >= 0) {
1382 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001383 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1384 && stackIndex == (belowFocusedIndex - 1)) {
1385 // The stack behind the docked or pinned stack is also visible so we can have a
1386 // complete backdrop to the translucent activity when the docked stack is up.
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001387 return true;
1388 }
1389 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001390 }
1391
Wale Ogunwale3797c222015-10-27 14:21:58 -07001392 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001393 // Visibility of any static stack should have been determined by the conditions above.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001394 return false;
1395 }
1396
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001397 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001398 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001399
1400 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1401 continue;
1402 }
1403
Wale Ogunwale3797c222015-10-27 14:21:58 -07001404 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001405 // These stacks can't have any dynamic stacks visible behind them.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001406 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001407 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001408
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001409 if (!hasTranslucentActivity(stack)) {
1410 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001411 }
1412 }
1413
1414 return true;
1415 }
1416
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001417 final int rankTaskLayers(int baseLayer) {
1418 int layer = 0;
1419 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1420 final TaskRecord task = mTaskHistory.get(taskNdx);
1421 ActivityRecord r = task.topRunningActivityLocked();
1422 if (r == null || r.finishing || !r.visible) {
1423 task.mLayerRank = -1;
1424 } else {
1425 task.mLayerRank = baseLayer + layer++;
1426 }
1427 }
1428 return layer;
1429 }
1430
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001431 /**
1432 * Make sure that all activities that need to be visible (that is, they
1433 * currently can be seen by the user) actually are.
1434 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001435 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1436 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001437 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001438 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001439 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001440 if (top != null) {
1441 checkTranslucentActivityWaiting(top);
1442 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001443
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001444 // If the top activity is not fullscreen, then we need to
1445 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001446 boolean aboveTop = top != null;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001447 final boolean stackInvisible = !isStackVisibleLocked();
1448 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001449 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001450
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001451 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001452 final TaskRecord task = mTaskHistory.get(taskNdx);
1453 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001454
Craig Mautnerd44711d2013-02-23 11:24:36 -08001455 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1456 final ActivityRecord r = activities.get(activityNdx);
1457 if (r.finishing) {
1458 continue;
1459 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001460 final boolean isTop = r == top;
1461 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001462 continue;
1463 }
1464 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001465 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1466 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001467 if ((!behindFullscreenActivity || r.mLaunchTaskBehind)
1468 && okToShowLocked(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001469 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1470 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001471 + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001472 // First: if this is not the current activity being started, make
1473 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001474 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001475 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001476 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001477
1478 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001479 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001480 noStackActivityResumed, r)) {
1481 if (activityNdx >= activities.size()) {
1482 // Record may be removed if its process needs to restart.
1483 activityNdx = activities.size() - 1;
1484 } else {
1485 noStackActivityResumed = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001486 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001487 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001488 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001489 // If this activity is already visible, then there is nothing to do here.
1490 if (handleAlreadyVisible(r)) {
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001491 noStackActivityResumed = false;
1492 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001493 } else {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001494 makeVisible(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001495 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001496 // Aggregate current change flags.
1497 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001498 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1499 behindFullscreenActivity, task, r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001500 } else {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001501 makeInvisible(stackInvisible, behindFullscreenActivity, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001502 }
1503 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001504 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1505 // The visibility of tasks and the activities they contain in freeform stack are
1506 // determined individually unlike other stacks where the visibility or fullscreen
1507 // status of an activity in a previous task affects other.
1508 behindFullscreenActivity = stackInvisible;
1509 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001510 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001511
1512 if (mTranslucentActivityWaiting != null &&
1513 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1514 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1515 notifyActivityDrawnLocked(null);
1516 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001517 }
Craig Mautner58547802013-03-05 08:23:53 -08001518
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001519 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1520 if (mTranslucentActivityWaiting != top) {
1521 mUndrawnActivitiesBelowTopTranslucent.clear();
1522 if (mTranslucentActivityWaiting != null) {
1523 // Call the callback with a timeout indication.
1524 notifyActivityDrawnLocked(null);
1525 mTranslucentActivityWaiting = null;
1526 }
1527 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1528 }
1529 }
1530
1531 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001532 boolean isTop, boolean noStackActivityResumed, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001533 // We need to make sure the app is running if it's the top, or it is just made visible from
1534 // invisible. If the app is already visible, it must have died while it was visible. In this
1535 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1536 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001537 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001538 // This activity needs to be visible, but isn't even running...
1539 // get it started and resume if no other stack in this stack is resumed.
1540 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1541 if (r != starting) {
1542 r.startFreezingScreenLocked(r.app, configChanges);
1543 }
1544 if (!r.visible || r.mLaunchTaskBehind) {
1545 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1546 setVisible(r, true);
1547 }
1548 if (r != starting) {
1549 mStackSupervisor.startSpecificActivityLocked(r, noStackActivityResumed, false);
1550 return true;
1551 }
1552 }
1553 return false;
1554 }
1555
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001556 private void makeInvisible(boolean stackInvisible, boolean behindFullscreenActivity,
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001557 ActivityRecord r) {
1558 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r + " finishing="
1559 + r.finishing + " state=" + r.state + " stackInvisible=" + stackInvisible
1560 + " behindFullscreenActivity=" + behindFullscreenActivity);
1561 if (!r.visible) {
1562 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1563 return;
1564 }
1565 // Now for any activities that aren't visible to the user, make sure they no longer are
1566 // keeping the screen frozen.
1567 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
1568 try {
1569 setVisible(r, false);
1570 switch (r.state) {
1571 case STOPPING:
1572 case STOPPED:
1573 if (r.app != null && r.app.thread != null) {
1574 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1575 "Scheduling invisibility: " + r);
1576 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1577 }
1578 break;
1579
1580 case INITIALIZING:
1581 case RESUMED:
1582 case PAUSING:
1583 case PAUSED:
1584 // This case created for transitioning activities from
1585 // translucent to opaque {@link Activity#convertToOpaque}.
1586 if (getVisibleBehindActivity() == r) {
1587 releaseBackgroundResources(r);
1588 } else {
1589 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1590 mStackSupervisor.mStoppingActivities.add(r);
1591 }
1592 mStackSupervisor.scheduleIdleLocked();
1593 }
1594 break;
1595
1596 default:
1597 break;
1598 }
1599 } catch (Exception e) {
1600 // Just skip on any failure; we'll make it visible when it next restarts.
1601 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1602 }
1603 }
1604
1605 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1606 TaskRecord task, ActivityRecord r) {
1607 if (r.fullscreen) {
1608 // At this point, nothing else needs to be shown in this task.
1609 behindFullscreenActivity = true;
1610 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
1611 + " stackInvisible=" + stackInvisible
1612 + " behindFullscreenActivity=" + behindFullscreenActivity);
1613 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
1614 behindFullscreenActivity = true;
1615 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1616 + " stackInvisible=" + stackInvisible
1617 + " behindFullscreenActivity=" + behindFullscreenActivity);
1618 }
1619 return behindFullscreenActivity;
1620 }
1621
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001622 private void makeVisible(ActivityRecord starting, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001623 // This activity is not currently visible, but is running. Tell it to become visible.
1624 r.visible = true;
1625 if (r.state != ActivityState.RESUMED && r != starting) {
1626 // If this activity is paused, tell it to now show its window.
1627 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1628 "Making visible and scheduling visibility: " + r);
1629 try {
1630 if (mTranslucentActivityWaiting != null) {
1631 r.updateOptionsLocked(r.returningOptions);
1632 mUndrawnActivitiesBelowTopTranslucent.add(r);
1633 }
1634 setVisible(r, true);
1635 r.sleeping = false;
1636 r.app.pendingUiClean = true;
1637 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1638 r.stopFreezingScreenLocked(false);
1639 } catch (Exception e) {
1640 // Just skip on any failure; we'll make it
1641 // visible when it next restarts.
1642 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
1643 }
1644 }
1645 }
1646
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001647 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001648 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1649 r.stopFreezingScreenLocked(false);
1650 try {
1651 if (r.returningOptions != null) {
1652 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1653 }
1654 } catch(RemoteException e) {
1655 }
1656 return r.state == ActivityState.RESUMED;
1657 }
1658
Todd Kennedyaab56db2015-01-30 09:39:53 -08001659 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001660 mTranslucentActivityWaiting = r;
1661 mUndrawnActivitiesBelowTopTranslucent.clear();
1662 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1663 }
1664
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001665 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1666 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1667 final TaskRecord task = mTaskHistory.get(taskNdx);
1668 final ArrayList<ActivityRecord> activities = task.mActivities;
1669 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1670 final ActivityRecord r = activities.get(activityNdx);
1671 if ( r.appTimeTracker != except) {
1672 r.appTimeTracker = null;
1673 }
1674 }
1675 }
1676 }
1677
Craig Mautner5eda9b32013-07-02 11:58:16 -07001678 /**
1679 * Called as activities below the top translucent activity are redrawn. When the last one is
1680 * redrawn notify the top activity by calling
1681 * {@link Activity#onTranslucentConversionComplete}.
1682 *
1683 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1684 * occurred and the activity will be notified immediately.
1685 */
1686 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001687 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001688 if ((r == null)
1689 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1690 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1691 // The last undrawn activity below the top has just been drawn. If there is an
1692 // opaque activity at the top, notify it that it can become translucent safely now.
1693 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1694 mTranslucentActivityWaiting = null;
1695 mUndrawnActivitiesBelowTopTranslucent.clear();
1696 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1697
Craig Mautner71dd1b62014-02-18 15:48:52 -08001698 if (waitingActivity != null) {
1699 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1700 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1701 try {
1702 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1703 waitingActivity.appToken, r != null);
1704 } catch (RemoteException e) {
1705 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001706 }
1707 }
1708 }
1709 }
1710
Craig Mautnera61bc652013-10-28 15:43:18 -07001711 /** If any activities below the top running one are in the INITIALIZING state and they have a
1712 * starting window displayed then remove that starting window. It is possible that the activity
1713 * in this state will never resumed in which case that starting window will be orphaned. */
1714 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001715 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001716 boolean aboveTop = true;
1717 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1718 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1719 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1720 final ActivityRecord r = activities.get(activityNdx);
1721 if (aboveTop) {
1722 if (r == topActivity) {
1723 aboveTop = false;
1724 }
1725 continue;
1726 }
1727
1728 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001729 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1730 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001731 r.mStartingWindowShown = false;
1732 mWindowManager.removeAppStartingWindow(r.appToken);
1733 }
1734 }
1735 }
1736 }
1737
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001738 /**
1739 * Ensure that the top activity in the stack is resumed.
1740 *
1741 * @param prev The previously resumed activity, for when in the process
1742 * of pausing; can be null to call from elsewhere.
1743 *
1744 * @return Returns true if something is being resumed, or false if
1745 * nothing happened.
1746 */
1747 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001748 return resumeTopActivityLocked(prev, null);
1749 }
1750
Chong Zhang280d3322015-11-03 17:27:26 -08001751 final boolean resumeTopActivityLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001752 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001753 // Don't even start recursing.
1754 return false;
1755 }
1756
1757 boolean result = false;
1758 try {
1759 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001760 mStackSupervisor.inResumeTopActivity = true;
1761 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1762 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001763 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001764 }
Craig Mautner544efa72014-09-04 16:41:20 -07001765 result = resumeTopActivityInnerLocked(prev, options);
1766 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001767 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001768 }
1769 return result;
1770 }
1771
Chong Zhang280d3322015-11-03 17:27:26 -08001772 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001773 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001774
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001775 if (!mService.mBooting && !mService.mBooted) {
1776 // Not ready yet!
1777 return false;
1778 }
1779
Craig Mautnerdf88d732014-01-27 09:21:32 -08001780 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001781 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001782 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001783 // Do not resume this stack if its parent is not resumed.
1784 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1785 return false;
1786 }
1787
Craig Mautnera61bc652013-10-28 15:43:18 -07001788 cancelInitializingActivities();
1789
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001790 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001791 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001792
1793 // Remember how we'll process this pause/resume situation, and ensure
1794 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001795 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1796 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001797
Craig Mautner84984fa2014-06-19 11:19:20 -07001798 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001799 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001800 // There are no more activities!
1801 final String reason = "noMoreActivities";
1802 if (!mFullscreen) {
1803 // Try to move focus to the next visible stack with a running activity if this
1804 // stack is not covering the entire screen.
1805 final ActivityStack stack = getNextVisibleStackLocked();
1806 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1807 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1808 }
1809 }
1810 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001811 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001812 if (DEBUG_STATES) Slog.d(TAG_STATES,
1813 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001814 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001815 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001816 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1817 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1818 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001819 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001820 }
1821
1822 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001823
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001824 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001825 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1826 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001827 // Make sure we have executed any pending transitions, since there
1828 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001829 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001830 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001831 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001832 if (DEBUG_STATES) Slog.d(TAG_STATES,
1833 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001834 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835 return false;
1836 }
1837
Craig Mautner525f3d92013-05-07 14:01:50 -07001838 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001839 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001840 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001841 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001842 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001843 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001844 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001845 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001846 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001847 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001848 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001849 } else if (!isOnHomeDisplay()) {
1850 return false;
1851 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001852 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001853 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001854 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1855 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1856 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001857 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001858 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001859 }
1860
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001861 // If we are sleeping, and there is no resumed activity, and the top
1862 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001863 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001864 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001865 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001866 // Make sure we have executed any pending transitions, since there
1867 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001868 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001869 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001870 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001871 if (DEBUG_STATES) Slog.d(TAG_STATES,
1872 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001873 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001874 return false;
1875 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001876
1877 // Make sure that the user who owns this activity is started. If not,
1878 // we will just leave it as is because someone should be bringing
1879 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001880 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001881 Slog.w(TAG, "Skipping resume of top activity " + next
1882 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001883 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001884 return false;
1885 }
1886
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001887 // The activity may be waiting for stop, but that is no longer
1888 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001889 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001890 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001891 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001892 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001893
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001894 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001895
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001896 // If we are currently pausing an activity, then don't do anything
1897 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001898 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001899 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001900 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001901 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001902 return false;
1903 }
1904
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001905 // Okay we are now going to start a switch, to 'next'. We may first
1906 // have to pause the current activity, but this is an important point
1907 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001908 // XXX "App Redirected" dialog is getting too many false positives
1909 // at this point, so turn off for now.
1910 if (false) {
1911 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1912 long now = SystemClock.uptimeMillis();
1913 final boolean inTime = mLastStartedActivity.startTime != 0
1914 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1915 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1916 final int nextUid = next.info.applicationInfo.uid;
1917 if (inTime && lastUid != nextUid
1918 && lastUid != next.launchedFromUid
1919 && mService.checkPermission(
1920 android.Manifest.permission.STOP_APP_SWITCHES,
1921 -1, next.launchedFromUid)
1922 != PackageManager.PERMISSION_GRANTED) {
1923 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1924 } else {
1925 next.startTime = now;
1926 mLastStartedActivity = next;
1927 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001928 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001929 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001930 mLastStartedActivity = next;
1931 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001932 }
Craig Mautner58547802013-03-05 08:23:53 -08001933
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001934 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1935
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001936 // We need to start pausing the current activity so the top one
1937 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001938 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1939 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001940 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001941 if (DEBUG_STATES) Slog.d(TAG_STATES,
1942 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001943 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001944 }
1945 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001946 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001947 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001948 // At this point we want to put the upcoming activity's process
1949 // at the top of the LRU list, since we know we will be needing it
1950 // very soon and it would be a waste to let it get killed if it
1951 // happens to be sitting towards the end.
1952 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001953 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001954 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001955 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001956 return true;
1957 }
1958
Christopher Tated3f175c2012-06-14 14:16:54 -07001959 // If the most recent activity was noHistory but was only stopped rather
1960 // than stopped+finished because the device went to sleep, we need to make
1961 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001962 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001963 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001964 if (DEBUG_STATES) Slog.d(TAG_STATES,
1965 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001966 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001967 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001968 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001969 }
1970
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001971 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001972 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1973 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001974 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001975 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1976 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001977 } else {
1978 // The next activity is already visible, so hide the previous
1979 // activity's windows right now so we can show the new one ASAP.
1980 // We only do this if the previous is finishing, which should mean
1981 // it is on top of the one being resumed so hiding it quickly
1982 // is good. Otherwise, we want to do the normal route of allowing
1983 // the resumed activity to be shown so we can decide if the
1984 // previous should actually be hidden depending on whether the
1985 // new one is found to be full-screen or not.
1986 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001987 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001988 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1989 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08001990 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001991 + ", nowVisible=" + next.nowVisible);
1992 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001993 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08001994 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001995 + ", waitingVisible="
1996 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1997 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001998 }
1999 }
2000 }
2001
Dianne Hackborne7f97212011-02-24 14:40:20 -08002002 // Launching this app's activity, make sure the app is no longer
2003 // considered stopped.
2004 try {
2005 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002006 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002007 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002008 } catch (IllegalArgumentException e) {
2009 Slog.w(TAG, "Failed trying to unstop package "
2010 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002011 }
2012
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002013 // We are starting up the next activity, so tell the window manager
2014 // that the previous one will be hidden soon. This way it can know
2015 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002016 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002017 if (prev != null) {
2018 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002019 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002020 "Prepare close transition: prev=" + prev);
2021 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002022 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002023 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002024 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002025 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002026 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2027 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002028 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002029 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002030 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002031 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2032 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002033 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002034 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002035 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002036 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002037 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002038 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002039 : next.mLaunchTaskBehind
2040 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2041 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002042 }
2043 }
Craig Mautner967212c2013-04-13 21:10:58 -07002044 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002045 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002046 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002047 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002048 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002049 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002050 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002051 }
2052 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002053
2054 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002055 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002056 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2057 if (opts != null) {
2058 resumeAnimOptions = opts.toBundle();
2059 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002060 next.applyOptionsLocked();
2061 } else {
2062 next.clearOptionsLocked();
2063 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002064
Craig Mautnercf910b02013-04-23 11:23:27 -07002065 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002066 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002067 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002068
2069 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002070 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002071
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002072 // schedule launch ticks to collect information about slow apps.
2073 next.startLaunchTickingLocked();
2074
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002075 ActivityRecord lastResumedActivity =
2076 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002077 ActivityState lastState = next.state;
2078
2079 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002080
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002081 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002082 next.state = ActivityState.RESUMED;
2083 mResumedActivity = next;
2084 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002085 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002086 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002087 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002088 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002089
2090 // Have the window manager re-evaluate the orientation of
2091 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002092 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002093 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002094 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002095 mService.mConfiguration,
2096 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2097 if (config != null) {
2098 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002099 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002100 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002101 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002102
Craig Mautner525f3d92013-05-07 14:01:50 -07002103 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 // The configuration update wasn't able to keep the existing
2105 // instance of the activity, and instead started a new one.
2106 // We should be all done, but let's just make sure our activity
2107 // is still at the top and schedule another run if something
2108 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002109 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002110 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002111 "Activity config changed during resume: " + next
2112 + ", new next: " + nextNext);
2113 if (nextNext != next) {
2114 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002115 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002116 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002117 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002118 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002119 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002120 return true;
2121 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002122 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002123 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002124 }
Craig Mautner58547802013-03-05 08:23:53 -08002125
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002126 try {
2127 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002128 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002129 if (a != null) {
2130 final int N = a.size();
2131 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002132 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2133 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002134 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002135 }
2136 }
2137
2138 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002139 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002140 }
2141
Craig Mautner299f9602015-01-26 09:47:33 -08002142 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2143 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002144
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002145 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002146 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002147 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002148 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002149 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002150 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002151 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002152
Craig Mautner0eea92c2013-05-16 13:35:39 -07002153 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002154
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002155 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002156 } catch (Exception e) {
2157 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002158 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002159 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002160 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002161 if (lastStack != null) {
2162 lastStack.mResumedActivity = lastResumedActivity;
2163 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002164 Slog.i(TAG, "Restarting because process died: " + next);
2165 if (!next.hasBeenLaunched) {
2166 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002167 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2168 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002169 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002170 next.appToken, next.packageName, next.theme,
2171 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002172 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2173 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002174 }
George Mount2c92c972014-03-20 09:38:23 -07002175 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002176 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002177 return true;
2178 }
2179
2180 // From this point on, if something goes wrong there is no way
2181 // to recover the activity.
2182 try {
2183 next.visible = true;
2184 completeResumeLocked(next);
2185 } catch (Exception e) {
2186 // If any exception gets thrown, toss away this
2187 // activity and try the next one.
2188 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002189 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002190 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002191 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002192 return true;
2193 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002194 next.stopped = false;
2195
2196 } else {
2197 // Whoops, need to restart this activity!
2198 if (!next.hasBeenLaunched) {
2199 next.hasBeenLaunched = true;
2200 } else {
2201 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002202 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002203 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002204 mService.compatibilityInfoForPackageLocked(
2205 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002206 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002207 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002208 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002209 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002210 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002211 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002212 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002213 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002214 }
2215
Craig Mautnercf910b02013-04-23 11:23:27 -07002216 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002217 return true;
2218 }
2219
riddle_hsuc215a4f2014-12-27 12:10:45 +08002220 private TaskRecord getNextTask(TaskRecord targetTask) {
2221 final int index = mTaskHistory.indexOf(targetTask);
2222 if (index >= 0) {
2223 final int numTasks = mTaskHistory.size();
2224 for (int i = index + 1; i < numTasks; ++i) {
2225 TaskRecord task = mTaskHistory.get(i);
2226 if (task.userId == targetTask.userId) {
2227 return task;
2228 }
2229 }
2230 }
2231 return null;
2232 }
2233
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002234 private void insertTaskAtPosition(TaskRecord task, int position) {
2235 if (position >= mTaskHistory.size()) {
2236 insertTaskAtTop(task, null);
2237 return;
2238 }
2239 // Calculate maximum possible position for this task.
2240 int maxPosition = mTaskHistory.size();
2241 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002242 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002243 // Put non-current user tasks below current user tasks.
2244 while (maxPosition > 0) {
2245 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2246 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002247 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002248 break;
2249 }
2250 maxPosition--;
2251 }
2252 }
2253 position = Math.min(position, maxPosition);
2254 mTaskHistory.remove(task);
2255 mTaskHistory.add(position, task);
2256 updateTaskMovement(task, true);
2257 }
2258
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002259 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002260 // If the moving task is over home stack, transfer its return type to next task
2261 if (task.isOverHomeStack()) {
2262 final TaskRecord nextTask = getNextTask(task);
2263 if (nextTask != null) {
2264 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2265 }
2266 }
2267
Craig Mautner9c85c202013-10-04 20:11:26 -07002268 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002269 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002270 if (isOnHomeDisplay()) {
2271 ActivityStack lastStack = mStackSupervisor.getLastStack();
2272 final boolean fromHome = lastStack.isHomeStack();
2273 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002274 task.setTaskToReturnTo(fromHome
2275 ? lastStack.topTask() == null
2276 ? HOME_ACTIVITY_TYPE
2277 : lastStack.topTask().taskType
2278 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002279 }
2280 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002281 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002282 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002283
Craig Mautnerac6f8432013-07-17 13:24:59 -07002284 mTaskHistory.remove(task);
2285 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002286 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002287 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002288 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002289 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002290 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002291 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002292 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002293 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002294 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002295 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002296 break;
2297 }
2298 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002299 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002300 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002301 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002302 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002303 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002304
Craig Mautner8849a5e2013-04-02 16:41:03 -07002305 final void startActivityLocked(ActivityRecord r, boolean newTask,
Chong Zhang280d3322015-11-03 17:27:26 -08002306 boolean doResume, boolean keepCurTransition, ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002307 TaskRecord rTask = r.task;
2308 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002309 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2310 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002311 // Last activity in task had been removed or ActivityManagerService is reusing task.
2312 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002313 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002314 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002315 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002316 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002317 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002318 if (!newTask) {
2319 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002320 boolean startIt = true;
2321 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2322 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002323 if (task.getTopActivity() == null) {
2324 // All activities in task are finishing.
2325 continue;
2326 }
Craig Mautner70a86932013-02-28 22:37:44 -08002327 if (task == r.task) {
2328 // Here it is! Now, if this is not yet visible to the
2329 // user, then just add it without starting; it will
2330 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002331 if (!startIt) {
2332 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2333 + task, new RuntimeException("here").fillInStackTrace());
2334 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002335 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002336 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002337 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002338 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002339 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002340 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002341 return;
2342 }
2343 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002344 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002345 startIt = false;
2346 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002347 }
2348 }
2349
2350 // Place a new activity at top of stack, so it is next to interact
2351 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002352
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002353 // If we are not placing the new activity frontmost, we do not want
2354 // to deliver the onUserLeaving callback to the actual frontmost
2355 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002356 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002357 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002358 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002359 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 }
Craig Mautner70a86932013-02-28 22:37:44 -08002361
2362 task = r.task;
2363
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002364 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002365 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002366 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002367 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002368 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002369
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002370 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002371 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002372 // We want to show the starting preview window if we are
2373 // switching to a new task, or the next activity's process is
2374 // not currently running.
2375 boolean showStartingIcon = newTask;
2376 ProcessRecord proc = r.app;
2377 if (proc == null) {
2378 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2379 }
2380 if (proc == null || proc.thread == null) {
2381 showStartingIcon = true;
2382 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002383 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002384 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002385 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002386 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002387 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002388 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002389 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002390 ? r.mLaunchTaskBehind
2391 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2392 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002393 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002394 mNoAnimActivities.remove(r);
2395 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002396 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002397 boolean doShow = true;
2398 if (newTask) {
2399 // Even though this activity is starting fresh, we still need
2400 // to reset it to make sure we apply affinities to move any
2401 // existing activities from other tasks in to it.
2402 // If the caller has requested that the target task be
2403 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002404 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002405 resetTaskIfNeededLocked(r, r);
2406 doShow = topRunningNonDelayedActivityLocked(null) == r;
2407 }
Chong Zhang280d3322015-11-03 17:27:26 -08002408 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002409 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2410 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002411 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002412 if (r.mLaunchTaskBehind) {
2413 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2414 // tell WindowManager that r is visible even though it is at the back of the stack.
2415 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002416 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002417 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002418 // Figure out if we are transitioning from another activity that is
2419 // "has the same starting icon" as the next one. This allows the
2420 // window manager to keep the previous window it had previously
2421 // created, if it still had one.
2422 ActivityRecord prev = mResumedActivity;
2423 if (prev != null) {
2424 // We don't want to reuse the previous starting preview if:
2425 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002426 if (prev.task != r.task) {
2427 prev = null;
2428 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002429 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002430 else if (prev.nowVisible) {
2431 prev = null;
2432 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002433 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002434 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002435 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002436 mService.compatibilityInfoForPackageLocked(
2437 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002438 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002439 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002440 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002441 }
2442 } else {
2443 // If this is the first activity, don't do any fancy animations,
2444 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002445 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002446 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002447 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002448 }
2449 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002450 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002451 }
2452
2453 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002454 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002455 } else {
2456 addRecentActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002457 }
2458 }
2459
Dianne Hackbornbe707852011-11-11 14:32:10 -08002460 final void validateAppTokensLocked() {
2461 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002462 mValidateAppTokens.ensureCapacity(numActivities());
2463 final int numTasks = mTaskHistory.size();
2464 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2465 TaskRecord task = mTaskHistory.get(taskNdx);
2466 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002467 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002468 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002469 }
Craig Mautner000f0022013-02-26 15:04:29 -08002470 TaskGroup group = new TaskGroup();
2471 group.taskId = task.taskId;
2472 mValidateAppTokens.add(group);
2473 final int numActivities = activities.size();
2474 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2475 final ActivityRecord r = activities.get(activityNdx);
2476 group.tokens.add(r.appToken);
2477 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002478 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002479 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002480 }
2481
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002482 /**
2483 * Perform a reset of the given task, if needed as part of launching it.
2484 * Returns the new HistoryRecord at the top of the task.
2485 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002486 /**
2487 * Helper method for #resetTaskIfNeededLocked.
2488 * We are inside of the task being reset... we'll either finish this activity, push it out
2489 * for another task, or leave it as-is.
2490 * @param task The task containing the Activity (taskTop) that might be reset.
2491 * @param forceReset
2492 * @return An ActivityOptions that needs to be processed.
2493 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002494 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002495 ActivityOptions topOptions = null;
2496
2497 int replyChainEnd = -1;
2498 boolean canMoveOptions = true;
2499
2500 // We only do this for activities that are not the root of the task (since if we finish
2501 // the root, we may no longer have the task!).
2502 final ArrayList<ActivityRecord> activities = task.mActivities;
2503 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002504 final int rootActivityNdx = task.findEffectiveRootIndex();
2505 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002506 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002507 if (target.frontOfTask)
2508 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002509
2510 final int flags = target.info.flags;
2511 final boolean finishOnTaskLaunch =
2512 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2513 final boolean allowTaskReparenting =
2514 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2515 final boolean clearWhenTaskReset =
2516 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2517
2518 if (!finishOnTaskLaunch
2519 && !clearWhenTaskReset
2520 && target.resultTo != null) {
2521 // If this activity is sending a reply to a previous
2522 // activity, we can't do anything with it now until
2523 // we reach the start of the reply chain.
2524 // XXX note that we are assuming the result is always
2525 // to the previous activity, which is almost always
2526 // the case but we really shouldn't count on.
2527 if (replyChainEnd < 0) {
2528 replyChainEnd = i;
2529 }
2530 } else if (!finishOnTaskLaunch
2531 && !clearWhenTaskReset
2532 && allowTaskReparenting
2533 && target.taskAffinity != null
2534 && !target.taskAffinity.equals(task.affinity)) {
2535 // If this activity has an affinity for another
2536 // task, then we need to move it out of here. We will
2537 // move it as far out of the way as possible, to the
2538 // bottom of the activity stack. This also keeps it
2539 // correctly ordered with any activities we previously
2540 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002541 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002542 final ActivityRecord bottom =
2543 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002544 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002545 if (bottom != null && target.taskAffinity != null
2546 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002547 // If the activity currently at the bottom has the
2548 // same task affinity as the one we are moving,
2549 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002550 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002551 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002552 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002553 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002554 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002555 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002556 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002557 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002558 + " out to new task " + target.task);
2559 }
2560
Wale Ogunwale706ed792015-08-02 10:29:44 -07002561 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002562
Craig Mautner525f3d92013-05-07 14:01:50 -07002563 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002564 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2565 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002566 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002567 if (p.finishing) {
2568 continue;
2569 }
2570
Craig Mautnere3a74d52013-02-22 14:14:58 -08002571 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002572 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002573 topOptions = p.takeOptionsLocked();
2574 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002575 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002576 }
2577 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002578 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2579 "Removing activity " + p + " from task=" + task + " adding to task="
2580 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002581 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2582 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002583 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002584 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002585
Wale Ogunwale706ed792015-08-02 10:29:44 -07002586 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002587 }
2588
Wale Ogunwale706ed792015-08-02 10:29:44 -07002589 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002590 if (VALIDATE_TOKENS) {
2591 validateAppTokensLocked();
2592 }
2593
2594 replyChainEnd = -1;
2595 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2596 // If the activity should just be removed -- either
2597 // because it asks for it, or the task should be
2598 // cleared -- then finish it and anything that is
2599 // part of its reply chain.
2600 int end;
2601 if (clearWhenTaskReset) {
2602 // In this case, we want to finish this activity
2603 // and everything above it, so be sneaky and pretend
2604 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002605 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002606 } else if (replyChainEnd < 0) {
2607 end = i;
2608 } else {
2609 end = replyChainEnd;
2610 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002611 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002612 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002613 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002614 if (p.finishing) {
2615 continue;
2616 }
2617 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002618 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002619 topOptions = p.takeOptionsLocked();
2620 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002621 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002622 }
2623 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002624 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002625 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002626 if (finishActivityLocked(
2627 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002628 end--;
2629 srcPos--;
2630 }
2631 }
2632 replyChainEnd = -1;
2633 } else {
2634 // If we were in the middle of a chain, well the
2635 // activity that started it all doesn't want anything
2636 // special, so leave it all as-is.
2637 replyChainEnd = -1;
2638 }
2639 }
2640
2641 return topOptions;
2642 }
2643
2644 /**
2645 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2646 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2647 * @param affinityTask The task we are looking for an affinity to.
2648 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2649 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2650 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2651 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002652 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002653 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002654 int replyChainEnd = -1;
2655 final int taskId = task.taskId;
2656 final String taskAffinity = task.affinity;
2657
2658 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2659 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002660 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2661
2662 // Do not operate on or below the effective root Activity.
2663 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002664 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002665 if (target.frontOfTask)
2666 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002667
2668 final int flags = target.info.flags;
2669 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2670 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2671
2672 if (target.resultTo != null) {
2673 // If this activity is sending a reply to a previous
2674 // activity, we can't do anything with it now until
2675 // we reach the start of the reply chain.
2676 // XXX note that we are assuming the result is always
2677 // to the previous activity, which is almost always
2678 // the case but we really shouldn't count on.
2679 if (replyChainEnd < 0) {
2680 replyChainEnd = i;
2681 }
2682 } else if (topTaskIsHigher
2683 && allowTaskReparenting
2684 && taskAffinity != null
2685 && taskAffinity.equals(target.taskAffinity)) {
2686 // This activity has an affinity for our task. Either remove it if we are
2687 // clearing or move it over to our task. Note that
2688 // we currently punt on the case where we are resetting a
2689 // task that is not at the top but who has activities above
2690 // with an affinity to it... this is really not a normal
2691 // case, and we will need to later pull that task to the front
2692 // and usually at that point we will do the reset and pick
2693 // up those remaining activities. (This only happens if
2694 // someone starts an activity in a new task from an activity
2695 // in a task that is not currently on top.)
2696 if (forceReset || finishOnTaskLaunch) {
2697 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002698 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2699 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002700 for (int srcPos = start; srcPos >= i; --srcPos) {
2701 final ActivityRecord p = activities.get(srcPos);
2702 if (p.finishing) {
2703 continue;
2704 }
Todd Kennedy539db512014-12-15 09:57:55 -08002705 finishActivityLocked(
2706 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002707 }
2708 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002709 if (taskInsertionPoint < 0) {
2710 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002711
Craig Mautner77878772013-03-04 19:46:24 -08002712 }
Craig Mautner77878772013-03-04 19:46:24 -08002713
2714 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002715 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2716 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2717 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002718 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002719 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002720 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002721 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002722
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002723 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2724 "Removing and adding activity " + p + " to stack at " + task
2725 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002726 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2727 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002728 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002729 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002730 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002731 if (VALIDATE_TOKENS) {
2732 validateAppTokensLocked();
2733 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002734
2735 // Now we've moved it in to place... but what if this is
2736 // a singleTop activity and we have put it on top of another
2737 // instance of the same activity? Then we drop the instance
2738 // below so it remains singleTop.
2739 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2740 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002741 int targetNdx = taskActivities.indexOf(target);
2742 if (targetNdx > 0) {
2743 ActivityRecord p = taskActivities.get(targetNdx - 1);
2744 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002745 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2746 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002747 }
2748 }
2749 }
2750 }
2751
2752 replyChainEnd = -1;
2753 }
2754 }
Craig Mautner77878772013-03-04 19:46:24 -08002755 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002756 }
2757
Craig Mautner8849a5e2013-04-02 16:41:03 -07002758 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002759 ActivityRecord newActivity) {
2760 boolean forceReset =
2761 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2762 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2763 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2764 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2765 forceReset = true;
2766 }
2767 }
2768
2769 final TaskRecord task = taskTop.task;
2770
2771 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2772 * for remaining tasks. Used for later tasks to reparent to task. */
2773 boolean taskFound = false;
2774
2775 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2776 ActivityOptions topOptions = null;
2777
Craig Mautner77878772013-03-04 19:46:24 -08002778 // Preserve the location for reparenting in the new task.
2779 int reparentInsertionPoint = -1;
2780
Craig Mautnere3a74d52013-02-22 14:14:58 -08002781 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2782 final TaskRecord targetTask = mTaskHistory.get(i);
2783
2784 if (targetTask == task) {
2785 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2786 taskFound = true;
2787 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002788 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2789 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002790 }
2791 }
2792
Craig Mautner70a86932013-02-28 22:37:44 -08002793 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002794 if (taskNdx >= 0) {
2795 do {
2796 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2797 } while (taskTop == null && taskNdx >= 0);
2798 }
Craig Mautner70a86932013-02-28 22:37:44 -08002799
Craig Mautnere3a74d52013-02-22 14:14:58 -08002800 if (topOptions != null) {
2801 // If we got some ActivityOptions from an activity on top that
2802 // was removed from the task, propagate them to the new real top.
2803 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002804 taskTop.updateOptionsLocked(topOptions);
2805 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002806 topOptions.abort();
2807 }
2808 }
2809
2810 return taskTop;
2811 }
2812
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002813 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2814 String resultWho, int requestCode, int resultCode, Intent data) {
2815
2816 if (callingUid > 0) {
2817 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002818 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002819 }
2820
2821 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2822 + " : who=" + resultWho + " req=" + requestCode
2823 + " res=" + resultCode + " data=" + data);
2824 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2825 try {
2826 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2827 list.add(new ResultInfo(resultWho, requestCode,
2828 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002829 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002830 return;
2831 } catch (Exception e) {
2832 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2833 }
2834 }
2835
2836 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2837 }
2838
Craig Mautner299f9602015-01-26 09:47:33 -08002839 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002840 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2841 return;
2842 }
2843
2844 final ActivityRecord next = topRunningActivityLocked();
2845 final String myReason = reason + " adjustFocus";
2846 if (next != r) {
2847 if (next != null && StackId.keepFocusInStackIfPossible(mStackId)) {
2848 // For freeform, docked, and pinned stacks we always keep the focus within the
2849 // stack as long as there is a running activity in the stack that we can adjust
2850 // focus to.
2851 mService.setFocusedActivityLocked(next, myReason);
2852 return;
2853 } else {
2854 final TaskRecord task = r.task;
2855 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2856 // For non-fullscreen stack, we want to move the focus to the next visible
2857 // stack to prevent the home screen from moving to the top and obscuring
2858 // other visible stacks.
2859 if (!mFullscreen
2860 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2861 return;
2862 }
2863 // Move the home stack to the top if this stack is fullscreen or there is no
2864 // other visible stack.
2865 if (mStackSupervisor.moveHomeStackTaskToTop(
2866 task.getTaskToReturnTo(), myReason)) {
2867 // Activity focus was already adjusted. Nothing else to do...
2868 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002869 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002870 }
2871 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08002872 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002873
Wale Ogunwaled045c822015-12-02 09:14:28 -08002874 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2875 if (top != null) {
2876 mService.setFocusedActivityLocked(top, myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002877 }
2878 }
2879
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002880 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2881 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2882 final String myReason = reason + " adjustFocusToNextVisibleStack";
2883 if (stack == null) {
2884 return false;
2885 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002886 final ActivityRecord top = stack.topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002887 if (top == null) {
2888 return false;
2889 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002890 mService.setFocusedActivityLocked(top, myReason);
2891 return true;
2892 }
2893
Craig Mautnerf3333272013-04-22 10:55:53 -07002894 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002895 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002896 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2897 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2898 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002899 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002900 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002901 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2902 "stop-no-history", false)) {
2903 // Activity was finished, no need to continue trying to schedule stop.
2904 adjustFocusedActivityLocked(r, "stopActivityFinished");
2905 r.resumeKeyDispatchingLocked();
2906 return;
2907 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002908 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002909 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002910 + " on stop because we're just sleeping");
2911 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002912 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002913 }
2914
2915 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002916 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002917 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002918 try {
2919 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002920 if (DEBUG_STATES) Slog.v(TAG_STATES,
2921 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002922 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002923 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2924 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002925 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002926 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002927 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002928 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002929 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002930 r.setSleeping(true);
2931 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002932 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002933 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002934 } catch (Exception e) {
2935 // Maybe just ignore exceptions here... if the process
2936 // has crashed, our death notification will clean things
2937 // up.
2938 Slog.w(TAG, "Exception thrown during pause", e);
2939 // Just in case, assume it to be stopped.
2940 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002941 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002942 r.state = ActivityState.STOPPED;
2943 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002944 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002945 }
2946 }
2947 }
2948 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002949
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002950 /**
2951 * @return Returns true if the activity is being finished, false if for
2952 * some reason it is being left as-is.
2953 */
2954 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002955 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002956 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002957 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2958 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002959 + ", result=" + resultCode + ", data=" + resultData
2960 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002961 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002962 return false;
2963 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002964
Craig Mautnerd44711d2013-02-23 11:24:36 -08002965 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002966 return true;
2967 }
2968
Craig Mautnerd2328952013-03-05 12:46:26 -08002969 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002970 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2971 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2972 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2973 ActivityRecord r = activities.get(activityNdx);
2974 if (r.resultTo == self && r.requestCode == requestCode) {
2975 if ((r.resultWho == null && resultWho == null) ||
2976 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2977 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2978 false);
2979 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002980 }
2981 }
2982 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002983 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002984 }
2985
Todd Kennedy539db512014-12-15 09:57:55 -08002986 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002987 ActivityRecord r = topRunningActivityLocked();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002988 if (r != null && r.app == app) {
2989 // If the top running activity is from this crashing
2990 // process, then terminate it to avoid getting in a loop.
2991 Slog.w(TAG, " Force finishing activity "
2992 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08002993 int taskNdx = mTaskHistory.indexOf(r.task);
2994 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08002995 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002996 // Also terminate any activities below it that aren't yet
2997 // stopped, to avoid a situation where one will get
2998 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08002999 --activityNdx;
3000 if (activityNdx < 0) {
3001 do {
3002 --taskNdx;
3003 if (taskNdx < 0) {
3004 break;
3005 }
3006 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3007 } while (activityNdx < 0);
3008 }
3009 if (activityNdx >= 0) {
3010 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003011 if (r.state == ActivityState.RESUMED
3012 || r.state == ActivityState.PAUSING
3013 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07003014 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003015 Slog.w(TAG, " Force finishing activity "
3016 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08003017 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003018 }
3019 }
3020 }
3021 }
3022 }
3023
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003024 final void finishVoiceTask(IVoiceInteractionSession session) {
3025 IBinder sessionBinder = session.asBinder();
3026 boolean didOne = false;
3027 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3028 TaskRecord tr = mTaskHistory.get(taskNdx);
3029 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3030 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3031 ActivityRecord r = tr.mActivities.get(activityNdx);
3032 if (!r.finishing) {
3033 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3034 false);
3035 didOne = true;
3036 }
3037 }
3038 }
3039 }
3040 if (didOne) {
3041 mService.updateOomAdjLocked();
3042 }
3043 }
3044
Craig Mautnerd2328952013-03-05 12:46:26 -08003045 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003046 ArrayList<ActivityRecord> activities = r.task.mActivities;
3047 for (int index = activities.indexOf(r); index >= 0; --index) {
3048 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003049 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003050 break;
3051 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003052 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003053 }
3054 return true;
3055 }
3056
Dianne Hackborn5c607432012-02-28 14:44:19 -08003057 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3058 // send the result
3059 ActivityRecord resultTo = r.resultTo;
3060 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003061 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003062 + " who=" + r.resultWho + " req=" + r.requestCode
3063 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003064 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003065 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003066 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003067 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003068 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003069 if (r.info.applicationInfo.uid > 0) {
3070 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3071 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003072 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003073 }
3074 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3075 resultData);
3076 r.resultTo = null;
3077 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003078 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003079
3080 // Make sure this HistoryRecord is not holding on to other resources,
3081 // because clients have remote IPC references to this object so we
3082 // can't assume that will go away and want to avoid circular IPC refs.
3083 r.results = null;
3084 r.pendingResults = null;
3085 r.newIntents = null;
3086 r.icicle = null;
3087 }
3088
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003089 /**
3090 * @return Returns true if this activity has been removed from the history
3091 * list, or false if it is still in the list and will be removed later.
3092 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003093 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3094 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003095 if (r.finishing) {
3096 Slog.w(TAG, "Duplicate finish request for " + r);
3097 return false;
3098 }
3099
Wale Ogunwale7d701172015-03-11 15:36:30 -07003100 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003101 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003102 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003103 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003104 task.taskId, r.shortComponentName, reason);
3105 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003106 final int index = activities.indexOf(r);
3107 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003108 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003109 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003110 // If the caller asked that this activity (and all above it)
3111 // be cleared when the task is reset, don't lose that information,
3112 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003113 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003114 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003115 }
3116 }
3117
3118 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003119
Craig Mautner299f9602015-01-26 09:47:33 -08003120 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003121
Dianne Hackborn5c607432012-02-28 14:44:19 -08003122 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003123
Craig Mautnerde4ef022013-04-07 19:01:33 -07003124 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003125 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003126 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003127 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003128 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003129 ? AppTransition.TRANSIT_TASK_CLOSE
3130 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003131
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003132 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003133 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003134
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003135 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003136 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3137 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3138 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003139 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003140 }
3141
Craig Mautneraea74a52014-03-08 14:23:10 -08003142 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003143 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003144 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003145 } else if (r.state != ActivityState.PAUSING) {
3146 // If the activity is PAUSING, we will complete the finish once
3147 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003148 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003149 if (r.visible) {
3150 mWindowManager.setAppVisibility(r.appToken, false);
3151 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003152 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003153 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003154 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003155 }
3156
3157 return false;
3158 }
3159
Craig Mautnerf3333272013-04-22 10:55:53 -07003160 static final int FINISH_IMMEDIATELY = 0;
3161 static final int FINISH_AFTER_PAUSE = 1;
3162 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003163
Craig Mautnerf3333272013-04-22 10:55:53 -07003164 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003165 // First things first: if this activity is currently visible,
3166 // and the resumed activity is not yet visible, then hold off on
3167 // finishing until the resumed one becomes visible.
3168 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003169 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
3170 mStackSupervisor.mStoppingActivities.add(r);
Craig Mautner29219d92013-04-16 20:19:12 -07003171 if (mStackSupervisor.mStoppingActivities.size() > 3
3172 || r.frontOfTask && mTaskHistory.size() <= 1) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003173 // If we already have a few activities waiting to stop,
3174 // then give up on things going idle and start clearing
Craig Mautner29219d92013-04-16 20:19:12 -07003175 // them out. Or if r is the last of activity of the last task the stack
3176 // will be empty and must be cleared immediately.
Craig Mautnerf3333272013-04-22 10:55:53 -07003177 mStackSupervisor.scheduleIdleLocked();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003178 } else {
Craig Mautner0eea92c2013-05-16 13:35:39 -07003179 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003180 }
3181 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003182 if (DEBUG_STATES) Slog.v(TAG_STATES,
3183 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003184 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003185 if (oomAdj) {
3186 mService.updateOomAdjLocked();
3187 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003188 return r;
3189 }
3190
3191 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003192 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003193 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003194 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003195 if (mResumedActivity == r) {
3196 mResumedActivity = null;
3197 }
3198 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003199 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003200 r.state = ActivityState.FINISHING;
3201
3202 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003203 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003204 || prevState == ActivityState.STOPPED
3205 || prevState == ActivityState.INITIALIZING) {
3206 // If this activity is already stopped, we can just finish
3207 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003208 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003209 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003210 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003211 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003212 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003213 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003214 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3215 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003216 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003217 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003218
3219 // Need to go through the full pause cycle to get this
3220 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003221 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003222 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003223 r.resumeKeyDispatchingLocked();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003224 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003225 return r;
3226 }
3227
Craig Mautneree36c772014-07-16 14:56:05 -07003228 void finishAllActivitiesLocked(boolean immediately) {
3229 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003230 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3231 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3232 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3233 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003234 noActivitiesInStack = false;
3235 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003236 continue;
3237 }
Craig Mautneree36c772014-07-16 14:56:05 -07003238 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003239 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3240 }
3241 }
Craig Mautneree36c772014-07-16 14:56:05 -07003242 if (noActivitiesInStack) {
3243 mActivityContainer.onTaskListEmptyLocked();
3244 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003245 }
3246
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003247 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3248 // Basic case: for simple app-centric recents, we need to recreate
3249 // the task if the affinity has changed.
3250 if (srec == null || srec.task.affinity == null ||
3251 !srec.task.affinity.equals(destAffinity)) {
3252 return true;
3253 }
3254 // Document-centric case: an app may be split in to multiple documents;
3255 // they need to re-create their task if this current activity is the root
3256 // of a document, unless simply finishing it will return them to the the
3257 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003258 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3259 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003260 // Okay, this activity is at the root of its task. What to do, what to do...
3261 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3262 // Finishing won't return to an application, so we need to recreate.
3263 return true;
3264 }
3265 // We now need to get the task below it to determine what to do.
3266 int taskIdx = mTaskHistory.indexOf(srec.task);
3267 if (taskIdx <= 0) {
3268 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3269 return false;
3270 }
3271 if (taskIdx == 0) {
3272 // At the bottom of the stack, nothing to go back to.
3273 return true;
3274 }
3275 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3276 if (!srec.task.affinity.equals(prevTask.affinity)) {
3277 // These are different apps, so need to recreate.
3278 return true;
3279 }
3280 }
3281 return false;
3282 }
3283
Wale Ogunwale7d701172015-03-11 15:36:30 -07003284 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003285 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003286 final TaskRecord task = srec.task;
3287 final ArrayList<ActivityRecord> activities = task.mActivities;
3288 final int start = activities.indexOf(srec);
3289 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003290 return false;
3291 }
3292 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003293 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003294 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003295 final ComponentName dest = destIntent.getComponent();
3296 if (start > 0 && dest != null) {
3297 for (int i = finishTo; i >= 0; i--) {
3298 ActivityRecord r = activities.get(i);
3299 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003300 r.info.name.equals(dest.getClassName())) {
3301 finishTo = i;
3302 parent = r;
3303 foundParentInTask = true;
3304 break;
3305 }
3306 }
3307 }
3308
3309 IActivityController controller = mService.mController;
3310 if (controller != null) {
3311 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3312 if (next != null) {
3313 // ask watcher if this is allowed
3314 boolean resumeOK = true;
3315 try {
3316 resumeOK = controller.activityResuming(next.packageName);
3317 } catch (RemoteException e) {
3318 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003319 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003320 }
3321
3322 if (!resumeOK) {
3323 return false;
3324 }
3325 }
3326 }
3327 final long origId = Binder.clearCallingIdentity();
3328 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003329 ActivityRecord r = activities.get(i);
3330 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003331 // Only return the supplied result for the first activity finished
3332 resultCode = Activity.RESULT_CANCELED;
3333 resultData = null;
3334 }
3335
3336 if (parent != null && foundParentInTask) {
3337 final int parentLaunchMode = parent.info.launchMode;
3338 final int destIntentFlags = destIntent.getFlags();
3339 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3340 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3341 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3342 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003343 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3344 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003345 } else {
3346 try {
3347 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3348 destIntent.getComponent(), 0, srec.userId);
Craig Mautner6170f732013-04-02 13:05:23 -07003349 int res = mStackSupervisor.startActivityLocked(srec.app.thread, destIntent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07003350 null, aInfo, null, null, parent.appToken, null,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003351 0, -1, parent.launchedFromUid, parent.launchedFromPackage,
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07003352 -1, parent.launchedFromUid, 0, null, false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003353 foundParentInTask = res == ActivityManager.START_SUCCESS;
3354 } catch (RemoteException e) {
3355 foundParentInTask = false;
3356 }
3357 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003358 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003359 }
3360 }
3361 Binder.restoreCallingIdentity(origId);
3362 return foundParentInTask;
3363 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003364 /**
3365 * Perform the common clean-up of an activity record. This is called both
3366 * as part of destroyActivityLocked() (when destroying the client-side
3367 * representation) and cleaning things up as a result of its hosting
3368 * processing going away, in which case there is no remaining client-side
3369 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003370 *
3371 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003372 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003373 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3374 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003375 if (mResumedActivity == r) {
3376 mResumedActivity = null;
3377 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003378 if (mPausingActivity == r) {
3379 mPausingActivity = null;
3380 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003381 mService.clearFocusedActivity(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003382
3383 r.configDestroy = false;
3384 r.frozenBeforeDestroy = false;
3385
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003386 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003387 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003388 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003389 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003390 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003391 }
3392
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003393 // Make sure this record is no longer in the pending finishes list.
3394 // This could happen, for example, if we are trimming activities
3395 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003396 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003397 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003398
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003399 // Remove any pending results.
3400 if (r.finishing && r.pendingResults != null) {
3401 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3402 PendingIntentRecord rec = apr.get();
3403 if (rec != null) {
3404 mService.cancelIntentSenderLocked(rec, false);
3405 }
3406 }
3407 r.pendingResults = null;
3408 }
3409
3410 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003411 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003412 }
3413
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003414 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003415 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003416 if (getVisibleBehindActivity() == r) {
3417 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003418 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003419 }
3420
3421 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003422 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003423 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003424 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003425 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003426 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003427 }
3428
Craig Mautner299f9602015-01-26 09:47:33 -08003429 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003430 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003431 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003432 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003433 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3434 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3435
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003436 r.takeFromHistory();
3437 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003438 if (DEBUG_STATES) Slog.v(TAG_STATES,
3439 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003440 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003441 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003442 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003443 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003444 if (VALIDATE_TOKENS) {
3445 validateAppTokensLocked();
3446 }
Craig Mautner312ba862014-02-10 17:55:01 -08003447 final TaskRecord task = r.task;
3448 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003449 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003450 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003451 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003452 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003453 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003454 }
Craig Mautner299f9602015-01-26 09:47:33 -08003455 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003456 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003457 cleanUpActivityServicesLocked(r);
3458 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003459 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003460
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003461 /**
3462 * Perform clean-up of service connections in an activity record.
3463 */
3464 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3465 // Throw away any services that have been bound by this activity.
3466 if (r.connections != null) {
3467 Iterator<ConnectionRecord> it = r.connections.iterator();
3468 while (it.hasNext()) {
3469 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003470 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003471 }
3472 r.connections = null;
3473 }
3474 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003475
Craig Mautneree2e45a2014-06-27 12:10:03 -07003476 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003477 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003478 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003479 mHandler.sendMessage(msg);
3480 }
3481
Craig Mautneree2e45a2014-06-27 12:10:03 -07003482 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003483 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003484 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003485 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3486 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3487 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3488 final ActivityRecord r = activities.get(activityNdx);
3489 if (r.finishing) {
3490 continue;
3491 }
3492 if (r.fullscreen) {
3493 lastIsOpaque = true;
3494 }
3495 if (owner != null && r.app != owner) {
3496 continue;
3497 }
3498 if (!lastIsOpaque) {
3499 continue;
3500 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003501 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003502 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003503 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003504 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003505 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003506 activityRemoved = true;
3507 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003508 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003509 }
3510 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003511 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07003512 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003513 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003514 }
3515
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003516 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3517 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003518 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3519 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003520 + " pausing=" + mPausingActivity + " for reason " + reason);
3521 return destroyActivityLocked(r, true, reason);
3522 }
3523 return false;
3524 }
3525
3526 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3527 String reason) {
3528 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003529 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003530 int maxTasks = tasks.size() / 4;
3531 if (maxTasks < 1) {
3532 maxTasks = 1;
3533 }
3534 int numReleased = 0;
3535 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3536 final TaskRecord task = mTaskHistory.get(taskNdx);
3537 if (!tasks.contains(task)) {
3538 continue;
3539 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003540 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003541 int curNum = 0;
3542 final ArrayList<ActivityRecord> activities = task.mActivities;
3543 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3544 final ActivityRecord activity = activities.get(actNdx);
3545 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003546 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003547 + " in state " + activity.state + " resumed=" + mResumedActivity
3548 + " pausing=" + mPausingActivity + " for reason " + reason);
3549 destroyActivityLocked(activity, true, reason);
3550 if (activities.get(actNdx) != activity) {
3551 // Was removed from list, back up so we don't miss the next one.
3552 actNdx--;
3553 }
3554 curNum++;
3555 }
3556 }
3557 if (curNum > 0) {
3558 numReleased += curNum;
3559 maxTasks--;
3560 if (mTaskHistory.get(taskNdx) != task) {
3561 // The entire task got removed, back up so we don't miss the next one.
3562 taskNdx--;
3563 }
3564 }
3565 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003566 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3567 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003568 return numReleased;
3569 }
3570
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003571 /**
3572 * Destroy the current CLIENT SIDE instance of an activity. This may be
3573 * called both when actually finishing an activity, or when performing
3574 * a configuration switch where we destroy the current client-side object
3575 * but then create a new client-side object for this same HistoryRecord.
3576 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003577 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003578 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3579 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003580 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003581 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003582 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003583 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003584
3585 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003586
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003587 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003588
3589 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003590
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003591 if (hadApp) {
3592 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003593 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003594 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3595 mService.mHeavyWeightProcess = null;
3596 mService.mHandler.sendEmptyMessage(
3597 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3598 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003599 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003600 // Update any services we are bound to that might care about whether
3601 // their client may have activities.
3602 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003603 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003604 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003605 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003606 }
3607 }
3608
3609 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003610
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003611 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003612 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003613 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003614 r.configChangeFlags);
3615 } catch (Exception e) {
3616 // We can just ignore exceptions here... if the process
3617 // has crashed, our death notification will clean things
3618 // up.
3619 //Slog.w(TAG, "Exception thrown during finish", e);
3620 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003621 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003622 removedFromHistory = true;
3623 skipDestroy = true;
3624 }
3625 }
3626
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003627 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003628
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003629 // If the activity is finishing, we need to wait on removing it
3630 // from the list to give it a chance to do its cleanup. During
3631 // that time it may make calls back with its token so we need to
3632 // be able to find it on the list and so we don't want to remove
3633 // it from the list yet. Otherwise, we can just immediately put
3634 // it in the destroyed state since we are not removing it from the
3635 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003636 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003637 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003638 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003639 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003640 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003641 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3642 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003643 if (DEBUG_STATES) Slog.v(TAG_STATES,
3644 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003645 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003646 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003647 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003648 }
3649 } else {
3650 // remove this record from the history.
3651 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003652 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003653 removedFromHistory = true;
3654 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003655 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003656 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003657 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003658 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003659 }
3660 }
3661
3662 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003663
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003664 if (!mLRUActivities.remove(r) && hadApp) {
3665 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3666 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003667
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003668 return removedFromHistory;
3669 }
3670
Craig Mautner299f9602015-01-26 09:47:33 -08003671 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003672 final long origId = Binder.clearCallingIdentity();
3673 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003674 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003675 if (r != null) {
3676 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003677 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003678 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003679
Wale Ogunwale60454db2015-01-23 16:05:07 -08003680 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003681 if (r.state == ActivityState.DESTROYING) {
3682 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003683 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003684 }
3685 }
Craig Mautner05d29032013-05-03 13:40:13 -07003686 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003687 } finally {
3688 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003689 }
3690 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003691
Todd Kennedyaab56db2015-01-30 09:39:53 -08003692 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003693 if (hasVisibleBehindActivity() &&
3694 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003695 if (r == topRunningActivityLocked()) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003696 // Don't release the top activity if it has requested to run behind the next
3697 // activity.
3698 return;
3699 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003700 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003701 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003702 " thread=" + r.app.thread);
3703 if (r != null && r.app != null && r.app.thread != null) {
3704 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003705 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003706 } catch (RemoteException e) {
3707 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003708 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003709 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003710 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003711 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003712 }
3713 }
3714 }
3715
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003716 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003717 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3718 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003719 if (r != null) {
3720 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003721 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003722 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003723 }
3724 mStackSupervisor.resumeTopActivitiesLocked();
3725 }
3726
Jose Lima4b6c6692014-08-12 17:41:12 -07003727 boolean hasVisibleBehindActivity() {
3728 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003729 }
3730
Jose Lima4b6c6692014-08-12 17:41:12 -07003731 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003732 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003733 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003734 }
3735 }
3736
Jose Lima4b6c6692014-08-12 17:41:12 -07003737 ActivityRecord getVisibleBehindActivity() {
3738 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003739 }
3740
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003741 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3742 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003743 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003744 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3745 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003746 while (i > 0) {
3747 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003748 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003749 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003750 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003751 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003752 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003753 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003754 }
3755 }
3756 }
3757
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003758 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3759 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003760 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3761 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003762 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3763 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003764 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003765 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003766 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3767 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003768
3769 boolean hasVisibleActivities = false;
3770
3771 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003772 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003773 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3774 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003775 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3776 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3777 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3778 final ActivityRecord r = activities.get(activityNdx);
3779 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003780 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3781 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003782 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003783 if (r.visible) {
3784 hasVisibleActivities = true;
3785 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003786 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003787 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3788 // Don't currently have state for the activity, or
3789 // it is finishing -- always remove it.
3790 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003791 } else if (!r.visible && r.launchCount > 2 &&
3792 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003793 // We have launched this activity too many times since it was
3794 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003795 // (Note if the activity is visible, we don't remove the record.
3796 // We leave the dead window on the screen but the process will
3797 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003798 remove = true;
3799 } else {
3800 // The process may be gone, but the activity lives on!
3801 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003802 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003803 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003804 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3805 "Removing activity " + r + " from stack at " + i
3806 + ": haveState=" + r.haveState
3807 + " stateNotNeeded=" + r.stateNotNeeded
3808 + " finishing=" + r.finishing
3809 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003810 if (!r.finishing) {
3811 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3812 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3813 r.userId, System.identityHashCode(r),
3814 r.task.taskId, r.shortComponentName,
3815 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003816 if (r.state == ActivityState.RESUMED) {
3817 mService.updateUsageStats(r, false);
3818 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003819 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003820 } else {
3821 // We have the current state for this activity, so
3822 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003823 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3824 if (DEBUG_APP) Slog.v(TAG_APP,
3825 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003826 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003827 // Set nowVisible to previous visible state. If the app was visible while
3828 // it died, we leave the dead window on screen so it's basically visible.
3829 // This is needed when user later tap on the dead window, we need to stop
3830 // other apps when user transfers focus to the restarted activity.
3831 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003832 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003833 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003834 "App died, clearing saved state of " + r);
3835 r.icicle = null;
3836 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003837 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003838 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003839 if (remove) {
3840 removeActivityFromHistoryLocked(r, "appDied");
3841 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003842 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003843 }
3844 }
3845
3846 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003847 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003848
Chong Zhang280d3322015-11-03 17:27:26 -08003849 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003850 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003851 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003852 if (r != null && r.state != ActivityState.RESUMED) {
3853 r.updateOptionsLocked(options);
3854 } else {
3855 ActivityOptions.abort(options);
3856 }
3857 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003858 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003859 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003860
Craig Mautner21d24a22014-04-23 11:45:37 -07003861 void updateTaskMovement(TaskRecord task, boolean toFront) {
3862 if (task.isPersistable) {
3863 task.mLastTimeMoved = System.currentTimeMillis();
3864 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3865 // recently will be most negative, tasks sent to the bottom before that will be less
3866 // negative. Similarly for recent tasks moved to the top which will be most positive.
3867 if (!toFront) {
3868 task.mLastTimeMoved *= -1;
3869 }
3870 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003871 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003872 }
3873
Craig Mautner84984fa2014-06-19 11:19:20 -07003874 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003875 final int top = mTaskHistory.size() - 1;
3876 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3877 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003878 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003879 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3880 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003881 mTaskHistory.remove(taskNdx);
3882 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003883 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003884 return;
3885 }
3886 }
3887 }
3888
Chong Zhang280d3322015-11-03 17:27:26 -08003889 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003890 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003891 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003892
Craig Mautner11bf9a52013-02-19 14:08:51 -08003893 final int numTasks = mTaskHistory.size();
3894 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003895 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003896 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003897 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003898 ActivityOptions.abort(options);
3899 } else {
3900 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3901 }
3902 return;
3903 }
3904
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003905 if (timeTracker != null) {
3906 // The caller wants a time tracker associated with this task.
3907 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3908 tr.mActivities.get(i).appTimeTracker = timeTracker;
3909 }
3910 }
3911
Craig Mautner11bf9a52013-02-19 14:08:51 -08003912 // Shift all activities with this task up to the top
3913 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003914 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003915
Chong Zhang45c25ce2015-08-10 22:18:26 -07003916 // Don't refocus if invisible to current user
3917 ActivityRecord top = tr.getTopActivity();
3918 if (!okToShowLocked(top)) {
3919 addRecentActivityLocked(top);
3920 ActivityOptions.abort(options);
3921 return;
3922 }
3923
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003924 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003925 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003926 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003927
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003928 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003929 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003930 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003931 if (r != null) {
3932 mNoAnimActivities.add(r);
3933 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003934 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003935 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003936 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003937 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003938
Craig Mautner05d29032013-05-03 13:40:13 -07003939 mStackSupervisor.resumeTopActivitiesLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003940 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003941
3942 if (VALIDATE_TOKENS) {
3943 validateAppTokensLocked();
3944 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003945 }
3946
3947 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003948 * Worker method for rearranging history stack. Implements the function of moving all
3949 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003950 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003951 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003952 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3953 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003954 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003955 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003956 * @return Returns true if the move completed, false if not.
3957 */
Craig Mautner299f9602015-01-26 09:47:33 -08003958 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003959 final TaskRecord tr = taskForIdLocked(taskId);
3960 if (tr == null) {
3961 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3962 return false;
3963 }
3964
3965 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003966 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003967
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003968 // If we have a watcher, preflight the move before committing to it. First check
3969 // for *other* available tasks, but if none are available, then try again allowing the
3970 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003971 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003972 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003973 if (next == null) {
3974 next = topRunningActivityLocked(null, 0);
3975 }
3976 if (next != null) {
3977 // ask watcher if this is allowed
3978 boolean moveOK = true;
3979 try {
3980 moveOK = mService.mController.activityResuming(next.packageName);
3981 } catch (RemoteException e) {
3982 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003983 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003984 }
3985 if (!moveOK) {
3986 return false;
3987 }
3988 }
3989 }
3990
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003991 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003992
riddle_hsuc215a4f2014-12-27 12:10:45 +08003993 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07003994
3995 // If true, we should resume the home activity next if the task we are moving to the
3996 // back is over the home stack. We force to false if the task we are moving to back
3997 // is the home task and we don't want it resumed after moving to the back.
3998 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
3999 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004000 final TaskRecord nextTask = getNextTask(tr);
4001 if (nextTask != null) {
4002 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4003 } else {
4004 prevIsHome = true;
4005 }
4006 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004007 mTaskHistory.remove(tr);
4008 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004009 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004010
Craig Mautnerc8143c62013-09-03 12:15:57 -07004011 // There is an assumption that moving a task to the back moves it behind the home activity.
4012 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004013 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004014 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4015 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004016 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004017 break;
4018 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004019 if (taskNdx == 1) {
4020 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004021 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004022 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004023 }
4024
Craig Mautner299f9602015-01-26 09:47:33 -08004025 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004026 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004027
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004028 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004029 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004030 }
4031
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004032 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004033 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004034 if (!mService.mBooting && !mService.mBooted) {
4035 // Not ready yet!
4036 return false;
4037 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004038 final int taskToReturnTo = tr.getTaskToReturnTo();
4039 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004040 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004041 }
4042
Craig Mautner05d29032013-05-03 13:40:13 -07004043 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004044 return true;
4045 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004046
Craig Mautner8849a5e2013-04-02 16:41:03 -07004047 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004048 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004049 final Uri data = r.intent.getData();
4050 final String strData = data != null ? data.toSafeString() : null;
4051
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004052 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004053 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004054 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004055 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004056 }
4057
4058 /**
4059 * Make sure the given activity matches the current configuration. Returns
4060 * false if the activity had to be destroyed. Returns true if the
4061 * configuration is the same, or the activity will remain running as-is
4062 * for whatever reason. Ensures the HistoryRecord is updated with the
4063 * correct configuration and all other bookkeeping is handled.
4064 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004065 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4066 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004067 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004068 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004069 "Skipping config check (will change): " + r);
4070 return true;
4071 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004072
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004073 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004074 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004075
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004076 // Short circuit: if the two configurations are the exact same
4077 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004078 final Configuration newConfig = mService.mConfiguration;
4079 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004080 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004081 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004082 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004083 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004084 "Configuration unchanged in " + r);
4085 return true;
4086 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004087
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004088 // We don't worry about activities that are finishing.
4089 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004090 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004091 "Configuration doesn't matter in finishing " + r);
4092 r.stopFreezingScreenLocked(false);
4093 return true;
4094 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004095
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004096 // Okay we now are going to make this activity have the new config.
4097 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004098 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004099 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004100 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004101 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004102
Filip Gruszczynski23493322015-07-29 17:02:59 -07004103 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004104 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004105 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004106 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004107 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004108 // There are no significant differences, so we won't relaunch but should still deliver
4109 // the new configuration to the client process.
4110 r.scheduleConfigurationChanged(taskConfig, false);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004111 return true;
4112 }
4113
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004114 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4115 "Configuration changes for " + r + " ; taskChanges="
4116 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4117 + Configuration.configurationDiffToString(changes));
4118
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004119 // If the activity isn't currently running, just leave the new
4120 // configuration and it will pick that up next time it starts.
4121 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004122 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004123 "Configuration doesn't matter not running " + r);
4124 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004125 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004126 return true;
4127 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004128
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004129 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004130 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4131 "Checking to restart " + r.info.name + ": changed=0x"
4132 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004133 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4134 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004135
Dianne Hackborne6676352011-06-01 16:51:20 -07004136 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004137 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4138 r.configChangeFlags |= changes;
4139 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004140 r.forceNewConfig = false;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004141 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004142 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004143 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004144 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004145 } else if (r.state == ActivityState.PAUSING) {
4146 // A little annoying: we are waiting for this activity to
4147 // finish pausing. Let's not do anything now, but just
4148 // flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004149 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004150 "Config is skipping already pausing " + r);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004151 r.configDestroy = true;
4152 return true;
4153 } else if (r.state == ActivityState.RESUMED) {
4154 // Try to optimize this case: the configuration is changing
4155 // and we need to restart the top, resumed activity.
4156 // Instead of doing the normal handshaking, just say
4157 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004158 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004159 "Config is relaunching resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004160 relaunchActivityLocked(r, r.configChangeFlags, true,
4161 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004162 r.configChangeFlags = 0;
4163 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004164 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004165 "Config is relaunching non-resumed " + r);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004166 relaunchActivityLocked(r, r.configChangeFlags, false,
4167 preserveWindow && isResizeOnlyChange(changes));
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004168 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004169 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004170
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004171 // All done... tell the caller we weren't able to keep this
4172 // activity around.
4173 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004174 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004175
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004176 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004177 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004178 // changes is always sent to all processes when they happen so it can just use whatever
4179 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004180 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004181 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004182
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004183 return true;
4184 }
4185
Filip Gruszczynski23493322015-07-29 17:02:59 -07004186 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4187 Configuration oldTaskOverride) {
4188 // Determine what has changed. May be nothing, if this is a config
4189 // that has come back from the app after going idle. In that case
4190 // we just want to leave the official config object now in the
4191 // activity and do nothing else.
4192 int taskChanges = oldTaskOverride.diff(taskConfig);
4193 // We don't want to use size changes if they don't cross boundaries that are important to
4194 // the app.
4195 if ((taskChanges & ActivityInfo.CONFIG_SCREEN_SIZE) != 0) {
4196 final boolean crosses = record.crossesHorizontalSizeThreshold(
4197 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4198 || record.crossesVerticalSizeThreshold(
4199 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4200 if (!crosses) {
4201 taskChanges &= ~ActivityInfo.CONFIG_SCREEN_SIZE;
4202 }
4203 }
4204 if ((taskChanges & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
4205 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4206 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004207 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004208 taskChanges &= ~ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4209 }
4210 }
4211 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4212 }
4213
4214 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4215 Configuration oldTaskOverride, int taskChanges) {
4216 if (taskChanges == 0) {
4217 // {@link Configuration#diff} doesn't catch changes from unset values.
4218 // Check for changes we care about.
4219 if (oldTaskOverride.orientation != taskConfig.orientation) {
4220 taskChanges |= ActivityInfo.CONFIG_ORIENTATION;
4221 }
4222 // We want to explicitly track situations where the size configuration goes from
4223 // undefined to defined. We don't care about crossing the threshold in that case,
4224 // because there is no threshold.
4225 final int oldHeight = oldTaskOverride.screenHeightDp;
4226 final int newHeight = taskConfig.screenHeightDp;
4227 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4228 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4229 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
4230 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4231 }
4232 final int oldWidth = oldTaskOverride.screenWidthDp;
4233 final int newWidth = taskConfig.screenWidthDp;
4234 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4235 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4236 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
4237 taskChanges |= ActivityInfo.CONFIG_SCREEN_SIZE;
4238 }
4239 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4240 final int newSmallest = taskConfig.smallestScreenWidthDp;
4241 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4242 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4243 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
4244 taskChanges |= ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
4245 }
4246 }
4247 return taskChanges;
4248 }
4249
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004250 private static boolean isResizeOnlyChange(int change) {
4251 return (change & ~(ActivityInfo.CONFIG_SCREEN_SIZE
4252 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_ORIENTATION)) == 0;
4253 }
4254
Wale Ogunwale83301a92015-09-24 15:54:08 -07004255 private void relaunchActivityLocked(
4256 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4257 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
4258 return;
4259 }
4260
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004261 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004262 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004263 if (andResume) {
4264 results = r.results;
4265 newIntents = r.newIntents;
4266 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004267 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4268 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004269 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004270 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004271 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004272 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004273
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004274 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004275
Craig Mautner34b73df2014-01-12 21:11:08 -08004276 mStackSupervisor.removeChildActivityContainers(r);
4277
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004278 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004279 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4280 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004281 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004282 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4283 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004284 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004285 // Note: don't need to call pauseIfSleepingLocked() here, because
4286 // the caller will only pass in 'andResume' if this activity is
4287 // currently resumed, which implies we aren't sleeping.
4288 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004289 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004290 }
4291
4292 if (andResume) {
4293 r.results = null;
4294 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004295 r.state = ActivityState.RESUMED;
4296 } else {
4297 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4298 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004299 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004300 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004301
4302 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004303 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4304 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4305 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4306 final ActivityRecord r = activities.get(activityNdx);
4307 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004308 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004309 }
4310 if (r.fullscreen && !r.finishing) {
4311 return false;
4312 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004313 }
4314 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004315 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004316 if (r == null) {
4317 return false;
4318 }
4319 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4320 + " would have returned true for r=" + r);
4321 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004322 }
4323
4324 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004325 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4326 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4327 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4328 final ActivityRecord r = activities.get(activityNdx);
4329 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004330 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004331 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004332 }
4333 }
4334 }
4335
Wale Ogunwale540e1232015-05-01 15:35:39 -07004336 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4337 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004338 boolean didSomething = false;
4339 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004340 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004341 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4342 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4343 int numActivities = activities.size();
4344 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4345 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004346 final boolean sameComponent =
4347 (r.packageName.equals(packageName) && (filterByClasses == null
4348 || filterByClasses.contains(r.realActivity.getClassName())))
4349 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004350 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004351 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004352 && (r.app == null || evenPersistent || !r.app.persistent)) {
4353 if (!doit) {
4354 if (r.finishing) {
4355 // If this activity is just finishing, then it is not
4356 // interesting as far as something to stop.
4357 continue;
4358 }
4359 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004360 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004361 if (r.isHomeActivity()) {
4362 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4363 Slog.i(TAG, "Skip force-stop again " + r);
4364 continue;
4365 } else {
4366 homeActivity = r.realActivity;
4367 }
4368 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004369 didSomething = true;
4370 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004371 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004372 if (r.app != null) {
4373 r.app.removed = true;
4374 }
4375 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004376 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004377 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004378 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4379 true)) {
4380 // r has been deleted from mActivities, accommodate.
4381 --numActivities;
4382 --activityNdx;
4383 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004384 }
4385 }
4386 }
4387 return didSomething;
4388 }
4389
Dianne Hackborn09233282014-04-30 11:33:59 -07004390 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004391 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4392 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004393 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004394 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004395 if (task.getTopActivity() == null) {
4396 continue;
4397 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004398 ActivityRecord r = null;
4399 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004400 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004401 int numActivities = 0;
4402 int numRunning = 0;
4403 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004404 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004405 continue;
4406 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004407 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004408 tmp = activities.get(activityNdx);
4409 if (tmp.finishing) {
4410 continue;
4411 }
4412 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004413
Craig Mautneraab647e2013-02-28 16:31:36 -08004414 // Initialize state for next task if needed.
4415 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4416 top = r;
4417 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004418 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004419
4420 // Add 'r' into the current task.
4421 numActivities++;
4422 if (r.app != null && r.app.thread != null) {
4423 numRunning++;
4424 }
4425
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004426 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004427 TAG, r.intent.getComponent().flattenToShortString()
4428 + ": task=" + r.task);
4429 }
4430
4431 RunningTaskInfo ci = new RunningTaskInfo();
4432 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004433 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004434 ci.baseActivity = r.intent.getComponent();
4435 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004436 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004437 if (focusedStack && topTask) {
4438 // Give the latest time to ensure foreground task can be sorted
4439 // at the first, because lastActiveTime of creating task is 0.
4440 ci.lastActiveTime = System.currentTimeMillis();
4441 topTask = false;
4442 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004443
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004444 if (top.task != null) {
4445 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004446 }
4447 ci.numActivities = numActivities;
4448 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004449 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004450 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004451 }
4452
4453 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004454 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004455 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004456 if (top >= 0) {
4457 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4458 int activityTop = activities.size() - 1;
4459 if (activityTop > 0) {
4460 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4461 "unhandled-back", true);
4462 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004463 }
4464 }
4465
Craig Mautner6b74cb52013-09-27 17:02:21 -07004466 /**
4467 * Reset local parameters because an app's activity died.
4468 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004469 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004470 */
4471 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004472 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004473 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004474 "App died while pausing: " + mPausingActivity);
4475 mPausingActivity = null;
4476 }
4477 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4478 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004479 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004480 }
4481
Craig Mautner19091252013-10-05 00:03:53 -07004482 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004483 }
4484
Craig Mautnercae015f2013-02-08 14:31:27 -08004485 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004486 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4487 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4488 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4489 final ActivityRecord r = activities.get(activityNdx);
4490 if (r.app == app) {
4491 Slog.w(TAG, " Force finishing activity "
4492 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004493 // Force the destroy to skip right to removal.
4494 r.app = null;
4495 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004496 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004497 }
4498 }
4499 }
4500
Dianne Hackborn390517b2013-05-30 15:03:32 -07004501 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004502 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004503 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004504 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4505 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004506 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4507 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004508 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004509 " Task id #" + task.taskId + "\n" +
4510 " mFullscreen=" + task.mFullscreen + "\n" +
4511 " mBounds=" + task.mBounds + "\n" +
4512 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004513 if (printed) {
4514 header = null;
4515 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004516 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004517 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004518 }
4519
4520 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4521 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4522
4523 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004524 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4525 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004526 }
4527 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004528 final int top = mTaskHistory.size() - 1;
4529 if (top >= 0) {
4530 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4531 int listTop = list.size() - 1;
4532 if (listTop >= 0) {
4533 activities.add(list.get(listTop));
4534 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004535 }
4536 } else {
4537 ItemMatcher matcher = new ItemMatcher();
4538 matcher.build(name);
4539
Craig Mautneraab647e2013-02-28 16:31:36 -08004540 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4541 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4542 if (matcher.match(r1, r1.intent.getComponent())) {
4543 activities.add(r1);
4544 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004545 }
4546 }
4547 }
4548
4549 return activities;
4550 }
4551
4552 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004553 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004554
4555 // All activities that came from the package must be
4556 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004557 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4558 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4559 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4560 final ActivityRecord a = activities.get(activityNdx);
4561 if (a.info.packageName.equals(packageName)) {
4562 a.forceNewConfig = true;
4563 if (starting != null && a == starting && a.visible) {
4564 a.startFreezingScreenLocked(starting.app,
4565 ActivityInfo.CONFIG_SCREEN_LAYOUT);
4566 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004567 }
4568 }
4569 }
4570
4571 return starting;
4572 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004573
Craig Mautner299f9602015-01-26 09:47:33 -08004574 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004575 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004576 }
4577
Wale Ogunwale000957c2015-04-03 08:19:12 -07004578 /**
4579 * Removes the input task from this stack.
4580 * @param task to remove.
4581 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004582 * @param moving task to another stack. In the case we are moving we don't want to perform
4583 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004584 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004585 void removeTask(TaskRecord task, String reason, boolean moving) {
4586 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004587 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004588 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004589 if (!StackId.persistTaskBounds(mStackId)) {
4590 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4591 // default configuration the next time it launches.
4592 task.updateOverrideConfiguration(null);
4593 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004594 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004595
Craig Mautner04a0ea62014-01-13 12:51:26 -08004596 final ActivityRecord r = mResumedActivity;
4597 if (r != null && r.task == task) {
4598 mResumedActivity = null;
4599 }
4600
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004601 final int taskNdx = mTaskHistory.indexOf(task);
4602 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004603 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4604 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4605 if (!nextTask.isOverHomeStack()) {
4606 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4607 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004608 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004609 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004610 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004611
Wale Ogunwale040b4702015-08-06 18:10:50 -07004612 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004613 final boolean isVoiceSession = task.voiceSession != null;
4614 if (isVoiceSession) {
4615 try {
4616 task.voiceSession.taskFinished(task.intent, task.taskId);
4617 } catch (RemoteException e) {
4618 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004619 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004620 if (task.autoRemoveFromRecents() || isVoiceSession) {
4621 // Task creator asked to remove this when done, or this task was a voice
4622 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004623 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004624 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004625 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004626 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004627
4628 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004629 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004630 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004631 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004632 String myReason = reason + " leftTaskHistoryEmpty";
4633 if (mFullscreen || !adjustFocusToNextVisibleStackLocked(null, myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004634 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004635 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004636 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004637 if (mStacks != null) {
4638 mStacks.remove(this);
4639 mStacks.add(0, this);
4640 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004641 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004642 mActivityContainer.onTaskListEmptyLocked();
4643 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004644 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004645
4646 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004647 }
4648
Dianne Hackborn91097de2014-04-04 18:02:06 -07004649 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4650 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4651 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004652 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4653 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004654 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08004655 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08004656 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
4657 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.mResizeable
4658 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004659 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004660 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004661 return task;
4662 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004663
Wale Ogunwale935e5022015-11-10 12:36:10 -08004664 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4665 if (mTaskPositioner == null) {
4666 return false;
4667 }
4668 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4669 return true;
4670 }
4671
Craig Mautnerc00204b2013-03-05 15:02:14 -08004672 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004673 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004674 }
4675
Wale Ogunwale5f986092015-12-04 15:35:38 -08004676 void addTask(final TaskRecord task, final boolean toTop, String reason) {
4677 final ActivityStack prevStack = preAddTask(task, reason);
4678
Craig Mautnerc00204b2013-03-05 15:02:14 -08004679 task.stack = this;
4680 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004681 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004682 } else {
4683 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004684 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004685 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08004686 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004687 }
4688
Wale Ogunwale5f986092015-12-04 15:35:38 -08004689 void positionTask(final TaskRecord task, int position) {
4690 final ActivityStack prevStack = preAddTask(task, "positionTask");
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004691 task.stack = this;
4692 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004693 postAddTask(task, prevStack);
4694 }
4695
4696 private ActivityStack preAddTask(TaskRecord task, String reason) {
4697 final ActivityStack prevStack = task.stack;
4698 if (prevStack != null && prevStack != this) {
4699 prevStack.removeTask(task, reason, MOVING);
4700 }
4701 return prevStack;
4702 }
4703
4704 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
4705 if (prevStack != null) {
4706 if (prevStack != this
4707 && (prevStack.mStackId == PINNED_STACK_ID || mStackId == PINNED_STACK_ID)) {
4708 task.reportPictureInPictureModeChange();
4709 }
4710 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004711 try {
4712 task.voiceSession.taskStarted(task.intent, task.taskId);
4713 } catch (RemoteException e) {
4714 }
4715 }
4716 }
4717
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004718 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07004719 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004720 task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004721 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4722 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
4723 (r.info.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId,
4724 r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind,
Wale Ogunwale61b009e2015-09-16 15:43:05 -07004725 bounds, task.mOverrideConfig, !r.isHomeActivity());
Chong Zhangb15758a2015-11-17 12:12:03 -08004726 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004727 r.taskConfigOverride = task.mOverrideConfig;
4728 }
4729
Wale Ogunwale079a0042015-10-24 11:44:07 -07004730 void setFocusAndResumeStateIfNeeded(
4731 ActivityRecord r, boolean setFocus, boolean setResume, String reason) {
4732 // If the activity had focus before move focus to this stack.
4733 if (setFocus) {
4734 // If the activity owns the last resumed activity, transfer that together,
4735 // so that we don't resume the same activity again in the new stack.
4736 // Apps may depend on onResume()/onPause() being called in pairs.
4737 if (setResume) {
4738 mResumedActivity = r;
4739 // Move the stack in which we are placing the activity to the front. We don't use
4740 // ActivityManagerService.setFocusedActivityLocked, because if the activity is
4741 // already focused, the call will short-circuit and do nothing.
4742 moveToFront(reason);
4743 } else {
4744 // We need to not only move the stack to the front, but also have the activity
4745 // focused. This will achieve both goals.
4746 mService.setFocusedActivityLocked(r, reason);
4747 }
4748 }
4749 }
4750
4751 /**
4752 * Moves the input activity from its current stack to this one.
4753 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4754 * created on this stack which the activity is added to.
4755 * */
4756 void moveActivityToStack(ActivityRecord r) {
4757 final ActivityStack prevStack = r.task.stack;
4758 if (prevStack.mStackId == mStackId) {
4759 // You are already in the right stack silly...
4760 return;
4761 }
4762
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004763 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004764 && (mStackSupervisor.topRunningActivityLocked() == r);
4765 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4766
4767 final TaskRecord task = createTaskRecord(
4768 mStackSupervisor.getNextTaskId(), r.info, r.intent, null, null, true);
4769 r.setTask(task, null);
4770 task.addActivityToTop(r);
4771 setAppTask(r, task);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004772 task.reportPictureInPictureModeChange();
Wale Ogunwale079a0042015-10-24 11:44:07 -07004773 setFocusAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4774 }
4775
Wale Ogunwale706ed792015-08-02 10:29:44 -07004776 private void setAppTask(ActivityRecord r, TaskRecord task) {
4777 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004778 task.updateOverrideConfiguration(bounds);
4779 mWindowManager.setAppTask(
Wale Ogunwale2998eef2015-12-02 19:46:29 -08004780 r.appToken, task.taskId, mStackId, task.getLaunchBounds(), task.mOverrideConfig);
Chong Zhangb15758a2015-11-17 12:12:03 -08004781 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004782 r.taskConfigOverride = task.mOverrideConfig;
4783 }
4784
Craig Mautnerc00204b2013-03-05 15:02:14 -08004785 public int getStackId() {
4786 return mStackId;
4787 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004788
4789 @Override
4790 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004791 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4792 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004793 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004794
Craig Mautner15df08a2015-04-01 12:17:18 -07004795 void onLockTaskPackagesUpdatedLocked() {
4796 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4797 mTaskHistory.get(taskNdx).setLockTaskAuth();
4798 }
4799 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004800}