blob: e50722ac04d84c99eb3b0f481b7947725095820c [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);
417 }
418
Winsonc809cbb2015-11-02 12:06:15 -0800419 public void getDisplaySize(Point out) {
420 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
421 }
422
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700423 void setBounds(Rect bounds) {
424 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700425 if (mTaskPositioner != null) {
426 mTaskPositioner.configure(bounds);
427 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700428 }
Craig Mautner5962b122012-10-05 14:45:52 -0700429
Amith Yamasani734983f2014-03-04 16:48:05 -0800430 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700431 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700432 }
433
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700434 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800435 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700436 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700437 if (r != null) {
438 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800439 }
440 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700441 return null;
442 }
443
444 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800445 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
446 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800447 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800448 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
449 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800450 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800451 return r;
452 }
453 }
454 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700455 return null;
456 }
457
458 /**
459 * This is a simplified version of topRunningActivityLocked that provides a number of
460 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800461 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700462 * @param token If non-null, any history records matching this token will be skipped.
463 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800464 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700465 * @return Returns the HistoryRecord of the next activity on the stack.
466 */
467 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800468 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
469 TaskRecord task = mTaskHistory.get(taskNdx);
470 if (task.taskId == taskId) {
471 continue;
472 }
473 ArrayList<ActivityRecord> activities = task.mActivities;
474 for (int i = activities.size() - 1; i >= 0; --i) {
475 final ActivityRecord r = activities.get(i);
476 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800477 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800478 return r;
479 }
480 }
481 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700482 return null;
483 }
484
Craig Mautner8849a5e2013-04-02 16:41:03 -0700485 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700486 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
487 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700488 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
489 final ActivityRecord r = activities.get(activityNdx);
490 if (!r.finishing) {
491 return r;
492 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700493 }
494 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700495 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700496 }
497
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700498 final TaskRecord topTask() {
499 final int size = mTaskHistory.size();
500 if (size > 0) {
501 return mTaskHistory.get(size - 1);
502 }
503 return null;
504 }
505
Craig Mautnerd2328952013-03-05 12:46:26 -0800506 TaskRecord taskForIdLocked(int id) {
507 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
508 final TaskRecord task = mTaskHistory.get(taskNdx);
509 if (task.taskId == id) {
510 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800511 }
512 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700513 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700514 }
515
Craig Mautnerd2328952013-03-05 12:46:26 -0800516 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700517 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800518 return isInStackLocked(r);
519 }
520
521 ActivityRecord isInStackLocked(ActivityRecord r) {
522 if (r == null) {
523 return null;
524 }
525 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700526 if (task != null && task.stack != null
527 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800528 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800529 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800530 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800531 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800532 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800533 }
534
Craig Mautner2420ead2013-04-01 17:13:20 -0700535 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700536 final boolean hadit = mLRUActivities.remove(r);
537 mLRUActivities.add(r);
538 return hadit;
539 }
540
Craig Mautnerde4ef022013-04-07 19:01:33 -0700541 final boolean isHomeStack() {
542 return mStackId == HOME_STACK_ID;
543 }
544
Winson Chung0583d3d2015-12-18 10:03:12 -0500545 final boolean isDockedStack() {
546 return mStackId == DOCKED_STACK_ID;
547 }
548
549 final boolean isPinnedStack() {
550 return mStackId == PINNED_STACK_ID;
551 }
552
Craig Mautnere0a38842013-12-16 16:14:02 -0800553 final boolean isOnHomeDisplay() {
554 return isAttached() &&
555 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
556 }
557
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700558 void moveToFront(String reason) {
559 moveToFront(reason, null);
560 }
561
562 /**
563 * @param reason The reason for moving the stack to the front.
564 * @param task If non-null, the task will be moved to the top of the stack.
565 * */
566 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700567 if (!isAttached()) {
568 return;
569 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700570
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700571 mStacks.remove(this);
572 int addIndex = mStacks.size();
573
574 if (addIndex > 0) {
575 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800576 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
577 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700578 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800579 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700580 }
581
582 mStacks.add(addIndex, this);
583
584 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
585 if (isOnHomeDisplay()) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800586 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700587 }
588 if (task != null) {
589 insertTaskAtTop(task, null);
590 } else {
591 task = topTask();
592 }
593 if (task != null) {
594 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800595 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800596 }
597
Wale Ogunwaled046a012015-12-24 13:05:59 -0800598 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800599 if (StackId.canReceiveKeys(mStackId)) {
600 return true;
601 }
602 // The stack isn't focusable. See if its top activity is focusable to force focus on the
603 // stack.
604 final ActivityRecord r = topRunningActivityLocked();
605 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800606 }
607
Craig Mautnere0a38842013-12-16 16:14:02 -0800608 final boolean isAttached() {
609 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800610 }
611
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700612 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800613 * Returns the top activity in any existing task matching the given Intent in the input result.
614 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700615 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800616 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700617 Intent intent = target.intent;
618 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700619 ComponentName cls = intent.getComponent();
620 if (info.targetActivity != null) {
621 cls = new ComponentName(info.packageName, info.targetActivity);
622 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700623 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700624 boolean isDocument = intent != null & intent.isDocument();
625 // If documentData is non-null then it must match the existing task data.
626 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800627
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700628 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800629 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
630 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700631 if (task.voiceSession != null) {
632 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700633 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700634 continue;
635 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700636 if (task.userId != userId) {
637 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700638 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700639 continue;
640 }
Craig Mautner000f0022013-02-26 15:04:29 -0800641 final ActivityRecord r = task.getTopActivity();
642 if (r == null || r.finishing || r.userId != userId ||
643 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700644 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800645 continue;
646 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700647 if (r.mActivityType != target.mActivityType) {
648 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
649 continue;
650 }
Craig Mautner000f0022013-02-26 15:04:29 -0800651
Craig Mautnerd00f4742014-03-12 14:17:26 -0700652 final Intent taskIntent = task.intent;
653 final Intent affinityIntent = task.affinityIntent;
654 final boolean taskIsDocument;
655 final Uri taskDocumentData;
656 if (taskIntent != null && taskIntent.isDocument()) {
657 taskIsDocument = true;
658 taskDocumentData = taskIntent.getData();
659 } else if (affinityIntent != null && affinityIntent.isDocument()) {
660 taskIsDocument = true;
661 taskDocumentData = affinityIntent.getData();
662 } else {
663 taskIsDocument = false;
664 taskDocumentData = null;
665 }
666
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700667 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700668 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700669 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700670 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Wale Ogunwale39381972015-12-17 17:15:29 -0800671 if (!isDocument && !taskIsDocument
672 && result.r == null && task.canMatchRootAffinity()) {
Dianne Hackborn79228822014-09-16 11:11:23 -0700673 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwale39381972015-12-17 17:15:29 -0800674 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
675 // It is possible for multiple tasks to have the same root affinity especially
676 // if they are in separate stacks. We save off this candidate, but keep looking
677 // to see if there is a better candidate.
678 result.r = r;
679 result.matchedByRootAffinity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700680 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700681 } else if (taskIntent != null && taskIntent.getComponent() != null &&
682 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700683 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700684 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800685 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700686 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
687 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800688 result.r = r;
689 result.matchedByRootAffinity = false;
690 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700691 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
692 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700693 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700694 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800695 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700696 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
697 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800698 result.r = r;
699 result.matchedByRootAffinity = false;
700 break;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700701 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700702 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700703 }
704
705 /**
706 * Returns the first activity (starting from the top of the stack) that
707 * is the same as the given activity. Returns null if no such activity
708 * is found.
709 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700710 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700711 ComponentName cls = intent.getComponent();
712 if (info.targetActivity != null) {
713 cls = new ComponentName(info.packageName, info.targetActivity);
714 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700715 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700716
Craig Mautner000f0022013-02-26 15:04:29 -0800717 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700718 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700719 final boolean notCurrentUserTask =
720 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700721 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700722
Craig Mautner000f0022013-02-26 15:04:29 -0800723 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
724 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700725 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700726 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700727 }
Craig Mautner000f0022013-02-26 15:04:29 -0800728 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700729 return r;
730 }
731 }
732 }
733
734 return null;
735 }
736
Amith Yamasani742a6712011-05-04 14:49:28 -0700737 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700738 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700739 */
Craig Mautner93529a42013-10-04 15:03:13 -0700740 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800741 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700742 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800743 }
744 mCurrentUser = userId;
745
746 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800747 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800748 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700749 final TaskRecord task = mTaskHistory.get(i);
750
751 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
752 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700753 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700754 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700755 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700756 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800757 mTaskHistory.remove(i);
758 mTaskHistory.add(task);
759 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800760 // Use same value for i.
761 } else {
762 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800763 }
764 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700765 if (VALIDATE_TOKENS) {
766 validateAppTokensLocked();
767 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700768 }
769
Craig Mautner2420ead2013-04-01 17:13:20 -0700770 void minimalResumeActivityLocked(ActivityRecord r) {
771 r.state = ActivityState.RESUMED;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800772 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
773 + " callers=" + Debug.getCallers(5));
Craig Mautner2420ead2013-04-01 17:13:20 -0700774 mResumedActivity = r;
775 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800776 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700777 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700778 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700779 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700780 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
781 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700782 }
783
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800784 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700785 if (r != null) {
786 mRecentTasks.addLocked(r.task);
787 r.task.touchActiveTime();
788 }
789 }
790
Narayan Kamath7829c812015-06-08 17:39:43 +0100791 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700792 if (mFullyDrawnStartTime != 0) {
793 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
794 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100795 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700796 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
797 }
798
799 private void stopFullyDrawnTraceIfNeeded() {
800 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
801 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
802 mFullyDrawnStartTime = 0;
803 }
804 }
805
Craig Mautnere79d42682013-04-01 19:01:53 -0700806 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700807 if (r.displayStartTime == 0) {
808 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
809 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100810 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700811 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700812 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700813 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100814 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700815 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700816 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700817 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800818
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700819 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700820 // Make sure that there is no activity waiting for this to launch.
821 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
822 r.displayStartTime = r.fullyDrawnStartTime = 0;
823 } else {
824 mStackSupervisor.removeTimeoutsForActivityLocked(r);
825 mStackSupervisor.scheduleIdleTimeoutLocked(r);
826 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700827 }
828
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800829 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800830 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800831 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
832 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
833 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
834 activities.get(activityNdx).setSleeping(false);
835 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800836 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800837 if (mPausingActivity != null) {
838 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
839 activityPausedLocked(mPausingActivity.appToken, true);
840 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800841 }
842
Todd Kennedy39bfee52016-02-24 10:28:21 -0800843 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
844 final String packageName = aInfo.packageName;
845 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
846 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
847 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
848 if (packageName.equals(activities.get(activityNdx).packageName)) {
849 activities.get(activityNdx).info.applicationInfo = aInfo;
850 }
851 }
852 }
853 }
854
Craig Mautner0eea92c2013-05-16 13:35:39 -0700855 /**
856 * @return true if something must be done before going to sleep.
857 */
858 boolean checkReadyForSleepLocked() {
859 if (mResumedActivity != null) {
860 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700861 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
862 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
863 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700864 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700865 return true;
866 }
867 if (mPausingActivity != null) {
868 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700869 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700870 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800871 }
872
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700873 if (hasVisibleBehindActivity()) {
874 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800875 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700876 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800877 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700878 return true;
879 }
880
Craig Mautner0eea92c2013-05-16 13:35:39 -0700881 return false;
882 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800883
Craig Mautner0eea92c2013-05-16 13:35:39 -0700884 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700885 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800886
Craig Mautner0eea92c2013-05-16 13:35:39 -0700887 // Make sure any stopped but visible activities are now sleeping.
888 // This ensures that the activity's onStop() is called.
889 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
890 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
891 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
892 final ActivityRecord r = activities.get(activityNdx);
893 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
894 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800895 }
896 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800897 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700898 }
Craig Mautner59c00972012-07-30 12:10:24 -0700899
Winson8b1871d2015-11-20 09:56:20 -0800900 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
901 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800902 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700903 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800904 return null;
905 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800906
Winson Chung083baf92014-07-11 10:32:42 -0700907 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700908 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800909 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700910 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700911 return null;
912 }
913
Winson Chung48a10a52014-08-27 14:36:51 -0700914 int w = mService.mThumbnailWidth;
915 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800916 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800917 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700918 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800919
920 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800921 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
922 // use within SystemUI while keeping memory usage low.
Winson Chungead5c0f2015-12-14 11:18:57 -0500923 if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
Winson8b1871d2015-11-20 09:56:20 -0800924 w = h = -1;
Winson387aac62015-11-25 11:18:56 -0800925 scale = 0.5f;
Winson8b1871d2015-11-20 09:56:20 -0800926 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800927 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800928 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800929 }
Winson Chung376543b2014-05-21 17:43:28 -0700930 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800931 return null;
932 }
933
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700934 /**
935 * Start pausing the currently resumed activity. It is an error to call this if there
936 * is already an activity being paused or there is no resumed activity.
937 *
938 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
939 * @param uiSleeping True if this is happening with the user interface going to sleep (the
940 * screen turning off).
941 * @param resuming True if this is being called as part of resuming the top activity, so
942 * we shouldn't try to instigate a resume here.
943 * @param dontWait True if the caller does not want to wait for the pause to complete. If
944 * set to true, we will immediately complete the pause here before returning.
945 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
946 * it to tell us when it is done.
947 */
948 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
949 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800950 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800951 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
952 + " state=" + mPausingActivity.state);
953 if (!mService.isSleeping()) {
954 // Avoid recursion among check for sleep and complete pause during sleeping.
955 // Because activity will be paused immediately after resume, just let pause
956 // be completed by the order of activity paused from clients.
957 completePauseLocked(false);
958 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800959 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700960 ActivityRecord prev = mResumedActivity;
961 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700962 if (!resuming) {
963 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -0800964 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700965 }
966 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700967 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800968
969 if (mActivityContainer.mParentActivity == null) {
970 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700971 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800972 }
973
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700974 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700975 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700976 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800977 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700978 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700979 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
980 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700981 prev.state = ActivityState.PAUSING;
982 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700983 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700984 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700985 if (mService.mHasRecents
986 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800987 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700988 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700989 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700990
991 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800992
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700993 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700994 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700995 try {
996 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700997 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700998 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700999 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001000 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001001 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001002 } catch (Exception e) {
1003 // Ignore exception, if process died other code will cleanup.
1004 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001005 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001006 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001007 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001008 }
1009 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001010 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001011 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001012 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001013 }
1014
1015 // If we are not going to sleep, we want to ensure the device is
1016 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001017 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001018 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001019 }
1020
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001021 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001022 // Have the window manager pause its key dispatching until the new
1023 // activity has started. If we're pausing the activity just because
1024 // the screen is being turned off and the UI is sleeping, don't interrupt
1025 // key dispatch; the same activity will pick it up again on wakeup.
1026 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001027 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001028 } else if (DEBUG_PAUSE) {
1029 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001030 }
1031
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001032 if (dontWait) {
1033 // If the caller said they don't want to wait for the pause, then complete
1034 // the pause now.
1035 completePauseLocked(false);
1036 return false;
1037
1038 } else {
1039 // Schedule a pause timeout in case the app doesn't respond.
1040 // We don't give it much time because this directly impacts the
1041 // responsiveness seen by the user.
1042 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1043 msg.obj = prev;
1044 prev.pauseTime = SystemClock.uptimeMillis();
1045 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001046 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001047 return true;
1048 }
1049
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001050 } else {
1051 // This activity failed to schedule the
1052 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001053 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001054 if (!resuming) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001055 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001056 }
1057 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001058 }
1059 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001060
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001061 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001062 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1063 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001064
Craig Mautnerd2328952013-03-05 12:46:26 -08001065 final ActivityRecord r = isInStackLocked(token);
1066 if (r != null) {
1067 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1068 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001069 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001070 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001071 completePauseLocked(true);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001072 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001073 } else {
1074 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1075 r.userId, System.identityHashCode(r), r.shortComponentName,
1076 mPausingActivity != null
1077 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001078 if (r.state == ActivityState.PAUSING) {
1079 r.state = ActivityState.PAUSED;
1080 if (r.finishing) {
1081 if (DEBUG_PAUSE) Slog.v(TAG,
1082 "Executing finish of failed to pause activity: " + r);
1083 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1084 }
louis_chang047dfd42015-04-08 16:35:55 +08001085 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001086 }
1087 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001088 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001089 }
1090
Craig Mautnera0026042014-04-23 11:45:37 -07001091 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1092 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001093 if (r.state != ActivityState.STOPPING) {
1094 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1095 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1096 return;
1097 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001098 if (persistentState != null) {
1099 r.persistentState = persistentState;
1100 mService.notifyTaskPersisterLocked(r.task, false);
1101 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001102 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001103 if (icicle != null) {
1104 // If icicle is null, this is happening due to a timeout, so we
1105 // haven't really saved the state.
1106 r.icicle = icicle;
1107 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001108 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001109 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001110 }
1111 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001112 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001113 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1114 r.stopped = true;
1115 r.state = ActivityState.STOPPED;
Robert Carre12aece2016-02-02 22:43:27 -08001116
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08001117 mWindowManager.notifyAppStopped(r.appToken, true);
Robert Carre12aece2016-02-02 22:43:27 -08001118
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001119 if (getVisibleBehindActivity() == r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001120 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001121 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001122 if (r.finishing) {
1123 r.clearOptionsLocked();
1124 } else {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001125 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001126 destroyActivityLocked(r, true, "stop-config");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001127 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001128 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001129 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001130 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001131 }
1132 }
1133 }
1134
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001135 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001136 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001137 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001138
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001139 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001140 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001141 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001142 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001143 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001144 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001145 } else if (prev.app != null) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001146 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1147 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Craig Mautner8c14c152015-01-15 17:32:07 -08001148 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001149 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1150 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001151 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001152 if (prev.deferRelaunchUntilPaused) {
1153 // Complete the deferred relaunch that was waiting for pause to complete.
1154 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
1155 relaunchActivityLocked(prev, prev.configChangeFlags, false,
1156 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001157 } else if (wasStopping) {
1158 // We are also stopping, the stop request must have gone soon after the pause.
1159 // We can't clobber it, because the stop confirmation will not be handled.
1160 // We don't need to schedule another stop, we only need to let it happen.
1161 prev.state = ActivityState.STOPPING;
Wale Ogunwaled8026642016-02-09 20:40:18 -08001162 } else if ((!prev.visible && !hasVisibleBehindActivity())
1163 || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001164 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001165 // stopping.
Chong Zhang46b1ac62016-02-18 17:53:57 -08001166 addToStopping(prev, true /* immediate */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001167 }
1168 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001169 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001170 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001171 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001172 // It is possible the activity was freezing the screen before it was paused.
1173 // In that case go ahead and remove the freeze this activity has on the screen
1174 // since it is no longer visible.
1175 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001176 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001177 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001178
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001179 if (resumeNext) {
1180 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1181 if (!mService.isSleepingOrShuttingDown()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001182 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001183 } else {
1184 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001185 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001186 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001187 // If there are no more activities available to run, do resume anyway to start
1188 // something. Also if the top activity on the stack is not the just paused
1189 // activity, we need to go ahead and resume it to ensure we complete an
1190 // in-flight app switch.
1191 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001192 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001193 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001194 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001195
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001196 if (prev != null) {
1197 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001198
Craig Mautner525f3d92013-05-07 14:01:50 -07001199 if (prev.app != null && prev.cpuTimeAtResume > 0
1200 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001201 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1202 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001203 if (diff > 0) {
1204 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1205 synchronized (bsi) {
1206 BatteryStatsImpl.Uid.Proc ps =
1207 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001208 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001209 if (ps != null) {
1210 ps.addForegroundTimeLocked(diff);
1211 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001212 }
1213 }
1214 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001215 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001216 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001217
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001218 // Notify when the task stack has changed, but only if visibilities changed (not just focus)
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001219 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause) {
1220 mService.notifyTaskStackChangedLocked();
1221 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1222 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001223
1224 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001225 }
1226
Chong Zhang46b1ac62016-02-18 17:53:57 -08001227 private void addToStopping(ActivityRecord r, boolean immediate) {
1228 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1229 mStackSupervisor.mStoppingActivities.add(r);
1230 }
1231
1232 // If we already have a few activities waiting to stop, then give up
1233 // on things going idle and start clearing them out. Or if r is the
1234 // last of activity of the last task the stack will be empty and must
1235 // be cleared immediately.
1236 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1237 || (r.frontOfTask && mTaskHistory.size() <= 1);
1238
1239 if (immediate || forceIdle) {
1240 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
1241 + forceIdle + "immediate=" + immediate);
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001242 mStackSupervisor.scheduleIdleLocked();
1243 } else {
1244 mStackSupervisor.checkReadyForSleepLocked();
1245 }
1246 }
1247
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001248 /**
1249 * Once we know that we have asked an application to put an activity in
1250 * the resumed state (either by launching it or explicitly telling it),
1251 * this function updates the rest of our state to match that fact.
1252 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001253 private void completeResumeLocked(ActivityRecord next) {
Chong Zhang2b79af12016-02-10 18:47:06 -08001254 next.visible = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001255 next.idle = false;
1256 next.results = null;
1257 next.newIntents = null;
Chong Zhang2b79af12016-02-10 18:47:06 -08001258 next.stopped = false;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001259
Chong Zhang85ee6542015-10-02 13:36:38 -07001260 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001261 ProcessRecord app = next.task.mActivities.get(0).app;
1262 if (app != null && app != mService.mHomeProcess) {
1263 mService.mHomeProcess = app;
1264 }
1265 }
1266
Craig Mautner07566322013-09-26 16:42:55 -07001267 if (next.nowVisible) {
1268 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001269 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001270 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001271
1272 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001273 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001274
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001275 mStackSupervisor.reportResumedActivityLocked(next);
1276
1277 next.resumeKeyDispatchingLocked();
1278 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001279
1280 // Mark the point when the activity is resuming
1281 // TODO: To be more accurate, the mark should be before the onCreate,
1282 // not after the onResume. But for subsequent starts, onResume is fine.
1283 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001284 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001285 } else {
1286 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1287 }
Winson Chung376543b2014-05-21 17:43:28 -07001288
George Mount6ba042b2014-07-28 11:12:28 -07001289 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001290
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001291 if (getVisibleBehindActivity() == next) {
Craig Mautner64ccb702014-10-01 09:38:40 -07001292 // When resuming an activity, require it to call requestVisibleBehind() again.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001293 setVisibleBehindActivity(null);
Craig Mautner64ccb702014-10-01 09:38:40 -07001294 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001295 }
1296
Craig Mautner2568c3a2015-03-26 14:22:34 -07001297 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001298 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001299 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1300 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1301 r.mUpdateTaskThumbnailWhenHidden = false;
1302 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001303 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001304 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001305 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001306 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001307 container.setVisible(visible);
1308 }
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001309 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001310 }
1311
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001312 // Find the first visible activity above the passed activity and if it is translucent return it
1313 // otherwise return null;
1314 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1315 TaskRecord task = r.task;
1316 if (task == null) {
1317 return null;
1318 }
1319
1320 ActivityStack stack = task.stack;
1321 if (stack == null) {
1322 return null;
1323 }
1324
1325 int stackNdx = mStacks.indexOf(stack);
1326
1327 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1328 int taskNdx = tasks.indexOf(task);
1329
1330 ArrayList<ActivityRecord> activities = task.mActivities;
1331 int activityNdx = activities.indexOf(r) + 1;
1332
1333 final int numStacks = mStacks.size();
1334 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001335 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001336 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001337 final int numTasks = tasks.size();
1338 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001339 final TaskRecord currentTask = tasks.get(taskNdx);
1340 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001341 final int numActivities = activities.size();
1342 while (activityNdx < numActivities) {
1343 final ActivityRecord activity = activities.get(activityNdx);
1344 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001345 return historyStack.mFullscreen
1346 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001347 }
1348 ++activityNdx;
1349 }
1350 activityNdx = 0;
1351 ++taskNdx;
1352 }
1353 taskNdx = 0;
1354 ++stackNdx;
1355 }
1356
1357 return null;
1358 }
1359
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001360 ActivityStack getNextFocusableStackLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001361 ArrayList<ActivityStack> stacks = mStacks;
1362 final ActivityRecord parent = mActivityContainer.mParentActivity;
1363 if (parent != null) {
1364 stacks = parent.task.stack.mStacks;
1365 }
1366 if (stacks != null) {
1367 for (int i = stacks.size() - 1; i >= 0; --i) {
1368 ActivityStack stack = stacks.get(i);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001369 if (stack != this && stack.isFocusable()
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001370 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001371 return stack;
1372 }
1373 }
1374 }
1375 return null;
1376 }
1377
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001378 /** Returns true if the stack contains a fullscreen task. */
1379 private boolean hasFullscreenTask() {
1380 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1381 final TaskRecord task = mTaskHistory.get(i);
1382 if (task.mFullscreen) {
1383 return true;
1384 }
1385 }
1386 return false;
1387 }
1388
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001389 /**
1390 * Returns true if the stack is translucent and can have other contents visible behind it if
1391 * needed. A stack is considered translucent if it don't contain a visible or
1392 * starting (about to be visible) activity that is fullscreen (opaque).
1393 * @param starting The currently starting activity or null if there is none.
1394 */
1395 private boolean isStackTranslucent(ActivityRecord starting) {
1396 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1397 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001398 final ArrayList<ActivityRecord> activities = task.mActivities;
1399 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1400 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001401
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001402 // Conditions for an activity to obscure the stack we're
1403 // examining:
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001404 // 1. Not Finishing AND (Visible or the Starting activity) AND:
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001405 // 2. Either:
1406 // - Full Screen Activity OR
1407 // - On top of Home and our stack is NOT home
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001408 if (!r.finishing && (r.visible || r == starting) && (r.fullscreen ||
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001409 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1410 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001411 }
1412 }
1413 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001414 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001415 }
1416
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001417 /**
1418 * Returns stack's visibility: {@link #STACK_INVISIBLE}, {@link #STACK_VISIBLE} or
1419 * {@link #STACK_VISIBLE_ACTIVITY_BEHIND}.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001420 * @param starting The currently starting activity or null if there is none.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001421 */
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001422 int getStackVisibilityLocked(ActivityRecord starting) {
Jose Lima7ba71252014-04-30 12:59:27 -07001423 if (!isAttached()) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001424 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001425 }
1426
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001427 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001428 return STACK_VISIBLE;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001429 }
1430
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001431 final int stackIndex = mStacks.indexOf(this);
1432
1433 if (stackIndex == mStacks.size() - 1) {
1434 Slog.wtf(TAG,
1435 "Stack=" + this + " isn't front stack but is at the top of the stack list");
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001436 return STACK_INVISIBLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001437 }
1438
Chong Zhang75b37202015-12-04 14:16:36 -08001439 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1440 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001441 return STACK_INVISIBLE;
Chong Zhang75b37202015-12-04 14:16:36 -08001442 }
1443
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001444 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1445 final int focusedStackId = focusedStack.mStackId;
1446
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001447 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001448 && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID
1449 && !focusedStack.topActivity().fullscreen) {
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001450 // The fullscreen stack should be visible if it has a visible behind activity behind
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001451 // the home stack that is translucent.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001452 return STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001453 }
1454
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001455 if (mStackId == DOCKED_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001456 // Docked stack is always visible, except in the case where the top running activity
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001457 // task in the focus stack doesn't support any form of resizing but we show it for the
1458 // home task even though it's not resizable.
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001459 final ActivityRecord r = focusedStack.topRunningActivityLocked();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001460 final TaskRecord task = r != null ? r.task : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001461 return task == null || task.canGoInDockedStack() || task.isHomeTask() ? STACK_VISIBLE
1462 : STACK_INVISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001463 }
1464
Chong Zhangb16cf342015-11-12 17:22:40 -08001465 // Find the first stack below focused stack that actually got something visible.
1466 int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1467 while (belowFocusedIndex >= 0 &&
1468 mStacks.get(belowFocusedIndex).topRunningActivityLocked() == null) {
1469 belowFocusedIndex--;
1470 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001471 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1472 && stackIndex == belowFocusedIndex) {
1473 // Stacks directly behind the docked or pinned stack are always visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001474 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001475 }
1476
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001477 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001478 && focusedStack.isStackTranslucent(starting)) {
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001479 // Stacks behind the fullscreen stack with a translucent activity are always
1480 // visible so they can act as a backdrop to the translucent activity.
1481 // For example, dialog activities
1482 if (stackIndex == belowFocusedIndex) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001483 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001484 }
1485 if (belowFocusedIndex >= 0) {
1486 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001487 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1488 && stackIndex == (belowFocusedIndex - 1)) {
1489 // The stack behind the docked or pinned stack is also visible so we can have a
1490 // complete backdrop to the translucent activity when the docked stack is up.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001491 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001492 }
1493 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001494 }
1495
Wale Ogunwale3797c222015-10-27 14:21:58 -07001496 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001497 // Visibility of any static stack should have been determined by the conditions above.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001498 return STACK_INVISIBLE;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001499 }
1500
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001501 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001502 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001503
1504 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1505 continue;
1506 }
1507
Wale Ogunwale3797c222015-10-27 14:21:58 -07001508 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001509 // These stacks can't have any dynamic stacks visible behind them.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001510 return STACK_INVISIBLE;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001511 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001512
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001513 if (!stack.isStackTranslucent(starting)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001514 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001515 }
1516 }
1517
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001518 return STACK_VISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001519 }
1520
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001521 final int rankTaskLayers(int baseLayer) {
1522 int layer = 0;
1523 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1524 final TaskRecord task = mTaskHistory.get(taskNdx);
1525 ActivityRecord r = task.topRunningActivityLocked();
1526 if (r == null || r.finishing || !r.visible) {
1527 task.mLayerRank = -1;
1528 } else {
1529 task.mLayerRank = baseLayer + layer++;
1530 }
1531 }
1532 return layer;
1533 }
1534
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001535 /**
1536 * Make sure that all activities that need to be visible (that is, they
1537 * currently can be seen by the user) actually are.
1538 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001539 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1540 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001541 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001542 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001543 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001544 if (top != null) {
1545 checkTranslucentActivityWaiting(top);
1546 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001547
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001548 // If the top activity is not fullscreen, then we need to
1549 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001550 boolean aboveTop = top != null;
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001551 final int stackVisibility = getStackVisibilityLocked(starting);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001552 final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
1553 final boolean stackVisibleBehind = stackVisibility == STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001554 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaled046a012015-12-24 13:05:59 -08001555 boolean resumeNextActivity = isFocusable() && (isInStackLocked(starting) == null);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001556 boolean behindTranslucentActivity = false;
1557 final ActivityRecord visibleBehind = getVisibleBehindActivity();
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001558 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001559 final TaskRecord task = mTaskHistory.get(taskNdx);
1560 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001561 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1562 final ActivityRecord r = activities.get(activityNdx);
1563 if (r.finishing) {
1564 continue;
1565 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001566 final boolean isTop = r == top;
1567 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001568 continue;
1569 }
1570 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001571 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1572 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001573 final boolean activityVisibleBehind =
1574 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == r;
1575 final boolean isVisible = (!behindFullscreenActivity || r.mLaunchTaskBehind
1576 || activityVisibleBehind) && okToShowLocked(r);
1577 if (isVisible) {
1578 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1579 + " finishing=" + r.finishing + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001580 // First: if this is not the current activity being started, make
1581 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001582 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001583 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001584 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001585
1586 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001587 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001588 resumeNextActivity, r)) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001589 if (activityNdx >= activities.size()) {
1590 // Record may be removed if its process needs to restart.
1591 activityNdx = activities.size() - 1;
1592 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001593 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001594 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001595 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001596 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001597 // If this activity is already visible, then there is nothing to do here.
1598 if (handleAlreadyVisible(r)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001599 resumeNextActivity = false;
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001600 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001601 } else {
Jorim Jaggie66edb12016-02-05 12:41:17 -08001602 makeVisibleIfNeeded(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001603 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001604 // Aggregate current change flags.
1605 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001606 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1607 behindFullscreenActivity, task, r);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001608 if (behindFullscreenActivity && !r.fullscreen) {
1609 behindTranslucentActivity = true;
1610 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001611 } else {
Wale Ogunwale834c2362016-01-23 18:14:58 -08001612 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001613 + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1614 + stackInvisible + " behindFullscreenActivity="
1615 + behindFullscreenActivity + " mLaunchTaskBehind="
1616 + r.mLaunchTaskBehind);
1617 makeInvisible(r, visibleBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001618 }
1619 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001620 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1621 // The visibility of tasks and the activities they contain in freeform stack are
1622 // determined individually unlike other stacks where the visibility or fullscreen
1623 // status of an activity in a previous task affects other.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001624 behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001625 } else if (mStackId == HOME_STACK_ID) {
1626 if (task.isHomeTask()) {
1627 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
1628 + " stackInvisible=" + stackInvisible
1629 + " behindFullscreenActivity=" + behindFullscreenActivity);
1630 // No other task in the home stack should be visible behind the home activity.
1631 // Home activities is usually a translucent activity with the wallpaper behind
1632 // them. However, when they don't have the wallpaper behind them, we want to
1633 // show activities in the next application stack behind them vs. another
1634 // task in the home stack like recents.
1635 behindFullscreenActivity = true;
1636 } else if (task.isRecentsTask()
1637 && task.getTaskToReturnTo() == APPLICATION_ACTIVITY_TYPE) {
1638 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1639 "Recents task returning to app: at " + task
1640 + " stackInvisible=" + stackInvisible
1641 + " behindFullscreenActivity=" + behindFullscreenActivity);
1642 // We don't want any other tasks in the home stack visible if the recents
1643 // activity is going to be returning to an application activity type.
1644 // We do this to preserve the visible order the user used to get into the
1645 // recents activity. The recents activity is normally translucent and if it
1646 // doesn't have the wallpaper behind it the next activity in the home stack
1647 // shouldn't be visible when the home stack is brought to the front to display
1648 // the recents activity from an app.
1649 behindFullscreenActivity = true;
1650 }
1651
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001652 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001653 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001654
1655 if (mTranslucentActivityWaiting != null &&
1656 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1657 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1658 notifyActivityDrawnLocked(null);
1659 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001660 }
Craig Mautner58547802013-03-05 08:23:53 -08001661
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001662 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1663 if (mTranslucentActivityWaiting != top) {
1664 mUndrawnActivitiesBelowTopTranslucent.clear();
1665 if (mTranslucentActivityWaiting != null) {
1666 // Call the callback with a timeout indication.
1667 notifyActivityDrawnLocked(null);
1668 mTranslucentActivityWaiting = null;
1669 }
1670 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1671 }
1672 }
1673
1674 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001675 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001676 // We need to make sure the app is running if it's the top, or it is just made visible from
1677 // invisible. If the app is already visible, it must have died while it was visible. In this
1678 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1679 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001680 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001681 // This activity needs to be visible, but isn't even running...
1682 // get it started and resume if no other stack in this stack is resumed.
1683 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1684 if (r != starting) {
1685 r.startFreezingScreenLocked(r.app, configChanges);
1686 }
1687 if (!r.visible || r.mLaunchTaskBehind) {
1688 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1689 setVisible(r, true);
1690 }
1691 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001692 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001693 return true;
1694 }
1695 }
1696 return false;
1697 }
1698
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001699 private void makeInvisible(ActivityRecord r, ActivityRecord visibleBehind) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001700 if (!r.visible) {
1701 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1702 return;
1703 }
1704 // Now for any activities that aren't visible to the user, make sure they no longer are
1705 // keeping the screen frozen.
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001706 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001707 try {
1708 setVisible(r, false);
1709 switch (r.state) {
1710 case STOPPING:
1711 case STOPPED:
1712 if (r.app != null && r.app.thread != null) {
1713 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1714 "Scheduling invisibility: " + r);
1715 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1716 }
1717 break;
1718
1719 case INITIALIZING:
1720 case RESUMED:
1721 case PAUSING:
1722 case PAUSED:
1723 // This case created for transitioning activities from
1724 // translucent to opaque {@link Activity#convertToOpaque}.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001725 if (visibleBehind == r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001726 releaseBackgroundResources(r);
1727 } else {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001728 addToStopping(r, true /* immediate */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001729 }
1730 break;
1731
1732 default:
1733 break;
1734 }
1735 } catch (Exception e) {
1736 // Just skip on any failure; we'll make it visible when it next restarts.
1737 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1738 }
1739 }
1740
1741 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1742 TaskRecord task, ActivityRecord r) {
1743 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001744 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08001745 + " stackInvisible=" + stackInvisible
1746 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001747 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001748 behindFullscreenActivity = true;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001749 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001750 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1751 + " stackInvisible=" + stackInvisible
1752 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001753 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001754 }
1755 return behindFullscreenActivity;
1756 }
1757
Jorim Jaggie66edb12016-02-05 12:41:17 -08001758 private void makeVisibleIfNeeded(ActivityRecord starting, ActivityRecord r) {
1759
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001760 // This activity is not currently visible, but is running. Tell it to become visible.
Jorim Jaggie66edb12016-02-05 12:41:17 -08001761 if (r.state == ActivityState.RESUMED || r == starting) {
Chong Zhange05db742016-02-16 16:58:37 -08001762 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1763 "Not making visible, r=" + r + " state=" + r.state + " starting=" + starting);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001764 return;
1765 }
1766
1767 // If this activity is paused, tell it to now show its window.
1768 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1769 "Making visible and scheduling visibility: " + r);
1770 try {
1771 if (mTranslucentActivityWaiting != null) {
1772 r.updateOptionsLocked(r.returningOptions);
1773 mUndrawnActivitiesBelowTopTranslucent.add(r);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001774 }
Jorim Jaggie66edb12016-02-05 12:41:17 -08001775 setVisible(r, true);
1776 r.sleeping = false;
1777 r.app.pendingUiClean = true;
1778 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1779 r.stopFreezingScreenLocked(false);
1780 } catch (Exception e) {
1781 // Just skip on any failure; we'll make it
1782 // visible when it next restarts.
1783 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001784 }
1785 }
1786
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001787 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001788 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1789 r.stopFreezingScreenLocked(false);
1790 try {
1791 if (r.returningOptions != null) {
1792 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1793 }
1794 } catch(RemoteException e) {
1795 }
1796 return r.state == ActivityState.RESUMED;
1797 }
1798
Todd Kennedyaab56db2015-01-30 09:39:53 -08001799 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001800 mTranslucentActivityWaiting = r;
1801 mUndrawnActivitiesBelowTopTranslucent.clear();
1802 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1803 }
1804
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001805 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1806 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1807 final TaskRecord task = mTaskHistory.get(taskNdx);
1808 final ArrayList<ActivityRecord> activities = task.mActivities;
1809 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1810 final ActivityRecord r = activities.get(activityNdx);
1811 if ( r.appTimeTracker != except) {
1812 r.appTimeTracker = null;
1813 }
1814 }
1815 }
1816 }
1817
Craig Mautner5eda9b32013-07-02 11:58:16 -07001818 /**
1819 * Called as activities below the top translucent activity are redrawn. When the last one is
1820 * redrawn notify the top activity by calling
1821 * {@link Activity#onTranslucentConversionComplete}.
1822 *
1823 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1824 * occurred and the activity will be notified immediately.
1825 */
1826 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001827 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001828 if ((r == null)
1829 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1830 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1831 // The last undrawn activity below the top has just been drawn. If there is an
1832 // opaque activity at the top, notify it that it can become translucent safely now.
1833 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1834 mTranslucentActivityWaiting = null;
1835 mUndrawnActivitiesBelowTopTranslucent.clear();
1836 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1837
Craig Mautner71dd1b62014-02-18 15:48:52 -08001838 if (waitingActivity != null) {
1839 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1840 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1841 try {
1842 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1843 waitingActivity.appToken, r != null);
1844 } catch (RemoteException e) {
1845 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001846 }
1847 }
1848 }
1849 }
1850
Craig Mautnera61bc652013-10-28 15:43:18 -07001851 /** If any activities below the top running one are in the INITIALIZING state and they have a
1852 * starting window displayed then remove that starting window. It is possible that the activity
1853 * in this state will never resumed in which case that starting window will be orphaned. */
1854 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001855 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001856 boolean aboveTop = true;
1857 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1858 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1859 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1860 final ActivityRecord r = activities.get(activityNdx);
1861 if (aboveTop) {
1862 if (r == topActivity) {
1863 aboveTop = false;
1864 }
1865 continue;
1866 }
1867
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08001868 if (r.state == ActivityState.INITIALIZING
1869 && r.mStartingWindowState == STARTING_WINDOW_SHOWN) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001870 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1871 "Found orphaned starting window " + r);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08001872 r.mStartingWindowState = STARTING_WINDOW_REMOVED;
Craig Mautnera61bc652013-10-28 15:43:18 -07001873 mWindowManager.removeAppStartingWindow(r.appToken);
1874 }
1875 }
1876 }
1877 }
1878
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001879 /**
1880 * Ensure that the top activity in the stack is resumed.
1881 *
1882 * @param prev The previously resumed activity, for when in the process
1883 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001884 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001885 *
1886 * @return Returns true if something is being resumed, or false if
1887 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001888 *
1889 * NOTE: It is not safe to call this method directly as it can cause an activity in a
1890 * non-focused stack to be resumed.
1891 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
1892 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001893 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08001894 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001895 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001896 // Don't even start recursing.
1897 return false;
1898 }
1899
1900 boolean result = false;
1901 try {
1902 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001903 mStackSupervisor.inResumeTopActivity = true;
1904 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1905 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001906 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001907 }
Craig Mautner544efa72014-09-04 16:41:20 -07001908 result = resumeTopActivityInnerLocked(prev, options);
1909 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001910 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001911 }
1912 return result;
1913 }
1914
Chong Zhang280d3322015-11-03 17:27:26 -08001915 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001916 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001917
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001918 if (!mService.mBooting && !mService.mBooted) {
1919 // Not ready yet!
1920 return false;
1921 }
1922
Craig Mautnerdf88d732014-01-27 09:21:32 -08001923 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001924 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001925 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001926 // Do not resume this stack if its parent is not resumed.
1927 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1928 return false;
1929 }
1930
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001931 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07001932
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001933 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001934 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001935
1936 // Remember how we'll process this pause/resume situation, and ensure
1937 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001938 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1939 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001940
Craig Mautner84984fa2014-06-19 11:19:20 -07001941 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001942 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001943 // There are no more activities!
1944 final String reason = "noMoreActivities";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001945 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(reason)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001946 // Try to move focus to the next visible stack with a running activity if this
1947 // stack is not covering the entire screen.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001948 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
1949 mStackSupervisor.getFocusedStack(), prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001950 }
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001951
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001952 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001953 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001954 if (DEBUG_STATES) Slog.d(TAG_STATES,
1955 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001956 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001957 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001958 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1959 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1960 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001961 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001962 }
1963
1964 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001965
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001966 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001967 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1968 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001969 // Make sure we have executed any pending transitions, since there
1970 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001971 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001972 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001973 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001974 if (DEBUG_STATES) Slog.d(TAG_STATES,
1975 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001976 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001977 return false;
1978 }
1979
Craig Mautner525f3d92013-05-07 14:01:50 -07001980 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001981 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001982 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001983 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001984 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001985 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001986 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001987 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001988 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001989 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001990 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001991 } else if (!isOnHomeDisplay()) {
1992 return false;
1993 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001994 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001995 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001996 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1997 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1998 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001999 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07002000 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002001 }
2002
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002003 // If we are sleeping, and there is no resumed activity, and the top
2004 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07002005 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09002006 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002007 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002008 // Make sure we have executed any pending transitions, since there
2009 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002010 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002011 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002012 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002013 if (DEBUG_STATES) Slog.d(TAG_STATES,
2014 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002015 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002016 return false;
2017 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002018
2019 // Make sure that the user who owns this activity is started. If not,
2020 // we will just leave it as is because someone should be bringing
2021 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002022 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002023 Slog.w(TAG, "Skipping resume of top activity " + next
2024 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002025 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002026 return false;
2027 }
2028
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002029 // The activity may be waiting for stop, but that is no longer
2030 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002031 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002032 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002033 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002034 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002035
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002036 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002037
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002038 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002039 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002040 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002041 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002042 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002043 return false;
2044 }
2045
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002046 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2047
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002048 // We need to start pausing the current activity so the top one can be resumed...
2049 final boolean dontWaitForPause = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002050 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002051 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002052 if (DEBUG_STATES) Slog.d(TAG_STATES,
2053 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08002054 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002055 }
2056 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002057 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002058 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002059 // At this point we want to put the upcoming activity's process
2060 // at the top of the LRU list, since we know we will be needing it
2061 // very soon and it would be a waste to let it get killed if it
2062 // happens to be sitting towards the end.
2063 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002064 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002065 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002066 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002067 return true;
2068 }
2069
Christopher Tated3f175c2012-06-14 14:16:54 -07002070 // If the most recent activity was noHistory but was only stopped rather
2071 // than stopped+finished because the device went to sleep, we need to make
2072 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07002073 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002074 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002075 if (DEBUG_STATES) Slog.d(TAG_STATES,
2076 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002077 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002078 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002079 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002080 }
2081
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002082 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002083 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2084 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002085 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002086 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2087 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002088 } else {
2089 // The next activity is already visible, so hide the previous
2090 // activity's windows right now so we can show the new one ASAP.
2091 // We only do this if the previous is finishing, which should mean
2092 // it is on top of the one being resumed so hiding it quickly
2093 // is good. Otherwise, we want to do the normal route of allowing
2094 // the resumed activity to be shown so we can decide if the
2095 // previous should actually be hidden depending on whether the
2096 // new one is found to be full-screen or not.
2097 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002098 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002099 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2100 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002101 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002102 + ", nowVisible=" + next.nowVisible);
2103 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002104 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002105 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002106 + ", waitingVisible="
2107 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2108 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002109 }
2110 }
2111 }
2112
Dianne Hackborne7f97212011-02-24 14:40:20 -08002113 // Launching this app's activity, make sure the app is no longer
2114 // considered stopped.
2115 try {
2116 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002117 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002118 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002119 } catch (IllegalArgumentException e) {
2120 Slog.w(TAG, "Failed trying to unstop package "
2121 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002122 }
2123
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002124 // We are starting up the next activity, so tell the window manager
2125 // that the previous one will be hidden soon. This way it can know
2126 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002127 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002128 if (prev != null) {
2129 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002130 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002131 "Prepare close transition: prev=" + prev);
2132 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002133 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002134 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002135 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002136 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002137 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2138 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002139 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002140 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002141 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002142 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2143 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002144 if (mNoAnimActivities.contains(next)) {
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_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002150 : next.mLaunchTaskBehind
2151 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2152 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002153 }
2154 }
Craig Mautner967212c2013-04-13 21:10:58 -07002155 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002156 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002157 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002158 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002159 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002160 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002161 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002162 }
2163 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002164
2165 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002166 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002167 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2168 if (opts != null) {
2169 resumeAnimOptions = opts.toBundle();
2170 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002171 next.applyOptionsLocked();
2172 } else {
2173 next.clearOptionsLocked();
2174 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002175
Craig Mautnercf910b02013-04-23 11:23:27 -07002176 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002177 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002178 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002179
2180 // This activity is now becoming visible.
Jorim Jaggi1b025a62016-02-03 19:27:49 -08002181 if (!next.visible) {
2182 mWindowManager.setAppVisibility(next.appToken, true);
2183 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002184
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002185 // schedule launch ticks to collect information about slow apps.
2186 next.startLaunchTickingLocked();
2187
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002188 ActivityRecord lastResumedActivity =
2189 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002190 ActivityState lastState = next.state;
2191
2192 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002193
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002194 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002195 next.state = ActivityState.RESUMED;
2196 mResumedActivity = next;
2197 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002198 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002199 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002200 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002201 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002202
2203 // Have the window manager re-evaluate the orientation of
2204 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002205 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002206 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002207 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002208 mService.mConfiguration,
2209 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2210 if (config != null) {
2211 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002212 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002213 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002214 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002215
Craig Mautner525f3d92013-05-07 14:01:50 -07002216 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002217 // The configuration update wasn't able to keep the existing
2218 // instance of the activity, and instead started a new one.
2219 // We should be all done, but let's just make sure our activity
2220 // is still at the top and schedule another run if something
2221 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002222 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002223 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002224 "Activity config changed during resume: " + next
2225 + ", new next: " + nextNext);
2226 if (nextNext != next) {
2227 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002228 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002229 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002230 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002231 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002232 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002233 return true;
2234 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002235 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002236 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002237 }
Craig Mautner58547802013-03-05 08:23:53 -08002238
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002239 try {
2240 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002241 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002242 if (a != null) {
2243 final int N = a.size();
2244 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002245 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2246 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002247 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002248 }
2249 }
2250
2251 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002252 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002253 }
2254
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002255 // Well the app will no longer be stopped.
2256 // Clear app token stopped state in window manager if needed.
2257 mWindowManager.notifyAppStopped(next.appToken, false);
2258
Craig Mautner299f9602015-01-26 09:47:33 -08002259 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2260 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002261
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002262 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002263 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002264 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002265 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002266 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002267 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002268 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002269
Craig Mautner0eea92c2013-05-16 13:35:39 -07002270 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002271
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002272 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002273 } catch (Exception e) {
2274 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002275 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002276 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002277 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002278 if (lastStack != null) {
2279 lastStack.mResumedActivity = lastResumedActivity;
2280 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002281 Slog.i(TAG, "Restarting because process died: " + next);
2282 if (!next.hasBeenLaunched) {
2283 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002284 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2285 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002286 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002287 next.appToken, next.packageName, next.theme,
2288 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002289 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2290 next.windowFlags, null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002291 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002292 }
George Mount2c92c972014-03-20 09:38:23 -07002293 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002294 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002295 return true;
2296 }
2297
2298 // From this point on, if something goes wrong there is no way
2299 // to recover the activity.
2300 try {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002301 completeResumeLocked(next);
2302 } catch (Exception e) {
2303 // If any exception gets thrown, toss away this
2304 // activity and try the next one.
2305 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002306 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002307 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002308 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002309 return true;
2310 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002311 } else {
2312 // Whoops, need to restart this activity!
2313 if (!next.hasBeenLaunched) {
2314 next.hasBeenLaunched = true;
2315 } else {
2316 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002317 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002318 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002319 mService.compatibilityInfoForPackageLocked(
2320 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002321 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002322 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002323 null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002324 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002325 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002326 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002327 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002328 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002329 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002330 }
2331
Craig Mautnercf910b02013-04-23 11:23:27 -07002332 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002333 return true;
2334 }
2335
riddle_hsuc215a4f2014-12-27 12:10:45 +08002336 private TaskRecord getNextTask(TaskRecord targetTask) {
2337 final int index = mTaskHistory.indexOf(targetTask);
2338 if (index >= 0) {
2339 final int numTasks = mTaskHistory.size();
2340 for (int i = index + 1; i < numTasks; ++i) {
2341 TaskRecord task = mTaskHistory.get(i);
2342 if (task.userId == targetTask.userId) {
2343 return task;
2344 }
2345 }
2346 }
2347 return null;
2348 }
2349
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002350 private void insertTaskAtPosition(TaskRecord task, int position) {
2351 if (position >= mTaskHistory.size()) {
2352 insertTaskAtTop(task, null);
2353 return;
2354 }
2355 // Calculate maximum possible position for this task.
2356 int maxPosition = mTaskHistory.size();
2357 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002358 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002359 // Put non-current user tasks below current user tasks.
2360 while (maxPosition > 0) {
2361 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2362 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002363 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002364 break;
2365 }
2366 maxPosition--;
2367 }
2368 }
2369 position = Math.min(position, maxPosition);
2370 mTaskHistory.remove(task);
2371 mTaskHistory.add(position, task);
2372 updateTaskMovement(task, true);
2373 }
2374
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002375 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002376 // If the moving task is over home stack, transfer its return type to next task
2377 if (task.isOverHomeStack()) {
2378 final TaskRecord nextTask = getNextTask(task);
2379 if (nextTask != null) {
2380 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2381 }
2382 }
2383
Craig Mautner9c85c202013-10-04 20:11:26 -07002384 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002385 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002386 if (isOnHomeDisplay()) {
2387 ActivityStack lastStack = mStackSupervisor.getLastStack();
2388 final boolean fromHome = lastStack.isHomeStack();
2389 if (!isHomeStack() && (fromHome || topTask() != task)) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002390 int returnToType = APPLICATION_ACTIVITY_TYPE;
2391 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2392 returnToType = lastStack.topTask() == null
2393 ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2394 }
2395 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002396 }
2397 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002398 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002399 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002400
Craig Mautnerac6f8432013-07-17 13:24:59 -07002401 mTaskHistory.remove(task);
2402 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002403 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002404 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002405 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002406 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002407 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002408 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002409 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002410 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002411 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002412 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002413 break;
2414 }
2415 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002416 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002417 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002418 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002419 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002420 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002421
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002422 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2423 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002424 TaskRecord rTask = r.task;
2425 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002426 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2427 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002428 // Last activity in task had been removed or ActivityManagerService is reusing task.
2429 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002430 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002431 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002432 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002433 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002434 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002435 if (!newTask) {
2436 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002437 boolean startIt = true;
2438 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2439 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002440 if (task.getTopActivity() == null) {
2441 // All activities in task are finishing.
2442 continue;
2443 }
Craig Mautner70a86932013-02-28 22:37:44 -08002444 if (task == r.task) {
2445 // Here it is! Now, if this is not yet visible to the
2446 // user, then just add it without starting; it will
2447 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002448 if (!startIt) {
2449 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2450 + task, new RuntimeException("here").fillInStackTrace());
2451 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002452 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002453 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002454 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002455 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002456 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002457 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002458 return;
2459 }
2460 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002461 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002462 startIt = false;
2463 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002464 }
2465 }
2466
2467 // Place a new activity at top of stack, so it is next to interact
2468 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002469
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002470 // If we are not placing the new activity frontmost, we do not want
2471 // to deliver the onUserLeaving callback to the actual frontmost
2472 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002473 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002474 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002475 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002476 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002477 }
Craig Mautner70a86932013-02-28 22:37:44 -08002478
2479 task = r.task;
2480
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002481 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002482 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002483 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002484 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002485 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002486
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002487 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002488 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002489 // We want to show the starting preview window if we are
2490 // switching to a new task, or the next activity's process is
2491 // not currently running.
2492 boolean showStartingIcon = newTask;
2493 ProcessRecord proc = r.app;
2494 if (proc == null) {
2495 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2496 }
2497 if (proc == null || proc.thread == null) {
2498 showStartingIcon = true;
2499 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002500 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002501 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002502 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002503 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002504 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002505 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002506 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002507 ? r.mLaunchTaskBehind
2508 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2509 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002510 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002511 mNoAnimActivities.remove(r);
2512 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002513 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002514 boolean doShow = true;
2515 if (newTask) {
2516 // Even though this activity is starting fresh, we still need
2517 // to reset it to make sure we apply affinities to move any
2518 // existing activities from other tasks in to it.
2519 // If the caller has requested that the target task be
2520 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002521 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002522 resetTaskIfNeededLocked(r, r);
2523 doShow = topRunningNonDelayedActivityLocked(null) == r;
2524 }
Chong Zhang280d3322015-11-03 17:27:26 -08002525 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002526 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2527 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002528 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002529 if (r.mLaunchTaskBehind) {
2530 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2531 // tell WindowManager that r is visible even though it is at the back of the stack.
2532 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002533 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002534 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002535 // Figure out if we are transitioning from another activity that is
2536 // "has the same starting icon" as the next one. This allows the
2537 // window manager to keep the previous window it had previously
2538 // created, if it still had one.
2539 ActivityRecord prev = mResumedActivity;
2540 if (prev != null) {
2541 // We don't want to reuse the previous starting preview if:
2542 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002543 if (prev.task != r.task) {
2544 prev = null;
2545 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002546 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002547 else if (prev.nowVisible) {
2548 prev = null;
2549 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002550 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002551 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002552 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002553 mService.compatibilityInfoForPackageLocked(
2554 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002555 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002556 prev != null ? prev.appToken : null, showStartingIcon);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002557 r.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002558 }
2559 } else {
2560 // If this is the first activity, don't do any fancy animations,
2561 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002562 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002563 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002564 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002565 }
2566 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002567 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002568 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002569 }
2570
Dianne Hackbornbe707852011-11-11 14:32:10 -08002571 final void validateAppTokensLocked() {
2572 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002573 mValidateAppTokens.ensureCapacity(numActivities());
2574 final int numTasks = mTaskHistory.size();
2575 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2576 TaskRecord task = mTaskHistory.get(taskNdx);
2577 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002578 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002579 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002580 }
Craig Mautner000f0022013-02-26 15:04:29 -08002581 TaskGroup group = new TaskGroup();
2582 group.taskId = task.taskId;
2583 mValidateAppTokens.add(group);
2584 final int numActivities = activities.size();
2585 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2586 final ActivityRecord r = activities.get(activityNdx);
2587 group.tokens.add(r.appToken);
2588 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002589 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002590 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002591 }
2592
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002593 /**
2594 * Perform a reset of the given task, if needed as part of launching it.
2595 * Returns the new HistoryRecord at the top of the task.
2596 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002597 /**
2598 * Helper method for #resetTaskIfNeededLocked.
2599 * We are inside of the task being reset... we'll either finish this activity, push it out
2600 * for another task, or leave it as-is.
2601 * @param task The task containing the Activity (taskTop) that might be reset.
2602 * @param forceReset
2603 * @return An ActivityOptions that needs to be processed.
2604 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002605 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002606 ActivityOptions topOptions = null;
2607
2608 int replyChainEnd = -1;
2609 boolean canMoveOptions = true;
2610
2611 // We only do this for activities that are not the root of the task (since if we finish
2612 // the root, we may no longer have the task!).
2613 final ArrayList<ActivityRecord> activities = task.mActivities;
2614 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002615 final int rootActivityNdx = task.findEffectiveRootIndex();
2616 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002617 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002618 if (target.frontOfTask)
2619 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002620
2621 final int flags = target.info.flags;
2622 final boolean finishOnTaskLaunch =
2623 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2624 final boolean allowTaskReparenting =
2625 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2626 final boolean clearWhenTaskReset =
2627 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2628
2629 if (!finishOnTaskLaunch
2630 && !clearWhenTaskReset
2631 && target.resultTo != null) {
2632 // If this activity is sending a reply to a previous
2633 // activity, we can't do anything with it now until
2634 // we reach the start of the reply chain.
2635 // XXX note that we are assuming the result is always
2636 // to the previous activity, which is almost always
2637 // the case but we really shouldn't count on.
2638 if (replyChainEnd < 0) {
2639 replyChainEnd = i;
2640 }
2641 } else if (!finishOnTaskLaunch
2642 && !clearWhenTaskReset
2643 && allowTaskReparenting
2644 && target.taskAffinity != null
2645 && !target.taskAffinity.equals(task.affinity)) {
2646 // If this activity has an affinity for another
2647 // task, then we need to move it out of here. We will
2648 // move it as far out of the way as possible, to the
2649 // bottom of the activity stack. This also keeps it
2650 // correctly ordered with any activities we previously
2651 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002652 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002653 final ActivityRecord bottom =
2654 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002655 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002656 if (bottom != null && target.taskAffinity != null
2657 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002658 // If the activity currently at the bottom has the
2659 // same task affinity as the one we are moving,
2660 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002661 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002662 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002663 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002664 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002665 targetTask = createTaskRecord(
2666 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
2667 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002668 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002669 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002670 + " out to new task " + target.task);
2671 }
2672
Wale Ogunwale706ed792015-08-02 10:29:44 -07002673 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002674
Craig Mautner525f3d92013-05-07 14:01:50 -07002675 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002676 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2677 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002678 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002679 if (p.finishing) {
2680 continue;
2681 }
2682
Craig Mautnere3a74d52013-02-22 14:14:58 -08002683 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002684 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002685 topOptions = p.takeOptionsLocked();
2686 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002687 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002688 }
2689 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002690 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2691 "Removing activity " + p + " from task=" + task + " adding to task="
2692 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002693 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2694 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002695 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002696 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002697
Wale Ogunwale706ed792015-08-02 10:29:44 -07002698 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002699 }
2700
Wale Ogunwale706ed792015-08-02 10:29:44 -07002701 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002702 if (VALIDATE_TOKENS) {
2703 validateAppTokensLocked();
2704 }
2705
2706 replyChainEnd = -1;
2707 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2708 // If the activity should just be removed -- either
2709 // because it asks for it, or the task should be
2710 // cleared -- then finish it and anything that is
2711 // part of its reply chain.
2712 int end;
2713 if (clearWhenTaskReset) {
2714 // In this case, we want to finish this activity
2715 // and everything above it, so be sneaky and pretend
2716 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002717 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002718 } else if (replyChainEnd < 0) {
2719 end = i;
2720 } else {
2721 end = replyChainEnd;
2722 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002723 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002724 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002725 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002726 if (p.finishing) {
2727 continue;
2728 }
2729 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002730 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002731 topOptions = p.takeOptionsLocked();
2732 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002733 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002734 }
2735 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002736 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002737 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002738 if (finishActivityLocked(
2739 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002740 end--;
2741 srcPos--;
2742 }
2743 }
2744 replyChainEnd = -1;
2745 } else {
2746 // If we were in the middle of a chain, well the
2747 // activity that started it all doesn't want anything
2748 // special, so leave it all as-is.
2749 replyChainEnd = -1;
2750 }
2751 }
2752
2753 return topOptions;
2754 }
2755
2756 /**
2757 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2758 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2759 * @param affinityTask The task we are looking for an affinity to.
2760 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2761 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2762 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2763 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002764 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002765 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002766 int replyChainEnd = -1;
2767 final int taskId = task.taskId;
2768 final String taskAffinity = task.affinity;
2769
2770 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2771 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002772 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2773
2774 // Do not operate on or below the effective root Activity.
2775 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002776 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002777 if (target.frontOfTask)
2778 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002779
2780 final int flags = target.info.flags;
2781 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2782 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2783
2784 if (target.resultTo != null) {
2785 // If this activity is sending a reply to a previous
2786 // activity, we can't do anything with it now until
2787 // we reach the start of the reply chain.
2788 // XXX note that we are assuming the result is always
2789 // to the previous activity, which is almost always
2790 // the case but we really shouldn't count on.
2791 if (replyChainEnd < 0) {
2792 replyChainEnd = i;
2793 }
2794 } else if (topTaskIsHigher
2795 && allowTaskReparenting
2796 && taskAffinity != null
2797 && taskAffinity.equals(target.taskAffinity)) {
2798 // This activity has an affinity for our task. Either remove it if we are
2799 // clearing or move it over to our task. Note that
2800 // we currently punt on the case where we are resetting a
2801 // task that is not at the top but who has activities above
2802 // with an affinity to it... this is really not a normal
2803 // case, and we will need to later pull that task to the front
2804 // and usually at that point we will do the reset and pick
2805 // up those remaining activities. (This only happens if
2806 // someone starts an activity in a new task from an activity
2807 // in a task that is not currently on top.)
2808 if (forceReset || finishOnTaskLaunch) {
2809 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002810 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2811 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002812 for (int srcPos = start; srcPos >= i; --srcPos) {
2813 final ActivityRecord p = activities.get(srcPos);
2814 if (p.finishing) {
2815 continue;
2816 }
Todd Kennedy539db512014-12-15 09:57:55 -08002817 finishActivityLocked(
2818 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002819 }
2820 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002821 if (taskInsertionPoint < 0) {
2822 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002823
Craig Mautner77878772013-03-04 19:46:24 -08002824 }
Craig Mautner77878772013-03-04 19:46:24 -08002825
2826 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002827 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2828 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2829 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002830 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002831 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002832 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002833 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002834
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002835 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2836 "Removing and adding activity " + p + " to stack at " + task
2837 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002838 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2839 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002840 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002841 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002842 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002843 if (VALIDATE_TOKENS) {
2844 validateAppTokensLocked();
2845 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002846
2847 // Now we've moved it in to place... but what if this is
2848 // a singleTop activity and we have put it on top of another
2849 // instance of the same activity? Then we drop the instance
2850 // below so it remains singleTop.
2851 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2852 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002853 int targetNdx = taskActivities.indexOf(target);
2854 if (targetNdx > 0) {
2855 ActivityRecord p = taskActivities.get(targetNdx - 1);
2856 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002857 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2858 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002859 }
2860 }
2861 }
2862 }
2863
2864 replyChainEnd = -1;
2865 }
2866 }
Craig Mautner77878772013-03-04 19:46:24 -08002867 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002868 }
2869
Craig Mautner8849a5e2013-04-02 16:41:03 -07002870 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002871 ActivityRecord newActivity) {
2872 boolean forceReset =
2873 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2874 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2875 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2876 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2877 forceReset = true;
2878 }
2879 }
2880
2881 final TaskRecord task = taskTop.task;
2882
2883 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2884 * for remaining tasks. Used for later tasks to reparent to task. */
2885 boolean taskFound = false;
2886
2887 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2888 ActivityOptions topOptions = null;
2889
Craig Mautner77878772013-03-04 19:46:24 -08002890 // Preserve the location for reparenting in the new task.
2891 int reparentInsertionPoint = -1;
2892
Craig Mautnere3a74d52013-02-22 14:14:58 -08002893 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2894 final TaskRecord targetTask = mTaskHistory.get(i);
2895
2896 if (targetTask == task) {
2897 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2898 taskFound = true;
2899 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002900 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2901 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002902 }
2903 }
2904
Craig Mautner70a86932013-02-28 22:37:44 -08002905 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002906 if (taskNdx >= 0) {
2907 do {
2908 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2909 } while (taskTop == null && taskNdx >= 0);
2910 }
Craig Mautner70a86932013-02-28 22:37:44 -08002911
Craig Mautnere3a74d52013-02-22 14:14:58 -08002912 if (topOptions != null) {
2913 // If we got some ActivityOptions from an activity on top that
2914 // was removed from the task, propagate them to the new real top.
2915 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002916 taskTop.updateOptionsLocked(topOptions);
2917 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002918 topOptions.abort();
2919 }
2920 }
2921
2922 return taskTop;
2923 }
2924
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002925 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2926 String resultWho, int requestCode, int resultCode, Intent data) {
2927
2928 if (callingUid > 0) {
2929 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002930 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002931 }
2932
2933 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2934 + " : who=" + resultWho + " req=" + requestCode
2935 + " res=" + resultCode + " data=" + data);
2936 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2937 try {
2938 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2939 list.add(new ResultInfo(resultWho, requestCode,
2940 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002941 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002942 return;
2943 } catch (Exception e) {
2944 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2945 }
2946 }
2947
2948 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2949 }
2950
Craig Mautner299f9602015-01-26 09:47:33 -08002951 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002952 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2953 return;
2954 }
2955
2956 final ActivityRecord next = topRunningActivityLocked();
2957 final String myReason = reason + " adjustFocus";
2958 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002959 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002960 // For freeform, docked, and pinned stacks we always keep the focus within the
2961 // stack as long as there is a running activity in the stack that we can adjust
2962 // focus to.
2963 mService.setFocusedActivityLocked(next, myReason);
2964 return;
2965 } else {
2966 final TaskRecord task = r.task;
2967 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2968 // For non-fullscreen stack, we want to move the focus to the next visible
2969 // stack to prevent the home screen from moving to the top and obscuring
2970 // other visible stacks.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002971 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002972 return;
2973 }
2974 // Move the home stack to the top if this stack is fullscreen or there is no
2975 // other visible stack.
2976 if (mStackSupervisor.moveHomeStackTaskToTop(
2977 task.getTaskToReturnTo(), myReason)) {
2978 // Activity focus was already adjusted. Nothing else to do...
2979 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002980 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002981 }
2982 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08002983 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002984
Wale Ogunwaled046a012015-12-24 13:05:59 -08002985 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002986 }
2987
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002988 private boolean adjustFocusToNextFocusableStackLocked(String reason) {
2989 final ActivityStack stack = getNextFocusableStackLocked();
2990 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002991 if (stack == null) {
2992 return false;
2993 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002994 return mService.setFocusedActivityLocked(stack.topRunningActivityLocked(), myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002995 }
2996
Craig Mautnerf3333272013-04-22 10:55:53 -07002997 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002998 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002999 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3000 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3001 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07003002 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003003 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003004 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3005 "stop-no-history", false)) {
3006 // Activity was finished, no need to continue trying to schedule stop.
3007 adjustFocusedActivityLocked(r, "stopActivityFinished");
3008 r.resumeKeyDispatchingLocked();
3009 return;
3010 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003011 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003012 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003013 + " on stop because we're just sleeping");
3014 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003015 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003016 }
3017
3018 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08003019 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003020 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003021 try {
3022 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003023 if (DEBUG_STATES) Slog.v(TAG_STATES,
3024 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003025 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003026 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3027 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003028 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003029 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003030 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003031 EventLogTags.writeAmStopActivity(
3032 r.userId, System.identityHashCode(r), r.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003033 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07003034 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003035 r.setSleeping(true);
3036 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003037 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003038 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003039 } catch (Exception e) {
3040 // Maybe just ignore exceptions here... if the process
3041 // has crashed, our death notification will clean things
3042 // up.
3043 Slog.w(TAG, "Exception thrown during pause", e);
3044 // Just in case, assume it to be stopped.
3045 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003046 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003047 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003048 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003049 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003050 }
3051 }
3052 }
3053 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003054
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003055 /**
3056 * @return Returns true if the activity is being finished, false if for
3057 * some reason it is being left as-is.
3058 */
3059 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003060 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003061 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003062 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3063 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003064 + ", result=" + resultCode + ", data=" + resultData
3065 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003066 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003067 return false;
3068 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003069
Craig Mautnerd44711d2013-02-23 11:24:36 -08003070 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003071 return true;
3072 }
3073
Craig Mautnerd2328952013-03-05 12:46:26 -08003074 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003075 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3076 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3077 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3078 ActivityRecord r = activities.get(activityNdx);
3079 if (r.resultTo == self && r.requestCode == requestCode) {
3080 if ((r.resultWho == null && resultWho == null) ||
3081 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3082 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3083 false);
3084 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003085 }
3086 }
3087 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003088 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003089 }
3090
Adrian Roos20d7df32016-01-12 18:59:43 +01003091 final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003092 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003093 TaskRecord finishedTask = null;
3094 if (r == null || r.app != app) {
3095 return null;
3096 }
3097 Slog.w(TAG, " Force finishing activity "
3098 + r.intent.getComponent().flattenToShortString());
3099 int taskNdx = mTaskHistory.indexOf(r.task);
3100 int activityNdx = r.task.mActivities.indexOf(r);
3101 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3102 finishedTask = r.task;
3103 // Also terminate any activities below it that aren't yet
3104 // stopped, to avoid a situation where one will get
3105 // re-start our crashing activity once it gets resumed again.
3106 --activityNdx;
3107 if (activityNdx < 0) {
3108 do {
3109 --taskNdx;
3110 if (taskNdx < 0) {
3111 break;
3112 }
3113 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3114 } while (activityNdx < 0);
3115 }
3116 if (activityNdx >= 0) {
3117 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3118 if (r.state == ActivityState.RESUMED
3119 || r.state == ActivityState.PAUSING
3120 || r.state == ActivityState.PAUSED) {
3121 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3122 Slog.w(TAG, " Force finishing activity "
3123 + r.intent.getComponent().flattenToShortString());
3124 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003125 }
3126 }
3127 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003128 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003129 }
3130
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003131 final void finishVoiceTask(IVoiceInteractionSession session) {
3132 IBinder sessionBinder = session.asBinder();
3133 boolean didOne = false;
3134 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3135 TaskRecord tr = mTaskHistory.get(taskNdx);
3136 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3137 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3138 ActivityRecord r = tr.mActivities.get(activityNdx);
3139 if (!r.finishing) {
3140 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3141 false);
3142 didOne = true;
3143 }
3144 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003145 } else {
3146 // Check if any of the activities are using voice
3147 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3148 ActivityRecord r = tr.mActivities.get(activityNdx);
3149 if (r.voiceSession != null
3150 && r.voiceSession.asBinder() == sessionBinder) {
3151 // Inform of cancellation
3152 r.clearVoiceSessionLocked();
3153 try {
3154 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3155 null);
3156 } catch (RemoteException re) {
3157 // Ok
3158 }
3159 // TODO: VI This is redundant in some cases
3160 mService.finishRunningVoiceLocked();
3161 break;
3162 }
3163 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003164 }
3165 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003166 Slog.d(TAG, "ActivityStack.finishVoiceTask()");
3167
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003168 if (didOne) {
3169 mService.updateOomAdjLocked();
3170 }
3171 }
3172
Craig Mautnerd2328952013-03-05 12:46:26 -08003173 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003174 ArrayList<ActivityRecord> activities = r.task.mActivities;
3175 for (int index = activities.indexOf(r); index >= 0; --index) {
3176 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003177 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003178 break;
3179 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003180 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003181 }
3182 return true;
3183 }
3184
Dianne Hackborn5c607432012-02-28 14:44:19 -08003185 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3186 // send the result
3187 ActivityRecord resultTo = r.resultTo;
3188 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003189 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003190 + " who=" + r.resultWho + " req=" + r.requestCode
3191 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003192 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003193 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003194 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003195 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003196 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003197 if (r.info.applicationInfo.uid > 0) {
3198 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3199 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003200 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003201 }
3202 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3203 resultData);
3204 r.resultTo = null;
3205 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003206 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003207
3208 // Make sure this HistoryRecord is not holding on to other resources,
3209 // because clients have remote IPC references to this object so we
3210 // can't assume that will go away and want to avoid circular IPC refs.
3211 r.results = null;
3212 r.pendingResults = null;
3213 r.newIntents = null;
3214 r.icicle = null;
3215 }
3216
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003217 /**
3218 * @return Returns true if this activity has been removed from the history
3219 * list, or false if it is still in the list and will be removed later.
3220 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003221 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3222 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003223 if (r.finishing) {
3224 Slog.w(TAG, "Duplicate finish request for " + r);
3225 return false;
3226 }
3227
Wale Ogunwale7d701172015-03-11 15:36:30 -07003228 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003229 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003230 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003231 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003232 task.taskId, r.shortComponentName, reason);
3233 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003234 final int index = activities.indexOf(r);
3235 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003236 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003237 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003238 // If the caller asked that this activity (and all above it)
3239 // be cleared when the task is reset, don't lose that information,
3240 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003241 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003242 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003243 }
3244 }
3245
3246 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003247
Craig Mautner299f9602015-01-26 09:47:33 -08003248 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003249
Dianne Hackborn5c607432012-02-28 14:44:19 -08003250 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003251
Craig Mautnerde4ef022013-04-07 19:01:33 -07003252 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003253 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003254 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003255 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003256 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003257 ? AppTransition.TRANSIT_TASK_CLOSE
3258 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003259
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003260 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003261 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003262
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003263 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003264 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3265 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3266 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003267 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003268 }
3269
Craig Mautneraea74a52014-03-08 14:23:10 -08003270 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003271 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003272 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003273 } else if (r.state != ActivityState.PAUSING) {
3274 // If the activity is PAUSING, we will complete the finish once
3275 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003276 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003277 if (r.visible) {
3278 mWindowManager.setAppVisibility(r.appToken, false);
3279 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003280 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003281 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003282 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003283 }
3284
3285 return false;
3286 }
3287
Craig Mautnerf3333272013-04-22 10:55:53 -07003288 static final int FINISH_IMMEDIATELY = 0;
3289 static final int FINISH_AFTER_PAUSE = 1;
3290 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003291
Craig Mautnerf3333272013-04-22 10:55:53 -07003292 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003293 // First things first: if this activity is currently visible,
3294 // and the resumed activity is not yet visible, then hold off on
3295 // finishing until the resumed one becomes visible.
3296 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003297 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08003298 addToStopping(r, false /* immediate */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003299 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003300 if (DEBUG_STATES) Slog.v(TAG_STATES,
3301 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003302 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003303 if (oomAdj) {
3304 mService.updateOomAdjLocked();
3305 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003306 return r;
3307 }
3308
3309 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003310 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003311 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003312 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003313 if (mResumedActivity == r) {
3314 mResumedActivity = null;
3315 }
3316 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003317 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003318 r.state = ActivityState.FINISHING;
3319
3320 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003321 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003322 || prevState == ActivityState.STOPPED
3323 || prevState == ActivityState.INITIALIZING) {
3324 // If this activity is already stopped, we can just finish
3325 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003326 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003327 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003328 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003329 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003330 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003331 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003332 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3333 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003334 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003335 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003336
3337 // Need to go through the full pause cycle to get this
3338 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003339 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003340 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003341 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003342 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003343 return r;
3344 }
3345
Craig Mautneree36c772014-07-16 14:56:05 -07003346 void finishAllActivitiesLocked(boolean immediately) {
3347 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003348 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3349 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3350 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3351 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003352 noActivitiesInStack = false;
3353 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003354 continue;
3355 }
Craig Mautneree36c772014-07-16 14:56:05 -07003356 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003357 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3358 }
3359 }
Craig Mautneree36c772014-07-16 14:56:05 -07003360 if (noActivitiesInStack) {
3361 mActivityContainer.onTaskListEmptyLocked();
3362 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003363 }
3364
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003365 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3366 // Basic case: for simple app-centric recents, we need to recreate
3367 // the task if the affinity has changed.
3368 if (srec == null || srec.task.affinity == null ||
3369 !srec.task.affinity.equals(destAffinity)) {
3370 return true;
3371 }
3372 // Document-centric case: an app may be split in to multiple documents;
3373 // they need to re-create their task if this current activity is the root
3374 // of a document, unless simply finishing it will return them to the the
3375 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003376 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3377 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003378 // Okay, this activity is at the root of its task. What to do, what to do...
3379 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3380 // Finishing won't return to an application, so we need to recreate.
3381 return true;
3382 }
3383 // We now need to get the task below it to determine what to do.
3384 int taskIdx = mTaskHistory.indexOf(srec.task);
3385 if (taskIdx <= 0) {
3386 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3387 return false;
3388 }
3389 if (taskIdx == 0) {
3390 // At the bottom of the stack, nothing to go back to.
3391 return true;
3392 }
3393 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3394 if (!srec.task.affinity.equals(prevTask.affinity)) {
3395 // These are different apps, so need to recreate.
3396 return true;
3397 }
3398 }
3399 return false;
3400 }
3401
Wale Ogunwale7d701172015-03-11 15:36:30 -07003402 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003403 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003404 final TaskRecord task = srec.task;
3405 final ArrayList<ActivityRecord> activities = task.mActivities;
3406 final int start = activities.indexOf(srec);
3407 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003408 return false;
3409 }
3410 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003411 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003412 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003413 final ComponentName dest = destIntent.getComponent();
3414 if (start > 0 && dest != null) {
3415 for (int i = finishTo; i >= 0; i--) {
3416 ActivityRecord r = activities.get(i);
3417 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003418 r.info.name.equals(dest.getClassName())) {
3419 finishTo = i;
3420 parent = r;
3421 foundParentInTask = true;
3422 break;
3423 }
3424 }
3425 }
3426
3427 IActivityController controller = mService.mController;
3428 if (controller != null) {
3429 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3430 if (next != null) {
3431 // ask watcher if this is allowed
3432 boolean resumeOK = true;
3433 try {
3434 resumeOK = controller.activityResuming(next.packageName);
3435 } catch (RemoteException e) {
3436 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003437 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003438 }
3439
3440 if (!resumeOK) {
3441 return false;
3442 }
3443 }
3444 }
3445 final long origId = Binder.clearCallingIdentity();
3446 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003447 ActivityRecord r = activities.get(i);
3448 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003449 // Only return the supplied result for the first activity finished
3450 resultCode = Activity.RESULT_CANCELED;
3451 resultData = null;
3452 }
3453
3454 if (parent != null && foundParentInTask) {
3455 final int parentLaunchMode = parent.info.launchMode;
3456 final int destIntentFlags = destIntent.getFlags();
3457 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3458 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3459 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3460 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003461 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3462 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003463 } else {
3464 try {
3465 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3466 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003467 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3468 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3469 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003470 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3471 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003472 foundParentInTask = res == ActivityManager.START_SUCCESS;
3473 } catch (RemoteException e) {
3474 foundParentInTask = false;
3475 }
3476 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003477 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003478 }
3479 }
3480 Binder.restoreCallingIdentity(origId);
3481 return foundParentInTask;
3482 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003483 /**
3484 * Perform the common clean-up of an activity record. This is called both
3485 * as part of destroyActivityLocked() (when destroying the client-side
3486 * representation) and cleaning things up as a result of its hosting
3487 * processing going away, in which case there is no remaining client-side
3488 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003489 *
3490 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003491 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003492 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3493 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003494 if (mResumedActivity == r) {
3495 mResumedActivity = null;
3496 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003497 if (mPausingActivity == r) {
3498 mPausingActivity = null;
3499 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003500 mService.resetFocusedActivityIfNeededLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003501
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003502 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003503 r.frozenBeforeDestroy = false;
3504
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003505 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003506 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003507 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003508 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003509 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003510 }
3511
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003512 // Make sure this record is no longer in the pending finishes list.
3513 // This could happen, for example, if we are trimming activities
3514 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003515 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003516 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003517
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003518 // Remove any pending results.
3519 if (r.finishing && r.pendingResults != null) {
3520 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3521 PendingIntentRecord rec = apr.get();
3522 if (rec != null) {
3523 mService.cancelIntentSenderLocked(rec, false);
3524 }
3525 }
3526 r.pendingResults = null;
3527 }
3528
3529 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003530 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003531 }
3532
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003533 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003534 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003535 if (getVisibleBehindActivity() == r) {
3536 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003537 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003538 }
3539
3540 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003541 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003542 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003543 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003544 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003545 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003546 }
3547
Craig Mautner299f9602015-01-26 09:47:33 -08003548 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003549 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003550 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003551 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003552 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3553 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3554
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003555 r.takeFromHistory();
3556 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003557 if (DEBUG_STATES) Slog.v(TAG_STATES,
3558 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003559 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003560 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003561 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003562 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003563 if (VALIDATE_TOKENS) {
3564 validateAppTokensLocked();
3565 }
Craig Mautner312ba862014-02-10 17:55:01 -08003566 final TaskRecord task = r.task;
3567 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003568 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003569 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003570 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003571 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003572 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003573 }
Craig Mautner299f9602015-01-26 09:47:33 -08003574 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003575 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003576 cleanUpActivityServicesLocked(r);
3577 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003578 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003579
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003580 /**
3581 * Perform clean-up of service connections in an activity record.
3582 */
3583 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3584 // Throw away any services that have been bound by this activity.
3585 if (r.connections != null) {
3586 Iterator<ConnectionRecord> it = r.connections.iterator();
3587 while (it.hasNext()) {
3588 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003589 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003590 }
3591 r.connections = null;
3592 }
3593 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003594
Craig Mautneree2e45a2014-06-27 12:10:03 -07003595 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003596 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003597 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003598 mHandler.sendMessage(msg);
3599 }
3600
Craig Mautneree2e45a2014-06-27 12:10:03 -07003601 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003602 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003603 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003604 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3605 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3606 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3607 final ActivityRecord r = activities.get(activityNdx);
3608 if (r.finishing) {
3609 continue;
3610 }
3611 if (r.fullscreen) {
3612 lastIsOpaque = true;
3613 }
3614 if (owner != null && r.app != owner) {
3615 continue;
3616 }
3617 if (!lastIsOpaque) {
3618 continue;
3619 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003620 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003621 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003622 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003623 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003624 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003625 activityRemoved = true;
3626 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003627 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003628 }
3629 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003630 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003631 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003632 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003633 }
3634
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003635 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3636 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003637 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3638 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003639 + " pausing=" + mPausingActivity + " for reason " + reason);
3640 return destroyActivityLocked(r, true, reason);
3641 }
3642 return false;
3643 }
3644
3645 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3646 String reason) {
3647 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003648 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003649 int maxTasks = tasks.size() / 4;
3650 if (maxTasks < 1) {
3651 maxTasks = 1;
3652 }
3653 int numReleased = 0;
3654 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3655 final TaskRecord task = mTaskHistory.get(taskNdx);
3656 if (!tasks.contains(task)) {
3657 continue;
3658 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003659 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003660 int curNum = 0;
3661 final ArrayList<ActivityRecord> activities = task.mActivities;
3662 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3663 final ActivityRecord activity = activities.get(actNdx);
3664 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003665 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003666 + " in state " + activity.state + " resumed=" + mResumedActivity
3667 + " pausing=" + mPausingActivity + " for reason " + reason);
3668 destroyActivityLocked(activity, true, reason);
3669 if (activities.get(actNdx) != activity) {
3670 // Was removed from list, back up so we don't miss the next one.
3671 actNdx--;
3672 }
3673 curNum++;
3674 }
3675 }
3676 if (curNum > 0) {
3677 numReleased += curNum;
3678 maxTasks--;
3679 if (mTaskHistory.get(taskNdx) != task) {
3680 // The entire task got removed, back up so we don't miss the next one.
3681 taskNdx--;
3682 }
3683 }
3684 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003685 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3686 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003687 return numReleased;
3688 }
3689
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003690 /**
3691 * Destroy the current CLIENT SIDE instance of an activity. This may be
3692 * called both when actually finishing an activity, or when performing
3693 * a configuration switch where we destroy the current client-side object
3694 * but then create a new client-side object for this same HistoryRecord.
3695 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003696 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003697 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3698 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003699 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003700 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003701 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003702 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003703
3704 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003705
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003706 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003707
3708 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003709
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003710 if (hadApp) {
3711 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003712 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003713 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3714 mService.mHeavyWeightProcess = null;
3715 mService.mHandler.sendEmptyMessage(
3716 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3717 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003718 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003719 // Update any services we are bound to that might care about whether
3720 // their client may have activities.
3721 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003722 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003723 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003724 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003725 }
3726 }
3727
3728 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003729
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003730 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003731 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003732 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003733 r.configChangeFlags);
3734 } catch (Exception e) {
3735 // We can just ignore exceptions here... if the process
3736 // has crashed, our death notification will clean things
3737 // up.
3738 //Slog.w(TAG, "Exception thrown during finish", e);
3739 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003740 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003741 removedFromHistory = true;
3742 skipDestroy = true;
3743 }
3744 }
3745
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003746 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003747
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003748 // If the activity is finishing, we need to wait on removing it
3749 // from the list to give it a chance to do its cleanup. During
3750 // that time it may make calls back with its token so we need to
3751 // be able to find it on the list and so we don't want to remove
3752 // it from the list yet. Otherwise, we can just immediately put
3753 // it in the destroyed state since we are not removing it from the
3754 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003755 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003756 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003757 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003758 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003759 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003760 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3761 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003762 if (DEBUG_STATES) Slog.v(TAG_STATES,
3763 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003764 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003765 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003766 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003767 }
3768 } else {
3769 // remove this record from the history.
3770 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003771 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003772 removedFromHistory = true;
3773 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003774 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003775 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003776 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003777 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003778 }
3779 }
3780
3781 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003782
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003783 if (!mLRUActivities.remove(r) && hadApp) {
3784 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3785 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003786
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003787 return removedFromHistory;
3788 }
3789
Craig Mautner299f9602015-01-26 09:47:33 -08003790 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003791 final long origId = Binder.clearCallingIdentity();
3792 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003793 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003794 if (r != null) {
3795 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003796 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003797 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003798
Wale Ogunwale60454db2015-01-23 16:05:07 -08003799 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003800 if (r.state == ActivityState.DESTROYING) {
3801 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003802 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003803 }
3804 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003805 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003806 } finally {
3807 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003808 }
3809 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003810
Todd Kennedyaab56db2015-01-30 09:39:53 -08003811 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003812 if (hasVisibleBehindActivity() &&
3813 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08003814 if (r == topRunningActivityLocked()
3815 && getStackVisibilityLocked(null) == STACK_VISIBLE) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003816 // Don't release the top activity if it has requested to run behind the next
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08003817 // activity and the stack is currently visible.
Craig Mautner64ccb702014-10-01 09:38:40 -07003818 return;
3819 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003820 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003821 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003822 " thread=" + r.app.thread);
3823 if (r != null && r.app != null && r.app.thread != null) {
3824 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003825 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003826 } catch (RemoteException e) {
3827 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003828 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003829 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003830 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003831 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003832 }
3833 }
3834 }
3835
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003836 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003837 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3838 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003839 if (r != null) {
3840 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003841 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003842 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003843 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003844 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003845 }
3846
Jose Lima4b6c6692014-08-12 17:41:12 -07003847 boolean hasVisibleBehindActivity() {
3848 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003849 }
3850
Jose Lima4b6c6692014-08-12 17:41:12 -07003851 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003852 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003853 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003854 }
3855 }
3856
Jose Lima4b6c6692014-08-12 17:41:12 -07003857 ActivityRecord getVisibleBehindActivity() {
3858 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003859 }
3860
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003861 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3862 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003863 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003864 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3865 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003866 while (i > 0) {
3867 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003868 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003869 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003870 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003871 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003872 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003873 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003874 }
3875 }
3876 }
3877
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003878 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3879 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003880 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3881 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003882 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3883 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003884 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003885 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003886 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3887 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003888
3889 boolean hasVisibleActivities = false;
3890
3891 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003892 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003893 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3894 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003895 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3896 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3897 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3898 final ActivityRecord r = activities.get(activityNdx);
3899 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003900 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3901 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003902 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003903 if (r.visible) {
3904 hasVisibleActivities = true;
3905 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003906 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003907 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3908 // Don't currently have state for the activity, or
3909 // it is finishing -- always remove it.
3910 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003911 } else if (!r.visible && r.launchCount > 2 &&
3912 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003913 // We have launched this activity too many times since it was
3914 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003915 // (Note if the activity is visible, we don't remove the record.
3916 // We leave the dead window on the screen but the process will
3917 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003918 remove = true;
3919 } else {
3920 // The process may be gone, but the activity lives on!
3921 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003922 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003923 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003924 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3925 "Removing activity " + r + " from stack at " + i
3926 + ": haveState=" + r.haveState
3927 + " stateNotNeeded=" + r.stateNotNeeded
3928 + " finishing=" + r.finishing
3929 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003930 if (!r.finishing) {
3931 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3932 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3933 r.userId, System.identityHashCode(r),
3934 r.task.taskId, r.shortComponentName,
3935 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003936 if (r.state == ActivityState.RESUMED) {
3937 mService.updateUsageStats(r, false);
3938 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003939 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003940 } else {
3941 // We have the current state for this activity, so
3942 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003943 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3944 if (DEBUG_APP) Slog.v(TAG_APP,
3945 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003946 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003947 // Set nowVisible to previous visible state. If the app was visible while
3948 // it died, we leave the dead window on screen so it's basically visible.
3949 // This is needed when user later tap on the dead window, we need to stop
3950 // other apps when user transfers focus to the restarted activity.
3951 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003952 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003953 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003954 "App died, clearing saved state of " + r);
3955 r.icicle = null;
3956 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003957 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003958 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003959 if (remove) {
3960 removeActivityFromHistoryLocked(r, "appDied");
3961 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003962 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003963 }
3964 }
3965
3966 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003967 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003968
Chong Zhang280d3322015-11-03 17:27:26 -08003969 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003970 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003971 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003972 if (r != null && r.state != ActivityState.RESUMED) {
3973 r.updateOptionsLocked(options);
3974 } else {
3975 ActivityOptions.abort(options);
3976 }
3977 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003978 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003979 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003980
Craig Mautner21d24a22014-04-23 11:45:37 -07003981 void updateTaskMovement(TaskRecord task, boolean toFront) {
3982 if (task.isPersistable) {
3983 task.mLastTimeMoved = System.currentTimeMillis();
3984 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3985 // recently will be most negative, tasks sent to the bottom before that will be less
3986 // negative. Similarly for recent tasks moved to the top which will be most positive.
3987 if (!toFront) {
3988 task.mLastTimeMoved *= -1;
3989 }
3990 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003991 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003992 }
3993
Craig Mautner84984fa2014-06-19 11:19:20 -07003994 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003995 final int top = mTaskHistory.size() - 1;
3996 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3997 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003998 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003999 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4000 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07004001 mTaskHistory.remove(taskNdx);
4002 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004003 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07004004 return;
4005 }
4006 }
4007 }
4008
Chong Zhang280d3322015-11-03 17:27:26 -08004009 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004010 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004011 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004012
Craig Mautner11bf9a52013-02-19 14:08:51 -08004013 final int numTasks = mTaskHistory.size();
4014 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004015 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004016 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004017 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004018 ActivityOptions.abort(options);
4019 } else {
4020 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
4021 }
4022 return;
4023 }
4024
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004025 if (timeTracker != null) {
4026 // The caller wants a time tracker associated with this task.
4027 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4028 tr.mActivities.get(i).appTimeTracker = timeTracker;
4029 }
4030 }
4031
Craig Mautner11bf9a52013-02-19 14:08:51 -08004032 // Shift all activities with this task up to the top
4033 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004034 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004035
Chong Zhang45c25ce2015-08-10 22:18:26 -07004036 // Don't refocus if invisible to current user
4037 ActivityRecord top = tr.getTopActivity();
4038 if (!okToShowLocked(top)) {
4039 addRecentActivityLocked(top);
4040 ActivityOptions.abort(options);
4041 return;
4042 }
4043
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004044 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004045 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004046 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004047
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004048 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004049 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004050 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004051 if (r != null) {
4052 mNoAnimActivities.add(r);
4053 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004054 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004055 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08004056 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004057 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004058
Wale Ogunwaled046a012015-12-24 13:05:59 -08004059 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004060 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004061
4062 if (VALIDATE_TOKENS) {
4063 validateAppTokensLocked();
4064 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004065 }
4066
4067 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004068 * Worker method for rearranging history stack. Implements the function of moving all
4069 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004070 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004071 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004072 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4073 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004074 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004075 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004076 * @return Returns true if the move completed, false if not.
4077 */
Craig Mautner299f9602015-01-26 09:47:33 -08004078 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004079 final TaskRecord tr = taskForIdLocked(taskId);
4080 if (tr == null) {
4081 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4082 return false;
4083 }
4084
4085 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07004086 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004087
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004088 // If we have a watcher, preflight the move before committing to it. First check
4089 // for *other* available tasks, but if none are available, then try again allowing the
4090 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004091 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004092 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004093 if (next == null) {
4094 next = topRunningActivityLocked(null, 0);
4095 }
4096 if (next != null) {
4097 // ask watcher if this is allowed
4098 boolean moveOK = true;
4099 try {
4100 moveOK = mService.mController.activityResuming(next.packageName);
4101 } catch (RemoteException e) {
4102 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004103 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004104 }
4105 if (!moveOK) {
4106 return false;
4107 }
4108 }
4109 }
4110
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004111 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004112
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004113 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4114 // For the case where we are moving the home task back and there is an activity visible
4115 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4116 // activity to maintain order with what the user is seeing.
4117 final ActivityStack fullscreenStack =
4118 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4119 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4120 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4121 mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004122 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004123 return true;
4124 }
4125 }
4126
riddle_hsuc215a4f2014-12-27 12:10:45 +08004127 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004128
4129 // If true, we should resume the home activity next if the task we are moving to the
4130 // back is over the home stack. We force to false if the task we are moving to back
4131 // is the home task and we don't want it resumed after moving to the back.
4132 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4133 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004134 final TaskRecord nextTask = getNextTask(tr);
4135 if (nextTask != null) {
4136 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4137 } else {
4138 prevIsHome = true;
4139 }
4140 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004141 mTaskHistory.remove(tr);
4142 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004143 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004144
Craig Mautnerc8143c62013-09-03 12:15:57 -07004145 // There is an assumption that moving a task to the back moves it behind the home activity.
4146 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004147 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004148 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4149 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004150 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004151 break;
4152 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004153 if (taskNdx == 1) {
4154 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004155 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004156 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004157 }
4158
Craig Mautner299f9602015-01-26 09:47:33 -08004159 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004160 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004161
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004162 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004163 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004164 }
4165
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004166 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004167 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004168 if (!mService.mBooting && !mService.mBooted) {
4169 // Not ready yet!
4170 return false;
4171 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004172 final int taskToReturnTo = tr.getTaskToReturnTo();
4173 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004174 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004175 }
4176
Wale Ogunwaled046a012015-12-24 13:05:59 -08004177 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004178 return true;
4179 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004180
Craig Mautner8849a5e2013-04-02 16:41:03 -07004181 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004182 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004183 final Uri data = r.intent.getData();
4184 final String strData = data != null ? data.toSafeString() : null;
4185
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004186 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004187 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004188 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004189 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004190 }
4191
4192 /**
4193 * Make sure the given activity matches the current configuration. Returns
4194 * false if the activity had to be destroyed. Returns true if the
4195 * configuration is the same, or the activity will remain running as-is
4196 * for whatever reason. Ensures the HistoryRecord is updated with the
4197 * correct configuration and all other bookkeeping is handled.
4198 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004199 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4200 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004201 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004202 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004203 "Skipping config check (will change): " + r);
4204 return true;
4205 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004206
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004207 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004208 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004209
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004210 // Short circuit: if the two configurations are the exact same
4211 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004212 final Configuration newConfig = mService.mConfiguration;
4213 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004214 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004215 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004216 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004217 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004218 "Configuration unchanged in " + r);
4219 return true;
4220 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004221
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004222 // We don't worry about activities that are finishing.
4223 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004224 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004225 "Configuration doesn't matter in finishing " + r);
4226 r.stopFreezingScreenLocked(false);
4227 return true;
4228 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004229
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004230 // Okay we now are going to make this activity have the new config.
4231 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004232 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004233 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004234 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004235 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004236
Filip Gruszczynski23493322015-07-29 17:02:59 -07004237 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004238 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004239 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004240 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004241 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004242 // There are no significant differences, so we won't relaunch but should still deliver
4243 // the new configuration to the client process.
4244 r.scheduleConfigurationChanged(taskConfig, false);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004245 return true;
4246 }
4247
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004248 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4249 "Configuration changes for " + r + " ; taskChanges="
4250 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4251 + Configuration.configurationDiffToString(changes));
4252
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004253 // If the activity isn't currently running, just leave the new
4254 // configuration and it will pick that up next time it starts.
4255 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004256 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004257 "Configuration doesn't matter not running " + r);
4258 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004259 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004260 return true;
4261 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004262
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004263 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004264 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4265 "Checking to restart " + r.info.name + ": changed=0x"
4266 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004267 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4268 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004269
Dianne Hackborne6676352011-06-01 16:51:20 -07004270 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004271 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4272 r.configChangeFlags |= changes;
4273 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004274 r.forceNewConfig = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004275 preserveWindow &= isResizeOnlyChange(changes);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004276 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004277 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004278 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004279 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004280 } else if (r.state == ActivityState.PAUSING) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004281 // A little annoying: we are waiting for this activity to finish pausing. Let's not
4282 // do anything now, but just flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004283 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004284 "Config is skipping already pausing " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004285 r.deferRelaunchUntilPaused = true;
4286 r.preserveWindowOnDeferredRelaunch = preserveWindow;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004287 return true;
4288 } else if (r.state == ActivityState.RESUMED) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004289 // Try to optimize this case: the configuration is changing and we need to restart
4290 // the top, resumed activity. Instead of doing the normal handshaking, just say
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004291 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004292 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004293 "Config is relaunching resumed " + r);
Chong Zhang2b79af12016-02-10 18:47:06 -08004294
4295 if (DEBUG_STATES && !r.visible) {
4296 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + r
4297 + " called by " + Debug.getCallers(4));
4298 }
4299
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004300 relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004301 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004302 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004303 "Config is relaunching non-resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004304 relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004305 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004306
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004307 // All done... tell the caller we weren't able to keep this activity around.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004308 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004309 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004310
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004311 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004312 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004313 // changes is always sent to all processes when they happen so it can just use whatever
4314 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004315 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004316 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004317
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004318 return true;
4319 }
4320
Filip Gruszczynski23493322015-07-29 17:02:59 -07004321 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4322 Configuration oldTaskOverride) {
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08004323
4324 // If we went from full-screen to non-full-screen, make sure to use the correct
4325 // configuration task diff, so the diff stays as small as possible.
4326 if (Configuration.EMPTY.equals(oldTaskOverride)
4327 && !Configuration.EMPTY.equals(taskConfig)) {
4328 oldTaskOverride = record.task.extractOverrideConfig(record.configuration);
4329 }
4330
Jorim Jaggi1b025a62016-02-03 19:27:49 -08004331 // Conversely, do the same when going the other direction.
4332 if (Configuration.EMPTY.equals(taskConfig)
4333 && !Configuration.EMPTY.equals(oldTaskOverride)) {
4334 taskConfig = record.task.extractOverrideConfig(record.configuration);
4335 }
4336
Filip Gruszczynski23493322015-07-29 17:02:59 -07004337 // Determine what has changed. May be nothing, if this is a config
4338 // that has come back from the app after going idle. In that case
4339 // we just want to leave the official config object now in the
4340 // activity and do nothing else.
4341 int taskChanges = oldTaskOverride.diff(taskConfig);
4342 // We don't want to use size changes if they don't cross boundaries that are important to
4343 // the app.
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004344 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004345 final boolean crosses = record.crossesHorizontalSizeThreshold(
4346 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4347 || record.crossesVerticalSizeThreshold(
4348 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4349 if (!crosses) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004350 taskChanges &= ~CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004351 }
4352 }
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004353 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004354 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4355 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004356 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004357 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004358 }
4359 }
4360 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4361 }
4362
4363 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4364 Configuration oldTaskOverride, int taskChanges) {
4365 if (taskChanges == 0) {
4366 // {@link Configuration#diff} doesn't catch changes from unset values.
4367 // Check for changes we care about.
4368 if (oldTaskOverride.orientation != taskConfig.orientation) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004369 taskChanges |= CONFIG_ORIENTATION;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004370 }
4371 // We want to explicitly track situations where the size configuration goes from
4372 // undefined to defined. We don't care about crossing the threshold in that case,
4373 // because there is no threshold.
4374 final int oldHeight = oldTaskOverride.screenHeightDp;
4375 final int newHeight = taskConfig.screenHeightDp;
4376 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4377 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4378 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004379 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004380 }
4381 final int oldWidth = oldTaskOverride.screenWidthDp;
4382 final int newWidth = taskConfig.screenWidthDp;
4383 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4384 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4385 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004386 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004387 }
4388 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4389 final int newSmallest = taskConfig.smallestScreenWidthDp;
4390 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4391 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4392 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004393 taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4394 }
4395 final int oldLayout = oldTaskOverride.screenLayout;
4396 final int newLayout = taskConfig.screenLayout;
4397 if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4398 || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4399 taskChanges |= CONFIG_SCREEN_LAYOUT;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004400 }
4401 }
4402 return taskChanges;
4403 }
4404
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004405 private static boolean isResizeOnlyChange(int change) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004406 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4407 | CONFIG_SCREEN_LAYOUT)) == 0;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004408 }
4409
Wale Ogunwale83301a92015-09-24 15:54:08 -07004410 private void relaunchActivityLocked(
4411 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4412 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004413 r.configChangeFlags = 0;
Wale Ogunwale83301a92015-09-24 15:54:08 -07004414 return;
4415 }
4416
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004417 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004418 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004419 if (andResume) {
4420 results = r.results;
4421 newIntents = r.newIntents;
4422 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004423 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4424 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004425 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004426 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004427 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004428 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004429
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004430 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004431
Craig Mautner34b73df2014-01-12 21:11:08 -08004432 mStackSupervisor.removeChildActivityContainers(r);
4433
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004434 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004435 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08004436 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r
4437 + " callers=" + Debug.getCallers(6));
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004438 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004439 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4440 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004441 new Configuration(r.task.mOverrideConfig), preserveWindow);
Jorim Jaggife89d122015-12-22 16:28:44 +01004442 mStackSupervisor.activityRelaunchingLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004443 // Note: don't need to call pauseIfSleepingLocked() here, because
4444 // the caller will only pass in 'andResume' if this activity is
4445 // currently resumed, which implies we aren't sleeping.
4446 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004447 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004448 }
4449
4450 if (andResume) {
Chong Zhang2b79af12016-02-10 18:47:06 -08004451 if (DEBUG_STATES) {
4452 Slog.d(TAG_STATES, "Resumed after relaunch " + r);
4453 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004454 r.state = ActivityState.RESUMED;
Chong Zhang2b79af12016-02-10 18:47:06 -08004455 // Relaunch-resume could happen either when the app is already in the front,
4456 // or while it's being brought to front. In the latter case, it's marked RESUMED
4457 // but not yet visible (or stopped). We need to complete the resume here as the
4458 // code in resumeTopActivityInnerLocked to complete the resume might be skipped.
4459 if (!r.visible || r.stopped) {
4460 mWindowManager.setAppVisibility(r.appToken, true);
4461 completeResumeLocked(r);
4462 } else {
4463 r.results = null;
4464 r.newIntents = null;
4465 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004466 } else {
4467 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4468 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004469 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004470
4471 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004472 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004473
4474 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004475 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4476 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4477 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4478 final ActivityRecord r = activities.get(activityNdx);
4479 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004480 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004481 }
4482 if (r.fullscreen && !r.finishing) {
4483 return false;
4484 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004485 }
4486 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004487 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004488 if (r == null) {
4489 return false;
4490 }
4491 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4492 + " would have returned true for r=" + r);
4493 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004494 }
4495
4496 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004497 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4498 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4499 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4500 final ActivityRecord r = activities.get(activityNdx);
4501 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004502 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004503 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004504 }
4505 }
4506 }
4507
Wale Ogunwale540e1232015-05-01 15:35:39 -07004508 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4509 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004510 boolean didSomething = false;
4511 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004512 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004513 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4514 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4515 int numActivities = activities.size();
4516 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4517 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004518 final boolean sameComponent =
4519 (r.packageName.equals(packageName) && (filterByClasses == null
4520 || filterByClasses.contains(r.realActivity.getClassName())))
4521 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004522 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004523 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004524 && (r.app == null || evenPersistent || !r.app.persistent)) {
4525 if (!doit) {
4526 if (r.finishing) {
4527 // If this activity is just finishing, then it is not
4528 // interesting as far as something to stop.
4529 continue;
4530 }
4531 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004532 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004533 if (r.isHomeActivity()) {
4534 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4535 Slog.i(TAG, "Skip force-stop again " + r);
4536 continue;
4537 } else {
4538 homeActivity = r.realActivity;
4539 }
4540 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004541 didSomething = true;
4542 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004543 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004544 if (r.app != null) {
4545 r.app.removed = true;
4546 }
4547 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004548 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004549 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004550 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4551 true)) {
4552 // r has been deleted from mActivities, accommodate.
4553 --numActivities;
4554 --activityNdx;
4555 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004556 }
4557 }
4558 }
4559 return didSomething;
4560 }
4561
Dianne Hackborn09233282014-04-30 11:33:59 -07004562 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004563 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4564 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004565 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004566 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004567 if (task.getTopActivity() == null) {
4568 continue;
4569 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004570 ActivityRecord r = null;
4571 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004572 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004573 int numActivities = 0;
4574 int numRunning = 0;
4575 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004576 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004577 continue;
4578 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004579 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004580 tmp = activities.get(activityNdx);
4581 if (tmp.finishing) {
4582 continue;
4583 }
4584 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004585
Craig Mautneraab647e2013-02-28 16:31:36 -08004586 // Initialize state for next task if needed.
4587 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4588 top = r;
4589 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004590 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004591
4592 // Add 'r' into the current task.
4593 numActivities++;
4594 if (r.app != null && r.app.thread != null) {
4595 numRunning++;
4596 }
4597
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004598 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004599 TAG, r.intent.getComponent().flattenToShortString()
4600 + ": task=" + r.task);
4601 }
4602
4603 RunningTaskInfo ci = new RunningTaskInfo();
4604 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004605 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004606 ci.baseActivity = r.intent.getComponent();
4607 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004608 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004609 if (focusedStack && topTask) {
4610 // Give the latest time to ensure foreground task can be sorted
4611 // at the first, because lastActiveTime of creating task is 0.
4612 ci.lastActiveTime = System.currentTimeMillis();
4613 topTask = false;
4614 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004615
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004616 if (top.task != null) {
4617 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004618 }
4619 ci.numActivities = numActivities;
4620 ci.numRunning = numRunning;
Wale Ogunwale21b60582016-01-27 12:34:16 -08004621 ci.isDockable = task.canGoInDockedStack();
Craig Mautneraab647e2013-02-28 16:31:36 -08004622 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004623 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004624 }
4625
4626 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004627 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004628 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004629 if (top >= 0) {
4630 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4631 int activityTop = activities.size() - 1;
4632 if (activityTop > 0) {
4633 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4634 "unhandled-back", true);
4635 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004636 }
4637 }
4638
Craig Mautner6b74cb52013-09-27 17:02:21 -07004639 /**
4640 * Reset local parameters because an app's activity died.
4641 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004642 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004643 */
4644 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004645 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004646 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004647 "App died while pausing: " + mPausingActivity);
4648 mPausingActivity = null;
4649 }
4650 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4651 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004652 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004653 }
4654
Craig Mautner19091252013-10-05 00:03:53 -07004655 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004656 }
4657
Craig Mautnercae015f2013-02-08 14:31:27 -08004658 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004659 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4660 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4661 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4662 final ActivityRecord r = activities.get(activityNdx);
4663 if (r.app == app) {
4664 Slog.w(TAG, " Force finishing activity "
4665 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004666 // Force the destroy to skip right to removal.
4667 r.app = null;
4668 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004669 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004670 }
4671 }
4672 }
4673
Dianne Hackborn390517b2013-05-30 15:03:32 -07004674 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004675 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004676 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004677 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4678 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004679 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4680 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004681 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004682 " Task id #" + task.taskId + "\n" +
4683 " mFullscreen=" + task.mFullscreen + "\n" +
4684 " mBounds=" + task.mBounds + "\n" +
4685 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004686 if (printed) {
4687 header = null;
4688 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004689 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004690 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004691 }
4692
4693 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4694 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4695
4696 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004697 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4698 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004699 }
4700 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004701 final int top = mTaskHistory.size() - 1;
4702 if (top >= 0) {
4703 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4704 int listTop = list.size() - 1;
4705 if (listTop >= 0) {
4706 activities.add(list.get(listTop));
4707 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004708 }
4709 } else {
4710 ItemMatcher matcher = new ItemMatcher();
4711 matcher.build(name);
4712
Craig Mautneraab647e2013-02-28 16:31:36 -08004713 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4714 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4715 if (matcher.match(r1, r1.intent.getComponent())) {
4716 activities.add(r1);
4717 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004718 }
4719 }
4720 }
4721
4722 return activities;
4723 }
4724
4725 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004726 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004727
4728 // All activities that came from the package must be
4729 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004730 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4731 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4732 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4733 final ActivityRecord a = activities.get(activityNdx);
4734 if (a.info.packageName.equals(packageName)) {
4735 a.forceNewConfig = true;
4736 if (starting != null && a == starting && a.visible) {
4737 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004738 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08004739 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004740 }
4741 }
4742 }
4743
4744 return starting;
4745 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004746
Craig Mautner299f9602015-01-26 09:47:33 -08004747 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004748 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004749 }
4750
Wale Ogunwale000957c2015-04-03 08:19:12 -07004751 /**
4752 * Removes the input task from this stack.
4753 * @param task to remove.
4754 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004755 * @param moving task to another stack. In the case we are moving we don't want to perform
4756 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004757 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004758 void removeTask(TaskRecord task, String reason, boolean moving) {
4759 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004760 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004761 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004762 if (!StackId.persistTaskBounds(mStackId)) {
4763 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4764 // default configuration the next time it launches.
4765 task.updateOverrideConfiguration(null);
4766 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004767 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004768
Craig Mautner04a0ea62014-01-13 12:51:26 -08004769 final ActivityRecord r = mResumedActivity;
4770 if (r != null && r.task == task) {
4771 mResumedActivity = null;
4772 }
4773
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004774 final int taskNdx = mTaskHistory.indexOf(task);
4775 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004776 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4777 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4778 if (!nextTask.isOverHomeStack()) {
4779 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4780 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004781 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004782 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004783 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004784
Wale Ogunwale040b4702015-08-06 18:10:50 -07004785 if (!moving && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004786 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07004787 final boolean isVoiceSession = task.voiceSession != null;
4788 if (isVoiceSession) {
4789 try {
4790 task.voiceSession.taskFinished(task.intent, task.taskId);
4791 } catch (RemoteException e) {
4792 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004793 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004794 if (task.autoRemoveFromRecents() || isVoiceSession) {
4795 // Task creator asked to remove this when done, or this task was a voice
4796 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004797 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004798 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004799 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004800 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004801
4802 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004803 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004804 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004805 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004806 String myReason = reason + " leftTaskHistoryEmpty";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08004807 if (mFullscreen || !adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004808 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004809 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004810 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004811 if (mStacks != null) {
4812 mStacks.remove(this);
4813 mStacks.add(0, this);
4814 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004815 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004816 mActivityContainer.onTaskListEmptyLocked();
4817 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004818 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004819
4820 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004821 }
4822
Dianne Hackborn91097de2014-04-04 18:02:06 -07004823 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4824 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4825 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004826 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4827 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004828 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08004829 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08004830 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004831 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.isResizeable()
Chong Zhang75b37202015-12-04 14:16:36 -08004832 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004833 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004834 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004835 return task;
4836 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004837
Wale Ogunwale935e5022015-11-10 12:36:10 -08004838 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4839 if (mTaskPositioner == null) {
4840 return false;
4841 }
4842 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4843 return true;
4844 }
4845
Craig Mautnerc00204b2013-03-05 15:02:14 -08004846 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004847 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004848 }
4849
Wale Ogunwale5f986092015-12-04 15:35:38 -08004850 void addTask(final TaskRecord task, final boolean toTop, String reason) {
4851 final ActivityStack prevStack = preAddTask(task, reason);
4852
Craig Mautnerc00204b2013-03-05 15:02:14 -08004853 task.stack = this;
4854 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004855 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004856 } else {
4857 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004858 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004859 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08004860 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004861 }
4862
Wale Ogunwale5f986092015-12-04 15:35:38 -08004863 void positionTask(final TaskRecord task, int position) {
4864 final ActivityStack prevStack = preAddTask(task, "positionTask");
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004865 task.stack = this;
4866 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004867 postAddTask(task, prevStack);
4868 }
4869
4870 private ActivityStack preAddTask(TaskRecord task, String reason) {
4871 final ActivityStack prevStack = task.stack;
4872 if (prevStack != null && prevStack != this) {
4873 prevStack.removeTask(task, reason, MOVING);
4874 }
4875 return prevStack;
4876 }
4877
4878 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
4879 if (prevStack != null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004880 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004881 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004882 try {
4883 task.voiceSession.taskStarted(task.intent, task.taskId);
4884 } catch (RemoteException e) {
4885 }
4886 }
4887 }
4888
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004889 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004890 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004891 // TODO: VI deal with activity
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004892 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4893 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6cae7652015-12-26 07:36:26 -08004894 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
4895 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004896 task.mResizeMode, r.isAlwaysFocusable(), task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004897 r.taskConfigOverride = task.mOverrideConfig;
4898 }
4899
Wale Ogunwaled046a012015-12-24 13:05:59 -08004900 void moveToFrontAndResumeStateIfNeeded(
4901 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
4902 if (!moveToFront) {
4903 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07004904 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004905
4906 // If the activity owns the last resumed activity, transfer that together,
4907 // so that we don't resume the same activity again in the new stack.
4908 // Apps may depend on onResume()/onPause() being called in pairs.
4909 if (setResume) {
4910 mResumedActivity = r;
4911 }
4912 // Move the stack in which we are placing the activity to the front. The call will also
4913 // make sure the activity focus is set.
4914 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004915 }
4916
4917 /**
4918 * Moves the input activity from its current stack to this one.
4919 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4920 * created on this stack which the activity is added to.
4921 * */
4922 void moveActivityToStack(ActivityRecord r) {
4923 final ActivityStack prevStack = r.task.stack;
4924 if (prevStack.mStackId == mStackId) {
4925 // You are already in the right stack silly...
4926 return;
4927 }
4928
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004929 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004930 && (mStackSupervisor.topRunningActivityLocked() == r);
4931 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4932
4933 final TaskRecord task = createTaskRecord(
Suprabh Shukla09a88f52015-12-02 14:36:31 -08004934 mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
4935 r.info, r.intent, null, null, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004936 r.setTask(task, null);
4937 task.addActivityToTop(r);
4938 setAppTask(r, task);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004939 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004940 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4941 if (wasResumed) {
4942 prevStack.mResumedActivity = null;
4943 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07004944 }
4945
Wale Ogunwale706ed792015-08-02 10:29:44 -07004946 private void setAppTask(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004947 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004948 mWindowManager.setAppTask(r.appToken, task.taskId, mStackId, bounds, task.mOverrideConfig,
4949 task.mResizeMode, task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004950 r.taskConfigOverride = task.mOverrideConfig;
4951 }
4952
Craig Mautnerc00204b2013-03-05 15:02:14 -08004953 public int getStackId() {
4954 return mStackId;
4955 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004956
4957 @Override
4958 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004959 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4960 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004961 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004962
Craig Mautner15df08a2015-04-01 12:17:18 -07004963 void onLockTaskPackagesUpdatedLocked() {
4964 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4965 mTaskHistory.get(taskNdx).setLockTaskAuth();
4966 }
4967 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004968}