blob: 9eda7ae822c0995cec245d051ca8d81577b53aa1 [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;
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080024import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
25import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
26import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
27import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080028import static android.content.pm.ActivityInfo.FLAG_RESUME_WHILE_PAUSING;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070029import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070030
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080031import static android.content.res.Configuration.SCREENLAYOUT_UNDEFINED;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080032import static com.android.server.am.ActivityManagerDebugConfig.*;
Chong Zhang75b37202015-12-04 14:16:36 -080033import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
Craig Mautner84984fa2014-06-19 11:19:20 -070034import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
35import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070036
Wale Ogunwalef40c11b2016-02-26 08:16:02 -080037import static com.android.server.am.ActivityRecord.STARTING_WINDOW_REMOVED;
38import static com.android.server.am.ActivityRecord.STARTING_WINDOW_SHOWN;
Wale Ogunwale39381972015-12-17 17:15:29 -080039import static com.android.server.am.ActivityStackSupervisor.FindTaskResult;
Wale Ogunwale040b4702015-08-06 18:10:50 -070040import static com.android.server.am.ActivityStackSupervisor.MOVING;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070041import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwale040b4702015-08-06 18:10:50 -070042
Winsonc809cbb2015-11-02 12:06:15 -080043import android.graphics.Point;
Wale Ogunwale706ed792015-08-02 10:29:44 -070044import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070045import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070046
Dianne Hackborn91097de2014-04-04 18:02:06 -070047import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080048import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070049import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070050import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080051import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080052import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080053import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080054import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070055import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056
57import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070058import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070059import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070060import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080062import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080064import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.content.pm.ActivityInfo;
Todd Kennedy39bfee52016-02-24 10:28:21 -080068import android.content.pm.ApplicationInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080070import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080071import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070072import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070073import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080074import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075import android.os.Handler;
76import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090077import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070078import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070079import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070080import android.os.RemoteException;
81import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080082import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070083import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070084import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070085import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070086import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070087import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070088
Craig Mautnercae015f2013-02-08 14:31:27 -080089import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080090import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070091import java.lang.ref.WeakReference;
92import java.util.ArrayList;
93import java.util.Iterator;
94import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080095import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070096import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070097
98/**
99 * State and management of a single stack of activities.
100 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700101final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800102
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800103 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700104 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
105 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800106 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700107 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700108 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700109 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700110 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700111 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700112 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
113 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700114 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700115 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700116 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
117 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
118 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
119 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
120 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700121
122 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800123
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700124 // Ticks during which we check progress while waiting for an app to launch.
125 static final int LAUNCH_TICK = 500;
126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700127 // How long we wait until giving up on the last activity to pause. This
128 // is short because it directly impacts the responsiveness of starting the
129 // next activity.
130 static final int PAUSE_TIMEOUT = 500;
131
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700132 // How long we wait for the activity to tell us it has stopped before
133 // giving up. This is a good amount of time because we really need this
134 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700135 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700136
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700137 // How long we wait until giving up on an activity telling us it has
138 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700139 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800140
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700141 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800142 // disabled.
143 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800144
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700145 // How long between activity launches that we consider safe to not warn
146 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700147 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800148
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700149 // Set to false to disable the preview that is shown while a new activity
150 // is being started.
151 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800152
Craig Mautner5eda9b32013-07-02 11:58:16 -0700153 // How long to wait for all background Activities to redraw following a call to
154 // convertToTranslucent().
155 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
156
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800157 // How many activities have to be scheduled to stop to force a stop pass.
158 private static final int MAX_STOPPING_TO_FORCE = 3;
159
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700160 enum ActivityState {
161 INITIALIZING,
162 RESUMED,
163 PAUSING,
164 PAUSED,
165 STOPPING,
166 STOPPED,
167 FINISHING,
168 DESTROYING,
169 DESTROYED
170 }
171
Filip Gruszczynski0e381e22016-01-14 16:31:33 -0800172 // Stack is not considered visible.
173 static final int STACK_INVISIBLE = 0;
174 // Stack is considered visible
175 static final int STACK_VISIBLE = 1;
176 // Stack is considered visible, but only becuase it has activity that is visible behind other
177 // activities and there is a specific combination of stacks.
178 static final int STACK_VISIBLE_ACTIVITY_BEHIND = 2;
179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700180 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700181 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800182 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800183
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700184 /**
185 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800186 * running) activities. It contains #TaskRecord objects.
187 */
Todd Kennedy39bfee52016-02-24 10:28:21 -0800188 private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800189
190 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800191 * Used for validating app tokens with window manager.
192 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800193 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800194
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700195 /**
196 * List of running activities, sorted by recent usage.
197 * The first entry in the list is the least recently used.
198 * It contains HistoryRecord objects.
199 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800200 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700201
202 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700203 * Animations that for the current transition have requested not to
204 * be considered for the transition animation.
205 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800206 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700207
208 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700209 * When we are in the process of pausing an activity, before starting the
210 * next one, this variable holds the activity that is currently being paused.
211 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800212 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700213
214 /**
215 * This is the last activity that we put into the paused state. This is
216 * used to determine if we need to do an activity transition while sleeping,
217 * when we normally hold the top activity paused.
218 */
219 ActivityRecord mLastPausedActivity = null;
220
221 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700222 * Activities that specify No History must be removed once the user navigates away from them.
223 * If the device goes to sleep with such an activity in the paused state then we save it here
224 * and finish it later if another activity replaces it on wakeup.
225 */
226 ActivityRecord mLastNoHistoryActivity = null;
227
228 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700229 * Current activity that is resumed, or null if there is none.
230 */
231 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800232
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700233 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700234 * This is the last activity that has been started. It is only used to
235 * identify when multiple activities are started at once so that the user
236 * can be warned they may not be in the activity they think they are.
237 */
238 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800239
Craig Mautner5eda9b32013-07-02 11:58:16 -0700240 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
241 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
242 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
243 // Activity in mTranslucentActivityWaiting is notified via
244 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
245 // background activity being drawn then the same call will be made with a true value.
246 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700247 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700248
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700249 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700250 * Set when we know we are going to be calling updateConfiguration()
251 * soon, so want to skip intermediate config checks.
252 */
253 boolean mConfigWillChange;
254
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700255 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800256 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700257 // Current bounds of the stack or null if fullscreen.
258 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800259
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700260 long mLaunchStartTime = 0;
261 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800262
Craig Mautner858d8a62013-04-23 17:08:34 -0700263 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700264
Craig Mautnerc00204b2013-03-05 15:02:14 -0800265 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800266 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800267 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
268 ArrayList<ActivityStack> mStacks;
269 /** The attached Display's unique identifier, or -1 if detached */
270 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800271
Craig Mautner27084302013-03-25 08:05:25 -0700272 /** Run all ActivityStacks through this */
273 final ActivityStackSupervisor mStackSupervisor;
274
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700275 private final LaunchingTaskPositioner mTaskPositioner;
276
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700277 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700278 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
279 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
280 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
281 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700282 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700283 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700284 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700285
286 static class ScheduleDestroyArgs {
287 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700288 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700289 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700290 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700291 mReason = reason;
292 }
293 }
294
Zoran Marcetaf958b322012-08-09 20:27:12 +0900295 final Handler mHandler;
296
297 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800298
Craig Mautnerc8143c62013-09-03 12:15:57 -0700299 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900300 super(looper);
301 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700302
Zoran Marcetaf958b322012-08-09 20:27:12 +0900303 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700304 public void handleMessage(Message msg) {
305 switch (msg.what) {
306 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800307 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700308 // We don't at this point know if the activity is fullscreen,
309 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800310 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700311 synchronized (mService) {
312 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700313 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700314 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700315 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800316 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700317 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700318 case LAUNCH_TICK_MSG: {
319 ActivityRecord r = (ActivityRecord)msg.obj;
320 synchronized (mService) {
321 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700322 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700323 }
324 }
325 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700326 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800327 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700328 // We don't at this point know if the activity is fullscreen,
329 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800330 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800331 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800332 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800333 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700334 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700335 case STOP_TIMEOUT_MSG: {
336 ActivityRecord r = (ActivityRecord)msg.obj;
337 // We don't at this point know if the activity is fullscreen,
338 // so we need to be conservative and assume it isn't.
339 Slog.w(TAG, "Activity stop timeout for " + r);
340 synchronized (mService) {
341 if (r.isInHistory()) {
342 activityStoppedLocked(r, null, null, null);
343 }
344 }
345 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700346 case DESTROY_ACTIVITIES_MSG: {
347 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
348 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700349 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700350 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700351 } break;
352 case TRANSLUCENT_TIMEOUT_MSG: {
353 synchronized (mService) {
354 notifyActivityDrawnLocked(null);
355 }
356 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700357 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700358 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700359 final ActivityRecord r = getVisibleBehindActivity();
360 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700361 if (r != null) {
362 mService.killAppAtUsersRequest(r.app, null);
363 }
364 }
365 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700366 }
367 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800368 }
369
Craig Mautner34b73df2014-01-12 21:11:08 -0800370 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800371 int count = 0;
372 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
373 count += mTaskHistory.get(taskNdx).mActivities.size();
374 }
375 return count;
376 }
377
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800378 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
379 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800380 mActivityContainer = activityContainer;
381 mStackSupervisor = activityContainer.getOuter();
382 mService = mStackSupervisor.mService;
383 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
384 mWindowManager = mService.mWindowManager;
385 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700386 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800387 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700388 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
389 ? new LaunchingTaskPositioner() : null;
390 }
391
392 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
393 mDisplayId = activityDisplay.mDisplayId;
394 mStacks = activityDisplay.mStacks;
395 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
396 mFullscreen = mBounds == null;
397 if (mTaskPositioner != null) {
398 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
399 mTaskPositioner.configure(mBounds);
400 }
Wale Ogunwale961f4852016-02-01 20:25:54 -0800401
402 if (mStackId == DOCKED_STACK_ID) {
403 // If we created a docked stack we want to resize it so it resizes all other stacks
404 // in the system.
405 mStackSupervisor.resizeDockedStackLocked(
406 mBounds, null, null, null, null, PRESERVE_WINDOWS);
407 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700408 }
409
410 void detachDisplay() {
411 mDisplayId = Display.INVALID_DISPLAY;
412 mStacks = null;
413 if (mTaskPositioner != null) {
414 mTaskPositioner.reset();
415 }
416 mWindowManager.detachStack(mStackId);
Jorim Jaggi899327f2016-02-25 20:44:18 -0500417 if (mStackId == DOCKED_STACK_ID) {
418 // If we removed a docked stack we want to resize it so it resizes all other stacks
419 // in the system to fullscreen.
420 mStackSupervisor.resizeDockedStackLocked(
421 null, null, null, null, null, PRESERVE_WINDOWS);
422 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700423 }
424
Winsonc809cbb2015-11-02 12:06:15 -0800425 public void getDisplaySize(Point out) {
426 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
427 }
428
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700429 void setBounds(Rect bounds) {
430 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700431 if (mTaskPositioner != null) {
432 mTaskPositioner.configure(bounds);
433 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700434 }
Craig Mautner5962b122012-10-05 14:45:52 -0700435
Amith Yamasani734983f2014-03-04 16:48:05 -0800436 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700437 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700438 }
439
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700440 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800441 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700442 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700443 if (r != null) {
444 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800445 }
446 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700447 return null;
448 }
449
450 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800451 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
452 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800453 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800454 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
455 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800456 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800457 return r;
458 }
459 }
460 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700461 return null;
462 }
463
464 /**
465 * This is a simplified version of topRunningActivityLocked that provides a number of
466 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800467 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700468 * @param token If non-null, any history records matching this token will be skipped.
469 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800470 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700471 * @return Returns the HistoryRecord of the next activity on the stack.
472 */
473 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800474 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
475 TaskRecord task = mTaskHistory.get(taskNdx);
476 if (task.taskId == taskId) {
477 continue;
478 }
479 ArrayList<ActivityRecord> activities = task.mActivities;
480 for (int i = activities.size() - 1; i >= 0; --i) {
481 final ActivityRecord r = activities.get(i);
482 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800483 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800484 return r;
485 }
486 }
487 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700488 return null;
489 }
490
Craig Mautner8849a5e2013-04-02 16:41:03 -0700491 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700492 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
493 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700494 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
495 final ActivityRecord r = activities.get(activityNdx);
496 if (!r.finishing) {
497 return r;
498 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700499 }
500 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700501 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700502 }
503
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700504 final TaskRecord topTask() {
505 final int size = mTaskHistory.size();
506 if (size > 0) {
507 return mTaskHistory.get(size - 1);
508 }
509 return null;
510 }
511
Craig Mautnerd2328952013-03-05 12:46:26 -0800512 TaskRecord taskForIdLocked(int id) {
513 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
514 final TaskRecord task = mTaskHistory.get(taskNdx);
515 if (task.taskId == id) {
516 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800517 }
518 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700519 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700520 }
521
Craig Mautnerd2328952013-03-05 12:46:26 -0800522 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700523 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800524 return isInStackLocked(r);
525 }
526
527 ActivityRecord isInStackLocked(ActivityRecord r) {
528 if (r == null) {
529 return null;
530 }
531 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700532 if (task != null && task.stack != null
533 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800534 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800535 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800536 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800537 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800538 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800539 }
540
Craig Mautner2420ead2013-04-01 17:13:20 -0700541 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700542 final boolean hadit = mLRUActivities.remove(r);
543 mLRUActivities.add(r);
544 return hadit;
545 }
546
Craig Mautnerde4ef022013-04-07 19:01:33 -0700547 final boolean isHomeStack() {
548 return mStackId == HOME_STACK_ID;
549 }
550
Winson Chung0583d3d2015-12-18 10:03:12 -0500551 final boolean isDockedStack() {
552 return mStackId == DOCKED_STACK_ID;
553 }
554
555 final boolean isPinnedStack() {
556 return mStackId == PINNED_STACK_ID;
557 }
558
Craig Mautnere0a38842013-12-16 16:14:02 -0800559 final boolean isOnHomeDisplay() {
560 return isAttached() &&
561 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
562 }
563
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700564 void moveToFront(String reason) {
565 moveToFront(reason, null);
566 }
567
568 /**
569 * @param reason The reason for moving the stack to the front.
570 * @param task If non-null, the task will be moved to the top of the stack.
571 * */
572 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700573 if (!isAttached()) {
574 return;
575 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700576
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700577 mStacks.remove(this);
578 int addIndex = mStacks.size();
579
580 if (addIndex > 0) {
581 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800582 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
583 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700584 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800585 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700586 }
587
588 mStacks.add(addIndex, this);
589
590 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
591 if (isOnHomeDisplay()) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800592 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700593 }
594 if (task != null) {
595 insertTaskAtTop(task, null);
596 } else {
597 task = topTask();
598 }
599 if (task != null) {
600 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800601 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800602 }
603
Wale Ogunwaled046a012015-12-24 13:05:59 -0800604 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800605 if (StackId.canReceiveKeys(mStackId)) {
606 return true;
607 }
608 // The stack isn't focusable. See if its top activity is focusable to force focus on the
609 // stack.
610 final ActivityRecord r = topRunningActivityLocked();
611 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800612 }
613
Craig Mautnere0a38842013-12-16 16:14:02 -0800614 final boolean isAttached() {
615 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800616 }
617
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700618 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800619 * Returns the top activity in any existing task matching the given Intent in the input result.
620 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700621 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800622 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700623 Intent intent = target.intent;
624 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700625 ComponentName cls = intent.getComponent();
626 if (info.targetActivity != null) {
627 cls = new ComponentName(info.packageName, info.targetActivity);
628 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700629 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700630 boolean isDocument = intent != null & intent.isDocument();
631 // If documentData is non-null then it must match the existing task data.
632 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800633
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700634 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800635 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
636 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700637 if (task.voiceSession != null) {
638 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700639 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700640 continue;
641 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700642 if (task.userId != userId) {
643 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700644 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700645 continue;
646 }
Craig Mautner000f0022013-02-26 15:04:29 -0800647 final ActivityRecord r = task.getTopActivity();
648 if (r == null || r.finishing || r.userId != userId ||
649 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700650 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800651 continue;
652 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700653 if (r.mActivityType != target.mActivityType) {
654 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
655 continue;
656 }
Craig Mautner000f0022013-02-26 15:04:29 -0800657
Craig Mautnerd00f4742014-03-12 14:17:26 -0700658 final Intent taskIntent = task.intent;
659 final Intent affinityIntent = task.affinityIntent;
660 final boolean taskIsDocument;
661 final Uri taskDocumentData;
662 if (taskIntent != null && taskIntent.isDocument()) {
663 taskIsDocument = true;
664 taskDocumentData = taskIntent.getData();
665 } else if (affinityIntent != null && affinityIntent.isDocument()) {
666 taskIsDocument = true;
667 taskDocumentData = affinityIntent.getData();
668 } else {
669 taskIsDocument = false;
670 taskDocumentData = null;
671 }
672
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700673 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700674 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700675 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700676 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Wale Ogunwale39381972015-12-17 17:15:29 -0800677 if (!isDocument && !taskIsDocument
678 && result.r == null && task.canMatchRootAffinity()) {
Dianne Hackborn79228822014-09-16 11:11:23 -0700679 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwale39381972015-12-17 17:15:29 -0800680 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
681 // It is possible for multiple tasks to have the same root affinity especially
682 // if they are in separate stacks. We save off this candidate, but keep looking
683 // to see if there is a better candidate.
684 result.r = r;
685 result.matchedByRootAffinity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700686 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700687 } else if (taskIntent != null && taskIntent.getComponent() != null &&
688 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700689 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700690 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800691 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700692 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
693 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800694 result.r = r;
695 result.matchedByRootAffinity = false;
696 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700697 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
698 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700699 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700700 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800701 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700702 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
703 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800704 result.r = r;
705 result.matchedByRootAffinity = false;
706 break;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700707 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700708 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700709 }
710
711 /**
712 * Returns the first activity (starting from the top of the stack) that
713 * is the same as the given activity. Returns null if no such activity
714 * is found.
715 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700716 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700717 ComponentName cls = intent.getComponent();
718 if (info.targetActivity != null) {
719 cls = new ComponentName(info.packageName, info.targetActivity);
720 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700721 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700722
Craig Mautner000f0022013-02-26 15:04:29 -0800723 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700724 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700725 final boolean notCurrentUserTask =
726 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700727 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700728
Craig Mautner000f0022013-02-26 15:04:29 -0800729 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
730 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700731 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700732 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700733 }
Craig Mautner000f0022013-02-26 15:04:29 -0800734 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700735 return r;
736 }
737 }
738 }
739
740 return null;
741 }
742
Amith Yamasani742a6712011-05-04 14:49:28 -0700743 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700744 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700745 */
Craig Mautner93529a42013-10-04 15:03:13 -0700746 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800747 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700748 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800749 }
750 mCurrentUser = userId;
751
752 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800753 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800754 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700755 final TaskRecord task = mTaskHistory.get(i);
756
757 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
758 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700759 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700760 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700761 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700762 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800763 mTaskHistory.remove(i);
764 mTaskHistory.add(task);
765 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800766 // Use same value for i.
767 } else {
768 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800769 }
770 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700771 if (VALIDATE_TOKENS) {
772 validateAppTokensLocked();
773 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700774 }
775
Craig Mautner2420ead2013-04-01 17:13:20 -0700776 void minimalResumeActivityLocked(ActivityRecord r) {
777 r.state = ActivityState.RESUMED;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800778 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
779 + " callers=" + Debug.getCallers(5));
Craig Mautner2420ead2013-04-01 17:13:20 -0700780 mResumedActivity = r;
781 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800782 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700783 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700784 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700785 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700786 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
787 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700788 }
789
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800790 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700791 if (r != null) {
792 mRecentTasks.addLocked(r.task);
793 r.task.touchActiveTime();
794 }
795 }
796
Narayan Kamath7829c812015-06-08 17:39:43 +0100797 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700798 if (mFullyDrawnStartTime != 0) {
799 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
800 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100801 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700802 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
803 }
804
805 private void stopFullyDrawnTraceIfNeeded() {
806 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
807 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
808 mFullyDrawnStartTime = 0;
809 }
810 }
811
Craig Mautnere79d42682013-04-01 19:01:53 -0700812 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700813 if (r.displayStartTime == 0) {
814 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
815 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100816 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700817 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700818 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700819 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100820 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700821 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700822 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700823 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800824
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700825 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700826 // Make sure that there is no activity waiting for this to launch.
827 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
828 r.displayStartTime = r.fullyDrawnStartTime = 0;
829 } else {
830 mStackSupervisor.removeTimeoutsForActivityLocked(r);
831 mStackSupervisor.scheduleIdleTimeoutLocked(r);
832 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700833 }
834
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800835 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800836 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800837 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
838 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
839 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
840 activities.get(activityNdx).setSleeping(false);
841 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800842 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800843 if (mPausingActivity != null) {
844 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
845 activityPausedLocked(mPausingActivity.appToken, true);
846 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800847 }
848
Todd Kennedy39bfee52016-02-24 10:28:21 -0800849 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
850 final String packageName = aInfo.packageName;
851 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
852 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
853 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
854 if (packageName.equals(activities.get(activityNdx).packageName)) {
855 activities.get(activityNdx).info.applicationInfo = aInfo;
856 }
857 }
858 }
859 }
860
Craig Mautner0eea92c2013-05-16 13:35:39 -0700861 /**
862 * @return true if something must be done before going to sleep.
863 */
864 boolean checkReadyForSleepLocked() {
865 if (mResumedActivity != null) {
866 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700867 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
868 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
869 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700870 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700871 return true;
872 }
873 if (mPausingActivity != null) {
874 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700875 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700876 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800877 }
878
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700879 if (hasVisibleBehindActivity()) {
880 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800881 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700882 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800883 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700884 return true;
885 }
886
Craig Mautner0eea92c2013-05-16 13:35:39 -0700887 return false;
888 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800889
Craig Mautner0eea92c2013-05-16 13:35:39 -0700890 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700891 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800892
Craig Mautner0eea92c2013-05-16 13:35:39 -0700893 // Make sure any stopped but visible activities are now sleeping.
894 // This ensures that the activity's onStop() is called.
895 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
896 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
897 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
898 final ActivityRecord r = activities.get(activityNdx);
899 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
900 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800901 }
902 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800903 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700904 }
Craig Mautner59c00972012-07-30 12:10:24 -0700905
Winson8b1871d2015-11-20 09:56:20 -0800906 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
907 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800908 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700909 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800910 return null;
911 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800912
Winson Chung083baf92014-07-11 10:32:42 -0700913 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700914 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800915 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700916 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700917 return null;
918 }
919
Winson Chung48a10a52014-08-27 14:36:51 -0700920 int w = mService.mThumbnailWidth;
921 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800922 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800923 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700924 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800925
926 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800927 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
928 // use within SystemUI while keeping memory usage low.
Winson Chungead5c0f2015-12-14 11:18:57 -0500929 if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
Winson8b1871d2015-11-20 09:56:20 -0800930 w = h = -1;
Winson387aac62015-11-25 11:18:56 -0800931 scale = 0.5f;
Winson8b1871d2015-11-20 09:56:20 -0800932 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800933 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800934 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800935 }
Winson Chung376543b2014-05-21 17:43:28 -0700936 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800937 return null;
938 }
939
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700940 /**
941 * Start pausing the currently resumed activity. It is an error to call this if there
942 * is already an activity being paused or there is no resumed activity.
943 *
944 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
945 * @param uiSleeping True if this is happening with the user interface going to sleep (the
946 * screen turning off).
947 * @param resuming True if this is being called as part of resuming the top activity, so
948 * we shouldn't try to instigate a resume here.
949 * @param dontWait True if the caller does not want to wait for the pause to complete. If
950 * set to true, we will immediately complete the pause here before returning.
951 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
952 * it to tell us when it is done.
953 */
954 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
955 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800956 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800957 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
958 + " state=" + mPausingActivity.state);
959 if (!mService.isSleeping()) {
960 // Avoid recursion among check for sleep and complete pause during sleeping.
961 // Because activity will be paused immediately after resume, just let pause
962 // be completed by the order of activity paused from clients.
963 completePauseLocked(false);
964 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800965 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700966 ActivityRecord prev = mResumedActivity;
967 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700968 if (!resuming) {
969 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -0800970 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700971 }
972 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700973 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800974
975 if (mActivityContainer.mParentActivity == null) {
976 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700977 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800978 }
979
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700980 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700981 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700982 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800983 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700984 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700985 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
986 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700987 prev.state = ActivityState.PAUSING;
988 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700989 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700990 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700991 if (mService.mHasRecents
992 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800993 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700994 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700995 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700996
997 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800998
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700999 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001000 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001001 try {
1002 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001003 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001004 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -07001005 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001006 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001007 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001008 } catch (Exception e) {
1009 // Ignore exception, if process died other code will cleanup.
1010 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001011 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001012 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001013 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001014 }
1015 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001016 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001017 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001018 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001019 }
1020
1021 // If we are not going to sleep, we want to ensure the device is
1022 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001023 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001024 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001025 }
1026
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001027 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001028 // Have the window manager pause its key dispatching until the new
1029 // activity has started. If we're pausing the activity just because
1030 // the screen is being turned off and the UI is sleeping, don't interrupt
1031 // key dispatch; the same activity will pick it up again on wakeup.
1032 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001033 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001034 } else if (DEBUG_PAUSE) {
1035 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001036 }
1037
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001038 if (dontWait) {
1039 // If the caller said they don't want to wait for the pause, then complete
1040 // the pause now.
1041 completePauseLocked(false);
1042 return false;
1043
1044 } else {
1045 // Schedule a pause timeout in case the app doesn't respond.
1046 // We don't give it much time because this directly impacts the
1047 // responsiveness seen by the user.
1048 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1049 msg.obj = prev;
1050 prev.pauseTime = SystemClock.uptimeMillis();
1051 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001052 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001053 return true;
1054 }
1055
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001056 } else {
1057 // This activity failed to schedule the
1058 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001059 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001060 if (!resuming) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001061 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001062 }
1063 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001064 }
1065 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001066
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001067 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001068 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1069 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001070
Craig Mautnerd2328952013-03-05 12:46:26 -08001071 final ActivityRecord r = isInStackLocked(token);
1072 if (r != null) {
1073 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1074 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001075 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001076 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001077 completePauseLocked(true);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001078 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001079 } else {
1080 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1081 r.userId, System.identityHashCode(r), r.shortComponentName,
1082 mPausingActivity != null
1083 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001084 if (r.state == ActivityState.PAUSING) {
1085 r.state = ActivityState.PAUSED;
1086 if (r.finishing) {
1087 if (DEBUG_PAUSE) Slog.v(TAG,
1088 "Executing finish of failed to pause activity: " + r);
1089 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1090 }
louis_chang047dfd42015-04-08 16:35:55 +08001091 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001092 }
1093 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001094 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001095 }
1096
Craig Mautnera0026042014-04-23 11:45:37 -07001097 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1098 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001099 if (r.state != ActivityState.STOPPING) {
1100 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1101 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1102 return;
1103 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001104 if (persistentState != null) {
1105 r.persistentState = persistentState;
1106 mService.notifyTaskPersisterLocked(r.task, false);
1107 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001108 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001109 if (icicle != null) {
1110 // If icicle is null, this is happening due to a timeout, so we
1111 // haven't really saved the state.
1112 r.icicle = icicle;
1113 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001114 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001115 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001116 }
1117 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001118 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001119 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1120 r.stopped = true;
1121 r.state = ActivityState.STOPPED;
Robert Carre12aece2016-02-02 22:43:27 -08001122
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08001123 mWindowManager.notifyAppStopped(r.appToken, true);
Robert Carre12aece2016-02-02 22:43:27 -08001124
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001125 if (getVisibleBehindActivity() == r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001126 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001127 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001128 if (r.finishing) {
1129 r.clearOptionsLocked();
1130 } else {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001131 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001132 destroyActivityLocked(r, true, "stop-config");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001133 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001134 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001135 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001136 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001137 }
1138 }
1139 }
1140
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001141 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001142 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001143 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001144
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001145 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001146 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001147 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001148 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001149 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001150 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001151 } else if (prev.app != null) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001152 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1153 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Craig Mautner8c14c152015-01-15 17:32:07 -08001154 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001155 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1156 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001157 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001158 if (prev.deferRelaunchUntilPaused) {
1159 // Complete the deferred relaunch that was waiting for pause to complete.
1160 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
1161 relaunchActivityLocked(prev, prev.configChangeFlags, false,
1162 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001163 } else if (wasStopping) {
1164 // We are also stopping, the stop request must have gone soon after the pause.
1165 // We can't clobber it, because the stop confirmation will not be handled.
1166 // We don't need to schedule another stop, we only need to let it happen.
1167 prev.state = ActivityState.STOPPING;
Wale Ogunwaled8026642016-02-09 20:40:18 -08001168 } else if ((!prev.visible && !hasVisibleBehindActivity())
1169 || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001170 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001171 // stopping.
Chong Zhang46b1ac62016-02-18 17:53:57 -08001172 addToStopping(prev, true /* immediate */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001173 }
1174 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001175 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001176 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001177 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001178 // It is possible the activity was freezing the screen before it was paused.
1179 // In that case go ahead and remove the freeze this activity has on the screen
1180 // since it is no longer visible.
1181 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001182 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001183 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001184
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001185 if (resumeNext) {
1186 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1187 if (!mService.isSleepingOrShuttingDown()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001188 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001189 } else {
1190 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001191 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001192 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001193 // If there are no more activities available to run, do resume anyway to start
1194 // something. Also if the top activity on the stack is not the just paused
1195 // activity, we need to go ahead and resume it to ensure we complete an
1196 // in-flight app switch.
1197 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001198 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001199 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001200 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001201
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001202 if (prev != null) {
1203 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001204
Craig Mautner525f3d92013-05-07 14:01:50 -07001205 if (prev.app != null && prev.cpuTimeAtResume > 0
1206 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001207 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1208 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001209 if (diff > 0) {
1210 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1211 synchronized (bsi) {
1212 BatteryStatsImpl.Uid.Proc ps =
1213 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001214 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001215 if (ps != null) {
1216 ps.addForegroundTimeLocked(diff);
1217 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001218 }
1219 }
1220 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001221 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001222 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001223
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001224 // Notify when the task stack has changed, but only if visibilities changed (not just focus)
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001225 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause) {
1226 mService.notifyTaskStackChangedLocked();
1227 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1228 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001229
1230 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001231 }
1232
Chong Zhang46b1ac62016-02-18 17:53:57 -08001233 private void addToStopping(ActivityRecord r, boolean immediate) {
1234 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1235 mStackSupervisor.mStoppingActivities.add(r);
1236 }
1237
1238 // If we already have a few activities waiting to stop, then give up
1239 // on things going idle and start clearing them out. Or if r is the
1240 // last of activity of the last task the stack will be empty and must
1241 // be cleared immediately.
1242 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1243 || (r.frontOfTask && mTaskHistory.size() <= 1);
1244
1245 if (immediate || forceIdle) {
1246 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
1247 + forceIdle + "immediate=" + immediate);
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001248 mStackSupervisor.scheduleIdleLocked();
1249 } else {
1250 mStackSupervisor.checkReadyForSleepLocked();
1251 }
1252 }
1253
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001254 /**
1255 * Once we know that we have asked an application to put an activity in
1256 * the resumed state (either by launching it or explicitly telling it),
1257 * this function updates the rest of our state to match that fact.
1258 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001259 private void completeResumeLocked(ActivityRecord next) {
Chong Zhang2b79af12016-02-10 18:47:06 -08001260 next.visible = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001261 next.idle = false;
1262 next.results = null;
1263 next.newIntents = null;
Chong Zhang2b79af12016-02-10 18:47:06 -08001264 next.stopped = false;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001265
Chong Zhang85ee6542015-10-02 13:36:38 -07001266 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001267 ProcessRecord app = next.task.mActivities.get(0).app;
1268 if (app != null && app != mService.mHomeProcess) {
1269 mService.mHomeProcess = app;
1270 }
1271 }
1272
Craig Mautner07566322013-09-26 16:42:55 -07001273 if (next.nowVisible) {
1274 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001275 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001276 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001277
1278 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001279 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001280
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001281 mStackSupervisor.reportResumedActivityLocked(next);
1282
1283 next.resumeKeyDispatchingLocked();
1284 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001285
1286 // Mark the point when the activity is resuming
1287 // TODO: To be more accurate, the mark should be before the onCreate,
1288 // not after the onResume. But for subsequent starts, onResume is fine.
1289 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001290 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001291 } else {
1292 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1293 }
Winson Chung376543b2014-05-21 17:43:28 -07001294
George Mount6ba042b2014-07-28 11:12:28 -07001295 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001296
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001297 if (getVisibleBehindActivity() == next) {
Craig Mautner64ccb702014-10-01 09:38:40 -07001298 // When resuming an activity, require it to call requestVisibleBehind() again.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001299 setVisibleBehindActivity(null);
Craig Mautner64ccb702014-10-01 09:38:40 -07001300 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001301 }
1302
Craig Mautner2568c3a2015-03-26 14:22:34 -07001303 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001304 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001305 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1306 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1307 r.mUpdateTaskThumbnailWhenHidden = false;
1308 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001309 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001310 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001311 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001312 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001313 container.setVisible(visible);
1314 }
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001315 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001316 }
1317
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001318 // Find the first visible activity above the passed activity and if it is translucent return it
1319 // otherwise return null;
1320 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1321 TaskRecord task = r.task;
1322 if (task == null) {
1323 return null;
1324 }
1325
1326 ActivityStack stack = task.stack;
1327 if (stack == null) {
1328 return null;
1329 }
1330
1331 int stackNdx = mStacks.indexOf(stack);
1332
1333 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1334 int taskNdx = tasks.indexOf(task);
1335
1336 ArrayList<ActivityRecord> activities = task.mActivities;
1337 int activityNdx = activities.indexOf(r) + 1;
1338
1339 final int numStacks = mStacks.size();
1340 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001341 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001342 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001343 final int numTasks = tasks.size();
1344 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001345 final TaskRecord currentTask = tasks.get(taskNdx);
1346 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001347 final int numActivities = activities.size();
1348 while (activityNdx < numActivities) {
1349 final ActivityRecord activity = activities.get(activityNdx);
1350 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001351 return historyStack.mFullscreen
1352 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001353 }
1354 ++activityNdx;
1355 }
1356 activityNdx = 0;
1357 ++taskNdx;
1358 }
1359 taskNdx = 0;
1360 ++stackNdx;
1361 }
1362
1363 return null;
1364 }
1365
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001366 ActivityStack getNextFocusableStackLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001367 ArrayList<ActivityStack> stacks = mStacks;
1368 final ActivityRecord parent = mActivityContainer.mParentActivity;
1369 if (parent != null) {
1370 stacks = parent.task.stack.mStacks;
1371 }
1372 if (stacks != null) {
1373 for (int i = stacks.size() - 1; i >= 0; --i) {
1374 ActivityStack stack = stacks.get(i);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001375 if (stack != this && stack.isFocusable()
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001376 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001377 return stack;
1378 }
1379 }
1380 }
1381 return null;
1382 }
1383
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001384 /** Returns true if the stack contains a fullscreen task. */
1385 private boolean hasFullscreenTask() {
1386 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1387 final TaskRecord task = mTaskHistory.get(i);
1388 if (task.mFullscreen) {
1389 return true;
1390 }
1391 }
1392 return false;
1393 }
1394
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001395 /**
1396 * Returns true if the stack is translucent and can have other contents visible behind it if
1397 * needed. A stack is considered translucent if it don't contain a visible or
1398 * starting (about to be visible) activity that is fullscreen (opaque).
1399 * @param starting The currently starting activity or null if there is none.
1400 */
1401 private boolean isStackTranslucent(ActivityRecord starting) {
1402 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1403 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001404 final ArrayList<ActivityRecord> activities = task.mActivities;
1405 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1406 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001407
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001408 // Conditions for an activity to obscure the stack we're
1409 // examining:
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001410 // 1. Not Finishing AND (Visible or the Starting activity) AND:
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001411 // 2. Either:
1412 // - Full Screen Activity OR
1413 // - On top of Home and our stack is NOT home
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001414 if (!r.finishing && (r.visible || r == starting) && (r.fullscreen ||
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001415 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1416 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001417 }
1418 }
1419 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001420 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001421 }
1422
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001423 /**
1424 * Returns stack's visibility: {@link #STACK_INVISIBLE}, {@link #STACK_VISIBLE} or
1425 * {@link #STACK_VISIBLE_ACTIVITY_BEHIND}.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001426 * @param starting The currently starting activity or null if there is none.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001427 */
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001428 int getStackVisibilityLocked(ActivityRecord starting) {
Jose Lima7ba71252014-04-30 12:59:27 -07001429 if (!isAttached()) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001430 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001431 }
1432
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001433 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001434 return STACK_VISIBLE;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001435 }
1436
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001437 final int stackIndex = mStacks.indexOf(this);
1438
1439 if (stackIndex == mStacks.size() - 1) {
1440 Slog.wtf(TAG,
1441 "Stack=" + this + " isn't front stack but is at the top of the stack list");
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001442 return STACK_INVISIBLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001443 }
1444
Chong Zhang75b37202015-12-04 14:16:36 -08001445 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1446 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001447 return STACK_INVISIBLE;
Chong Zhang75b37202015-12-04 14:16:36 -08001448 }
1449
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001450 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1451 final int focusedStackId = focusedStack.mStackId;
1452
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001453 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001454 && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID
1455 && !focusedStack.topActivity().fullscreen) {
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001456 // The fullscreen stack should be visible if it has a visible behind activity behind
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001457 // the home stack that is translucent.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001458 return STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001459 }
1460
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001461 if (mStackId == DOCKED_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001462 // Docked stack is always visible, except in the case where the top running activity
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001463 // task in the focus stack doesn't support any form of resizing but we show it for the
1464 // home task even though it's not resizable.
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001465 final ActivityRecord r = focusedStack.topRunningActivityLocked();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001466 final TaskRecord task = r != null ? r.task : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001467 return task == null || task.canGoInDockedStack() || task.isHomeTask() ? STACK_VISIBLE
1468 : STACK_INVISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001469 }
1470
Chong Zhangb16cf342015-11-12 17:22:40 -08001471 // Find the first stack below focused stack that actually got something visible.
1472 int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1473 while (belowFocusedIndex >= 0 &&
1474 mStacks.get(belowFocusedIndex).topRunningActivityLocked() == null) {
1475 belowFocusedIndex--;
1476 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001477 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1478 && stackIndex == belowFocusedIndex) {
1479 // Stacks directly behind the docked or pinned stack are always visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001480 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001481 }
1482
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001483 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001484 && focusedStack.isStackTranslucent(starting)) {
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001485 // Stacks behind the fullscreen stack with a translucent activity are always
1486 // visible so they can act as a backdrop to the translucent activity.
1487 // For example, dialog activities
1488 if (stackIndex == belowFocusedIndex) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001489 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001490 }
1491 if (belowFocusedIndex >= 0) {
1492 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001493 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1494 && stackIndex == (belowFocusedIndex - 1)) {
1495 // The stack behind the docked or pinned stack is also visible so we can have a
1496 // complete backdrop to the translucent activity when the docked stack is up.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001497 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001498 }
1499 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001500 }
1501
Wale Ogunwale3797c222015-10-27 14:21:58 -07001502 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001503 // Visibility of any static stack should have been determined by the conditions above.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001504 return STACK_INVISIBLE;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001505 }
1506
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001507 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001508 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001509
1510 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1511 continue;
1512 }
1513
Wale Ogunwale3797c222015-10-27 14:21:58 -07001514 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001515 // These stacks can't have any dynamic stacks visible behind them.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001516 return STACK_INVISIBLE;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001517 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001518
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001519 if (!stack.isStackTranslucent(starting)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001520 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001521 }
1522 }
1523
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001524 return STACK_VISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001525 }
1526
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001527 final int rankTaskLayers(int baseLayer) {
1528 int layer = 0;
1529 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1530 final TaskRecord task = mTaskHistory.get(taskNdx);
1531 ActivityRecord r = task.topRunningActivityLocked();
1532 if (r == null || r.finishing || !r.visible) {
1533 task.mLayerRank = -1;
1534 } else {
1535 task.mLayerRank = baseLayer + layer++;
1536 }
1537 }
1538 return layer;
1539 }
1540
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001541 /**
1542 * Make sure that all activities that need to be visible (that is, they
1543 * currently can be seen by the user) actually are.
1544 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001545 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1546 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001547 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001548 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001549 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001550 if (top != null) {
1551 checkTranslucentActivityWaiting(top);
1552 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001553
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001554 // If the top activity is not fullscreen, then we need to
1555 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001556 boolean aboveTop = top != null;
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001557 final int stackVisibility = getStackVisibilityLocked(starting);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001558 final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
1559 final boolean stackVisibleBehind = stackVisibility == STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001560 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaled046a012015-12-24 13:05:59 -08001561 boolean resumeNextActivity = isFocusable() && (isInStackLocked(starting) == null);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001562 boolean behindTranslucentActivity = false;
1563 final ActivityRecord visibleBehind = getVisibleBehindActivity();
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001564 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001565 final TaskRecord task = mTaskHistory.get(taskNdx);
1566 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001567 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1568 final ActivityRecord r = activities.get(activityNdx);
1569 if (r.finishing) {
1570 continue;
1571 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001572 final boolean isTop = r == top;
1573 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001574 continue;
1575 }
1576 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001577 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1578 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001579 final boolean activityVisibleBehind =
1580 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == r;
1581 final boolean isVisible = (!behindFullscreenActivity || r.mLaunchTaskBehind
1582 || activityVisibleBehind) && okToShowLocked(r);
1583 if (isVisible) {
1584 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1585 + " finishing=" + r.finishing + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001586 // First: if this is not the current activity being started, make
1587 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001588 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001589 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001590 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001591
1592 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001593 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001594 resumeNextActivity, r)) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001595 if (activityNdx >= activities.size()) {
1596 // Record may be removed if its process needs to restart.
1597 activityNdx = activities.size() - 1;
1598 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001599 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001600 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001601 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001602 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001603 // If this activity is already visible, then there is nothing to do here.
1604 if (handleAlreadyVisible(r)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001605 resumeNextActivity = false;
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001606 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001607 } else {
Jorim Jaggie66edb12016-02-05 12:41:17 -08001608 makeVisibleIfNeeded(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001609 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001610 // Aggregate current change flags.
1611 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001612 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1613 behindFullscreenActivity, task, r);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001614 if (behindFullscreenActivity && !r.fullscreen) {
1615 behindTranslucentActivity = true;
1616 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001617 } else {
Wale Ogunwale834c2362016-01-23 18:14:58 -08001618 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001619 + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1620 + stackInvisible + " behindFullscreenActivity="
1621 + behindFullscreenActivity + " mLaunchTaskBehind="
1622 + r.mLaunchTaskBehind);
1623 makeInvisible(r, visibleBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001624 }
1625 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001626 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1627 // The visibility of tasks and the activities they contain in freeform stack are
1628 // determined individually unlike other stacks where the visibility or fullscreen
1629 // status of an activity in a previous task affects other.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001630 behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001631 } else if (mStackId == HOME_STACK_ID) {
1632 if (task.isHomeTask()) {
1633 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
1634 + " stackInvisible=" + stackInvisible
1635 + " behindFullscreenActivity=" + behindFullscreenActivity);
1636 // No other task in the home stack should be visible behind the home activity.
1637 // Home activities is usually a translucent activity with the wallpaper behind
1638 // them. However, when they don't have the wallpaper behind them, we want to
1639 // show activities in the next application stack behind them vs. another
1640 // task in the home stack like recents.
1641 behindFullscreenActivity = true;
1642 } else if (task.isRecentsTask()
1643 && task.getTaskToReturnTo() == APPLICATION_ACTIVITY_TYPE) {
1644 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1645 "Recents task returning to app: at " + task
1646 + " stackInvisible=" + stackInvisible
1647 + " behindFullscreenActivity=" + behindFullscreenActivity);
1648 // We don't want any other tasks in the home stack visible if the recents
1649 // activity is going to be returning to an application activity type.
1650 // We do this to preserve the visible order the user used to get into the
1651 // recents activity. The recents activity is normally translucent and if it
1652 // doesn't have the wallpaper behind it the next activity in the home stack
1653 // shouldn't be visible when the home stack is brought to the front to display
1654 // the recents activity from an app.
1655 behindFullscreenActivity = true;
1656 }
1657
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001658 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001659 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001660
1661 if (mTranslucentActivityWaiting != null &&
1662 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1663 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1664 notifyActivityDrawnLocked(null);
1665 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001666 }
Craig Mautner58547802013-03-05 08:23:53 -08001667
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001668 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1669 if (mTranslucentActivityWaiting != top) {
1670 mUndrawnActivitiesBelowTopTranslucent.clear();
1671 if (mTranslucentActivityWaiting != null) {
1672 // Call the callback with a timeout indication.
1673 notifyActivityDrawnLocked(null);
1674 mTranslucentActivityWaiting = null;
1675 }
1676 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1677 }
1678 }
1679
1680 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001681 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001682 // We need to make sure the app is running if it's the top, or it is just made visible from
1683 // invisible. If the app is already visible, it must have died while it was visible. In this
1684 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1685 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001686 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001687 // This activity needs to be visible, but isn't even running...
1688 // get it started and resume if no other stack in this stack is resumed.
1689 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1690 if (r != starting) {
1691 r.startFreezingScreenLocked(r.app, configChanges);
1692 }
1693 if (!r.visible || r.mLaunchTaskBehind) {
1694 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1695 setVisible(r, true);
1696 }
1697 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001698 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001699 return true;
1700 }
1701 }
1702 return false;
1703 }
1704
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001705 private void makeInvisible(ActivityRecord r, ActivityRecord visibleBehind) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001706 if (!r.visible) {
1707 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1708 return;
1709 }
1710 // Now for any activities that aren't visible to the user, make sure they no longer are
1711 // keeping the screen frozen.
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001712 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001713 try {
1714 setVisible(r, false);
1715 switch (r.state) {
1716 case STOPPING:
1717 case STOPPED:
1718 if (r.app != null && r.app.thread != null) {
1719 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1720 "Scheduling invisibility: " + r);
1721 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1722 }
1723 break;
1724
1725 case INITIALIZING:
1726 case RESUMED:
1727 case PAUSING:
1728 case PAUSED:
1729 // This case created for transitioning activities from
1730 // translucent to opaque {@link Activity#convertToOpaque}.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001731 if (visibleBehind == r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001732 releaseBackgroundResources(r);
1733 } else {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001734 addToStopping(r, true /* immediate */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001735 }
1736 break;
1737
1738 default:
1739 break;
1740 }
1741 } catch (Exception e) {
1742 // Just skip on any failure; we'll make it visible when it next restarts.
1743 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1744 }
1745 }
1746
1747 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1748 TaskRecord task, ActivityRecord r) {
1749 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001750 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08001751 + " stackInvisible=" + stackInvisible
1752 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001753 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001754 behindFullscreenActivity = true;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001755 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001756 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1757 + " stackInvisible=" + stackInvisible
1758 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001759 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001760 }
1761 return behindFullscreenActivity;
1762 }
1763
Jorim Jaggie66edb12016-02-05 12:41:17 -08001764 private void makeVisibleIfNeeded(ActivityRecord starting, ActivityRecord r) {
1765
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001766 // This activity is not currently visible, but is running. Tell it to become visible.
Jorim Jaggie66edb12016-02-05 12:41:17 -08001767 if (r.state == ActivityState.RESUMED || r == starting) {
Chong Zhange05db742016-02-16 16:58:37 -08001768 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1769 "Not making visible, r=" + r + " state=" + r.state + " starting=" + starting);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001770 return;
1771 }
1772
1773 // If this activity is paused, tell it to now show its window.
1774 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1775 "Making visible and scheduling visibility: " + r);
1776 try {
1777 if (mTranslucentActivityWaiting != null) {
1778 r.updateOptionsLocked(r.returningOptions);
1779 mUndrawnActivitiesBelowTopTranslucent.add(r);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001780 }
Jorim Jaggie66edb12016-02-05 12:41:17 -08001781 setVisible(r, true);
1782 r.sleeping = false;
1783 r.app.pendingUiClean = true;
1784 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1785 r.stopFreezingScreenLocked(false);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001786
1787 // The activity may be waiting for stop, but that is no longer
1788 // appropriate for it.
1789 mStackSupervisor.mStoppingActivities.remove(r);
1790 mStackSupervisor.mGoingToSleepActivities.remove(r);
1791 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001792 } catch (Exception e) {
1793 // Just skip on any failure; we'll make it
1794 // visible when it next restarts.
1795 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001796 }
1797 }
1798
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001799 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001800 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1801 r.stopFreezingScreenLocked(false);
1802 try {
1803 if (r.returningOptions != null) {
1804 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1805 }
1806 } catch(RemoteException e) {
1807 }
1808 return r.state == ActivityState.RESUMED;
1809 }
1810
Todd Kennedyaab56db2015-01-30 09:39:53 -08001811 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001812 mTranslucentActivityWaiting = r;
1813 mUndrawnActivitiesBelowTopTranslucent.clear();
1814 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1815 }
1816
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001817 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1818 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1819 final TaskRecord task = mTaskHistory.get(taskNdx);
1820 final ArrayList<ActivityRecord> activities = task.mActivities;
1821 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1822 final ActivityRecord r = activities.get(activityNdx);
1823 if ( r.appTimeTracker != except) {
1824 r.appTimeTracker = null;
1825 }
1826 }
1827 }
1828 }
1829
Craig Mautner5eda9b32013-07-02 11:58:16 -07001830 /**
1831 * Called as activities below the top translucent activity are redrawn. When the last one is
1832 * redrawn notify the top activity by calling
1833 * {@link Activity#onTranslucentConversionComplete}.
1834 *
1835 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1836 * occurred and the activity will be notified immediately.
1837 */
1838 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001839 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001840 if ((r == null)
1841 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1842 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1843 // The last undrawn activity below the top has just been drawn. If there is an
1844 // opaque activity at the top, notify it that it can become translucent safely now.
1845 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1846 mTranslucentActivityWaiting = null;
1847 mUndrawnActivitiesBelowTopTranslucent.clear();
1848 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1849
Craig Mautner71dd1b62014-02-18 15:48:52 -08001850 if (waitingActivity != null) {
1851 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1852 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1853 try {
1854 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1855 waitingActivity.appToken, r != null);
1856 } catch (RemoteException e) {
1857 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001858 }
1859 }
1860 }
1861 }
1862
Craig Mautnera61bc652013-10-28 15:43:18 -07001863 /** If any activities below the top running one are in the INITIALIZING state and they have a
1864 * starting window displayed then remove that starting window. It is possible that the activity
1865 * in this state will never resumed in which case that starting window will be orphaned. */
1866 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001867 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001868 boolean aboveTop = true;
1869 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1870 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1871 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1872 final ActivityRecord r = activities.get(activityNdx);
1873 if (aboveTop) {
1874 if (r == topActivity) {
1875 aboveTop = false;
1876 }
1877 continue;
1878 }
1879
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08001880 if (r.state == ActivityState.INITIALIZING
1881 && r.mStartingWindowState == STARTING_WINDOW_SHOWN) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001882 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1883 "Found orphaned starting window " + r);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08001884 r.mStartingWindowState = STARTING_WINDOW_REMOVED;
Craig Mautnera61bc652013-10-28 15:43:18 -07001885 mWindowManager.removeAppStartingWindow(r.appToken);
1886 }
1887 }
1888 }
1889 }
1890
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001891 /**
1892 * Ensure that the top activity in the stack is resumed.
1893 *
1894 * @param prev The previously resumed activity, for when in the process
1895 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001896 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001897 *
1898 * @return Returns true if something is being resumed, or false if
1899 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001900 *
1901 * NOTE: It is not safe to call this method directly as it can cause an activity in a
1902 * non-focused stack to be resumed.
1903 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
1904 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001905 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08001906 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001907 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001908 // Don't even start recursing.
1909 return false;
1910 }
1911
1912 boolean result = false;
1913 try {
1914 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001915 mStackSupervisor.inResumeTopActivity = true;
1916 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1917 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001918 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001919 }
Craig Mautner544efa72014-09-04 16:41:20 -07001920 result = resumeTopActivityInnerLocked(prev, options);
1921 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001922 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001923 }
1924 return result;
1925 }
1926
Chong Zhang280d3322015-11-03 17:27:26 -08001927 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001928 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001929
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001930 if (!mService.mBooting && !mService.mBooted) {
1931 // Not ready yet!
1932 return false;
1933 }
1934
Craig Mautnerdf88d732014-01-27 09:21:32 -08001935 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001936 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001937 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001938 // Do not resume this stack if its parent is not resumed.
1939 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1940 return false;
1941 }
1942
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001943 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07001944
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001945 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001946 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001947
1948 // Remember how we'll process this pause/resume situation, and ensure
1949 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001950 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1951 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001952
Craig Mautner84984fa2014-06-19 11:19:20 -07001953 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001954 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001955 // There are no more activities!
1956 final String reason = "noMoreActivities";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001957 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(reason)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001958 // Try to move focus to the next visible stack with a running activity if this
1959 // stack is not covering the entire screen.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001960 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
1961 mStackSupervisor.getFocusedStack(), prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001962 }
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001963
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001964 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001965 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001966 if (DEBUG_STATES) Slog.d(TAG_STATES,
1967 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001968 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001969 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001970 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1971 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1972 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001973 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001974 }
1975
1976 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001977
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001978 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001979 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1980 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001981 // Make sure we have executed any pending transitions, since there
1982 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001983 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001984 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001985 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001986 if (DEBUG_STATES) Slog.d(TAG_STATES,
1987 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001988 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001989 return false;
1990 }
1991
Craig Mautner525f3d92013-05-07 14:01:50 -07001992 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001993 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001994 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001995 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001996 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001997 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001998 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001999 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07002000 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07002001 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07002002 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08002003 } else if (!isOnHomeDisplay()) {
2004 return false;
2005 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002006 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08002007 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07002008 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
2009 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2010 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08002011 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07002012 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002013 }
2014
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002015 // If we are sleeping, and there is no resumed activity, and the top
2016 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07002017 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09002018 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002019 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002020 // Make sure we have executed any pending transitions, since there
2021 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002022 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002023 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002024 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002025 if (DEBUG_STATES) Slog.d(TAG_STATES,
2026 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002027 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002028 return false;
2029 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002030
2031 // Make sure that the user who owns this activity is started. If not,
2032 // we will just leave it as is because someone should be bringing
2033 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002034 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002035 Slog.w(TAG, "Skipping resume of top activity " + next
2036 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002037 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002038 return false;
2039 }
2040
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002041 // The activity may be waiting for stop, but that is no longer
2042 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002043 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002044 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002045 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002046 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002047
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002048 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002049
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002050 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002051 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002052 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002053 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002054 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002055 return false;
2056 }
2057
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002058 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2059
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002060 // We need to start pausing the current activity so the top one can be resumed...
2061 final boolean dontWaitForPause = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002062 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002063 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002064 if (DEBUG_STATES) Slog.d(TAG_STATES,
2065 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08002066 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002067 }
2068 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002069 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002070 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002071 // At this point we want to put the upcoming activity's process
2072 // at the top of the LRU list, since we know we will be needing it
2073 // very soon and it would be a waste to let it get killed if it
2074 // happens to be sitting towards the end.
2075 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002076 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002077 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002078 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002079 return true;
2080 }
2081
Christopher Tated3f175c2012-06-14 14:16:54 -07002082 // If the most recent activity was noHistory but was only stopped rather
2083 // than stopped+finished because the device went to sleep, we need to make
2084 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07002085 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002086 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002087 if (DEBUG_STATES) Slog.d(TAG_STATES,
2088 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002089 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002090 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002091 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002092 }
2093
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002094 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002095 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2096 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002097 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002098 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2099 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002100 } else {
2101 // The next activity is already visible, so hide the previous
2102 // activity's windows right now so we can show the new one ASAP.
2103 // We only do this if the previous is finishing, which should mean
2104 // it is on top of the one being resumed so hiding it quickly
2105 // is good. Otherwise, we want to do the normal route of allowing
2106 // the resumed activity to be shown so we can decide if the
2107 // previous should actually be hidden depending on whether the
2108 // new one is found to be full-screen or not.
2109 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002110 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002111 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2112 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002113 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002114 + ", nowVisible=" + next.nowVisible);
2115 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002116 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002117 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002118 + ", waitingVisible="
2119 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2120 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002121 }
2122 }
2123 }
2124
Dianne Hackborne7f97212011-02-24 14:40:20 -08002125 // Launching this app's activity, make sure the app is no longer
2126 // considered stopped.
2127 try {
2128 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002129 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002130 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002131 } catch (IllegalArgumentException e) {
2132 Slog.w(TAG, "Failed trying to unstop package "
2133 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002134 }
2135
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002136 // We are starting up the next activity, so tell the window manager
2137 // that the previous one will be hidden soon. This way it can know
2138 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002139 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002140 if (prev != null) {
2141 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002142 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002143 "Prepare close transition: prev=" + prev);
2144 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002145 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002146 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002147 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002148 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002149 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2150 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002151 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002152 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002153 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002154 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2155 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002156 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002157 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002158 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002159 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002160 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002161 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002162 : next.mLaunchTaskBehind
2163 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2164 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002165 }
2166 }
Craig Mautner967212c2013-04-13 21:10:58 -07002167 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002168 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002169 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002170 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002171 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002172 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002173 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002174 }
2175 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002176
2177 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002178 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002179 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2180 if (opts != null) {
2181 resumeAnimOptions = opts.toBundle();
2182 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002183 next.applyOptionsLocked();
2184 } else {
2185 next.clearOptionsLocked();
2186 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002187
Craig Mautnercf910b02013-04-23 11:23:27 -07002188 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002189 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002190 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002191
2192 // This activity is now becoming visible.
Jorim Jaggi1b025a62016-02-03 19:27:49 -08002193 if (!next.visible) {
2194 mWindowManager.setAppVisibility(next.appToken, true);
2195 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002196
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002197 // schedule launch ticks to collect information about slow apps.
2198 next.startLaunchTickingLocked();
2199
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002200 ActivityRecord lastResumedActivity =
2201 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002202 ActivityState lastState = next.state;
2203
2204 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002205
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002206 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002207 next.state = ActivityState.RESUMED;
2208 mResumedActivity = next;
2209 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002210 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002211 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002212 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002213 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002214
2215 // Have the window manager re-evaluate the orientation of
2216 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002217 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002218 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002219 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002220 mService.mConfiguration,
2221 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2222 if (config != null) {
2223 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002224 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002225 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002226 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002227
Craig Mautner525f3d92013-05-07 14:01:50 -07002228 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002229 // The configuration update wasn't able to keep the existing
2230 // instance of the activity, and instead started a new one.
2231 // We should be all done, but let's just make sure our activity
2232 // is still at the top and schedule another run if something
2233 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002234 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002235 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002236 "Activity config changed during resume: " + next
2237 + ", new next: " + nextNext);
2238 if (nextNext != next) {
2239 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002240 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002241 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002242 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002243 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002244 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002245 return true;
2246 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002247 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002248 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002249 }
Craig Mautner58547802013-03-05 08:23:53 -08002250
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002251 try {
2252 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002253 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002254 if (a != null) {
2255 final int N = a.size();
2256 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002257 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2258 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002259 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002260 }
2261 }
2262
2263 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002264 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002265 }
2266
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002267 // Well the app will no longer be stopped.
2268 // Clear app token stopped state in window manager if needed.
2269 mWindowManager.notifyAppStopped(next.appToken, false);
2270
Craig Mautner299f9602015-01-26 09:47:33 -08002271 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2272 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002273
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002274 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002275 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002276 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002277 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002278 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002279 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002280 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002281
Craig Mautner0eea92c2013-05-16 13:35:39 -07002282 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002283
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002284 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002285 } catch (Exception e) {
2286 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002287 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002288 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002289 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002290 if (lastStack != null) {
2291 lastStack.mResumedActivity = lastResumedActivity;
2292 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002293 Slog.i(TAG, "Restarting because process died: " + next);
2294 if (!next.hasBeenLaunched) {
2295 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002296 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2297 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002298 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002299 next.appToken, next.packageName, next.theme,
2300 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002301 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2302 next.windowFlags, null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002303 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002304 }
George Mount2c92c972014-03-20 09:38:23 -07002305 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002306 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002307 return true;
2308 }
2309
2310 // From this point on, if something goes wrong there is no way
2311 // to recover the activity.
2312 try {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002313 completeResumeLocked(next);
2314 } catch (Exception e) {
2315 // If any exception gets thrown, toss away this
2316 // activity and try the next one.
2317 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002318 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002319 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002320 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002321 return true;
2322 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002323 } else {
2324 // Whoops, need to restart this activity!
2325 if (!next.hasBeenLaunched) {
2326 next.hasBeenLaunched = true;
2327 } else {
2328 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002329 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002330 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002331 mService.compatibilityInfoForPackageLocked(
2332 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002333 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002334 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002335 null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002336 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002337 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002338 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002339 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002340 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002341 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002342 }
2343
Craig Mautnercf910b02013-04-23 11:23:27 -07002344 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002345 return true;
2346 }
2347
riddle_hsuc215a4f2014-12-27 12:10:45 +08002348 private TaskRecord getNextTask(TaskRecord targetTask) {
2349 final int index = mTaskHistory.indexOf(targetTask);
2350 if (index >= 0) {
2351 final int numTasks = mTaskHistory.size();
2352 for (int i = index + 1; i < numTasks; ++i) {
2353 TaskRecord task = mTaskHistory.get(i);
2354 if (task.userId == targetTask.userId) {
2355 return task;
2356 }
2357 }
2358 }
2359 return null;
2360 }
2361
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002362 private void insertTaskAtPosition(TaskRecord task, int position) {
2363 if (position >= mTaskHistory.size()) {
2364 insertTaskAtTop(task, null);
2365 return;
2366 }
2367 // Calculate maximum possible position for this task.
2368 int maxPosition = mTaskHistory.size();
2369 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002370 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002371 // Put non-current user tasks below current user tasks.
2372 while (maxPosition > 0) {
2373 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2374 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002375 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002376 break;
2377 }
2378 maxPosition--;
2379 }
2380 }
2381 position = Math.min(position, maxPosition);
2382 mTaskHistory.remove(task);
2383 mTaskHistory.add(position, task);
2384 updateTaskMovement(task, true);
2385 }
2386
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002387 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002388 // If the moving task is over home stack, transfer its return type to next task
2389 if (task.isOverHomeStack()) {
2390 final TaskRecord nextTask = getNextTask(task);
2391 if (nextTask != null) {
2392 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2393 }
2394 }
2395
Craig Mautner9c85c202013-10-04 20:11:26 -07002396 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002397 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002398 if (isOnHomeDisplay()) {
2399 ActivityStack lastStack = mStackSupervisor.getLastStack();
2400 final boolean fromHome = lastStack.isHomeStack();
2401 if (!isHomeStack() && (fromHome || topTask() != task)) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002402 int returnToType = APPLICATION_ACTIVITY_TYPE;
2403 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2404 returnToType = lastStack.topTask() == null
2405 ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2406 }
2407 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002408 }
2409 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002410 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002411 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002412
Craig Mautnerac6f8432013-07-17 13:24:59 -07002413 mTaskHistory.remove(task);
2414 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002415 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002416 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002417 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002418 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002419 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002420 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002421 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002422 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002423 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002424 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002425 break;
2426 }
2427 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002428 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002429 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002430 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002431 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002432 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002433
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002434 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2435 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002436 TaskRecord rTask = r.task;
2437 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002438 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2439 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002440 // Last activity in task had been removed or ActivityManagerService is reusing task.
2441 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002442 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002443 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002444 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002445 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002446 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002447 if (!newTask) {
2448 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002449 boolean startIt = true;
2450 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2451 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002452 if (task.getTopActivity() == null) {
2453 // All activities in task are finishing.
2454 continue;
2455 }
Craig Mautner70a86932013-02-28 22:37:44 -08002456 if (task == r.task) {
2457 // Here it is! Now, if this is not yet visible to the
2458 // user, then just add it without starting; it will
2459 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002460 if (!startIt) {
2461 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2462 + task, new RuntimeException("here").fillInStackTrace());
2463 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002464 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002465 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002466 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002467 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002468 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002469 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002470 return;
2471 }
2472 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002473 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002474 startIt = false;
2475 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002476 }
2477 }
2478
2479 // Place a new activity at top of stack, so it is next to interact
2480 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002481
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002482 // If we are not placing the new activity frontmost, we do not want
2483 // to deliver the onUserLeaving callback to the actual frontmost
2484 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002485 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002486 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002487 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002488 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002489 }
Craig Mautner70a86932013-02-28 22:37:44 -08002490
2491 task = r.task;
2492
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002493 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002494 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002495 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002496 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002497 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002498
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002499 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002500 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002501 // We want to show the starting preview window if we are
2502 // switching to a new task, or the next activity's process is
2503 // not currently running.
2504 boolean showStartingIcon = newTask;
2505 ProcessRecord proc = r.app;
2506 if (proc == null) {
2507 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2508 }
2509 if (proc == null || proc.thread == null) {
2510 showStartingIcon = true;
2511 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002512 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002513 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002514 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002515 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002516 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002517 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002518 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002519 ? r.mLaunchTaskBehind
2520 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2521 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002522 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002523 mNoAnimActivities.remove(r);
2524 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002525 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002526 boolean doShow = true;
2527 if (newTask) {
2528 // Even though this activity is starting fresh, we still need
2529 // to reset it to make sure we apply affinities to move any
2530 // existing activities from other tasks in to it.
2531 // If the caller has requested that the target task be
2532 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002533 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002534 resetTaskIfNeededLocked(r, r);
2535 doShow = topRunningNonDelayedActivityLocked(null) == r;
2536 }
Chong Zhang280d3322015-11-03 17:27:26 -08002537 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002538 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2539 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002540 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002541 if (r.mLaunchTaskBehind) {
2542 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2543 // tell WindowManager that r is visible even though it is at the back of the stack.
2544 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002545 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002546 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002547 // Figure out if we are transitioning from another activity that is
2548 // "has the same starting icon" as the next one. This allows the
2549 // window manager to keep the previous window it had previously
2550 // created, if it still had one.
2551 ActivityRecord prev = mResumedActivity;
2552 if (prev != null) {
2553 // We don't want to reuse the previous starting preview if:
2554 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002555 if (prev.task != r.task) {
2556 prev = null;
2557 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002558 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002559 else if (prev.nowVisible) {
2560 prev = null;
2561 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002562 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002563 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002564 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002565 mService.compatibilityInfoForPackageLocked(
2566 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002567 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002568 prev != null ? prev.appToken : null, showStartingIcon);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002569 r.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002570 }
2571 } else {
2572 // If this is the first activity, don't do any fancy animations,
2573 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002574 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002575 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002576 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002577 }
2578 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002579 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002580 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002581 }
2582
Dianne Hackbornbe707852011-11-11 14:32:10 -08002583 final void validateAppTokensLocked() {
2584 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002585 mValidateAppTokens.ensureCapacity(numActivities());
2586 final int numTasks = mTaskHistory.size();
2587 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2588 TaskRecord task = mTaskHistory.get(taskNdx);
2589 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002590 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002591 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002592 }
Craig Mautner000f0022013-02-26 15:04:29 -08002593 TaskGroup group = new TaskGroup();
2594 group.taskId = task.taskId;
2595 mValidateAppTokens.add(group);
2596 final int numActivities = activities.size();
2597 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2598 final ActivityRecord r = activities.get(activityNdx);
2599 group.tokens.add(r.appToken);
2600 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002601 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002602 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002603 }
2604
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002605 /**
2606 * Perform a reset of the given task, if needed as part of launching it.
2607 * Returns the new HistoryRecord at the top of the task.
2608 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002609 /**
2610 * Helper method for #resetTaskIfNeededLocked.
2611 * We are inside of the task being reset... we'll either finish this activity, push it out
2612 * for another task, or leave it as-is.
2613 * @param task The task containing the Activity (taskTop) that might be reset.
2614 * @param forceReset
2615 * @return An ActivityOptions that needs to be processed.
2616 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002617 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002618 ActivityOptions topOptions = null;
2619
2620 int replyChainEnd = -1;
2621 boolean canMoveOptions = true;
2622
2623 // We only do this for activities that are not the root of the task (since if we finish
2624 // the root, we may no longer have the task!).
2625 final ArrayList<ActivityRecord> activities = task.mActivities;
2626 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002627 final int rootActivityNdx = task.findEffectiveRootIndex();
2628 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002629 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002630 if (target.frontOfTask)
2631 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002632
2633 final int flags = target.info.flags;
2634 final boolean finishOnTaskLaunch =
2635 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2636 final boolean allowTaskReparenting =
2637 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2638 final boolean clearWhenTaskReset =
2639 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2640
2641 if (!finishOnTaskLaunch
2642 && !clearWhenTaskReset
2643 && target.resultTo != null) {
2644 // If this activity is sending a reply to a previous
2645 // activity, we can't do anything with it now until
2646 // we reach the start of the reply chain.
2647 // XXX note that we are assuming the result is always
2648 // to the previous activity, which is almost always
2649 // the case but we really shouldn't count on.
2650 if (replyChainEnd < 0) {
2651 replyChainEnd = i;
2652 }
2653 } else if (!finishOnTaskLaunch
2654 && !clearWhenTaskReset
2655 && allowTaskReparenting
2656 && target.taskAffinity != null
2657 && !target.taskAffinity.equals(task.affinity)) {
2658 // If this activity has an affinity for another
2659 // task, then we need to move it out of here. We will
2660 // move it as far out of the way as possible, to the
2661 // bottom of the activity stack. This also keeps it
2662 // correctly ordered with any activities we previously
2663 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002664 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002665 final ActivityRecord bottom =
2666 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002667 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002668 if (bottom != null && target.taskAffinity != null
2669 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002670 // If the activity currently at the bottom has the
2671 // same task affinity as the one we are moving,
2672 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002673 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002674 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002675 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002676 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002677 targetTask = createTaskRecord(
2678 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
2679 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002680 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002681 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002682 + " out to new task " + target.task);
2683 }
2684
Wale Ogunwale706ed792015-08-02 10:29:44 -07002685 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002686
Craig Mautner525f3d92013-05-07 14:01:50 -07002687 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002688 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2689 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002690 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002691 if (p.finishing) {
2692 continue;
2693 }
2694
Craig Mautnere3a74d52013-02-22 14:14:58 -08002695 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002696 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002697 topOptions = p.takeOptionsLocked();
2698 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002699 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002700 }
2701 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002702 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2703 "Removing activity " + p + " from task=" + task + " adding to task="
2704 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002705 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2706 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002707 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002708 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002709
Wale Ogunwale706ed792015-08-02 10:29:44 -07002710 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002711 }
2712
Wale Ogunwale706ed792015-08-02 10:29:44 -07002713 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002714 if (VALIDATE_TOKENS) {
2715 validateAppTokensLocked();
2716 }
2717
2718 replyChainEnd = -1;
2719 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2720 // If the activity should just be removed -- either
2721 // because it asks for it, or the task should be
2722 // cleared -- then finish it and anything that is
2723 // part of its reply chain.
2724 int end;
2725 if (clearWhenTaskReset) {
2726 // In this case, we want to finish this activity
2727 // and everything above it, so be sneaky and pretend
2728 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002729 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002730 } else if (replyChainEnd < 0) {
2731 end = i;
2732 } else {
2733 end = replyChainEnd;
2734 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002735 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002736 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002737 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002738 if (p.finishing) {
2739 continue;
2740 }
2741 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002742 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002743 topOptions = p.takeOptionsLocked();
2744 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002745 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002746 }
2747 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002748 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002749 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002750 if (finishActivityLocked(
2751 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002752 end--;
2753 srcPos--;
2754 }
2755 }
2756 replyChainEnd = -1;
2757 } else {
2758 // If we were in the middle of a chain, well the
2759 // activity that started it all doesn't want anything
2760 // special, so leave it all as-is.
2761 replyChainEnd = -1;
2762 }
2763 }
2764
2765 return topOptions;
2766 }
2767
2768 /**
2769 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2770 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2771 * @param affinityTask The task we are looking for an affinity to.
2772 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2773 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2774 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2775 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002776 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002777 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002778 int replyChainEnd = -1;
2779 final int taskId = task.taskId;
2780 final String taskAffinity = task.affinity;
2781
2782 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2783 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002784 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2785
2786 // Do not operate on or below the effective root Activity.
2787 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002788 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002789 if (target.frontOfTask)
2790 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002791
2792 final int flags = target.info.flags;
2793 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2794 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2795
2796 if (target.resultTo != null) {
2797 // If this activity is sending a reply to a previous
2798 // activity, we can't do anything with it now until
2799 // we reach the start of the reply chain.
2800 // XXX note that we are assuming the result is always
2801 // to the previous activity, which is almost always
2802 // the case but we really shouldn't count on.
2803 if (replyChainEnd < 0) {
2804 replyChainEnd = i;
2805 }
2806 } else if (topTaskIsHigher
2807 && allowTaskReparenting
2808 && taskAffinity != null
2809 && taskAffinity.equals(target.taskAffinity)) {
2810 // This activity has an affinity for our task. Either remove it if we are
2811 // clearing or move it over to our task. Note that
2812 // we currently punt on the case where we are resetting a
2813 // task that is not at the top but who has activities above
2814 // with an affinity to it... this is really not a normal
2815 // case, and we will need to later pull that task to the front
2816 // and usually at that point we will do the reset and pick
2817 // up those remaining activities. (This only happens if
2818 // someone starts an activity in a new task from an activity
2819 // in a task that is not currently on top.)
2820 if (forceReset || finishOnTaskLaunch) {
2821 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002822 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2823 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002824 for (int srcPos = start; srcPos >= i; --srcPos) {
2825 final ActivityRecord p = activities.get(srcPos);
2826 if (p.finishing) {
2827 continue;
2828 }
Todd Kennedy539db512014-12-15 09:57:55 -08002829 finishActivityLocked(
2830 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002831 }
2832 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002833 if (taskInsertionPoint < 0) {
2834 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002835
Craig Mautner77878772013-03-04 19:46:24 -08002836 }
Craig Mautner77878772013-03-04 19:46:24 -08002837
2838 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002839 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2840 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2841 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002842 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002843 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002844 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002845 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002846
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002847 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2848 "Removing and adding activity " + p + " to stack at " + task
2849 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002850 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2851 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002852 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002853 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002854 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002855 if (VALIDATE_TOKENS) {
2856 validateAppTokensLocked();
2857 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002858
2859 // Now we've moved it in to place... but what if this is
2860 // a singleTop activity and we have put it on top of another
2861 // instance of the same activity? Then we drop the instance
2862 // below so it remains singleTop.
2863 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2864 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002865 int targetNdx = taskActivities.indexOf(target);
2866 if (targetNdx > 0) {
2867 ActivityRecord p = taskActivities.get(targetNdx - 1);
2868 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002869 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2870 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002871 }
2872 }
2873 }
2874 }
2875
2876 replyChainEnd = -1;
2877 }
2878 }
Craig Mautner77878772013-03-04 19:46:24 -08002879 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002880 }
2881
Craig Mautner8849a5e2013-04-02 16:41:03 -07002882 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002883 ActivityRecord newActivity) {
2884 boolean forceReset =
2885 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2886 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2887 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2888 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2889 forceReset = true;
2890 }
2891 }
2892
2893 final TaskRecord task = taskTop.task;
2894
2895 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2896 * for remaining tasks. Used for later tasks to reparent to task. */
2897 boolean taskFound = false;
2898
2899 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2900 ActivityOptions topOptions = null;
2901
Craig Mautner77878772013-03-04 19:46:24 -08002902 // Preserve the location for reparenting in the new task.
2903 int reparentInsertionPoint = -1;
2904
Craig Mautnere3a74d52013-02-22 14:14:58 -08002905 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2906 final TaskRecord targetTask = mTaskHistory.get(i);
2907
2908 if (targetTask == task) {
2909 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2910 taskFound = true;
2911 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002912 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2913 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002914 }
2915 }
2916
Craig Mautner70a86932013-02-28 22:37:44 -08002917 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002918 if (taskNdx >= 0) {
2919 do {
2920 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2921 } while (taskTop == null && taskNdx >= 0);
2922 }
Craig Mautner70a86932013-02-28 22:37:44 -08002923
Craig Mautnere3a74d52013-02-22 14:14:58 -08002924 if (topOptions != null) {
2925 // If we got some ActivityOptions from an activity on top that
2926 // was removed from the task, propagate them to the new real top.
2927 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002928 taskTop.updateOptionsLocked(topOptions);
2929 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002930 topOptions.abort();
2931 }
2932 }
2933
2934 return taskTop;
2935 }
2936
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002937 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2938 String resultWho, int requestCode, int resultCode, Intent data) {
2939
2940 if (callingUid > 0) {
2941 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002942 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002943 }
2944
2945 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2946 + " : who=" + resultWho + " req=" + requestCode
2947 + " res=" + resultCode + " data=" + data);
2948 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2949 try {
2950 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2951 list.add(new ResultInfo(resultWho, requestCode,
2952 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002953 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002954 return;
2955 } catch (Exception e) {
2956 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2957 }
2958 }
2959
2960 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2961 }
2962
Craig Mautner299f9602015-01-26 09:47:33 -08002963 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002964 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2965 return;
2966 }
2967
2968 final ActivityRecord next = topRunningActivityLocked();
2969 final String myReason = reason + " adjustFocus";
2970 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002971 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002972 // For freeform, docked, and pinned stacks we always keep the focus within the
2973 // stack as long as there is a running activity in the stack that we can adjust
2974 // focus to.
2975 mService.setFocusedActivityLocked(next, myReason);
2976 return;
2977 } else {
2978 final TaskRecord task = r.task;
2979 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2980 // For non-fullscreen stack, we want to move the focus to the next visible
2981 // stack to prevent the home screen from moving to the top and obscuring
2982 // other visible stacks.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002983 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002984 return;
2985 }
2986 // Move the home stack to the top if this stack is fullscreen or there is no
2987 // other visible stack.
2988 if (mStackSupervisor.moveHomeStackTaskToTop(
2989 task.getTaskToReturnTo(), myReason)) {
2990 // Activity focus was already adjusted. Nothing else to do...
2991 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002992 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002993 }
2994 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08002995 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002996
Wale Ogunwaled046a012015-12-24 13:05:59 -08002997 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002998 }
2999
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003000 private boolean adjustFocusToNextFocusableStackLocked(String reason) {
3001 final ActivityStack stack = getNextFocusableStackLocked();
3002 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003003 if (stack == null) {
3004 return false;
3005 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003006 return mService.setFocusedActivityLocked(stack.topRunningActivityLocked(), myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003007 }
3008
Craig Mautnerf3333272013-04-22 10:55:53 -07003009 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003010 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003011 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3012 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3013 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07003014 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003015 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003016 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3017 "stop-no-history", false)) {
3018 // Activity was finished, no need to continue trying to schedule stop.
3019 adjustFocusedActivityLocked(r, "stopActivityFinished");
3020 r.resumeKeyDispatchingLocked();
3021 return;
3022 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003023 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003024 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003025 + " on stop because we're just sleeping");
3026 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003027 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003028 }
3029
3030 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08003031 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003032 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003033 try {
3034 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003035 if (DEBUG_STATES) Slog.v(TAG_STATES,
3036 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003037 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003038 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3039 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003040 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003041 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003042 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003043 EventLogTags.writeAmStopActivity(
3044 r.userId, System.identityHashCode(r), r.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003045 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07003046 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003047 r.setSleeping(true);
3048 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003049 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003050 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003051 } catch (Exception e) {
3052 // Maybe just ignore exceptions here... if the process
3053 // has crashed, our death notification will clean things
3054 // up.
3055 Slog.w(TAG, "Exception thrown during pause", e);
3056 // Just in case, assume it to be stopped.
3057 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003058 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003059 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003060 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003061 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003062 }
3063 }
3064 }
3065 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003066
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003067 /**
3068 * @return Returns true if the activity is being finished, false if for
3069 * some reason it is being left as-is.
3070 */
3071 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003072 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003073 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003074 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3075 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003076 + ", result=" + resultCode + ", data=" + resultData
3077 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003078 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003079 return false;
3080 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003081
Craig Mautnerd44711d2013-02-23 11:24:36 -08003082 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003083 return true;
3084 }
3085
Craig Mautnerd2328952013-03-05 12:46:26 -08003086 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003087 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3088 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3089 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3090 ActivityRecord r = activities.get(activityNdx);
3091 if (r.resultTo == self && r.requestCode == requestCode) {
3092 if ((r.resultWho == null && resultWho == null) ||
3093 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3094 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3095 false);
3096 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003097 }
3098 }
3099 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003100 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003101 }
3102
Adrian Roos20d7df32016-01-12 18:59:43 +01003103 final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003104 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003105 TaskRecord finishedTask = null;
3106 if (r == null || r.app != app) {
3107 return null;
3108 }
3109 Slog.w(TAG, " Force finishing activity "
3110 + r.intent.getComponent().flattenToShortString());
3111 int taskNdx = mTaskHistory.indexOf(r.task);
3112 int activityNdx = r.task.mActivities.indexOf(r);
3113 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3114 finishedTask = r.task;
3115 // Also terminate any activities below it that aren't yet
3116 // stopped, to avoid a situation where one will get
3117 // re-start our crashing activity once it gets resumed again.
3118 --activityNdx;
3119 if (activityNdx < 0) {
3120 do {
3121 --taskNdx;
3122 if (taskNdx < 0) {
3123 break;
3124 }
3125 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3126 } while (activityNdx < 0);
3127 }
3128 if (activityNdx >= 0) {
3129 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3130 if (r.state == ActivityState.RESUMED
3131 || r.state == ActivityState.PAUSING
3132 || r.state == ActivityState.PAUSED) {
3133 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3134 Slog.w(TAG, " Force finishing activity "
3135 + r.intent.getComponent().flattenToShortString());
3136 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003137 }
3138 }
3139 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003140 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003141 }
3142
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003143 final void finishVoiceTask(IVoiceInteractionSession session) {
3144 IBinder sessionBinder = session.asBinder();
3145 boolean didOne = false;
3146 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3147 TaskRecord tr = mTaskHistory.get(taskNdx);
3148 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3149 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3150 ActivityRecord r = tr.mActivities.get(activityNdx);
3151 if (!r.finishing) {
3152 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3153 false);
3154 didOne = true;
3155 }
3156 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003157 } else {
3158 // Check if any of the activities are using voice
3159 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3160 ActivityRecord r = tr.mActivities.get(activityNdx);
3161 if (r.voiceSession != null
3162 && r.voiceSession.asBinder() == sessionBinder) {
3163 // Inform of cancellation
3164 r.clearVoiceSessionLocked();
3165 try {
3166 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3167 null);
3168 } catch (RemoteException re) {
3169 // Ok
3170 }
3171 // TODO: VI This is redundant in some cases
3172 mService.finishRunningVoiceLocked();
3173 break;
3174 }
3175 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003176 }
3177 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003178 Slog.d(TAG, "ActivityStack.finishVoiceTask()");
3179
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003180 if (didOne) {
3181 mService.updateOomAdjLocked();
3182 }
3183 }
3184
Craig Mautnerd2328952013-03-05 12:46:26 -08003185 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003186 ArrayList<ActivityRecord> activities = r.task.mActivities;
3187 for (int index = activities.indexOf(r); index >= 0; --index) {
3188 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003189 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003190 break;
3191 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003192 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003193 }
3194 return true;
3195 }
3196
Dianne Hackborn5c607432012-02-28 14:44:19 -08003197 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3198 // send the result
3199 ActivityRecord resultTo = r.resultTo;
3200 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003201 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003202 + " who=" + r.resultWho + " req=" + r.requestCode
3203 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003204 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003205 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003206 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003207 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003208 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003209 if (r.info.applicationInfo.uid > 0) {
3210 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3211 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003212 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003213 }
3214 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3215 resultData);
3216 r.resultTo = null;
3217 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003218 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003219
3220 // Make sure this HistoryRecord is not holding on to other resources,
3221 // because clients have remote IPC references to this object so we
3222 // can't assume that will go away and want to avoid circular IPC refs.
3223 r.results = null;
3224 r.pendingResults = null;
3225 r.newIntents = null;
3226 r.icicle = null;
3227 }
3228
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003229 /**
3230 * @return Returns true if this activity has been removed from the history
3231 * list, or false if it is still in the list and will be removed later.
3232 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003233 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3234 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003235 if (r.finishing) {
3236 Slog.w(TAG, "Duplicate finish request for " + r);
3237 return false;
3238 }
3239
Wale Ogunwale7d701172015-03-11 15:36:30 -07003240 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003241 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003242 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003243 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003244 task.taskId, r.shortComponentName, reason);
3245 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003246 final int index = activities.indexOf(r);
3247 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003248 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003249 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003250 // If the caller asked that this activity (and all above it)
3251 // be cleared when the task is reset, don't lose that information,
3252 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003253 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003254 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003255 }
3256 }
3257
3258 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003259
Craig Mautner299f9602015-01-26 09:47:33 -08003260 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003261
Dianne Hackborn5c607432012-02-28 14:44:19 -08003262 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003263
Craig Mautnerde4ef022013-04-07 19:01:33 -07003264 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003265 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003266 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003267 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003268 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003269 ? AppTransition.TRANSIT_TASK_CLOSE
3270 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003271
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003272 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003273 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003274
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003275 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003276 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3277 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3278 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003279 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003280 }
3281
Craig Mautneraea74a52014-03-08 14:23:10 -08003282 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003283 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003284 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003285 } else if (r.state != ActivityState.PAUSING) {
3286 // If the activity is PAUSING, we will complete the finish once
3287 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003288 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003289 if (r.visible) {
3290 mWindowManager.setAppVisibility(r.appToken, false);
3291 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003292 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003293 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003294 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003295 }
3296
3297 return false;
3298 }
3299
Craig Mautnerf3333272013-04-22 10:55:53 -07003300 static final int FINISH_IMMEDIATELY = 0;
3301 static final int FINISH_AFTER_PAUSE = 1;
3302 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003303
Craig Mautnerf3333272013-04-22 10:55:53 -07003304 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003305 // First things first: if this activity is currently visible,
3306 // and the resumed activity is not yet visible, then hold off on
3307 // finishing until the resumed one becomes visible.
3308 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003309 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08003310 addToStopping(r, false /* immediate */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003311 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003312 if (DEBUG_STATES) Slog.v(TAG_STATES,
3313 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003314 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003315 if (oomAdj) {
3316 mService.updateOomAdjLocked();
3317 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003318 return r;
3319 }
3320
3321 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003322 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003323 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003324 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003325 if (mResumedActivity == r) {
3326 mResumedActivity = null;
3327 }
3328 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003329 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003330 r.state = ActivityState.FINISHING;
3331
3332 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003333 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003334 || prevState == ActivityState.STOPPED
3335 || prevState == ActivityState.INITIALIZING) {
3336 // If this activity is already stopped, we can just finish
3337 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003338 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003339 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003340 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003341 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003342 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003343 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003344 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3345 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003346 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003347 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003348
3349 // Need to go through the full pause cycle to get this
3350 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003351 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003352 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003353 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003354 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003355 return r;
3356 }
3357
Craig Mautneree36c772014-07-16 14:56:05 -07003358 void finishAllActivitiesLocked(boolean immediately) {
3359 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003360 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3361 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3362 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3363 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003364 noActivitiesInStack = false;
3365 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003366 continue;
3367 }
Craig Mautneree36c772014-07-16 14:56:05 -07003368 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003369 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3370 }
3371 }
Craig Mautneree36c772014-07-16 14:56:05 -07003372 if (noActivitiesInStack) {
3373 mActivityContainer.onTaskListEmptyLocked();
3374 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003375 }
3376
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003377 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3378 // Basic case: for simple app-centric recents, we need to recreate
3379 // the task if the affinity has changed.
3380 if (srec == null || srec.task.affinity == null ||
3381 !srec.task.affinity.equals(destAffinity)) {
3382 return true;
3383 }
3384 // Document-centric case: an app may be split in to multiple documents;
3385 // they need to re-create their task if this current activity is the root
3386 // of a document, unless simply finishing it will return them to the the
3387 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003388 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3389 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003390 // Okay, this activity is at the root of its task. What to do, what to do...
3391 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3392 // Finishing won't return to an application, so we need to recreate.
3393 return true;
3394 }
3395 // We now need to get the task below it to determine what to do.
3396 int taskIdx = mTaskHistory.indexOf(srec.task);
3397 if (taskIdx <= 0) {
3398 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3399 return false;
3400 }
3401 if (taskIdx == 0) {
3402 // At the bottom of the stack, nothing to go back to.
3403 return true;
3404 }
3405 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3406 if (!srec.task.affinity.equals(prevTask.affinity)) {
3407 // These are different apps, so need to recreate.
3408 return true;
3409 }
3410 }
3411 return false;
3412 }
3413
Wale Ogunwale7d701172015-03-11 15:36:30 -07003414 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003415 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003416 final TaskRecord task = srec.task;
3417 final ArrayList<ActivityRecord> activities = task.mActivities;
3418 final int start = activities.indexOf(srec);
3419 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003420 return false;
3421 }
3422 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003423 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003424 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003425 final ComponentName dest = destIntent.getComponent();
3426 if (start > 0 && dest != null) {
3427 for (int i = finishTo; i >= 0; i--) {
3428 ActivityRecord r = activities.get(i);
3429 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003430 r.info.name.equals(dest.getClassName())) {
3431 finishTo = i;
3432 parent = r;
3433 foundParentInTask = true;
3434 break;
3435 }
3436 }
3437 }
3438
3439 IActivityController controller = mService.mController;
3440 if (controller != null) {
3441 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3442 if (next != null) {
3443 // ask watcher if this is allowed
3444 boolean resumeOK = true;
3445 try {
3446 resumeOK = controller.activityResuming(next.packageName);
3447 } catch (RemoteException e) {
3448 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003449 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003450 }
3451
3452 if (!resumeOK) {
3453 return false;
3454 }
3455 }
3456 }
3457 final long origId = Binder.clearCallingIdentity();
3458 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003459 ActivityRecord r = activities.get(i);
3460 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003461 // Only return the supplied result for the first activity finished
3462 resultCode = Activity.RESULT_CANCELED;
3463 resultData = null;
3464 }
3465
3466 if (parent != null && foundParentInTask) {
3467 final int parentLaunchMode = parent.info.launchMode;
3468 final int destIntentFlags = destIntent.getFlags();
3469 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3470 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3471 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3472 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003473 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3474 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003475 } else {
3476 try {
3477 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3478 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003479 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3480 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3481 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003482 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3483 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003484 foundParentInTask = res == ActivityManager.START_SUCCESS;
3485 } catch (RemoteException e) {
3486 foundParentInTask = false;
3487 }
3488 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003489 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003490 }
3491 }
3492 Binder.restoreCallingIdentity(origId);
3493 return foundParentInTask;
3494 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003495 /**
3496 * Perform the common clean-up of an activity record. This is called both
3497 * as part of destroyActivityLocked() (when destroying the client-side
3498 * representation) and cleaning things up as a result of its hosting
3499 * processing going away, in which case there is no remaining client-side
3500 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003501 *
3502 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003503 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003504 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3505 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003506 if (mResumedActivity == r) {
3507 mResumedActivity = null;
3508 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003509 if (mPausingActivity == r) {
3510 mPausingActivity = null;
3511 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003512 mService.resetFocusedActivityIfNeededLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003513
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003514 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003515 r.frozenBeforeDestroy = false;
3516
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003517 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003518 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003519 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003520 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003521 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003522 }
3523
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003524 // Make sure this record is no longer in the pending finishes list.
3525 // This could happen, for example, if we are trimming activities
3526 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003527 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003528 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003529
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003530 // Remove any pending results.
3531 if (r.finishing && r.pendingResults != null) {
3532 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3533 PendingIntentRecord rec = apr.get();
3534 if (rec != null) {
3535 mService.cancelIntentSenderLocked(rec, false);
3536 }
3537 }
3538 r.pendingResults = null;
3539 }
3540
3541 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003542 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003543 }
3544
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003545 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003546 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003547 if (getVisibleBehindActivity() == r) {
3548 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003549 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003550 }
3551
3552 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003553 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003554 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003555 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003556 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003557 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003558 }
3559
Craig Mautner299f9602015-01-26 09:47:33 -08003560 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003561 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003562 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003563 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003564 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3565 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3566
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003567 r.takeFromHistory();
3568 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003569 if (DEBUG_STATES) Slog.v(TAG_STATES,
3570 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003571 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003572 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003573 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003574 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003575 if (VALIDATE_TOKENS) {
3576 validateAppTokensLocked();
3577 }
Craig Mautner312ba862014-02-10 17:55:01 -08003578 final TaskRecord task = r.task;
3579 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003580 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003581 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003582 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003583 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003584 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003585 }
Craig Mautner299f9602015-01-26 09:47:33 -08003586 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003587 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003588 cleanUpActivityServicesLocked(r);
3589 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003590 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003591
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003592 /**
3593 * Perform clean-up of service connections in an activity record.
3594 */
3595 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3596 // Throw away any services that have been bound by this activity.
3597 if (r.connections != null) {
3598 Iterator<ConnectionRecord> it = r.connections.iterator();
3599 while (it.hasNext()) {
3600 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003601 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003602 }
3603 r.connections = null;
3604 }
3605 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003606
Craig Mautneree2e45a2014-06-27 12:10:03 -07003607 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003608 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003609 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003610 mHandler.sendMessage(msg);
3611 }
3612
Craig Mautneree2e45a2014-06-27 12:10:03 -07003613 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003614 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003615 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003616 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3617 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3618 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3619 final ActivityRecord r = activities.get(activityNdx);
3620 if (r.finishing) {
3621 continue;
3622 }
3623 if (r.fullscreen) {
3624 lastIsOpaque = true;
3625 }
3626 if (owner != null && r.app != owner) {
3627 continue;
3628 }
3629 if (!lastIsOpaque) {
3630 continue;
3631 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003632 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003633 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003634 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003635 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003636 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003637 activityRemoved = true;
3638 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003639 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003640 }
3641 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003642 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003643 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003644 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003645 }
3646
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003647 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3648 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003649 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3650 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003651 + " pausing=" + mPausingActivity + " for reason " + reason);
3652 return destroyActivityLocked(r, true, reason);
3653 }
3654 return false;
3655 }
3656
3657 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3658 String reason) {
3659 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003660 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003661 int maxTasks = tasks.size() / 4;
3662 if (maxTasks < 1) {
3663 maxTasks = 1;
3664 }
3665 int numReleased = 0;
3666 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3667 final TaskRecord task = mTaskHistory.get(taskNdx);
3668 if (!tasks.contains(task)) {
3669 continue;
3670 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003671 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003672 int curNum = 0;
3673 final ArrayList<ActivityRecord> activities = task.mActivities;
3674 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3675 final ActivityRecord activity = activities.get(actNdx);
3676 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003677 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003678 + " in state " + activity.state + " resumed=" + mResumedActivity
3679 + " pausing=" + mPausingActivity + " for reason " + reason);
3680 destroyActivityLocked(activity, true, reason);
3681 if (activities.get(actNdx) != activity) {
3682 // Was removed from list, back up so we don't miss the next one.
3683 actNdx--;
3684 }
3685 curNum++;
3686 }
3687 }
3688 if (curNum > 0) {
3689 numReleased += curNum;
3690 maxTasks--;
3691 if (mTaskHistory.get(taskNdx) != task) {
3692 // The entire task got removed, back up so we don't miss the next one.
3693 taskNdx--;
3694 }
3695 }
3696 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003697 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3698 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003699 return numReleased;
3700 }
3701
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003702 /**
3703 * Destroy the current CLIENT SIDE instance of an activity. This may be
3704 * called both when actually finishing an activity, or when performing
3705 * a configuration switch where we destroy the current client-side object
3706 * but then create a new client-side object for this same HistoryRecord.
3707 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003708 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003709 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3710 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003711 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003712 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003713 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003714 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003715
3716 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003717
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003718 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003719
3720 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003721
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003722 if (hadApp) {
3723 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003724 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003725 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3726 mService.mHeavyWeightProcess = null;
3727 mService.mHandler.sendEmptyMessage(
3728 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3729 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003730 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003731 // Update any services we are bound to that might care about whether
3732 // their client may have activities.
3733 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003734 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003735 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003736 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003737 }
3738 }
3739
3740 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003741
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003742 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003743 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003744 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003745 r.configChangeFlags);
3746 } catch (Exception e) {
3747 // We can just ignore exceptions here... if the process
3748 // has crashed, our death notification will clean things
3749 // up.
3750 //Slog.w(TAG, "Exception thrown during finish", e);
3751 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003752 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003753 removedFromHistory = true;
3754 skipDestroy = true;
3755 }
3756 }
3757
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003758 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003759
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003760 // If the activity is finishing, we need to wait on removing it
3761 // from the list to give it a chance to do its cleanup. During
3762 // that time it may make calls back with its token so we need to
3763 // be able to find it on the list and so we don't want to remove
3764 // it from the list yet. Otherwise, we can just immediately put
3765 // it in the destroyed state since we are not removing it from the
3766 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003767 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003768 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003769 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003770 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003771 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003772 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3773 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003774 if (DEBUG_STATES) Slog.v(TAG_STATES,
3775 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003776 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003777 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003778 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003779 }
3780 } else {
3781 // remove this record from the history.
3782 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003783 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003784 removedFromHistory = true;
3785 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003786 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003787 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003788 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003789 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003790 }
3791 }
3792
3793 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003794
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003795 if (!mLRUActivities.remove(r) && hadApp) {
3796 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3797 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003798
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003799 return removedFromHistory;
3800 }
3801
Craig Mautner299f9602015-01-26 09:47:33 -08003802 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003803 final long origId = Binder.clearCallingIdentity();
3804 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003805 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003806 if (r != null) {
3807 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003808 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003809 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003810
Wale Ogunwale60454db2015-01-23 16:05:07 -08003811 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003812 if (r.state == ActivityState.DESTROYING) {
3813 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003814 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003815 }
3816 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003817 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003818 } finally {
3819 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003820 }
3821 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003822
Todd Kennedyaab56db2015-01-30 09:39:53 -08003823 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003824 if (hasVisibleBehindActivity() &&
3825 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08003826 if (r == topRunningActivityLocked()
3827 && getStackVisibilityLocked(null) == STACK_VISIBLE) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003828 // Don't release the top activity if it has requested to run behind the next
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08003829 // activity and the stack is currently visible.
Craig Mautner64ccb702014-10-01 09:38:40 -07003830 return;
3831 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003832 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003833 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003834 " thread=" + r.app.thread);
3835 if (r != null && r.app != null && r.app.thread != null) {
3836 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003837 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003838 } catch (RemoteException e) {
3839 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003840 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003841 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003842 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003843 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003844 }
3845 }
3846 }
3847
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003848 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003849 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3850 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003851 if (r != null) {
3852 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003853 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003854 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003855 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003856 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003857 }
3858
Jose Lima4b6c6692014-08-12 17:41:12 -07003859 boolean hasVisibleBehindActivity() {
3860 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003861 }
3862
Jose Lima4b6c6692014-08-12 17:41:12 -07003863 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003864 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003865 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003866 }
3867 }
3868
Jose Lima4b6c6692014-08-12 17:41:12 -07003869 ActivityRecord getVisibleBehindActivity() {
3870 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003871 }
3872
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003873 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3874 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003875 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003876 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3877 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003878 while (i > 0) {
3879 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003880 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003881 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003882 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003883 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003884 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003885 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003886 }
3887 }
3888 }
3889
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003890 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3891 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003892 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3893 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003894 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3895 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003896 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003897 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003898 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3899 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003900
3901 boolean hasVisibleActivities = false;
3902
3903 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003904 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003905 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3906 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003907 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3908 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3909 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3910 final ActivityRecord r = activities.get(activityNdx);
3911 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003912 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3913 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003914 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003915 if (r.visible) {
3916 hasVisibleActivities = true;
3917 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003918 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003919 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3920 // Don't currently have state for the activity, or
3921 // it is finishing -- always remove it.
3922 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003923 } else if (!r.visible && r.launchCount > 2 &&
3924 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003925 // We have launched this activity too many times since it was
3926 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003927 // (Note if the activity is visible, we don't remove the record.
3928 // We leave the dead window on the screen but the process will
3929 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003930 remove = true;
3931 } else {
3932 // The process may be gone, but the activity lives on!
3933 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003934 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003935 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003936 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3937 "Removing activity " + r + " from stack at " + i
3938 + ": haveState=" + r.haveState
3939 + " stateNotNeeded=" + r.stateNotNeeded
3940 + " finishing=" + r.finishing
3941 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003942 if (!r.finishing) {
3943 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3944 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3945 r.userId, System.identityHashCode(r),
3946 r.task.taskId, r.shortComponentName,
3947 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003948 if (r.state == ActivityState.RESUMED) {
3949 mService.updateUsageStats(r, false);
3950 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003951 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003952 } else {
3953 // We have the current state for this activity, so
3954 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003955 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3956 if (DEBUG_APP) Slog.v(TAG_APP,
3957 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003958 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003959 // Set nowVisible to previous visible state. If the app was visible while
3960 // it died, we leave the dead window on screen so it's basically visible.
3961 // This is needed when user later tap on the dead window, we need to stop
3962 // other apps when user transfers focus to the restarted activity.
3963 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003964 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003965 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003966 "App died, clearing saved state of " + r);
3967 r.icicle = null;
3968 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003969 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003970 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003971 if (remove) {
3972 removeActivityFromHistoryLocked(r, "appDied");
3973 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003974 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003975 }
3976 }
3977
3978 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003979 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003980
Chong Zhang280d3322015-11-03 17:27:26 -08003981 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003982 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003983 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003984 if (r != null && r.state != ActivityState.RESUMED) {
3985 r.updateOptionsLocked(options);
3986 } else {
3987 ActivityOptions.abort(options);
3988 }
3989 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003990 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003991 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003992
Craig Mautner21d24a22014-04-23 11:45:37 -07003993 void updateTaskMovement(TaskRecord task, boolean toFront) {
3994 if (task.isPersistable) {
3995 task.mLastTimeMoved = System.currentTimeMillis();
3996 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3997 // recently will be most negative, tasks sent to the bottom before that will be less
3998 // negative. Similarly for recent tasks moved to the top which will be most positive.
3999 if (!toFront) {
4000 task.mLastTimeMoved *= -1;
4001 }
4002 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07004003 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07004004 }
4005
Craig Mautner84984fa2014-06-19 11:19:20 -07004006 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07004007 final int top = mTaskHistory.size() - 1;
4008 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
4009 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004010 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004011 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4012 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07004013 mTaskHistory.remove(taskNdx);
4014 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004015 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07004016 return;
4017 }
4018 }
4019 }
4020
Chong Zhang280d3322015-11-03 17:27:26 -08004021 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004022 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004023 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004024
Craig Mautner11bf9a52013-02-19 14:08:51 -08004025 final int numTasks = mTaskHistory.size();
4026 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004027 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004028 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004029 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004030 ActivityOptions.abort(options);
4031 } else {
4032 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
4033 }
4034 return;
4035 }
4036
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004037 if (timeTracker != null) {
4038 // The caller wants a time tracker associated with this task.
4039 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4040 tr.mActivities.get(i).appTimeTracker = timeTracker;
4041 }
4042 }
4043
Craig Mautner11bf9a52013-02-19 14:08:51 -08004044 // Shift all activities with this task up to the top
4045 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004046 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004047
Chong Zhang45c25ce2015-08-10 22:18:26 -07004048 // Don't refocus if invisible to current user
4049 ActivityRecord top = tr.getTopActivity();
4050 if (!okToShowLocked(top)) {
4051 addRecentActivityLocked(top);
4052 ActivityOptions.abort(options);
4053 return;
4054 }
4055
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004056 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004057 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004058 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004059
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004060 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004061 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004062 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004063 if (r != null) {
4064 mNoAnimActivities.add(r);
4065 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004066 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004067 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08004068 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004069 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004070
Wale Ogunwaled046a012015-12-24 13:05:59 -08004071 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004072 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004073
4074 if (VALIDATE_TOKENS) {
4075 validateAppTokensLocked();
4076 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004077 }
4078
4079 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004080 * Worker method for rearranging history stack. Implements the function of moving all
4081 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004082 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004083 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004084 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4085 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004086 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004087 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004088 * @return Returns true if the move completed, false if not.
4089 */
Craig Mautner299f9602015-01-26 09:47:33 -08004090 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004091 final TaskRecord tr = taskForIdLocked(taskId);
4092 if (tr == null) {
4093 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4094 return false;
4095 }
4096
4097 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07004098 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004099
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004100 // If we have a watcher, preflight the move before committing to it. First check
4101 // for *other* available tasks, but if none are available, then try again allowing the
4102 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004103 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004104 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004105 if (next == null) {
4106 next = topRunningActivityLocked(null, 0);
4107 }
4108 if (next != null) {
4109 // ask watcher if this is allowed
4110 boolean moveOK = true;
4111 try {
4112 moveOK = mService.mController.activityResuming(next.packageName);
4113 } catch (RemoteException e) {
4114 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004115 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004116 }
4117 if (!moveOK) {
4118 return false;
4119 }
4120 }
4121 }
4122
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004123 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004124
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004125 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4126 // For the case where we are moving the home task back and there is an activity visible
4127 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4128 // activity to maintain order with what the user is seeing.
4129 final ActivityStack fullscreenStack =
4130 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4131 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4132 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4133 mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004134 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004135 return true;
4136 }
4137 }
4138
riddle_hsuc215a4f2014-12-27 12:10:45 +08004139 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004140
4141 // If true, we should resume the home activity next if the task we are moving to the
4142 // back is over the home stack. We force to false if the task we are moving to back
4143 // is the home task and we don't want it resumed after moving to the back.
4144 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4145 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004146 final TaskRecord nextTask = getNextTask(tr);
4147 if (nextTask != null) {
4148 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4149 } else {
4150 prevIsHome = true;
4151 }
4152 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004153 mTaskHistory.remove(tr);
4154 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004155 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004156
Craig Mautnerc8143c62013-09-03 12:15:57 -07004157 // There is an assumption that moving a task to the back moves it behind the home activity.
4158 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004159 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004160 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4161 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004162 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004163 break;
4164 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004165 if (taskNdx == 1) {
4166 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004167 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004168 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004169 }
4170
Craig Mautner299f9602015-01-26 09:47:33 -08004171 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004172 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004173
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004174 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004175 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004176 }
4177
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004178 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004179 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004180 if (!mService.mBooting && !mService.mBooted) {
4181 // Not ready yet!
4182 return false;
4183 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004184 final int taskToReturnTo = tr.getTaskToReturnTo();
4185 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004186 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004187 }
4188
Wale Ogunwaled046a012015-12-24 13:05:59 -08004189 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004190 return true;
4191 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004192
Craig Mautner8849a5e2013-04-02 16:41:03 -07004193 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004194 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004195 final Uri data = r.intent.getData();
4196 final String strData = data != null ? data.toSafeString() : null;
4197
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004198 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004199 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004200 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004201 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004202 }
4203
4204 /**
4205 * Make sure the given activity matches the current configuration. Returns
4206 * false if the activity had to be destroyed. Returns true if the
4207 * configuration is the same, or the activity will remain running as-is
4208 * for whatever reason. Ensures the HistoryRecord is updated with the
4209 * correct configuration and all other bookkeeping is handled.
4210 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004211 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4212 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004213 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004214 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004215 "Skipping config check (will change): " + r);
4216 return true;
4217 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004218
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004219 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004220 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004221
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004222 // Short circuit: if the two configurations are the exact same
4223 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004224 final Configuration newConfig = mService.mConfiguration;
4225 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004226 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004227 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004228 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004229 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004230 "Configuration unchanged in " + r);
4231 return true;
4232 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004233
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004234 // We don't worry about activities that are finishing.
4235 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004236 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004237 "Configuration doesn't matter in finishing " + r);
4238 r.stopFreezingScreenLocked(false);
4239 return true;
4240 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004241
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004242 // Okay we now are going to make this activity have the new config.
4243 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004244 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004245 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004246 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004247 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004248
Filip Gruszczynski23493322015-07-29 17:02:59 -07004249 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004250 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004251 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004252 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004253 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004254 // There are no significant differences, so we won't relaunch but should still deliver
4255 // the new configuration to the client process.
4256 r.scheduleConfigurationChanged(taskConfig, false);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004257 return true;
4258 }
4259
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004260 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4261 "Configuration changes for " + r + " ; taskChanges="
4262 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4263 + Configuration.configurationDiffToString(changes));
4264
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004265 // If the activity isn't currently running, just leave the new
4266 // configuration and it will pick that up next time it starts.
4267 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004268 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004269 "Configuration doesn't matter not running " + r);
4270 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004271 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004272 return true;
4273 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004274
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004275 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004276 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4277 "Checking to restart " + r.info.name + ": changed=0x"
4278 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004279 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4280 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004281
Dianne Hackborne6676352011-06-01 16:51:20 -07004282 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004283 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4284 r.configChangeFlags |= changes;
4285 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004286 r.forceNewConfig = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004287 preserveWindow &= isResizeOnlyChange(changes);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004288 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004289 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004290 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004291 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004292 } else if (r.state == ActivityState.PAUSING) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004293 // A little annoying: we are waiting for this activity to finish pausing. Let's not
4294 // do anything now, but just flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004295 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004296 "Config is skipping already pausing " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004297 r.deferRelaunchUntilPaused = true;
4298 r.preserveWindowOnDeferredRelaunch = preserveWindow;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004299 return true;
4300 } else if (r.state == ActivityState.RESUMED) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004301 // Try to optimize this case: the configuration is changing and we need to restart
4302 // the top, resumed activity. Instead of doing the normal handshaking, just say
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004303 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004304 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004305 "Config is relaunching resumed " + r);
Chong Zhang2b79af12016-02-10 18:47:06 -08004306
4307 if (DEBUG_STATES && !r.visible) {
4308 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + r
4309 + " called by " + Debug.getCallers(4));
4310 }
4311
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004312 relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004313 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004314 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004315 "Config is relaunching non-resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004316 relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004317 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004318
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004319 // All done... tell the caller we weren't able to keep this activity around.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004320 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004321 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004322
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004323 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004324 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004325 // changes is always sent to all processes when they happen so it can just use whatever
4326 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004327 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004328 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004329
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004330 return true;
4331 }
4332
Filip Gruszczynski23493322015-07-29 17:02:59 -07004333 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4334 Configuration oldTaskOverride) {
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08004335
4336 // If we went from full-screen to non-full-screen, make sure to use the correct
4337 // configuration task diff, so the diff stays as small as possible.
4338 if (Configuration.EMPTY.equals(oldTaskOverride)
4339 && !Configuration.EMPTY.equals(taskConfig)) {
4340 oldTaskOverride = record.task.extractOverrideConfig(record.configuration);
4341 }
4342
Jorim Jaggi1b025a62016-02-03 19:27:49 -08004343 // Conversely, do the same when going the other direction.
4344 if (Configuration.EMPTY.equals(taskConfig)
4345 && !Configuration.EMPTY.equals(oldTaskOverride)) {
4346 taskConfig = record.task.extractOverrideConfig(record.configuration);
4347 }
4348
Filip Gruszczynski23493322015-07-29 17:02:59 -07004349 // Determine what has changed. May be nothing, if this is a config
4350 // that has come back from the app after going idle. In that case
4351 // we just want to leave the official config object now in the
4352 // activity and do nothing else.
4353 int taskChanges = oldTaskOverride.diff(taskConfig);
4354 // We don't want to use size changes if they don't cross boundaries that are important to
4355 // the app.
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004356 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004357 final boolean crosses = record.crossesHorizontalSizeThreshold(
4358 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4359 || record.crossesVerticalSizeThreshold(
4360 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4361 if (!crosses) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004362 taskChanges &= ~CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004363 }
4364 }
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004365 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004366 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4367 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004368 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004369 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004370 }
4371 }
4372 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4373 }
4374
4375 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4376 Configuration oldTaskOverride, int taskChanges) {
4377 if (taskChanges == 0) {
4378 // {@link Configuration#diff} doesn't catch changes from unset values.
4379 // Check for changes we care about.
4380 if (oldTaskOverride.orientation != taskConfig.orientation) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004381 taskChanges |= CONFIG_ORIENTATION;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004382 }
4383 // We want to explicitly track situations where the size configuration goes from
4384 // undefined to defined. We don't care about crossing the threshold in that case,
4385 // because there is no threshold.
4386 final int oldHeight = oldTaskOverride.screenHeightDp;
4387 final int newHeight = taskConfig.screenHeightDp;
4388 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4389 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4390 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004391 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004392 }
4393 final int oldWidth = oldTaskOverride.screenWidthDp;
4394 final int newWidth = taskConfig.screenWidthDp;
4395 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4396 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4397 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004398 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004399 }
4400 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4401 final int newSmallest = taskConfig.smallestScreenWidthDp;
4402 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4403 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4404 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004405 taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4406 }
4407 final int oldLayout = oldTaskOverride.screenLayout;
4408 final int newLayout = taskConfig.screenLayout;
4409 if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4410 || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4411 taskChanges |= CONFIG_SCREEN_LAYOUT;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004412 }
4413 }
4414 return taskChanges;
4415 }
4416
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004417 private static boolean isResizeOnlyChange(int change) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004418 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4419 | CONFIG_SCREEN_LAYOUT)) == 0;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004420 }
4421
Wale Ogunwale83301a92015-09-24 15:54:08 -07004422 private void relaunchActivityLocked(
4423 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4424 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004425 r.configChangeFlags = 0;
Wale Ogunwale83301a92015-09-24 15:54:08 -07004426 return;
4427 }
4428
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004429 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004430 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004431 if (andResume) {
4432 results = r.results;
4433 newIntents = r.newIntents;
4434 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004435 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4436 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004437 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004438 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004439 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004440 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004441
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004442 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004443
Craig Mautner34b73df2014-01-12 21:11:08 -08004444 mStackSupervisor.removeChildActivityContainers(r);
4445
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004446 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004447 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08004448 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r
4449 + " callers=" + Debug.getCallers(6));
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004450 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004451 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4452 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004453 new Configuration(r.task.mOverrideConfig), preserveWindow);
Jorim Jaggife89d122015-12-22 16:28:44 +01004454 mStackSupervisor.activityRelaunchingLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004455 // Note: don't need to call pauseIfSleepingLocked() here, because
4456 // the caller will only pass in 'andResume' if this activity is
4457 // currently resumed, which implies we aren't sleeping.
4458 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004459 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004460 }
4461
4462 if (andResume) {
Chong Zhang2b79af12016-02-10 18:47:06 -08004463 if (DEBUG_STATES) {
4464 Slog.d(TAG_STATES, "Resumed after relaunch " + r);
4465 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004466 r.state = ActivityState.RESUMED;
Chong Zhang2b79af12016-02-10 18:47:06 -08004467 // Relaunch-resume could happen either when the app is already in the front,
4468 // or while it's being brought to front. In the latter case, it's marked RESUMED
4469 // but not yet visible (or stopped). We need to complete the resume here as the
4470 // code in resumeTopActivityInnerLocked to complete the resume might be skipped.
4471 if (!r.visible || r.stopped) {
4472 mWindowManager.setAppVisibility(r.appToken, true);
4473 completeResumeLocked(r);
4474 } else {
4475 r.results = null;
4476 r.newIntents = null;
4477 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004478 } else {
4479 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4480 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004481 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004482
4483 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004484 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004485
4486 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004487 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4488 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4489 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4490 final ActivityRecord r = activities.get(activityNdx);
4491 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004492 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004493 }
4494 if (r.fullscreen && !r.finishing) {
4495 return false;
4496 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004497 }
4498 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004499 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004500 if (r == null) {
4501 return false;
4502 }
4503 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4504 + " would have returned true for r=" + r);
4505 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004506 }
4507
4508 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004509 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4510 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4511 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4512 final ActivityRecord r = activities.get(activityNdx);
4513 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004514 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004515 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004516 }
4517 }
4518 }
4519
Wale Ogunwale540e1232015-05-01 15:35:39 -07004520 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4521 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004522 boolean didSomething = false;
4523 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004524 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004525 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4526 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4527 int numActivities = activities.size();
4528 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4529 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004530 final boolean sameComponent =
4531 (r.packageName.equals(packageName) && (filterByClasses == null
4532 || filterByClasses.contains(r.realActivity.getClassName())))
4533 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004534 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004535 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004536 && (r.app == null || evenPersistent || !r.app.persistent)) {
4537 if (!doit) {
4538 if (r.finishing) {
4539 // If this activity is just finishing, then it is not
4540 // interesting as far as something to stop.
4541 continue;
4542 }
4543 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004544 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004545 if (r.isHomeActivity()) {
4546 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4547 Slog.i(TAG, "Skip force-stop again " + r);
4548 continue;
4549 } else {
4550 homeActivity = r.realActivity;
4551 }
4552 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004553 didSomething = true;
4554 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004555 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004556 if (r.app != null) {
4557 r.app.removed = true;
4558 }
4559 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004560 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004561 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004562 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4563 true)) {
4564 // r has been deleted from mActivities, accommodate.
4565 --numActivities;
4566 --activityNdx;
4567 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004568 }
4569 }
4570 }
4571 return didSomething;
4572 }
4573
Dianne Hackborn09233282014-04-30 11:33:59 -07004574 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004575 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4576 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004577 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004578 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004579 if (task.getTopActivity() == null) {
4580 continue;
4581 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004582 ActivityRecord r = null;
4583 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004584 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004585 int numActivities = 0;
4586 int numRunning = 0;
4587 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004588 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004589 continue;
4590 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004591 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004592 tmp = activities.get(activityNdx);
4593 if (tmp.finishing) {
4594 continue;
4595 }
4596 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004597
Craig Mautneraab647e2013-02-28 16:31:36 -08004598 // Initialize state for next task if needed.
4599 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4600 top = r;
4601 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004602 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004603
4604 // Add 'r' into the current task.
4605 numActivities++;
4606 if (r.app != null && r.app.thread != null) {
4607 numRunning++;
4608 }
4609
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004610 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004611 TAG, r.intent.getComponent().flattenToShortString()
4612 + ": task=" + r.task);
4613 }
4614
4615 RunningTaskInfo ci = new RunningTaskInfo();
4616 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004617 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004618 ci.baseActivity = r.intent.getComponent();
4619 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004620 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004621 if (focusedStack && topTask) {
4622 // Give the latest time to ensure foreground task can be sorted
4623 // at the first, because lastActiveTime of creating task is 0.
4624 ci.lastActiveTime = System.currentTimeMillis();
4625 topTask = false;
4626 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004627
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004628 if (top.task != null) {
4629 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004630 }
4631 ci.numActivities = numActivities;
4632 ci.numRunning = numRunning;
Wale Ogunwale21b60582016-01-27 12:34:16 -08004633 ci.isDockable = task.canGoInDockedStack();
Craig Mautneraab647e2013-02-28 16:31:36 -08004634 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004635 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004636 }
4637
4638 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004639 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004640 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004641 if (top >= 0) {
4642 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4643 int activityTop = activities.size() - 1;
4644 if (activityTop > 0) {
4645 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4646 "unhandled-back", true);
4647 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004648 }
4649 }
4650
Craig Mautner6b74cb52013-09-27 17:02:21 -07004651 /**
4652 * Reset local parameters because an app's activity died.
4653 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004654 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004655 */
4656 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004657 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004658 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004659 "App died while pausing: " + mPausingActivity);
4660 mPausingActivity = null;
4661 }
4662 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4663 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004664 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004665 }
4666
Craig Mautner19091252013-10-05 00:03:53 -07004667 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004668 }
4669
Craig Mautnercae015f2013-02-08 14:31:27 -08004670 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004671 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4672 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4673 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4674 final ActivityRecord r = activities.get(activityNdx);
4675 if (r.app == app) {
4676 Slog.w(TAG, " Force finishing activity "
4677 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004678 // Force the destroy to skip right to removal.
4679 r.app = null;
4680 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004681 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004682 }
4683 }
4684 }
4685
Dianne Hackborn390517b2013-05-30 15:03:32 -07004686 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004687 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004688 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004689 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4690 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004691 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4692 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004693 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004694 " Task id #" + task.taskId + "\n" +
4695 " mFullscreen=" + task.mFullscreen + "\n" +
4696 " mBounds=" + task.mBounds + "\n" +
4697 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004698 if (printed) {
4699 header = null;
4700 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004701 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004702 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004703 }
4704
4705 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4706 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4707
4708 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004709 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4710 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004711 }
4712 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004713 final int top = mTaskHistory.size() - 1;
4714 if (top >= 0) {
4715 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4716 int listTop = list.size() - 1;
4717 if (listTop >= 0) {
4718 activities.add(list.get(listTop));
4719 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004720 }
4721 } else {
4722 ItemMatcher matcher = new ItemMatcher();
4723 matcher.build(name);
4724
Craig Mautneraab647e2013-02-28 16:31:36 -08004725 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4726 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4727 if (matcher.match(r1, r1.intent.getComponent())) {
4728 activities.add(r1);
4729 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004730 }
4731 }
4732 }
4733
4734 return activities;
4735 }
4736
4737 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004738 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004739
4740 // All activities that came from the package must be
4741 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004742 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4743 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4744 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4745 final ActivityRecord a = activities.get(activityNdx);
4746 if (a.info.packageName.equals(packageName)) {
4747 a.forceNewConfig = true;
4748 if (starting != null && a == starting && a.visible) {
4749 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004750 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08004751 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004752 }
4753 }
4754 }
4755
4756 return starting;
4757 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004758
Craig Mautner299f9602015-01-26 09:47:33 -08004759 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004760 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004761 }
4762
Wale Ogunwale000957c2015-04-03 08:19:12 -07004763 /**
4764 * Removes the input task from this stack.
4765 * @param task to remove.
4766 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004767 * @param moving task to another stack. In the case we are moving we don't want to perform
4768 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004769 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004770 void removeTask(TaskRecord task, String reason, boolean moving) {
4771 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004772 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004773 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004774 if (!StackId.persistTaskBounds(mStackId)) {
4775 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4776 // default configuration the next time it launches.
4777 task.updateOverrideConfiguration(null);
4778 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004779 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004780
Craig Mautner04a0ea62014-01-13 12:51:26 -08004781 final ActivityRecord r = mResumedActivity;
4782 if (r != null && r.task == task) {
4783 mResumedActivity = null;
4784 }
4785
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004786 final int taskNdx = mTaskHistory.indexOf(task);
4787 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004788 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4789 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4790 if (!nextTask.isOverHomeStack()) {
4791 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4792 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004793 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004794 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004795 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004796
Wale Ogunwale040b4702015-08-06 18:10:50 -07004797 if (!moving && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004798 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07004799 final boolean isVoiceSession = task.voiceSession != null;
4800 if (isVoiceSession) {
4801 try {
4802 task.voiceSession.taskFinished(task.intent, task.taskId);
4803 } catch (RemoteException e) {
4804 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004805 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004806 if (task.autoRemoveFromRecents() || isVoiceSession) {
4807 // Task creator asked to remove this when done, or this task was a voice
4808 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004809 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004810 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004811 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004812 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004813
4814 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004815 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004816 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004817 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004818 String myReason = reason + " leftTaskHistoryEmpty";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08004819 if (mFullscreen || !adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004820 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004821 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004822 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004823 if (mStacks != null) {
4824 mStacks.remove(this);
4825 mStacks.add(0, this);
4826 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004827 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004828 mActivityContainer.onTaskListEmptyLocked();
4829 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004830 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004831
4832 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004833 }
4834
Dianne Hackborn91097de2014-04-04 18:02:06 -07004835 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4836 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4837 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004838 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4839 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004840 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08004841 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08004842 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004843 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.isResizeable()
Chong Zhang75b37202015-12-04 14:16:36 -08004844 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004845 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004846 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004847 return task;
4848 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004849
Wale Ogunwale935e5022015-11-10 12:36:10 -08004850 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4851 if (mTaskPositioner == null) {
4852 return false;
4853 }
4854 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4855 return true;
4856 }
4857
Craig Mautnerc00204b2013-03-05 15:02:14 -08004858 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004859 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004860 }
4861
Wale Ogunwale5f986092015-12-04 15:35:38 -08004862 void addTask(final TaskRecord task, final boolean toTop, String reason) {
4863 final ActivityStack prevStack = preAddTask(task, reason);
4864
Craig Mautnerc00204b2013-03-05 15:02:14 -08004865 task.stack = this;
4866 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004867 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004868 } else {
4869 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004870 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004871 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08004872 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004873 }
4874
Wale Ogunwale5f986092015-12-04 15:35:38 -08004875 void positionTask(final TaskRecord task, int position) {
4876 final ActivityStack prevStack = preAddTask(task, "positionTask");
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004877 task.stack = this;
4878 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004879 postAddTask(task, prevStack);
4880 }
4881
4882 private ActivityStack preAddTask(TaskRecord task, String reason) {
4883 final ActivityStack prevStack = task.stack;
4884 if (prevStack != null && prevStack != this) {
4885 prevStack.removeTask(task, reason, MOVING);
4886 }
4887 return prevStack;
4888 }
4889
4890 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
4891 if (prevStack != null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004892 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004893 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004894 try {
4895 task.voiceSession.taskStarted(task.intent, task.taskId);
4896 } catch (RemoteException e) {
4897 }
4898 }
4899 }
4900
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004901 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004902 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004903 // TODO: VI deal with activity
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004904 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4905 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6cae7652015-12-26 07:36:26 -08004906 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
4907 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004908 task.mResizeMode, r.isAlwaysFocusable(), task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004909 r.taskConfigOverride = task.mOverrideConfig;
4910 }
4911
Wale Ogunwaled046a012015-12-24 13:05:59 -08004912 void moveToFrontAndResumeStateIfNeeded(
4913 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
4914 if (!moveToFront) {
4915 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07004916 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004917
4918 // If the activity owns the last resumed activity, transfer that together,
4919 // so that we don't resume the same activity again in the new stack.
4920 // Apps may depend on onResume()/onPause() being called in pairs.
4921 if (setResume) {
4922 mResumedActivity = r;
4923 }
4924 // Move the stack in which we are placing the activity to the front. The call will also
4925 // make sure the activity focus is set.
4926 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004927 }
4928
4929 /**
4930 * Moves the input activity from its current stack to this one.
4931 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4932 * created on this stack which the activity is added to.
4933 * */
4934 void moveActivityToStack(ActivityRecord r) {
4935 final ActivityStack prevStack = r.task.stack;
4936 if (prevStack.mStackId == mStackId) {
4937 // You are already in the right stack silly...
4938 return;
4939 }
4940
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004941 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004942 && (mStackSupervisor.topRunningActivityLocked() == r);
4943 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4944
4945 final TaskRecord task = createTaskRecord(
Suprabh Shukla09a88f52015-12-02 14:36:31 -08004946 mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
4947 r.info, r.intent, null, null, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004948 r.setTask(task, null);
4949 task.addActivityToTop(r);
4950 setAppTask(r, task);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004951 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004952 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4953 if (wasResumed) {
4954 prevStack.mResumedActivity = null;
4955 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07004956 }
4957
Wale Ogunwale706ed792015-08-02 10:29:44 -07004958 private void setAppTask(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004959 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004960 mWindowManager.setAppTask(r.appToken, task.taskId, mStackId, bounds, task.mOverrideConfig,
4961 task.mResizeMode, task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004962 r.taskConfigOverride = task.mOverrideConfig;
4963 }
4964
Craig Mautnerc00204b2013-03-05 15:02:14 -08004965 public int getStackId() {
4966 return mStackId;
4967 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004968
4969 @Override
4970 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004971 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4972 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004973 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004974
Craig Mautner15df08a2015-04-01 12:17:18 -07004975 void onLockTaskPackagesUpdatedLocked() {
4976 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4977 mTaskHistory.get(taskNdx).setLockTaskAuth();
4978 }
4979 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004980}