blob: b052d17a8dcb6b442c0e07afa67fa5b8d7b17b80 [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
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800148 // How many activities have to be scheduled to stop to force a stop pass.
149 private static final int MAX_STOPPING_TO_FORCE = 3;
150
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700151 enum ActivityState {
152 INITIALIZING,
153 RESUMED,
154 PAUSING,
155 PAUSED,
156 STOPPING,
157 STOPPED,
158 FINISHING,
159 DESTROYING,
160 DESTROYED
161 }
162
163 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700164 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800165 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800166
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700167 /**
168 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800169 * running) activities. It contains #TaskRecord objects.
170 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800171 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800172
173 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800174 * Used for validating app tokens with window manager.
175 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800176 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800177
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700178 /**
179 * List of running activities, sorted by recent usage.
180 * The first entry in the list is the least recently used.
181 * It contains HistoryRecord objects.
182 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800183 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700184
185 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700186 * Animations that for the current transition have requested not to
187 * be considered for the transition animation.
188 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800189 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700190
191 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700192 * When we are in the process of pausing an activity, before starting the
193 * next one, this variable holds the activity that is currently being paused.
194 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800195 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700196
197 /**
198 * This is the last activity that we put into the paused state. This is
199 * used to determine if we need to do an activity transition while sleeping,
200 * when we normally hold the top activity paused.
201 */
202 ActivityRecord mLastPausedActivity = null;
203
204 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700205 * Activities that specify No History must be removed once the user navigates away from them.
206 * If the device goes to sleep with such an activity in the paused state then we save it here
207 * and finish it later if another activity replaces it on wakeup.
208 */
209 ActivityRecord mLastNoHistoryActivity = null;
210
211 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700212 * Current activity that is resumed, or null if there is none.
213 */
214 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800215
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700216 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700217 * This is the last activity that has been started. It is only used to
218 * identify when multiple activities are started at once so that the user
219 * can be warned they may not be in the activity they think they are.
220 */
221 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800222
Craig Mautner5eda9b32013-07-02 11:58:16 -0700223 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
224 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
225 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
226 // Activity in mTranslucentActivityWaiting is notified via
227 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
228 // background activity being drawn then the same call will be made with a true value.
229 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700230 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700231
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700232 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700233 * Set when we know we are going to be calling updateConfiguration()
234 * soon, so want to skip intermediate config checks.
235 */
236 boolean mConfigWillChange;
237
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700238 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800239 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700240 // Current bounds of the stack or null if fullscreen.
241 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800242
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700243 long mLaunchStartTime = 0;
244 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800245
Craig Mautner858d8a62013-04-23 17:08:34 -0700246 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700247
Craig Mautnerc00204b2013-03-05 15:02:14 -0800248 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800249 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800250 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
251 ArrayList<ActivityStack> mStacks;
252 /** The attached Display's unique identifier, or -1 if detached */
253 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800254
Craig Mautner27084302013-03-25 08:05:25 -0700255 /** Run all ActivityStacks through this */
256 final ActivityStackSupervisor mStackSupervisor;
257
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700258 private final LaunchingTaskPositioner mTaskPositioner;
259
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700260 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700261 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
262 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
263 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
264 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700265 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700266 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700267 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700268
269 static class ScheduleDestroyArgs {
270 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700271 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700272 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700273 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700274 mReason = reason;
275 }
276 }
277
Zoran Marcetaf958b322012-08-09 20:27:12 +0900278 final Handler mHandler;
279
280 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800281
Craig Mautnerc8143c62013-09-03 12:15:57 -0700282 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900283 super(looper);
284 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700285
Zoran Marcetaf958b322012-08-09 20:27:12 +0900286 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700287 public void handleMessage(Message msg) {
288 switch (msg.what) {
289 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800290 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700291 // We don't at this point know if the activity is fullscreen,
292 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800293 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700294 synchronized (mService) {
295 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700296 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700297 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700298 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800299 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700300 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700301 case LAUNCH_TICK_MSG: {
302 ActivityRecord r = (ActivityRecord)msg.obj;
303 synchronized (mService) {
304 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700305 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700306 }
307 }
308 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700309 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800310 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700311 // We don't at this point know if the activity is fullscreen,
312 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800313 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800314 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800315 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800316 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700317 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700318 case STOP_TIMEOUT_MSG: {
319 ActivityRecord r = (ActivityRecord)msg.obj;
320 // We don't at this point know if the activity is fullscreen,
321 // so we need to be conservative and assume it isn't.
322 Slog.w(TAG, "Activity stop timeout for " + r);
323 synchronized (mService) {
324 if (r.isInHistory()) {
325 activityStoppedLocked(r, null, null, null);
326 }
327 }
328 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700329 case DESTROY_ACTIVITIES_MSG: {
330 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
331 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700332 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700333 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700334 } break;
335 case TRANSLUCENT_TIMEOUT_MSG: {
336 synchronized (mService) {
337 notifyActivityDrawnLocked(null);
338 }
339 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700340 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700341 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700342 final ActivityRecord r = getVisibleBehindActivity();
343 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700344 if (r != null) {
345 mService.killAppAtUsersRequest(r.app, null);
346 }
347 }
348 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700349 }
350 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800351 }
352
Craig Mautner34b73df2014-01-12 21:11:08 -0800353 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800354 int count = 0;
355 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
356 count += mTaskHistory.get(taskNdx).mActivities.size();
357 }
358 return count;
359 }
360
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800361 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
362 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800363 mActivityContainer = activityContainer;
364 mStackSupervisor = activityContainer.getOuter();
365 mService = mStackSupervisor.mService;
366 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
367 mWindowManager = mService.mWindowManager;
368 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700369 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800370 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700371 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
372 ? new LaunchingTaskPositioner() : null;
373 }
374
375 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
376 mDisplayId = activityDisplay.mDisplayId;
377 mStacks = activityDisplay.mStacks;
378 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
379 mFullscreen = mBounds == null;
380 if (mTaskPositioner != null) {
381 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
382 mTaskPositioner.configure(mBounds);
383 }
384 }
385
386 void detachDisplay() {
387 mDisplayId = Display.INVALID_DISPLAY;
388 mStacks = null;
389 if (mTaskPositioner != null) {
390 mTaskPositioner.reset();
391 }
392 mWindowManager.detachStack(mStackId);
393 }
394
Winsonc809cbb2015-11-02 12:06:15 -0800395 public void getDisplaySize(Point out) {
396 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
397 }
398
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700399 void setBounds(Rect bounds) {
400 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700401 if (mTaskPositioner != null) {
402 mTaskPositioner.configure(bounds);
403 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700404 }
Craig Mautner5962b122012-10-05 14:45:52 -0700405
Amith Yamasani734983f2014-03-04 16:48:05 -0800406 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700407 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700408 }
409
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700410 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800411 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700412 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700413 if (r != null) {
414 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800415 }
416 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700417 return null;
418 }
419
420 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800421 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
422 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800423 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800424 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
425 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800426 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800427 return r;
428 }
429 }
430 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700431 return null;
432 }
433
434 /**
435 * This is a simplified version of topRunningActivityLocked that provides a number of
436 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800437 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700438 * @param token If non-null, any history records matching this token will be skipped.
439 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800440 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700441 * @return Returns the HistoryRecord of the next activity on the stack.
442 */
443 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800444 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
445 TaskRecord task = mTaskHistory.get(taskNdx);
446 if (task.taskId == taskId) {
447 continue;
448 }
449 ArrayList<ActivityRecord> activities = task.mActivities;
450 for (int i = activities.size() - 1; i >= 0; --i) {
451 final ActivityRecord r = activities.get(i);
452 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800453 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800454 return r;
455 }
456 }
457 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700458 return null;
459 }
460
Craig Mautner8849a5e2013-04-02 16:41:03 -0700461 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700462 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
463 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700464 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
465 final ActivityRecord r = activities.get(activityNdx);
466 if (!r.finishing) {
467 return r;
468 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700469 }
470 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700471 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700472 }
473
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700474 final TaskRecord topTask() {
475 final int size = mTaskHistory.size();
476 if (size > 0) {
477 return mTaskHistory.get(size - 1);
478 }
479 return null;
480 }
481
Craig Mautnerd2328952013-03-05 12:46:26 -0800482 TaskRecord taskForIdLocked(int id) {
483 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
484 final TaskRecord task = mTaskHistory.get(taskNdx);
485 if (task.taskId == id) {
486 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800487 }
488 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700489 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700490 }
491
Craig Mautnerd2328952013-03-05 12:46:26 -0800492 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700493 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800494 return isInStackLocked(r);
495 }
496
497 ActivityRecord isInStackLocked(ActivityRecord r) {
498 if (r == null) {
499 return null;
500 }
501 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700502 if (task != null && task.stack != null
503 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800504 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800505 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800506 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800507 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800508 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800509 }
510
Craig Mautner2420ead2013-04-01 17:13:20 -0700511 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700512 final boolean hadit = mLRUActivities.remove(r);
513 mLRUActivities.add(r);
514 return hadit;
515 }
516
Craig Mautnerde4ef022013-04-07 19:01:33 -0700517 final boolean isHomeStack() {
518 return mStackId == HOME_STACK_ID;
519 }
520
Craig Mautnere0a38842013-12-16 16:14:02 -0800521 final boolean isOnHomeDisplay() {
522 return isAttached() &&
523 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
524 }
525
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700526 void moveToFront(String reason) {
527 moveToFront(reason, null);
528 }
529
530 /**
531 * @param reason The reason for moving the stack to the front.
532 * @param task If non-null, the task will be moved to the top of the stack.
533 * */
534 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700535 if (!isAttached()) {
536 return;
537 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700538
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700539 mStacks.remove(this);
540 int addIndex = mStacks.size();
541
542 if (addIndex > 0) {
543 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800544 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
545 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700546 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800547 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700548 }
549
550 mStacks.add(addIndex, this);
551
552 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
553 if (isOnHomeDisplay()) {
554 mStackSupervisor.setFocusStack(reason, this);
555 }
556 if (task != null) {
557 insertTaskAtTop(task, null);
558 } else {
559 task = topTask();
560 }
561 if (task != null) {
562 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800563 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800564 }
565
566 final boolean isAttached() {
567 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800568 }
569
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700570 /**
571 * Returns the top activity in any existing task matching the given
572 * Intent. Returns null if no such task is found.
573 */
Craig Mautnerac6f8432013-07-17 13:24:59 -0700574 ActivityRecord findTaskLocked(ActivityRecord target) {
575 Intent intent = target.intent;
576 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700577 ComponentName cls = intent.getComponent();
578 if (info.targetActivity != null) {
579 cls = new ComponentName(info.packageName, info.targetActivity);
580 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700581 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700582 boolean isDocument = intent != null & intent.isDocument();
583 // If documentData is non-null then it must match the existing task data.
584 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800585
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700586 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800587 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
588 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700589 if (task.voiceSession != null) {
590 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700591 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700592 continue;
593 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700594 if (task.userId != userId) {
595 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700596 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700597 continue;
598 }
Craig Mautner000f0022013-02-26 15:04:29 -0800599 final ActivityRecord r = task.getTopActivity();
600 if (r == null || r.finishing || r.userId != userId ||
601 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700602 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800603 continue;
604 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700605 if (r.mActivityType != target.mActivityType) {
606 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
607 continue;
608 }
Craig Mautner000f0022013-02-26 15:04:29 -0800609
Craig Mautnerd00f4742014-03-12 14:17:26 -0700610 final Intent taskIntent = task.intent;
611 final Intent affinityIntent = task.affinityIntent;
612 final boolean taskIsDocument;
613 final Uri taskDocumentData;
614 if (taskIntent != null && taskIntent.isDocument()) {
615 taskIsDocument = true;
616 taskDocumentData = taskIntent.getData();
617 } else if (affinityIntent != null && affinityIntent.isDocument()) {
618 taskIsDocument = true;
619 taskDocumentData = affinityIntent.getData();
620 } else {
621 taskIsDocument = false;
622 taskDocumentData = null;
623 }
624
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700625 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700626 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700627 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700628 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Dianne Hackborn79228822014-09-16 11:11:23 -0700629 if (!isDocument && !taskIsDocument && task.rootAffinity != null) {
630 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700631 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700632 return r;
633 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700634 } else if (taskIntent != null && taskIntent.getComponent() != null &&
635 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700636 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700637 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800638 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700639 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
640 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800641 return r;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700642 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
643 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700644 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700645 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800646 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700647 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
648 "For Intent " + intent + " bringing to top: " + r.intent);
Craig Mautner000f0022013-02-26 15:04:29 -0800649 return r;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700650 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700651 }
652
653 return null;
654 }
655
656 /**
657 * Returns the first activity (starting from the top of the stack) that
658 * is the same as the given activity. Returns null if no such activity
659 * is found.
660 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700661 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700662 ComponentName cls = intent.getComponent();
663 if (info.targetActivity != null) {
664 cls = new ComponentName(info.packageName, info.targetActivity);
665 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700666 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700667
Craig Mautner000f0022013-02-26 15:04:29 -0800668 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700669 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700670 final boolean notCurrentUserTask =
671 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700672 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700673
Craig Mautner000f0022013-02-26 15:04:29 -0800674 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
675 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700676 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700677 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700678 }
Craig Mautner000f0022013-02-26 15:04:29 -0800679 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700680 return r;
681 }
682 }
683 }
684
685 return null;
686 }
687
Amith Yamasani742a6712011-05-04 14:49:28 -0700688 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700689 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700690 */
Craig Mautner93529a42013-10-04 15:03:13 -0700691 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800692 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700693 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800694 }
695 mCurrentUser = userId;
696
697 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800698 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800699 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700700 final TaskRecord task = mTaskHistory.get(i);
701
702 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
703 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700704 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700705 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700706 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700707 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800708 mTaskHistory.remove(i);
709 mTaskHistory.add(task);
710 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800711 // Use same value for i.
712 } else {
713 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800714 }
715 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700716 if (VALIDATE_TOKENS) {
717 validateAppTokensLocked();
718 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700719 }
720
Craig Mautner2420ead2013-04-01 17:13:20 -0700721 void minimalResumeActivityLocked(ActivityRecord r) {
722 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700723 if (DEBUG_STATES) Slog.v(TAG_STATES,
724 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700725 r.stopped = false;
726 mResumedActivity = r;
727 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800728 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700729 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700730 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700731 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700732 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
733 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700734 }
735
Chong Zhang45c25ce2015-08-10 22:18:26 -0700736 private void addRecentActivityLocked(ActivityRecord r) {
737 if (r != null) {
738 mRecentTasks.addLocked(r.task);
739 r.task.touchActiveTime();
740 }
741 }
742
Narayan Kamath7829c812015-06-08 17:39:43 +0100743 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700744 if (mFullyDrawnStartTime != 0) {
745 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
746 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100747 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700748 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
749 }
750
751 private void stopFullyDrawnTraceIfNeeded() {
752 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
753 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
754 mFullyDrawnStartTime = 0;
755 }
756 }
757
Craig Mautnere79d42682013-04-01 19:01:53 -0700758 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700759 if (r.displayStartTime == 0) {
760 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
761 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100762 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700763 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700764 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700765 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100766 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700767 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700768 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700769 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800770
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700771 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700772 // Make sure that there is no activity waiting for this to launch.
773 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
774 r.displayStartTime = r.fullyDrawnStartTime = 0;
775 } else {
776 mStackSupervisor.removeTimeoutsForActivityLocked(r);
777 mStackSupervisor.scheduleIdleTimeoutLocked(r);
778 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700779 }
780
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800781 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800782 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800783 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
784 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
785 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
786 activities.get(activityNdx).setSleeping(false);
787 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800788 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800789 if (mPausingActivity != null) {
790 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
791 activityPausedLocked(mPausingActivity.appToken, true);
792 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800793 }
794
Craig Mautner0eea92c2013-05-16 13:35:39 -0700795 /**
796 * @return true if something must be done before going to sleep.
797 */
798 boolean checkReadyForSleepLocked() {
799 if (mResumedActivity != null) {
800 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700801 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
802 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
803 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700804 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700805 return true;
806 }
807 if (mPausingActivity != null) {
808 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700809 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700810 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800811 }
812
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700813 if (hasVisibleBehindActivity()) {
814 // Stop visible behind activity before going to sleep.
815 final ActivityRecord r = mActivityContainer.mActivityDisplay.mVisibleBehindActivity;
816 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700817 if (DEBUG_STATES) Slog.v(TAG_STATES,
818 "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700819 return true;
820 }
821
Craig Mautner0eea92c2013-05-16 13:35:39 -0700822 return false;
823 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800824
Craig Mautner0eea92c2013-05-16 13:35:39 -0700825 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700826 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800827
Craig Mautner0eea92c2013-05-16 13:35:39 -0700828 // Make sure any stopped but visible activities are now sleeping.
829 // This ensures that the activity's onStop() is called.
830 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
831 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
832 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
833 final ActivityRecord r = activities.get(activityNdx);
834 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
835 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800836 }
837 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800838 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700839 }
Craig Mautner59c00972012-07-30 12:10:24 -0700840
Winson8b1871d2015-11-20 09:56:20 -0800841 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
842 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800843 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700844 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800845 return null;
846 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800847
Winson Chung083baf92014-07-11 10:32:42 -0700848 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700849 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800850 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700851 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700852 return null;
853 }
854
Winson Chung48a10a52014-08-27 14:36:51 -0700855 int w = mService.mThumbnailWidth;
856 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800857 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800858 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700859 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800860
861 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800862 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
863 // use within SystemUI while keeping memory usage low.
Winson8b1871d2015-11-20 09:56:20 -0800864 if (mService.mTakeFullscreenScreenshots) {
Winson8b1871d2015-11-20 09:56:20 -0800865 w = h = -1;
Winson387aac62015-11-25 11:18:56 -0800866 scale = 0.5f;
Winson8b1871d2015-11-20 09:56:20 -0800867 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800868 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800869 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800870 }
Winson Chung376543b2014-05-21 17:43:28 -0700871 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800872 return null;
873 }
874
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700875 /**
876 * Start pausing the currently resumed activity. It is an error to call this if there
877 * is already an activity being paused or there is no resumed activity.
878 *
879 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
880 * @param uiSleeping True if this is happening with the user interface going to sleep (the
881 * screen turning off).
882 * @param resuming True if this is being called as part of resuming the top activity, so
883 * we shouldn't try to instigate a resume here.
884 * @param dontWait True if the caller does not want to wait for the pause to complete. If
885 * set to true, we will immediately complete the pause here before returning.
886 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
887 * it to tell us when it is done.
888 */
889 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
890 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800891 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800892 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
893 + " state=" + mPausingActivity.state);
894 if (!mService.isSleeping()) {
895 // Avoid recursion among check for sleep and complete pause during sleeping.
896 // Because activity will be paused immediately after resume, just let pause
897 // be completed by the order of activity paused from clients.
898 completePauseLocked(false);
899 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800900 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700901 ActivityRecord prev = mResumedActivity;
902 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700903 if (!resuming) {
904 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
905 mStackSupervisor.resumeTopActivitiesLocked();
906 }
907 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700908 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800909
910 if (mActivityContainer.mParentActivity == null) {
911 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700912 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800913 }
914
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700915 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700916 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700917 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800918 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700919 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700920 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
921 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700922 prev.state = ActivityState.PAUSING;
923 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700924 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700925 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700926 if (mService.mHasRecents
927 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800928 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700929 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700930 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700931
932 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800933
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700934 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700935 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700936 try {
937 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700938 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700939 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700940 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800941 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700942 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700943 } catch (Exception e) {
944 // Ignore exception, if process died other code will cleanup.
945 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800946 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700947 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700948 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700949 }
950 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800951 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700952 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700953 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700954 }
955
956 // If we are not going to sleep, we want to ensure the device is
957 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700958 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700959 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700960 }
961
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800962 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700963 // Have the window manager pause its key dispatching until the new
964 // activity has started. If we're pausing the activity just because
965 // the screen is being turned off and the UI is sleeping, don't interrupt
966 // key dispatch; the same activity will pick it up again on wakeup.
967 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800968 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700969 } else if (DEBUG_PAUSE) {
970 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700971 }
972
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700973 if (dontWait) {
974 // If the caller said they don't want to wait for the pause, then complete
975 // the pause now.
976 completePauseLocked(false);
977 return false;
978
979 } else {
980 // Schedule a pause timeout in case the app doesn't respond.
981 // We don't give it much time because this directly impacts the
982 // responsiveness seen by the user.
983 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
984 msg.obj = prev;
985 prev.pauseTime = SystemClock.uptimeMillis();
986 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700987 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700988 return true;
989 }
990
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700991 } else {
992 // This activity failed to schedule the
993 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700994 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700995 if (!resuming) {
996 mStackSupervisor.getFocusedStack().resumeTopActivityLocked(null);
997 }
998 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700999 }
1000 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001001
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001002 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001003 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1004 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001005
Craig Mautnerd2328952013-03-05 12:46:26 -08001006 final ActivityRecord r = isInStackLocked(token);
1007 if (r != null) {
1008 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1009 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001010 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001011 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001012 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -08001013 } else {
1014 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1015 r.userId, System.identityHashCode(r), r.shortComponentName,
1016 mPausingActivity != null
1017 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001018 if (r.state == ActivityState.PAUSING) {
1019 r.state = ActivityState.PAUSED;
1020 if (r.finishing) {
1021 if (DEBUG_PAUSE) Slog.v(TAG,
1022 "Executing finish of failed to pause activity: " + r);
1023 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1024 }
louis_chang047dfd42015-04-08 16:35:55 +08001025 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001026 }
1027 }
1028 }
1029
Craig Mautnera0026042014-04-23 11:45:37 -07001030 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1031 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001032 if (r.state != ActivityState.STOPPING) {
1033 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1034 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1035 return;
1036 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001037 if (persistentState != null) {
1038 r.persistentState = persistentState;
1039 mService.notifyTaskPersisterLocked(r.task, false);
1040 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001041 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001042 if (icicle != null) {
1043 // If icicle is null, this is happening due to a timeout, so we
1044 // haven't really saved the state.
1045 r.icicle = icicle;
1046 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001047 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001048 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001049 }
1050 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001051 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001052 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1053 r.stopped = true;
1054 r.state = ActivityState.STOPPED;
Jose Lima4b6c6692014-08-12 17:41:12 -07001055 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == r) {
1056 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001057 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001058 if (r.finishing) {
1059 r.clearOptionsLocked();
1060 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001061 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001062 destroyActivityLocked(r, true, "stop-config");
Craig Mautner05d29032013-05-03 13:40:13 -07001063 mStackSupervisor.resumeTopActivitiesLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001064 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001065 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001066 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001067 }
1068 }
1069 }
1070
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001071 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001072 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001073 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001074
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001075 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001076 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001077 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001078 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001079 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001080 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001081 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001082 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001083 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001084 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1085 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001086 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001087 if (prev.configDestroy) {
1088 // The previous is being paused because the configuration
1089 // is changing, which means it is actually stopping...
1090 // To juggle the fact that we are also starting a new
1091 // instance right now, we need to first completely stop
1092 // the current instance before starting the new one.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001093 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Destroying after pause: " + prev);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001094 destroyActivityLocked(prev, true, "pause-config");
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001095 } else if (wasStopping) {
1096 // We are also stopping, the stop request must have gone soon after the pause.
1097 // We can't clobber it, because the stop confirmation will not be handled.
1098 // We don't need to schedule another stop, we only need to let it happen.
1099 prev.state = ActivityState.STOPPING;
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001100 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001101 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001102 // stopping.
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001103 addToStopping(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001104 }
1105 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001106 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001107 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001108 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001109 // It is possible the activity was freezing the screen before it was paused.
1110 // In that case go ahead and remove the freeze this activity has on the screen
1111 // since it is no longer visible.
1112 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001113 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001114 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001115
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001116 if (resumeNext) {
1117 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1118 if (!mService.isSleepingOrShuttingDown()) {
1119 mStackSupervisor.resumeTopActivitiesLocked(topStack, prev, null);
1120 } else {
1121 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001122 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001123 if (top == null || (prev != null && top != prev)) {
1124 // If there are no more activities available to run,
1125 // do resume anyway to start something. Also if the top
1126 // activity on the stack is not the just paused activity,
1127 // we need to go ahead and resume it to ensure we complete
1128 // an in-flight app switch.
1129 mStackSupervisor.resumeTopActivitiesLocked(topStack, null, null);
1130 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001131 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001132 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001133
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001134 if (prev != null) {
1135 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001136
Craig Mautner525f3d92013-05-07 14:01:50 -07001137 if (prev.app != null && prev.cpuTimeAtResume > 0
1138 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001139 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1140 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001141 if (diff > 0) {
1142 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1143 synchronized (bsi) {
1144 BatteryStatsImpl.Uid.Proc ps =
1145 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001146 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001147 if (ps != null) {
1148 ps.addForegroundTimeLocked(diff);
1149 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001150 }
1151 }
1152 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001153 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001154 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001155
1156 // Notfiy when the task stack has changed
1157 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001158 }
1159
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001160 private void addToStopping(ActivityRecord r) {
1161 mStackSupervisor.mStoppingActivities.add(r);
1162 if (mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE ||
1163 r.frontOfTask && mTaskHistory.size() <= 1) {
1164 // If we already have a few activities waiting to stop,
1165 // then give up on things going idle and start clearing
1166 // them out. Or if r is the last of activity of the last task the stack
1167 // will be empty and must be cleared immediately.
1168 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
1169 mStackSupervisor.scheduleIdleLocked();
1170 } else {
1171 mStackSupervisor.checkReadyForSleepLocked();
1172 }
1173 }
1174
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001175 /**
1176 * Once we know that we have asked an application to put an activity in
1177 * the resumed state (either by launching it or explicitly telling it),
1178 * this function updates the rest of our state to match that fact.
1179 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001180 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001181 next.idle = false;
1182 next.results = null;
1183 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001184
Chong Zhang85ee6542015-10-02 13:36:38 -07001185 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001186 ProcessRecord app = next.task.mActivities.get(0).app;
1187 if (app != null && app != mService.mHomeProcess) {
1188 mService.mHomeProcess = app;
1189 }
1190 }
1191
Craig Mautner07566322013-09-26 16:42:55 -07001192 if (next.nowVisible) {
1193 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001194 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001195 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001196
1197 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001198 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001199
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001200 mStackSupervisor.reportResumedActivityLocked(next);
1201
1202 next.resumeKeyDispatchingLocked();
1203 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001204
1205 // Mark the point when the activity is resuming
1206 // TODO: To be more accurate, the mark should be before the onCreate,
1207 // not after the onResume. But for subsequent starts, onResume is fine.
1208 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001209 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001210 } else {
1211 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1212 }
Winson Chung376543b2014-05-21 17:43:28 -07001213
George Mount6ba042b2014-07-28 11:12:28 -07001214 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001215
1216 if (mActivityContainer.mActivityDisplay.mVisibleBehindActivity == next) {
1217 // When resuming an activity, require it to call requestVisibleBehind() again.
1218 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(null);
1219 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001220 }
1221
Craig Mautner2568c3a2015-03-26 14:22:34 -07001222 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001223 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001224 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1225 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1226 r.mUpdateTaskThumbnailWhenHidden = false;
1227 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001228 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001229 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001230 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001231 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001232 container.setVisible(visible);
1233 }
1234 }
1235
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001236 // Find the first visible activity above the passed activity and if it is translucent return it
1237 // otherwise return null;
1238 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1239 TaskRecord task = r.task;
1240 if (task == null) {
1241 return null;
1242 }
1243
1244 ActivityStack stack = task.stack;
1245 if (stack == null) {
1246 return null;
1247 }
1248
1249 int stackNdx = mStacks.indexOf(stack);
1250
1251 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1252 int taskNdx = tasks.indexOf(task);
1253
1254 ArrayList<ActivityRecord> activities = task.mActivities;
1255 int activityNdx = activities.indexOf(r) + 1;
1256
1257 final int numStacks = mStacks.size();
1258 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001259 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001260 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001261 final int numTasks = tasks.size();
1262 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001263 final TaskRecord currentTask = tasks.get(taskNdx);
1264 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001265 final int numActivities = activities.size();
1266 while (activityNdx < numActivities) {
1267 final ActivityRecord activity = activities.get(activityNdx);
1268 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001269 return historyStack.mFullscreen
1270 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001271 }
1272 ++activityNdx;
1273 }
1274 activityNdx = 0;
1275 ++taskNdx;
1276 }
1277 taskNdx = 0;
1278 ++stackNdx;
1279 }
1280
1281 return null;
1282 }
1283
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001284 private ActivityStack getNextVisibleStackLocked() {
1285 ArrayList<ActivityStack> stacks = mStacks;
1286 final ActivityRecord parent = mActivityContainer.mParentActivity;
1287 if (parent != null) {
1288 stacks = parent.task.stack.mStacks;
1289 }
1290 if (stacks != null) {
1291 for (int i = stacks.size() - 1; i >= 0; --i) {
1292 ActivityStack stack = stacks.get(i);
1293 if (stack != this && stack.isStackVisibleLocked()) {
1294 return stack;
1295 }
1296 }
1297 }
1298 return null;
1299 }
1300
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001301 /** Returns true if the stack contains a fullscreen task. */
1302 private boolean hasFullscreenTask() {
1303 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1304 final TaskRecord task = mTaskHistory.get(i);
1305 if (task.mFullscreen) {
1306 return true;
1307 }
1308 }
1309 return false;
1310 }
1311
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001312 private boolean hasTranslucentActivity(ActivityStack stack) {
1313 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1314 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1315 final TaskRecord task = tasks.get(taskNdx);
1316 final ArrayList<ActivityRecord> activities = task.mActivities;
1317 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1318 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001319
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001320 // Conditions for an activity to obscure the stack we're
1321 // examining:
1322 // 1. Not Finishing AND Visible AND:
1323 // 2. Either:
1324 // - Full Screen Activity OR
1325 // - On top of Home and our stack is NOT home
1326 if (!r.finishing && r.visible && (r.fullscreen ||
1327 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1328 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001329 }
1330 }
1331 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001332 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001333 }
1334
1335 /** Returns true if the stack is considered visible. */
Todd Kennedyaab56db2015-01-30 09:39:53 -08001336 private boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001337 if (!isAttached()) {
1338 return false;
1339 }
1340
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001341 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001342 return true;
1343 }
1344
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001345 final int stackIndex = mStacks.indexOf(this);
1346
1347 if (stackIndex == mStacks.size() - 1) {
1348 Slog.wtf(TAG,
1349 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1350 return false;
1351 }
1352
Chong Zhang75b37202015-12-04 14:16:36 -08001353 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1354 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
1355 return false;
1356 }
1357
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001358 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1359 final int focusedStackId = focusedStack.mStackId;
1360
1361 if (mStackId == DOCKED_STACK_ID) {
1362 // Docked stack is always visible, except in the case where the home activity
1363 // is the top running activity in the focused home stack.
1364 if (focusedStackId != HOME_STACK_ID) {
1365 return true;
1366 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001367 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked();
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001368 return topHomeActivity == null || !topHomeActivity.isHomeActivity();
1369 }
1370
Chong Zhangb16cf342015-11-12 17:22:40 -08001371 // Find the first stack below focused stack that actually got something visible.
1372 int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1373 while (belowFocusedIndex >= 0 &&
1374 mStacks.get(belowFocusedIndex).topRunningActivityLocked() == null) {
1375 belowFocusedIndex--;
1376 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001377 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1378 && stackIndex == belowFocusedIndex) {
1379 // Stacks directly behind the docked or pinned stack are always visible.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001380 return true;
1381 }
1382
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001383 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1384 && hasTranslucentActivity(focusedStack)) {
1385 // Stacks behind the fullscreen stack with a translucent activity are always
1386 // visible so they can act as a backdrop to the translucent activity.
1387 // For example, dialog activities
1388 if (stackIndex == belowFocusedIndex) {
1389 return true;
1390 }
1391 if (belowFocusedIndex >= 0) {
1392 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001393 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1394 && stackIndex == (belowFocusedIndex - 1)) {
1395 // The stack behind the docked or pinned stack is also visible so we can have a
1396 // complete backdrop to the translucent activity when the docked stack is up.
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001397 return true;
1398 }
1399 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001400 }
1401
Wale Ogunwale3797c222015-10-27 14:21:58 -07001402 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001403 // Visibility of any static stack should have been determined by the conditions above.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001404 return false;
1405 }
1406
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001407 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001408 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001409
1410 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1411 continue;
1412 }
1413
Wale Ogunwale3797c222015-10-27 14:21:58 -07001414 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001415 // These stacks can't have any dynamic stacks visible behind them.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001416 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001417 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001418
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001419 if (!hasTranslucentActivity(stack)) {
1420 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001421 }
1422 }
1423
1424 return true;
1425 }
1426
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001427 final int rankTaskLayers(int baseLayer) {
1428 int layer = 0;
1429 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1430 final TaskRecord task = mTaskHistory.get(taskNdx);
1431 ActivityRecord r = task.topRunningActivityLocked();
1432 if (r == null || r.finishing || !r.visible) {
1433 task.mLayerRank = -1;
1434 } else {
1435 task.mLayerRank = baseLayer + layer++;
1436 }
1437 }
1438 return layer;
1439 }
1440
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001441 /**
1442 * Make sure that all activities that need to be visible (that is, they
1443 * currently can be seen by the user) actually are.
1444 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001445 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1446 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001447 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001448 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001449 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001450 if (top != null) {
1451 checkTranslucentActivityWaiting(top);
1452 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001453
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001454 // If the top activity is not fullscreen, then we need to
1455 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001456 boolean aboveTop = top != null;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001457 final boolean stackInvisible = !isStackVisibleLocked();
1458 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaleaddd00e2015-04-04 17:10:50 -07001459 boolean noStackActivityResumed = (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001460
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001461 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001462 final TaskRecord task = mTaskHistory.get(taskNdx);
1463 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001464
Craig Mautnerd44711d2013-02-23 11:24:36 -08001465 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1466 final ActivityRecord r = activities.get(activityNdx);
1467 if (r.finishing) {
1468 continue;
1469 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001470 final boolean isTop = r == top;
1471 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001472 continue;
1473 }
1474 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001475 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1476 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001477 if ((!behindFullscreenActivity || r.mLaunchTaskBehind)
1478 && okToShowLocked(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001479 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1480 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001481 + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001482 // First: if this is not the current activity being started, make
1483 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001484 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001485 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001486 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001487
1488 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001489 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001490 noStackActivityResumed, r)) {
1491 if (activityNdx >= activities.size()) {
1492 // Record may be removed if its process needs to restart.
1493 activityNdx = activities.size() - 1;
1494 } else {
1495 noStackActivityResumed = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001496 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001497 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001498 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001499 // If this activity is already visible, then there is nothing to do here.
1500 if (handleAlreadyVisible(r)) {
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001501 noStackActivityResumed = false;
1502 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001503 } else {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001504 makeVisible(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001505 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001506 // Aggregate current change flags.
1507 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001508 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1509 behindFullscreenActivity, task, r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001510 } else {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001511 makeInvisible(stackInvisible, behindFullscreenActivity, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001512 }
1513 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001514 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1515 // The visibility of tasks and the activities they contain in freeform stack are
1516 // determined individually unlike other stacks where the visibility or fullscreen
1517 // status of an activity in a previous task affects other.
1518 behindFullscreenActivity = stackInvisible;
1519 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001520 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001521
1522 if (mTranslucentActivityWaiting != null &&
1523 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1524 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1525 notifyActivityDrawnLocked(null);
1526 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001527 }
Craig Mautner58547802013-03-05 08:23:53 -08001528
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001529 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1530 if (mTranslucentActivityWaiting != top) {
1531 mUndrawnActivitiesBelowTopTranslucent.clear();
1532 if (mTranslucentActivityWaiting != null) {
1533 // Call the callback with a timeout indication.
1534 notifyActivityDrawnLocked(null);
1535 mTranslucentActivityWaiting = null;
1536 }
1537 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1538 }
1539 }
1540
1541 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001542 boolean isTop, boolean noStackActivityResumed, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001543 // We need to make sure the app is running if it's the top, or it is just made visible from
1544 // invisible. If the app is already visible, it must have died while it was visible. In this
1545 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1546 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001547 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001548 // This activity needs to be visible, but isn't even running...
1549 // get it started and resume if no other stack in this stack is resumed.
1550 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1551 if (r != starting) {
1552 r.startFreezingScreenLocked(r.app, configChanges);
1553 }
1554 if (!r.visible || r.mLaunchTaskBehind) {
1555 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1556 setVisible(r, true);
1557 }
1558 if (r != starting) {
1559 mStackSupervisor.startSpecificActivityLocked(r, noStackActivityResumed, false);
1560 return true;
1561 }
1562 }
1563 return false;
1564 }
1565
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001566 private void makeInvisible(boolean stackInvisible, boolean behindFullscreenActivity,
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001567 ActivityRecord r) {
1568 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r + " finishing="
1569 + r.finishing + " state=" + r.state + " stackInvisible=" + stackInvisible
1570 + " behindFullscreenActivity=" + behindFullscreenActivity);
1571 if (!r.visible) {
1572 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1573 return;
1574 }
1575 // Now for any activities that aren't visible to the user, make sure they no longer are
1576 // keeping the screen frozen.
1577 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
1578 try {
1579 setVisible(r, false);
1580 switch (r.state) {
1581 case STOPPING:
1582 case STOPPED:
1583 if (r.app != null && r.app.thread != null) {
1584 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1585 "Scheduling invisibility: " + r);
1586 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1587 }
1588 break;
1589
1590 case INITIALIZING:
1591 case RESUMED:
1592 case PAUSING:
1593 case PAUSED:
1594 // This case created for transitioning activities from
1595 // translucent to opaque {@link Activity#convertToOpaque}.
1596 if (getVisibleBehindActivity() == r) {
1597 releaseBackgroundResources(r);
1598 } else {
1599 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1600 mStackSupervisor.mStoppingActivities.add(r);
1601 }
1602 mStackSupervisor.scheduleIdleLocked();
1603 }
1604 break;
1605
1606 default:
1607 break;
1608 }
1609 } catch (Exception e) {
1610 // Just skip on any failure; we'll make it visible when it next restarts.
1611 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1612 }
1613 }
1614
1615 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1616 TaskRecord task, ActivityRecord r) {
1617 if (r.fullscreen) {
1618 // At this point, nothing else needs to be shown in this task.
1619 behindFullscreenActivity = true;
1620 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
1621 + " stackInvisible=" + stackInvisible
1622 + " behindFullscreenActivity=" + behindFullscreenActivity);
1623 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
1624 behindFullscreenActivity = true;
1625 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1626 + " stackInvisible=" + stackInvisible
1627 + " behindFullscreenActivity=" + behindFullscreenActivity);
1628 }
1629 return behindFullscreenActivity;
1630 }
1631
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001632 private void makeVisible(ActivityRecord starting, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001633 // This activity is not currently visible, but is running. Tell it to become visible.
1634 r.visible = true;
1635 if (r.state != ActivityState.RESUMED && r != starting) {
1636 // If this activity is paused, tell it to now show its window.
1637 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1638 "Making visible and scheduling visibility: " + r);
1639 try {
1640 if (mTranslucentActivityWaiting != null) {
1641 r.updateOptionsLocked(r.returningOptions);
1642 mUndrawnActivitiesBelowTopTranslucent.add(r);
1643 }
1644 setVisible(r, true);
1645 r.sleeping = false;
1646 r.app.pendingUiClean = true;
1647 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1648 r.stopFreezingScreenLocked(false);
1649 } catch (Exception e) {
1650 // Just skip on any failure; we'll make it
1651 // visible when it next restarts.
1652 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
1653 }
1654 }
1655 }
1656
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001657 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001658 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1659 r.stopFreezingScreenLocked(false);
1660 try {
1661 if (r.returningOptions != null) {
1662 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1663 }
1664 } catch(RemoteException e) {
1665 }
1666 return r.state == ActivityState.RESUMED;
1667 }
1668
Todd Kennedyaab56db2015-01-30 09:39:53 -08001669 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001670 mTranslucentActivityWaiting = r;
1671 mUndrawnActivitiesBelowTopTranslucent.clear();
1672 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1673 }
1674
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001675 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1676 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1677 final TaskRecord task = mTaskHistory.get(taskNdx);
1678 final ArrayList<ActivityRecord> activities = task.mActivities;
1679 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1680 final ActivityRecord r = activities.get(activityNdx);
1681 if ( r.appTimeTracker != except) {
1682 r.appTimeTracker = null;
1683 }
1684 }
1685 }
1686 }
1687
Craig Mautner5eda9b32013-07-02 11:58:16 -07001688 /**
1689 * Called as activities below the top translucent activity are redrawn. When the last one is
1690 * redrawn notify the top activity by calling
1691 * {@link Activity#onTranslucentConversionComplete}.
1692 *
1693 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1694 * occurred and the activity will be notified immediately.
1695 */
1696 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001697 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001698 if ((r == null)
1699 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1700 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1701 // The last undrawn activity below the top has just been drawn. If there is an
1702 // opaque activity at the top, notify it that it can become translucent safely now.
1703 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1704 mTranslucentActivityWaiting = null;
1705 mUndrawnActivitiesBelowTopTranslucent.clear();
1706 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1707
Craig Mautner71dd1b62014-02-18 15:48:52 -08001708 if (waitingActivity != null) {
1709 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1710 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1711 try {
1712 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1713 waitingActivity.appToken, r != null);
1714 } catch (RemoteException e) {
1715 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001716 }
1717 }
1718 }
1719 }
1720
Craig Mautnera61bc652013-10-28 15:43:18 -07001721 /** If any activities below the top running one are in the INITIALIZING state and they have a
1722 * starting window displayed then remove that starting window. It is possible that the activity
1723 * in this state will never resumed in which case that starting window will be orphaned. */
1724 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001725 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001726 boolean aboveTop = true;
1727 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1728 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1729 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1730 final ActivityRecord r = activities.get(activityNdx);
1731 if (aboveTop) {
1732 if (r == topActivity) {
1733 aboveTop = false;
1734 }
1735 continue;
1736 }
1737
1738 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001739 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1740 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001741 r.mStartingWindowShown = false;
1742 mWindowManager.removeAppStartingWindow(r.appToken);
1743 }
1744 }
1745 }
1746 }
1747
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001748 /**
1749 * Ensure that the top activity in the stack is resumed.
1750 *
1751 * @param prev The previously resumed activity, for when in the process
1752 * of pausing; can be null to call from elsewhere.
1753 *
1754 * @return Returns true if something is being resumed, or false if
1755 * nothing happened.
1756 */
1757 final boolean resumeTopActivityLocked(ActivityRecord prev) {
Dianne Hackborn84375872012-06-01 19:03:50 -07001758 return resumeTopActivityLocked(prev, null);
1759 }
1760
Chong Zhang280d3322015-11-03 17:27:26 -08001761 final boolean resumeTopActivityLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001762 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001763 // Don't even start recursing.
1764 return false;
1765 }
1766
1767 boolean result = false;
1768 try {
1769 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001770 mStackSupervisor.inResumeTopActivity = true;
1771 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1772 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001773 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001774 }
Craig Mautner544efa72014-09-04 16:41:20 -07001775 result = resumeTopActivityInnerLocked(prev, options);
1776 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001777 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001778 }
1779 return result;
1780 }
1781
Chong Zhang280d3322015-11-03 17:27:26 -08001782 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001783 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001784
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001785 if (!mService.mBooting && !mService.mBooted) {
1786 // Not ready yet!
1787 return false;
1788 }
1789
Craig Mautnerdf88d732014-01-27 09:21:32 -08001790 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001791 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001792 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001793 // Do not resume this stack if its parent is not resumed.
1794 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1795 return false;
1796 }
1797
Craig Mautnera61bc652013-10-28 15:43:18 -07001798 cancelInitializingActivities();
1799
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001800 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001801 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001802
1803 // Remember how we'll process this pause/resume situation, and ensure
1804 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001805 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1806 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001807
Craig Mautner84984fa2014-06-19 11:19:20 -07001808 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001809 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001810 // There are no more activities!
1811 final String reason = "noMoreActivities";
1812 if (!mFullscreen) {
1813 // Try to move focus to the next visible stack with a running activity if this
1814 // stack is not covering the entire screen.
1815 final ActivityStack stack = getNextVisibleStackLocked();
1816 if (adjustFocusToNextVisibleStackLocked(stack, reason)) {
1817 return mStackSupervisor.resumeTopActivitiesLocked(stack, prev, null);
1818 }
1819 }
1820 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001821 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001822 if (DEBUG_STATES) Slog.d(TAG_STATES,
1823 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001824 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001825 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001826 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1827 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1828 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001829 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001830 }
1831
1832 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001833
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001834 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001835 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1836 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001837 // Make sure we have executed any pending transitions, since there
1838 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001839 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001840 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001841 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001842 if (DEBUG_STATES) Slog.d(TAG_STATES,
1843 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001844 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001845 return false;
1846 }
1847
Craig Mautner525f3d92013-05-07 14:01:50 -07001848 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001849 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001850 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001851 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001852 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001853 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001854 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001855 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001856 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001857 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001858 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001859 } else if (!isOnHomeDisplay()) {
1860 return false;
1861 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001862 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001863 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001864 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1865 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1866 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001867 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001868 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001869 }
1870
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001871 // If we are sleeping, and there is no resumed activity, and the top
1872 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001873 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001874 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001875 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001876 // Make sure we have executed any pending transitions, since there
1877 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001878 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001879 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001880 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001881 if (DEBUG_STATES) Slog.d(TAG_STATES,
1882 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001883 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001884 return false;
1885 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001886
1887 // Make sure that the user who owns this activity is started. If not,
1888 // we will just leave it as is because someone should be bringing
1889 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001890 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001891 Slog.w(TAG, "Skipping resume of top activity " + next
1892 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001893 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001894 return false;
1895 }
1896
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001897 // The activity may be waiting for stop, but that is no longer
1898 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001899 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001900 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001901 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001902 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001903
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001904 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001905
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001906 // If we are currently pausing an activity, then don't do anything
1907 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001908 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001909 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001910 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001911 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001912 return false;
1913 }
1914
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001915 // Okay we are now going to start a switch, to 'next'. We may first
1916 // have to pause the current activity, but this is an important point
1917 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001918 // XXX "App Redirected" dialog is getting too many false positives
1919 // at this point, so turn off for now.
1920 if (false) {
1921 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1922 long now = SystemClock.uptimeMillis();
1923 final boolean inTime = mLastStartedActivity.startTime != 0
1924 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1925 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1926 final int nextUid = next.info.applicationInfo.uid;
1927 if (inTime && lastUid != nextUid
1928 && lastUid != next.launchedFromUid
1929 && mService.checkPermission(
1930 android.Manifest.permission.STOP_APP_SWITCHES,
1931 -1, next.launchedFromUid)
1932 != PackageManager.PERMISSION_GRANTED) {
1933 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1934 } else {
1935 next.startTime = now;
1936 mLastStartedActivity = next;
1937 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001938 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001939 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001940 mLastStartedActivity = next;
1941 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001942 }
Craig Mautner58547802013-03-05 08:23:53 -08001943
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001944 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1945
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001946 // We need to start pausing the current activity so the top one
1947 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001948 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1949 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001950 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001951 if (DEBUG_STATES) Slog.d(TAG_STATES,
1952 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001953 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001954 }
1955 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001956 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001957 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001958 // At this point we want to put the upcoming activity's process
1959 // at the top of the LRU list, since we know we will be needing it
1960 // very soon and it would be a waste to let it get killed if it
1961 // happens to be sitting towards the end.
1962 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001963 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001964 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001965 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001966 return true;
1967 }
1968
Christopher Tated3f175c2012-06-14 14:16:54 -07001969 // If the most recent activity was noHistory but was only stopped rather
1970 // than stopped+finished because the device went to sleep, we need to make
1971 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07001972 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07001973 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001974 if (DEBUG_STATES) Slog.d(TAG_STATES,
1975 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07001976 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08001977 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07001978 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07001979 }
1980
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001981 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08001982 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
1983 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001984 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001985 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1986 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001987 } else {
1988 // The next activity is already visible, so hide the previous
1989 // activity's windows right now so we can show the new one ASAP.
1990 // We only do this if the previous is finishing, which should mean
1991 // it is on top of the one being resumed so hiding it quickly
1992 // is good. Otherwise, we want to do the normal route of allowing
1993 // the resumed activity to be shown so we can decide if the
1994 // previous should actually be hidden depending on whether the
1995 // new one is found to be full-screen or not.
1996 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001997 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001998 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
1999 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002000 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002001 + ", nowVisible=" + next.nowVisible);
2002 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002003 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002004 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002005 + ", waitingVisible="
2006 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2007 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002008 }
2009 }
2010 }
2011
Dianne Hackborne7f97212011-02-24 14:40:20 -08002012 // Launching this app's activity, make sure the app is no longer
2013 // considered stopped.
2014 try {
2015 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002016 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002017 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002018 } catch (IllegalArgumentException e) {
2019 Slog.w(TAG, "Failed trying to unstop package "
2020 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002021 }
2022
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002023 // We are starting up the next activity, so tell the window manager
2024 // that the previous one will be hidden soon. This way it can know
2025 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002026 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002027 if (prev != null) {
2028 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002029 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002030 "Prepare close transition: prev=" + prev);
2031 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002032 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002033 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002034 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002035 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002036 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2037 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002038 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002039 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002040 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002041 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2042 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002043 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002044 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002045 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002046 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002047 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002048 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002049 : next.mLaunchTaskBehind
2050 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2051 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002052 }
2053 }
Craig Mautner967212c2013-04-13 21:10:58 -07002054 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002055 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002056 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002057 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002058 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002059 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002060 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002061 }
2062 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002063
2064 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002065 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002066 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2067 if (opts != null) {
2068 resumeAnimOptions = opts.toBundle();
2069 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002070 next.applyOptionsLocked();
2071 } else {
2072 next.clearOptionsLocked();
2073 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002074
Craig Mautnercf910b02013-04-23 11:23:27 -07002075 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002076 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002077 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002078
2079 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002080 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002081
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002082 // schedule launch ticks to collect information about slow apps.
2083 next.startLaunchTickingLocked();
2084
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002085 ActivityRecord lastResumedActivity =
2086 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002087 ActivityState lastState = next.state;
2088
2089 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002090
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002091 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002092 next.state = ActivityState.RESUMED;
2093 mResumedActivity = next;
2094 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002095 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002096 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002097 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002098 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002099
2100 // Have the window manager re-evaluate the orientation of
2101 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002102 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002103 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002104 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002105 mService.mConfiguration,
2106 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2107 if (config != null) {
2108 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002109 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002110 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002111 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002112
Craig Mautner525f3d92013-05-07 14:01:50 -07002113 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002114 // The configuration update wasn't able to keep the existing
2115 // instance of the activity, and instead started a new one.
2116 // We should be all done, but let's just make sure our activity
2117 // is still at the top and schedule another run if something
2118 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002119 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002120 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002121 "Activity config changed during resume: " + next
2122 + ", new next: " + nextNext);
2123 if (nextNext != next) {
2124 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002125 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002126 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002127 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002128 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002129 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002130 return true;
2131 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002132 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002133 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002134 }
Craig Mautner58547802013-03-05 08:23:53 -08002135
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002136 try {
2137 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002138 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002139 if (a != null) {
2140 final int N = a.size();
2141 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002142 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2143 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002144 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002145 }
2146 }
2147
2148 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002149 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002150 }
2151
Craig Mautner299f9602015-01-26 09:47:33 -08002152 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2153 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002154
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002155 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002156 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002157 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002158 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002159 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002160 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002161 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002162
Craig Mautner0eea92c2013-05-16 13:35:39 -07002163 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002164
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002165 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002166 } catch (Exception e) {
2167 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002168 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002169 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002170 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002171 if (lastStack != null) {
2172 lastStack.mResumedActivity = lastResumedActivity;
2173 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002174 Slog.i(TAG, "Restarting because process died: " + next);
2175 if (!next.hasBeenLaunched) {
2176 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002177 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2178 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002179 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002180 next.appToken, next.packageName, next.theme,
2181 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002182 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2183 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002184 }
George Mount2c92c972014-03-20 09:38:23 -07002185 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002186 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002187 return true;
2188 }
2189
2190 // From this point on, if something goes wrong there is no way
2191 // to recover the activity.
2192 try {
2193 next.visible = true;
2194 completeResumeLocked(next);
2195 } catch (Exception e) {
2196 // If any exception gets thrown, toss away this
2197 // activity and try the next one.
2198 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002199 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002200 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002201 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002202 return true;
2203 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002204 next.stopped = false;
2205
2206 } else {
2207 // Whoops, need to restart this activity!
2208 if (!next.hasBeenLaunched) {
2209 next.hasBeenLaunched = true;
2210 } else {
2211 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002212 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002213 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002214 mService.compatibilityInfoForPackageLocked(
2215 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002216 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002217 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002218 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002219 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002220 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002221 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002222 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002223 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002224 }
2225
Craig Mautnercf910b02013-04-23 11:23:27 -07002226 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002227 return true;
2228 }
2229
riddle_hsuc215a4f2014-12-27 12:10:45 +08002230 private TaskRecord getNextTask(TaskRecord targetTask) {
2231 final int index = mTaskHistory.indexOf(targetTask);
2232 if (index >= 0) {
2233 final int numTasks = mTaskHistory.size();
2234 for (int i = index + 1; i < numTasks; ++i) {
2235 TaskRecord task = mTaskHistory.get(i);
2236 if (task.userId == targetTask.userId) {
2237 return task;
2238 }
2239 }
2240 }
2241 return null;
2242 }
2243
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002244 private void insertTaskAtPosition(TaskRecord task, int position) {
2245 if (position >= mTaskHistory.size()) {
2246 insertTaskAtTop(task, null);
2247 return;
2248 }
2249 // Calculate maximum possible position for this task.
2250 int maxPosition = mTaskHistory.size();
2251 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002252 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002253 // Put non-current user tasks below current user tasks.
2254 while (maxPosition > 0) {
2255 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2256 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002257 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002258 break;
2259 }
2260 maxPosition--;
2261 }
2262 }
2263 position = Math.min(position, maxPosition);
2264 mTaskHistory.remove(task);
2265 mTaskHistory.add(position, task);
2266 updateTaskMovement(task, true);
2267 }
2268
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002269 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002270 // If the moving task is over home stack, transfer its return type to next task
2271 if (task.isOverHomeStack()) {
2272 final TaskRecord nextTask = getNextTask(task);
2273 if (nextTask != null) {
2274 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2275 }
2276 }
2277
Craig Mautner9c85c202013-10-04 20:11:26 -07002278 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002279 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002280 if (isOnHomeDisplay()) {
2281 ActivityStack lastStack = mStackSupervisor.getLastStack();
2282 final boolean fromHome = lastStack.isHomeStack();
2283 if (!isHomeStack() && (fromHome || topTask() != task)) {
Craig Mautner3b1dac82014-07-15 09:51:33 -07002284 task.setTaskToReturnTo(fromHome
2285 ? lastStack.topTask() == null
2286 ? HOME_ACTIVITY_TYPE
2287 : lastStack.topTask().taskType
2288 : APPLICATION_ACTIVITY_TYPE);
Craig Mautnere0a38842013-12-16 16:14:02 -08002289 }
2290 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002291 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002292 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002293
Craig Mautnerac6f8432013-07-17 13:24:59 -07002294 mTaskHistory.remove(task);
2295 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002296 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002297 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002298 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002299 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002300 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002301 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002302 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002303 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002304 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002305 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002306 break;
2307 }
2308 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002309 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002310 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002311 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002312 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002313 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002314
Craig Mautner8849a5e2013-04-02 16:41:03 -07002315 final void startActivityLocked(ActivityRecord r, boolean newTask,
Chong Zhang280d3322015-11-03 17:27:26 -08002316 boolean doResume, boolean keepCurTransition, ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002317 TaskRecord rTask = r.task;
2318 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002319 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2320 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002321 // Last activity in task had been removed or ActivityManagerService is reusing task.
2322 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002323 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002324 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002325 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002326 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002327 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002328 if (!newTask) {
2329 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002330 boolean startIt = true;
2331 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2332 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002333 if (task.getTopActivity() == null) {
2334 // All activities in task are finishing.
2335 continue;
2336 }
Craig Mautner70a86932013-02-28 22:37:44 -08002337 if (task == r.task) {
2338 // Here it is! Now, if this is not yet visible to the
2339 // user, then just add it without starting; it will
2340 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002341 if (!startIt) {
2342 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2343 + task, new RuntimeException("here").fillInStackTrace());
2344 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002345 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002346 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002347 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002348 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002349 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002350 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002351 return;
2352 }
2353 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002354 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002355 startIt = false;
2356 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002357 }
2358 }
2359
2360 // Place a new activity at top of stack, so it is next to interact
2361 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002362
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002363 // If we are not placing the new activity frontmost, we do not want
2364 // to deliver the onUserLeaving callback to the actual frontmost
2365 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002366 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002367 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002368 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002369 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002370 }
Craig Mautner70a86932013-02-28 22:37:44 -08002371
2372 task = r.task;
2373
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002374 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002375 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002376 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002377 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002378 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002379
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002380 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002381 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002382 // We want to show the starting preview window if we are
2383 // switching to a new task, or the next activity's process is
2384 // not currently running.
2385 boolean showStartingIcon = newTask;
2386 ProcessRecord proc = r.app;
2387 if (proc == null) {
2388 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2389 }
2390 if (proc == null || proc.thread == null) {
2391 showStartingIcon = true;
2392 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002393 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002394 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002395 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002396 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002397 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002398 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002399 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002400 ? r.mLaunchTaskBehind
2401 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2402 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002403 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002404 mNoAnimActivities.remove(r);
2405 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002406 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002407 boolean doShow = true;
2408 if (newTask) {
2409 // Even though this activity is starting fresh, we still need
2410 // to reset it to make sure we apply affinities to move any
2411 // existing activities from other tasks in to it.
2412 // If the caller has requested that the target task be
2413 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002414 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002415 resetTaskIfNeededLocked(r, r);
2416 doShow = topRunningNonDelayedActivityLocked(null) == r;
2417 }
Chong Zhang280d3322015-11-03 17:27:26 -08002418 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002419 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2420 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002421 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002422 if (r.mLaunchTaskBehind) {
2423 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2424 // tell WindowManager that r is visible even though it is at the back of the stack.
2425 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002426 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002427 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002428 // Figure out if we are transitioning from another activity that is
2429 // "has the same starting icon" as the next one. This allows the
2430 // window manager to keep the previous window it had previously
2431 // created, if it still had one.
2432 ActivityRecord prev = mResumedActivity;
2433 if (prev != null) {
2434 // We don't want to reuse the previous starting preview if:
2435 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002436 if (prev.task != r.task) {
2437 prev = null;
2438 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002439 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002440 else if (prev.nowVisible) {
2441 prev = null;
2442 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002443 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002444 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002445 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002446 mService.compatibilityInfoForPackageLocked(
2447 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002448 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002449 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002450 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002451 }
2452 } else {
2453 // If this is the first activity, don't do any fancy animations,
2454 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002455 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002456 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002457 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002458 }
2459 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002460 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002461 }
2462
2463 if (doResume) {
Craig Mautner233ceee2014-05-09 17:05:11 -07002464 mStackSupervisor.resumeTopActivitiesLocked(this, r, options);
Chong Zhang45c25ce2015-08-10 22:18:26 -07002465 } else {
2466 addRecentActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002467 }
2468 }
2469
Dianne Hackbornbe707852011-11-11 14:32:10 -08002470 final void validateAppTokensLocked() {
2471 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002472 mValidateAppTokens.ensureCapacity(numActivities());
2473 final int numTasks = mTaskHistory.size();
2474 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2475 TaskRecord task = mTaskHistory.get(taskNdx);
2476 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002477 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002478 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002479 }
Craig Mautner000f0022013-02-26 15:04:29 -08002480 TaskGroup group = new TaskGroup();
2481 group.taskId = task.taskId;
2482 mValidateAppTokens.add(group);
2483 final int numActivities = activities.size();
2484 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2485 final ActivityRecord r = activities.get(activityNdx);
2486 group.tokens.add(r.appToken);
2487 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002488 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002489 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002490 }
2491
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002492 /**
2493 * Perform a reset of the given task, if needed as part of launching it.
2494 * Returns the new HistoryRecord at the top of the task.
2495 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002496 /**
2497 * Helper method for #resetTaskIfNeededLocked.
2498 * We are inside of the task being reset... we'll either finish this activity, push it out
2499 * for another task, or leave it as-is.
2500 * @param task The task containing the Activity (taskTop) that might be reset.
2501 * @param forceReset
2502 * @return An ActivityOptions that needs to be processed.
2503 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002504 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002505 ActivityOptions topOptions = null;
2506
2507 int replyChainEnd = -1;
2508 boolean canMoveOptions = true;
2509
2510 // We only do this for activities that are not the root of the task (since if we finish
2511 // the root, we may no longer have the task!).
2512 final ArrayList<ActivityRecord> activities = task.mActivities;
2513 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002514 final int rootActivityNdx = task.findEffectiveRootIndex();
2515 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002516 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002517 if (target.frontOfTask)
2518 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002519
2520 final int flags = target.info.flags;
2521 final boolean finishOnTaskLaunch =
2522 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2523 final boolean allowTaskReparenting =
2524 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2525 final boolean clearWhenTaskReset =
2526 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2527
2528 if (!finishOnTaskLaunch
2529 && !clearWhenTaskReset
2530 && target.resultTo != null) {
2531 // If this activity is sending a reply to a previous
2532 // activity, we can't do anything with it now until
2533 // we reach the start of the reply chain.
2534 // XXX note that we are assuming the result is always
2535 // to the previous activity, which is almost always
2536 // the case but we really shouldn't count on.
2537 if (replyChainEnd < 0) {
2538 replyChainEnd = i;
2539 }
2540 } else if (!finishOnTaskLaunch
2541 && !clearWhenTaskReset
2542 && allowTaskReparenting
2543 && target.taskAffinity != null
2544 && !target.taskAffinity.equals(task.affinity)) {
2545 // If this activity has an affinity for another
2546 // task, then we need to move it out of here. We will
2547 // move it as far out of the way as possible, to the
2548 // bottom of the activity stack. This also keeps it
2549 // correctly ordered with any activities we previously
2550 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002551 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002552 final ActivityRecord bottom =
2553 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002554 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002555 if (bottom != null && target.taskAffinity != null
2556 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002557 // If the activity currently at the bottom has the
2558 // same task affinity as the one we are moving,
2559 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002560 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002561 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002562 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002563 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002564 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002565 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002566 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002567 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002568 + " out to new task " + target.task);
2569 }
2570
Wale Ogunwale706ed792015-08-02 10:29:44 -07002571 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002572
Craig Mautner525f3d92013-05-07 14:01:50 -07002573 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002574 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2575 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002576 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002577 if (p.finishing) {
2578 continue;
2579 }
2580
Craig Mautnere3a74d52013-02-22 14:14:58 -08002581 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002582 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002583 topOptions = p.takeOptionsLocked();
2584 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002585 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002586 }
2587 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002588 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2589 "Removing activity " + p + " from task=" + task + " adding to task="
2590 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002591 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2592 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002593 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002594 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002595
Wale Ogunwale706ed792015-08-02 10:29:44 -07002596 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002597 }
2598
Wale Ogunwale706ed792015-08-02 10:29:44 -07002599 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002600 if (VALIDATE_TOKENS) {
2601 validateAppTokensLocked();
2602 }
2603
2604 replyChainEnd = -1;
2605 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2606 // If the activity should just be removed -- either
2607 // because it asks for it, or the task should be
2608 // cleared -- then finish it and anything that is
2609 // part of its reply chain.
2610 int end;
2611 if (clearWhenTaskReset) {
2612 // In this case, we want to finish this activity
2613 // and everything above it, so be sneaky and pretend
2614 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002615 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002616 } else if (replyChainEnd < 0) {
2617 end = i;
2618 } else {
2619 end = replyChainEnd;
2620 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002621 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002622 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002623 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002624 if (p.finishing) {
2625 continue;
2626 }
2627 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002628 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002629 topOptions = p.takeOptionsLocked();
2630 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002631 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002632 }
2633 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002634 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002635 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002636 if (finishActivityLocked(
2637 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002638 end--;
2639 srcPos--;
2640 }
2641 }
2642 replyChainEnd = -1;
2643 } else {
2644 // If we were in the middle of a chain, well the
2645 // activity that started it all doesn't want anything
2646 // special, so leave it all as-is.
2647 replyChainEnd = -1;
2648 }
2649 }
2650
2651 return topOptions;
2652 }
2653
2654 /**
2655 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2656 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2657 * @param affinityTask The task we are looking for an affinity to.
2658 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2659 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2660 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2661 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002662 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002663 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002664 int replyChainEnd = -1;
2665 final int taskId = task.taskId;
2666 final String taskAffinity = task.affinity;
2667
2668 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2669 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002670 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2671
2672 // Do not operate on or below the effective root Activity.
2673 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002674 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002675 if (target.frontOfTask)
2676 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002677
2678 final int flags = target.info.flags;
2679 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2680 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2681
2682 if (target.resultTo != null) {
2683 // If this activity is sending a reply to a previous
2684 // activity, we can't do anything with it now until
2685 // we reach the start of the reply chain.
2686 // XXX note that we are assuming the result is always
2687 // to the previous activity, which is almost always
2688 // the case but we really shouldn't count on.
2689 if (replyChainEnd < 0) {
2690 replyChainEnd = i;
2691 }
2692 } else if (topTaskIsHigher
2693 && allowTaskReparenting
2694 && taskAffinity != null
2695 && taskAffinity.equals(target.taskAffinity)) {
2696 // This activity has an affinity for our task. Either remove it if we are
2697 // clearing or move it over to our task. Note that
2698 // we currently punt on the case where we are resetting a
2699 // task that is not at the top but who has activities above
2700 // with an affinity to it... this is really not a normal
2701 // case, and we will need to later pull that task to the front
2702 // and usually at that point we will do the reset and pick
2703 // up those remaining activities. (This only happens if
2704 // someone starts an activity in a new task from an activity
2705 // in a task that is not currently on top.)
2706 if (forceReset || finishOnTaskLaunch) {
2707 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002708 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2709 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002710 for (int srcPos = start; srcPos >= i; --srcPos) {
2711 final ActivityRecord p = activities.get(srcPos);
2712 if (p.finishing) {
2713 continue;
2714 }
Todd Kennedy539db512014-12-15 09:57:55 -08002715 finishActivityLocked(
2716 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002717 }
2718 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002719 if (taskInsertionPoint < 0) {
2720 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002721
Craig Mautner77878772013-03-04 19:46:24 -08002722 }
Craig Mautner77878772013-03-04 19:46:24 -08002723
2724 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002725 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2726 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2727 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002728 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002729 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002730 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002731 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002732
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002733 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2734 "Removing and adding activity " + p + " to stack at " + task
2735 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002736 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2737 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002738 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002739 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002740 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002741 if (VALIDATE_TOKENS) {
2742 validateAppTokensLocked();
2743 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002744
2745 // Now we've moved it in to place... but what if this is
2746 // a singleTop activity and we have put it on top of another
2747 // instance of the same activity? Then we drop the instance
2748 // below so it remains singleTop.
2749 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2750 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002751 int targetNdx = taskActivities.indexOf(target);
2752 if (targetNdx > 0) {
2753 ActivityRecord p = taskActivities.get(targetNdx - 1);
2754 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002755 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2756 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002757 }
2758 }
2759 }
2760 }
2761
2762 replyChainEnd = -1;
2763 }
2764 }
Craig Mautner77878772013-03-04 19:46:24 -08002765 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002766 }
2767
Craig Mautner8849a5e2013-04-02 16:41:03 -07002768 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002769 ActivityRecord newActivity) {
2770 boolean forceReset =
2771 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2772 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2773 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2774 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2775 forceReset = true;
2776 }
2777 }
2778
2779 final TaskRecord task = taskTop.task;
2780
2781 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2782 * for remaining tasks. Used for later tasks to reparent to task. */
2783 boolean taskFound = false;
2784
2785 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2786 ActivityOptions topOptions = null;
2787
Craig Mautner77878772013-03-04 19:46:24 -08002788 // Preserve the location for reparenting in the new task.
2789 int reparentInsertionPoint = -1;
2790
Craig Mautnere3a74d52013-02-22 14:14:58 -08002791 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2792 final TaskRecord targetTask = mTaskHistory.get(i);
2793
2794 if (targetTask == task) {
2795 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2796 taskFound = true;
2797 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002798 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2799 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002800 }
2801 }
2802
Craig Mautner70a86932013-02-28 22:37:44 -08002803 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002804 if (taskNdx >= 0) {
2805 do {
2806 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2807 } while (taskTop == null && taskNdx >= 0);
2808 }
Craig Mautner70a86932013-02-28 22:37:44 -08002809
Craig Mautnere3a74d52013-02-22 14:14:58 -08002810 if (topOptions != null) {
2811 // If we got some ActivityOptions from an activity on top that
2812 // was removed from the task, propagate them to the new real top.
2813 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002814 taskTop.updateOptionsLocked(topOptions);
2815 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002816 topOptions.abort();
2817 }
2818 }
2819
2820 return taskTop;
2821 }
2822
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002823 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2824 String resultWho, int requestCode, int resultCode, Intent data) {
2825
2826 if (callingUid > 0) {
2827 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002828 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002829 }
2830
2831 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2832 + " : who=" + resultWho + " req=" + requestCode
2833 + " res=" + resultCode + " data=" + data);
2834 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2835 try {
2836 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2837 list.add(new ResultInfo(resultWho, requestCode,
2838 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002839 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002840 return;
2841 } catch (Exception e) {
2842 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2843 }
2844 }
2845
2846 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2847 }
2848
Craig Mautner299f9602015-01-26 09:47:33 -08002849 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002850 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2851 return;
2852 }
2853
2854 final ActivityRecord next = topRunningActivityLocked();
2855 final String myReason = reason + " adjustFocus";
2856 if (next != r) {
2857 if (next != null && StackId.keepFocusInStackIfPossible(mStackId)) {
2858 // For freeform, docked, and pinned stacks we always keep the focus within the
2859 // stack as long as there is a running activity in the stack that we can adjust
2860 // focus to.
2861 mService.setFocusedActivityLocked(next, myReason);
2862 return;
2863 } else {
2864 final TaskRecord task = r.task;
2865 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2866 // For non-fullscreen stack, we want to move the focus to the next visible
2867 // stack to prevent the home screen from moving to the top and obscuring
2868 // other visible stacks.
2869 if (!mFullscreen
2870 && adjustFocusToNextVisibleStackLocked(null, myReason)) {
2871 return;
2872 }
2873 // Move the home stack to the top if this stack is fullscreen or there is no
2874 // other visible stack.
2875 if (mStackSupervisor.moveHomeStackTaskToTop(
2876 task.getTaskToReturnTo(), myReason)) {
2877 // Activity focus was already adjusted. Nothing else to do...
2878 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002879 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002880 }
2881 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08002882 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002883
Wale Ogunwaled045c822015-12-02 09:14:28 -08002884 final ActivityRecord top = mStackSupervisor.topRunningActivityLocked();
2885 if (top != null) {
2886 mService.setFocusedActivityLocked(top, myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002887 }
2888 }
2889
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002890 private boolean adjustFocusToNextVisibleStackLocked(ActivityStack inStack, String reason) {
2891 final ActivityStack stack = (inStack != null) ? inStack : getNextVisibleStackLocked();
2892 final String myReason = reason + " adjustFocusToNextVisibleStack";
2893 if (stack == null) {
2894 return false;
2895 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002896 final ActivityRecord top = stack.topRunningActivityLocked();
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002897 if (top == null) {
2898 return false;
2899 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002900 mService.setFocusedActivityLocked(top, myReason);
2901 return true;
2902 }
2903
Craig Mautnerf3333272013-04-22 10:55:53 -07002904 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002905 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002906 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2907 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2908 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002909 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002910 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002911 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2912 "stop-no-history", false)) {
2913 // Activity was finished, no need to continue trying to schedule stop.
2914 adjustFocusedActivityLocked(r, "stopActivityFinished");
2915 r.resumeKeyDispatchingLocked();
2916 return;
2917 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002918 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002919 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002920 + " on stop because we're just sleeping");
2921 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002922 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002923 }
2924
2925 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002926 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002927 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002928 try {
2929 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002930 if (DEBUG_STATES) Slog.v(TAG_STATES,
2931 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002932 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002933 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2934 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002935 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002936 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002937 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002938 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002939 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002940 r.setSleeping(true);
2941 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002942 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002943 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002944 } catch (Exception e) {
2945 // Maybe just ignore exceptions here... if the process
2946 // has crashed, our death notification will clean things
2947 // up.
2948 Slog.w(TAG, "Exception thrown during pause", e);
2949 // Just in case, assume it to be stopped.
2950 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002951 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002952 r.state = ActivityState.STOPPED;
2953 if (r.configDestroy) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002954 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002955 }
2956 }
2957 }
2958 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002959
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002960 /**
2961 * @return Returns true if the activity is being finished, false if for
2962 * some reason it is being left as-is.
2963 */
2964 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002965 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002966 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002967 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2968 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002969 + ", result=" + resultCode + ", data=" + resultData
2970 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002971 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002972 return false;
2973 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002974
Craig Mautnerd44711d2013-02-23 11:24:36 -08002975 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002976 return true;
2977 }
2978
Craig Mautnerd2328952013-03-05 12:46:26 -08002979 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08002980 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2981 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
2982 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2983 ActivityRecord r = activities.get(activityNdx);
2984 if (r.resultTo == self && r.requestCode == requestCode) {
2985 if ((r.resultWho == null && resultWho == null) ||
2986 (r.resultWho != null && r.resultWho.equals(resultWho))) {
2987 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
2988 false);
2989 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002990 }
2991 }
2992 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002993 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002994 }
2995
Todd Kennedy539db512014-12-15 09:57:55 -08002996 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002997 ActivityRecord r = topRunningActivityLocked();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002998 if (r != null && r.app == app) {
2999 // If the top running activity is from this crashing
3000 // process, then terminate it to avoid getting in a loop.
3001 Slog.w(TAG, " Force finishing activity "
3002 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08003003 int taskNdx = mTaskHistory.indexOf(r.task);
3004 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08003005 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003006 // Also terminate any activities below it that aren't yet
3007 // stopped, to avoid a situation where one will get
3008 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08003009 --activityNdx;
3010 if (activityNdx < 0) {
3011 do {
3012 --taskNdx;
3013 if (taskNdx < 0) {
3014 break;
3015 }
3016 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3017 } while (activityNdx < 0);
3018 }
3019 if (activityNdx >= 0) {
3020 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003021 if (r.state == ActivityState.RESUMED
3022 || r.state == ActivityState.PAUSING
3023 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07003024 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003025 Slog.w(TAG, " Force finishing activity "
3026 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08003027 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003028 }
3029 }
3030 }
3031 }
3032 }
3033
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003034 final void finishVoiceTask(IVoiceInteractionSession session) {
3035 IBinder sessionBinder = session.asBinder();
3036 boolean didOne = false;
3037 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3038 TaskRecord tr = mTaskHistory.get(taskNdx);
3039 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3040 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3041 ActivityRecord r = tr.mActivities.get(activityNdx);
3042 if (!r.finishing) {
3043 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3044 false);
3045 didOne = true;
3046 }
3047 }
3048 }
3049 }
3050 if (didOne) {
3051 mService.updateOomAdjLocked();
3052 }
3053 }
3054
Craig Mautnerd2328952013-03-05 12:46:26 -08003055 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003056 ArrayList<ActivityRecord> activities = r.task.mActivities;
3057 for (int index = activities.indexOf(r); index >= 0; --index) {
3058 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003059 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003060 break;
3061 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003062 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003063 }
3064 return true;
3065 }
3066
Dianne Hackborn5c607432012-02-28 14:44:19 -08003067 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3068 // send the result
3069 ActivityRecord resultTo = r.resultTo;
3070 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003071 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003072 + " who=" + r.resultWho + " req=" + r.requestCode
3073 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003074 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003075 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003076 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003077 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003078 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003079 if (r.info.applicationInfo.uid > 0) {
3080 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3081 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003082 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003083 }
3084 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3085 resultData);
3086 r.resultTo = null;
3087 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003088 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003089
3090 // Make sure this HistoryRecord is not holding on to other resources,
3091 // because clients have remote IPC references to this object so we
3092 // can't assume that will go away and want to avoid circular IPC refs.
3093 r.results = null;
3094 r.pendingResults = null;
3095 r.newIntents = null;
3096 r.icicle = null;
3097 }
3098
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003099 /**
3100 * @return Returns true if this activity has been removed from the history
3101 * list, or false if it is still in the list and will be removed later.
3102 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003103 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3104 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003105 if (r.finishing) {
3106 Slog.w(TAG, "Duplicate finish request for " + r);
3107 return false;
3108 }
3109
Wale Ogunwale7d701172015-03-11 15:36:30 -07003110 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003111 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003112 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003113 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003114 task.taskId, r.shortComponentName, reason);
3115 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003116 final int index = activities.indexOf(r);
3117 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003118 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003119 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003120 // If the caller asked that this activity (and all above it)
3121 // be cleared when the task is reset, don't lose that information,
3122 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003123 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003124 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003125 }
3126 }
3127
3128 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003129
Craig Mautner299f9602015-01-26 09:47:33 -08003130 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003131
Dianne Hackborn5c607432012-02-28 14:44:19 -08003132 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003133
Craig Mautnerde4ef022013-04-07 19:01:33 -07003134 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003135 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003136 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003137 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003138 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003139 ? AppTransition.TRANSIT_TASK_CLOSE
3140 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003141
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003142 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003143 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003144
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003145 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003146 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3147 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3148 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003149 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003150 }
3151
Craig Mautneraea74a52014-03-08 14:23:10 -08003152 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003153 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003154 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003155 } else if (r.state != ActivityState.PAUSING) {
3156 // If the activity is PAUSING, we will complete the finish once
3157 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003158 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003159 if (r.visible) {
3160 mWindowManager.setAppVisibility(r.appToken, false);
3161 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003162 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003163 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003164 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003165 }
3166
3167 return false;
3168 }
3169
Craig Mautnerf3333272013-04-22 10:55:53 -07003170 static final int FINISH_IMMEDIATELY = 0;
3171 static final int FINISH_AFTER_PAUSE = 1;
3172 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003173
Craig Mautnerf3333272013-04-22 10:55:53 -07003174 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003175 // First things first: if this activity is currently visible,
3176 // and the resumed activity is not yet visible, then hold off on
3177 // finishing until the resumed one becomes visible.
3178 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003179 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08003180 addToStopping(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003181 }
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}