blob: d1e1d27af1f6957be76dcdf9551f37858bb23b56 [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;
69import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070070import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080071import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080072import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070073import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070074import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080075import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070076import android.os.Handler;
77import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090078import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070080import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070081import android.os.RemoteException;
82import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080083import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070084import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070085import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070086import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070087import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070088import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070089
Craig Mautnercae015f2013-02-08 14:31:27 -080090import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080091import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070092import java.lang.ref.WeakReference;
93import java.util.ArrayList;
94import java.util.Iterator;
95import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080096import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070097import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070098
99/**
100 * State and management of a single stack of activities.
101 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700102final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800103
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800104 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700105 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
106 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800107 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700108 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700109 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700110 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700111 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700112 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700113 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
114 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700115 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700116 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700117 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
118 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
119 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
120 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
121 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700122
123 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800124
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700125 // Ticks during which we check progress while waiting for an app to launch.
126 static final int LAUNCH_TICK = 500;
127
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700128 // How long we wait until giving up on the last activity to pause. This
129 // is short because it directly impacts the responsiveness of starting the
130 // next activity.
131 static final int PAUSE_TIMEOUT = 500;
132
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700133 // How long we wait for the activity to tell us it has stopped before
134 // giving up. This is a good amount of time because we really need this
135 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700136 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700137
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700138 // How long we wait until giving up on an activity telling us it has
139 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700140 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800141
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700142 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800143 // disabled.
144 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800145
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700146 // How long between activity launches that we consider safe to not warn
147 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700148 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800149
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700150 // Set to false to disable the preview that is shown while a new activity
151 // is being started.
152 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800153
Craig Mautner5eda9b32013-07-02 11:58:16 -0700154 // How long to wait for all background Activities to redraw following a call to
155 // convertToTranslucent().
156 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
157
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800158 // How many activities have to be scheduled to stop to force a stop pass.
159 private static final int MAX_STOPPING_TO_FORCE = 3;
160
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700161 enum ActivityState {
162 INITIALIZING,
163 RESUMED,
164 PAUSING,
165 PAUSED,
166 STOPPING,
167 STOPPED,
168 FINISHING,
169 DESTROYING,
170 DESTROYED
171 }
172
Filip Gruszczynski0e381e22016-01-14 16:31:33 -0800173 // Stack is not considered visible.
174 static final int STACK_INVISIBLE = 0;
175 // Stack is considered visible
176 static final int STACK_VISIBLE = 1;
177 // Stack is considered visible, but only becuase it has activity that is visible behind other
178 // activities and there is a specific combination of stacks.
179 static final int STACK_VISIBLE_ACTIVITY_BEHIND = 2;
180
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700181 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700182 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800183 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800184
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700185 /**
186 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800187 * running) activities. It contains #TaskRecord objects.
188 */
Todd Kennedy39bfee52016-02-24 10:28:21 -0800189 private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800190
191 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800192 * Used for validating app tokens with window manager.
193 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800194 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800195
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700196 /**
197 * List of running activities, sorted by recent usage.
198 * The first entry in the list is the least recently used.
199 * It contains HistoryRecord objects.
200 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800201 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700202
203 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700204 * Animations that for the current transition have requested not to
205 * be considered for the transition animation.
206 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800207 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700208
209 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700210 * When we are in the process of pausing an activity, before starting the
211 * next one, this variable holds the activity that is currently being paused.
212 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800213 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700214
215 /**
216 * This is the last activity that we put into the paused state. This is
217 * used to determine if we need to do an activity transition while sleeping,
218 * when we normally hold the top activity paused.
219 */
220 ActivityRecord mLastPausedActivity = null;
221
222 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700223 * Activities that specify No History must be removed once the user navigates away from them.
224 * If the device goes to sleep with such an activity in the paused state then we save it here
225 * and finish it later if another activity replaces it on wakeup.
226 */
227 ActivityRecord mLastNoHistoryActivity = null;
228
229 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700230 * Current activity that is resumed, or null if there is none.
231 */
232 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800233
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700234 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700235 * This is the last activity that has been started. It is only used to
236 * identify when multiple activities are started at once so that the user
237 * can be warned they may not be in the activity they think they are.
238 */
239 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800240
Craig Mautner5eda9b32013-07-02 11:58:16 -0700241 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
242 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
243 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
244 // Activity in mTranslucentActivityWaiting is notified via
245 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
246 // background activity being drawn then the same call will be made with a true value.
247 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700248 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700249
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700250 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700251 * Set when we know we are going to be calling updateConfiguration()
252 * soon, so want to skip intermediate config checks.
253 */
254 boolean mConfigWillChange;
255
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700256 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800257 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700258 // Current bounds of the stack or null if fullscreen.
259 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800260
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700261 long mLaunchStartTime = 0;
262 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800263
Craig Mautner858d8a62013-04-23 17:08:34 -0700264 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700265
Craig Mautnerc00204b2013-03-05 15:02:14 -0800266 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800267 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800268 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
269 ArrayList<ActivityStack> mStacks;
270 /** The attached Display's unique identifier, or -1 if detached */
271 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800272
Craig Mautner27084302013-03-25 08:05:25 -0700273 /** Run all ActivityStacks through this */
274 final ActivityStackSupervisor mStackSupervisor;
275
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700276 private final LaunchingTaskPositioner mTaskPositioner;
277
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700278 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700279 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
280 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
281 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
282 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700283 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700284 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700285 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700286
287 static class ScheduleDestroyArgs {
288 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700289 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700290 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700291 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700292 mReason = reason;
293 }
294 }
295
Zoran Marcetaf958b322012-08-09 20:27:12 +0900296 final Handler mHandler;
297
298 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800299
Craig Mautnerc8143c62013-09-03 12:15:57 -0700300 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900301 super(looper);
302 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700303
Zoran Marcetaf958b322012-08-09 20:27:12 +0900304 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700305 public void handleMessage(Message msg) {
306 switch (msg.what) {
307 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800308 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700309 // We don't at this point know if the activity is fullscreen,
310 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800311 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700312 synchronized (mService) {
313 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700314 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700315 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700316 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800317 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700318 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700319 case LAUNCH_TICK_MSG: {
320 ActivityRecord r = (ActivityRecord)msg.obj;
321 synchronized (mService) {
322 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700323 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700324 }
325 }
326 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700327 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800328 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700329 // We don't at this point know if the activity is fullscreen,
330 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800331 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800332 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800333 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800334 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700335 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700336 case STOP_TIMEOUT_MSG: {
337 ActivityRecord r = (ActivityRecord)msg.obj;
338 // We don't at this point know if the activity is fullscreen,
339 // so we need to be conservative and assume it isn't.
340 Slog.w(TAG, "Activity stop timeout for " + r);
341 synchronized (mService) {
342 if (r.isInHistory()) {
343 activityStoppedLocked(r, null, null, null);
344 }
345 }
346 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700347 case DESTROY_ACTIVITIES_MSG: {
348 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
349 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700350 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700351 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700352 } break;
353 case TRANSLUCENT_TIMEOUT_MSG: {
354 synchronized (mService) {
355 notifyActivityDrawnLocked(null);
356 }
357 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700358 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700359 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700360 final ActivityRecord r = getVisibleBehindActivity();
361 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700362 if (r != null) {
363 mService.killAppAtUsersRequest(r.app, null);
364 }
365 }
366 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700367 }
368 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800369 }
370
Craig Mautner34b73df2014-01-12 21:11:08 -0800371 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800372 int count = 0;
373 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
374 count += mTaskHistory.get(taskNdx).mActivities.size();
375 }
376 return count;
377 }
378
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800379 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
380 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800381 mActivityContainer = activityContainer;
382 mStackSupervisor = activityContainer.getOuter();
383 mService = mStackSupervisor.mService;
384 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
385 mWindowManager = mService.mWindowManager;
386 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700387 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800388 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700389 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
390 ? new LaunchingTaskPositioner() : null;
391 }
392
393 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
394 mDisplayId = activityDisplay.mDisplayId;
395 mStacks = activityDisplay.mStacks;
396 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
397 mFullscreen = mBounds == null;
398 if (mTaskPositioner != null) {
399 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
400 mTaskPositioner.configure(mBounds);
401 }
Wale Ogunwale961f4852016-02-01 20:25:54 -0800402
403 if (mStackId == DOCKED_STACK_ID) {
404 // If we created a docked stack we want to resize it so it resizes all other stacks
405 // in the system.
406 mStackSupervisor.resizeDockedStackLocked(
407 mBounds, null, null, null, null, PRESERVE_WINDOWS);
408 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700409 }
410
411 void detachDisplay() {
412 mDisplayId = Display.INVALID_DISPLAY;
413 mStacks = null;
414 if (mTaskPositioner != null) {
415 mTaskPositioner.reset();
416 }
417 mWindowManager.detachStack(mStackId);
418 }
419
Winsonc809cbb2015-11-02 12:06:15 -0800420 public void getDisplaySize(Point out) {
421 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
422 }
423
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700424 void setBounds(Rect bounds) {
425 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700426 if (mTaskPositioner != null) {
427 mTaskPositioner.configure(bounds);
428 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700429 }
Craig Mautner5962b122012-10-05 14:45:52 -0700430
Amith Yamasani734983f2014-03-04 16:48:05 -0800431 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700432 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700433 }
434
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700435 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800436 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700437 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700438 if (r != null) {
439 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800440 }
441 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700442 return null;
443 }
444
445 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800446 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
447 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800448 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800449 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
450 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800451 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800452 return r;
453 }
454 }
455 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700456 return null;
457 }
458
459 /**
460 * This is a simplified version of topRunningActivityLocked that provides a number of
461 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800462 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700463 * @param token If non-null, any history records matching this token will be skipped.
464 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800465 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700466 * @return Returns the HistoryRecord of the next activity on the stack.
467 */
468 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800469 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
470 TaskRecord task = mTaskHistory.get(taskNdx);
471 if (task.taskId == taskId) {
472 continue;
473 }
474 ArrayList<ActivityRecord> activities = task.mActivities;
475 for (int i = activities.size() - 1; i >= 0; --i) {
476 final ActivityRecord r = activities.get(i);
477 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800478 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800479 return r;
480 }
481 }
482 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700483 return null;
484 }
485
Craig Mautner8849a5e2013-04-02 16:41:03 -0700486 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700487 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
488 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700489 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
490 final ActivityRecord r = activities.get(activityNdx);
491 if (!r.finishing) {
492 return r;
493 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700494 }
495 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700496 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700497 }
498
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700499 final TaskRecord topTask() {
500 final int size = mTaskHistory.size();
501 if (size > 0) {
502 return mTaskHistory.get(size - 1);
503 }
504 return null;
505 }
506
Craig Mautnerd2328952013-03-05 12:46:26 -0800507 TaskRecord taskForIdLocked(int id) {
508 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
509 final TaskRecord task = mTaskHistory.get(taskNdx);
510 if (task.taskId == id) {
511 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800512 }
513 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700514 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700515 }
516
Craig Mautnerd2328952013-03-05 12:46:26 -0800517 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700518 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800519 return isInStackLocked(r);
520 }
521
522 ActivityRecord isInStackLocked(ActivityRecord r) {
523 if (r == null) {
524 return null;
525 }
526 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700527 if (task != null && task.stack != null
528 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800529 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800530 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800531 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800532 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800533 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800534 }
535
Craig Mautner2420ead2013-04-01 17:13:20 -0700536 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700537 final boolean hadit = mLRUActivities.remove(r);
538 mLRUActivities.add(r);
539 return hadit;
540 }
541
Craig Mautnerde4ef022013-04-07 19:01:33 -0700542 final boolean isHomeStack() {
543 return mStackId == HOME_STACK_ID;
544 }
545
Winson Chung0583d3d2015-12-18 10:03:12 -0500546 final boolean isDockedStack() {
547 return mStackId == DOCKED_STACK_ID;
548 }
549
550 final boolean isPinnedStack() {
551 return mStackId == PINNED_STACK_ID;
552 }
553
Craig Mautnere0a38842013-12-16 16:14:02 -0800554 final boolean isOnHomeDisplay() {
555 return isAttached() &&
556 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
557 }
558
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700559 void moveToFront(String reason) {
560 moveToFront(reason, null);
561 }
562
563 /**
564 * @param reason The reason for moving the stack to the front.
565 * @param task If non-null, the task will be moved to the top of the stack.
566 * */
567 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700568 if (!isAttached()) {
569 return;
570 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700571
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700572 mStacks.remove(this);
573 int addIndex = mStacks.size();
574
575 if (addIndex > 0) {
576 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800577 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
578 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700579 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800580 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700581 }
582
583 mStacks.add(addIndex, this);
584
585 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
586 if (isOnHomeDisplay()) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800587 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700588 }
589 if (task != null) {
590 insertTaskAtTop(task, null);
591 } else {
592 task = topTask();
593 }
594 if (task != null) {
595 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800596 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800597 }
598
Wale Ogunwaled046a012015-12-24 13:05:59 -0800599 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800600 if (StackId.canReceiveKeys(mStackId)) {
601 return true;
602 }
603 // The stack isn't focusable. See if its top activity is focusable to force focus on the
604 // stack.
605 final ActivityRecord r = topRunningActivityLocked();
606 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800607 }
608
Craig Mautnere0a38842013-12-16 16:14:02 -0800609 final boolean isAttached() {
610 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800611 }
612
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700613 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800614 * Returns the top activity in any existing task matching the given Intent in the input result.
615 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700616 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800617 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700618 Intent intent = target.intent;
619 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700620 ComponentName cls = intent.getComponent();
621 if (info.targetActivity != null) {
622 cls = new ComponentName(info.packageName, info.targetActivity);
623 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700624 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700625 boolean isDocument = intent != null & intent.isDocument();
626 // If documentData is non-null then it must match the existing task data.
627 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800628
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700629 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800630 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
631 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700632 if (task.voiceSession != null) {
633 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700634 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700635 continue;
636 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700637 if (task.userId != userId) {
638 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700639 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700640 continue;
641 }
Craig Mautner000f0022013-02-26 15:04:29 -0800642 final ActivityRecord r = task.getTopActivity();
643 if (r == null || r.finishing || r.userId != userId ||
644 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700645 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800646 continue;
647 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700648 if (r.mActivityType != target.mActivityType) {
649 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
650 continue;
651 }
Craig Mautner000f0022013-02-26 15:04:29 -0800652
Craig Mautnerd00f4742014-03-12 14:17:26 -0700653 final Intent taskIntent = task.intent;
654 final Intent affinityIntent = task.affinityIntent;
655 final boolean taskIsDocument;
656 final Uri taskDocumentData;
657 if (taskIntent != null && taskIntent.isDocument()) {
658 taskIsDocument = true;
659 taskDocumentData = taskIntent.getData();
660 } else if (affinityIntent != null && affinityIntent.isDocument()) {
661 taskIsDocument = true;
662 taskDocumentData = affinityIntent.getData();
663 } else {
664 taskIsDocument = false;
665 taskDocumentData = null;
666 }
667
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700668 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700669 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700670 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700671 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Wale Ogunwale39381972015-12-17 17:15:29 -0800672 if (!isDocument && !taskIsDocument
673 && result.r == null && task.canMatchRootAffinity()) {
Dianne Hackborn79228822014-09-16 11:11:23 -0700674 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwale39381972015-12-17 17:15:29 -0800675 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
676 // It is possible for multiple tasks to have the same root affinity especially
677 // if they are in separate stacks. We save off this candidate, but keep looking
678 // to see if there is a better candidate.
679 result.r = r;
680 result.matchedByRootAffinity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700681 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700682 } else if (taskIntent != null && taskIntent.getComponent() != null &&
683 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700684 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700685 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800686 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700687 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
688 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800689 result.r = r;
690 result.matchedByRootAffinity = false;
691 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700692 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
693 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700694 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700695 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800696 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700697 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
698 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800699 result.r = r;
700 result.matchedByRootAffinity = false;
701 break;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700702 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700703 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700704 }
705
706 /**
707 * Returns the first activity (starting from the top of the stack) that
708 * is the same as the given activity. Returns null if no such activity
709 * is found.
710 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700711 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700712 ComponentName cls = intent.getComponent();
713 if (info.targetActivity != null) {
714 cls = new ComponentName(info.packageName, info.targetActivity);
715 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700716 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700717
Craig Mautner000f0022013-02-26 15:04:29 -0800718 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700719 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700720 final boolean notCurrentUserTask =
721 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700722 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700723
Craig Mautner000f0022013-02-26 15:04:29 -0800724 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
725 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700726 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700727 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700728 }
Craig Mautner000f0022013-02-26 15:04:29 -0800729 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700730 return r;
731 }
732 }
733 }
734
735 return null;
736 }
737
Amith Yamasani742a6712011-05-04 14:49:28 -0700738 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700739 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700740 */
Craig Mautner93529a42013-10-04 15:03:13 -0700741 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800742 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700743 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800744 }
745 mCurrentUser = userId;
746
747 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800748 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800749 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700750 final TaskRecord task = mTaskHistory.get(i);
751
752 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
753 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700754 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700755 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700756 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700757 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800758 mTaskHistory.remove(i);
759 mTaskHistory.add(task);
760 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800761 // Use same value for i.
762 } else {
763 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800764 }
765 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700766 if (VALIDATE_TOKENS) {
767 validateAppTokensLocked();
768 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700769 }
770
Craig Mautner2420ead2013-04-01 17:13:20 -0700771 void minimalResumeActivityLocked(ActivityRecord r) {
772 r.state = ActivityState.RESUMED;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800773 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
774 + " callers=" + Debug.getCallers(5));
Craig Mautner2420ead2013-04-01 17:13:20 -0700775 mResumedActivity = r;
776 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800777 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700778 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700779 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700780 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700781 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
782 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700783 }
784
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800785 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700786 if (r != null) {
787 mRecentTasks.addLocked(r.task);
788 r.task.touchActiveTime();
789 }
790 }
791
Narayan Kamath7829c812015-06-08 17:39:43 +0100792 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700793 if (mFullyDrawnStartTime != 0) {
794 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
795 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100796 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700797 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
798 }
799
800 private void stopFullyDrawnTraceIfNeeded() {
801 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
802 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
803 mFullyDrawnStartTime = 0;
804 }
805 }
806
Craig Mautnere79d42682013-04-01 19:01:53 -0700807 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700808 if (r.displayStartTime == 0) {
809 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
810 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100811 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700812 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700813 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700814 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100815 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700816 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700817 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700818 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800819
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700820 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700821 // Make sure that there is no activity waiting for this to launch.
822 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
823 r.displayStartTime = r.fullyDrawnStartTime = 0;
824 } else {
825 mStackSupervisor.removeTimeoutsForActivityLocked(r);
826 mStackSupervisor.scheduleIdleTimeoutLocked(r);
827 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700828 }
829
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800830 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800831 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800832 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
833 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
834 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
835 activities.get(activityNdx).setSleeping(false);
836 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800837 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800838 if (mPausingActivity != null) {
839 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
840 activityPausedLocked(mPausingActivity.appToken, true);
841 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800842 }
843
Todd Kennedy39bfee52016-02-24 10:28:21 -0800844 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
845 final String packageName = aInfo.packageName;
846 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
847 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
848 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
849 if (packageName.equals(activities.get(activityNdx).packageName)) {
850 activities.get(activityNdx).info.applicationInfo = aInfo;
851 }
852 }
853 }
854 }
855
Craig Mautner0eea92c2013-05-16 13:35:39 -0700856 /**
857 * @return true if something must be done before going to sleep.
858 */
859 boolean checkReadyForSleepLocked() {
860 if (mResumedActivity != null) {
861 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700862 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
863 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
864 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700865 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700866 return true;
867 }
868 if (mPausingActivity != null) {
869 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700870 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700871 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800872 }
873
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700874 if (hasVisibleBehindActivity()) {
875 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800876 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700877 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800878 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700879 return true;
880 }
881
Craig Mautner0eea92c2013-05-16 13:35:39 -0700882 return false;
883 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800884
Craig Mautner0eea92c2013-05-16 13:35:39 -0700885 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700886 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800887
Craig Mautner0eea92c2013-05-16 13:35:39 -0700888 // Make sure any stopped but visible activities are now sleeping.
889 // This ensures that the activity's onStop() is called.
890 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
891 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
892 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
893 final ActivityRecord r = activities.get(activityNdx);
894 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
895 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800896 }
897 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800898 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700899 }
Craig Mautner59c00972012-07-30 12:10:24 -0700900
Winson8b1871d2015-11-20 09:56:20 -0800901 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
902 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800903 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700904 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800905 return null;
906 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800907
Winson Chung083baf92014-07-11 10:32:42 -0700908 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700909 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800910 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700911 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700912 return null;
913 }
914
Winson Chung48a10a52014-08-27 14:36:51 -0700915 int w = mService.mThumbnailWidth;
916 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800917 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800918 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700919 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800920
921 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800922 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
923 // use within SystemUI while keeping memory usage low.
Winson Chungead5c0f2015-12-14 11:18:57 -0500924 if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
Winson8b1871d2015-11-20 09:56:20 -0800925 w = h = -1;
Winson387aac62015-11-25 11:18:56 -0800926 scale = 0.5f;
Winson8b1871d2015-11-20 09:56:20 -0800927 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800928 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800929 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800930 }
Winson Chung376543b2014-05-21 17:43:28 -0700931 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800932 return null;
933 }
934
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700935 /**
936 * Start pausing the currently resumed activity. It is an error to call this if there
937 * is already an activity being paused or there is no resumed activity.
938 *
939 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
940 * @param uiSleeping True if this is happening with the user interface going to sleep (the
941 * screen turning off).
942 * @param resuming True if this is being called as part of resuming the top activity, so
943 * we shouldn't try to instigate a resume here.
944 * @param dontWait True if the caller does not want to wait for the pause to complete. If
945 * set to true, we will immediately complete the pause here before returning.
946 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
947 * it to tell us when it is done.
948 */
949 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
950 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800951 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800952 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
953 + " state=" + mPausingActivity.state);
954 if (!mService.isSleeping()) {
955 // Avoid recursion among check for sleep and complete pause during sleeping.
956 // Because activity will be paused immediately after resume, just let pause
957 // be completed by the order of activity paused from clients.
958 completePauseLocked(false);
959 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800960 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700961 ActivityRecord prev = mResumedActivity;
962 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700963 if (!resuming) {
964 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -0800965 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700966 }
967 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700968 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800969
970 if (mActivityContainer.mParentActivity == null) {
971 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700972 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800973 }
974
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700975 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700976 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700977 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800978 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700979 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700980 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
981 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700982 prev.state = ActivityState.PAUSING;
983 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700984 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700985 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700986 if (mService.mHasRecents
987 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800988 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700989 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700990 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700991
992 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800993
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700994 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700995 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700996 try {
997 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700998 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700999 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -07001000 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001001 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001002 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001003 } catch (Exception e) {
1004 // Ignore exception, if process died other code will cleanup.
1005 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001006 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001007 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001008 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001009 }
1010 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001011 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001012 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001013 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001014 }
1015
1016 // If we are not going to sleep, we want to ensure the device is
1017 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001018 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001019 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001020 }
1021
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001022 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001023 // Have the window manager pause its key dispatching until the new
1024 // activity has started. If we're pausing the activity just because
1025 // the screen is being turned off and the UI is sleeping, don't interrupt
1026 // key dispatch; the same activity will pick it up again on wakeup.
1027 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001028 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001029 } else if (DEBUG_PAUSE) {
1030 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001031 }
1032
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001033 if (dontWait) {
1034 // If the caller said they don't want to wait for the pause, then complete
1035 // the pause now.
1036 completePauseLocked(false);
1037 return false;
1038
1039 } else {
1040 // Schedule a pause timeout in case the app doesn't respond.
1041 // We don't give it much time because this directly impacts the
1042 // responsiveness seen by the user.
1043 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1044 msg.obj = prev;
1045 prev.pauseTime = SystemClock.uptimeMillis();
1046 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001047 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001048 return true;
1049 }
1050
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001051 } else {
1052 // This activity failed to schedule the
1053 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001054 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001055 if (!resuming) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001056 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001057 }
1058 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001059 }
1060 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001061
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001062 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001063 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1064 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001065
Craig Mautnerd2328952013-03-05 12:46:26 -08001066 final ActivityRecord r = isInStackLocked(token);
1067 if (r != null) {
1068 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1069 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001070 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001071 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001072 completePauseLocked(true);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001073 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001074 } else {
1075 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1076 r.userId, System.identityHashCode(r), r.shortComponentName,
1077 mPausingActivity != null
1078 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001079 if (r.state == ActivityState.PAUSING) {
1080 r.state = ActivityState.PAUSED;
1081 if (r.finishing) {
1082 if (DEBUG_PAUSE) Slog.v(TAG,
1083 "Executing finish of failed to pause activity: " + r);
1084 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1085 }
louis_chang047dfd42015-04-08 16:35:55 +08001086 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001087 }
1088 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001089 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001090 }
1091
Craig Mautnera0026042014-04-23 11:45:37 -07001092 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1093 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001094 if (r.state != ActivityState.STOPPING) {
1095 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1096 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1097 return;
1098 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001099 if (persistentState != null) {
1100 r.persistentState = persistentState;
1101 mService.notifyTaskPersisterLocked(r.task, false);
1102 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001103 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001104 if (icicle != null) {
1105 // If icicle is null, this is happening due to a timeout, so we
1106 // haven't really saved the state.
1107 r.icicle = icicle;
1108 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001109 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001110 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001111 }
1112 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001113 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001114 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1115 r.stopped = true;
1116 r.state = ActivityState.STOPPED;
Robert Carre12aece2016-02-02 22:43:27 -08001117
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08001118 mWindowManager.notifyAppStopped(r.appToken, true);
Robert Carre12aece2016-02-02 22:43:27 -08001119
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001120 if (getVisibleBehindActivity() == r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001121 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001122 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001123 if (r.finishing) {
1124 r.clearOptionsLocked();
1125 } else {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001126 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001127 destroyActivityLocked(r, true, "stop-config");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001128 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001129 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001130 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001131 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001132 }
1133 }
1134 }
1135
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001136 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001137 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001138 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001139
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001140 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001141 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001142 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001143 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001144 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001145 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001146 } else if (prev.app != null) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001147 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1148 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Craig Mautner8c14c152015-01-15 17:32:07 -08001149 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001150 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1151 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001152 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001153 if (prev.deferRelaunchUntilPaused) {
1154 // Complete the deferred relaunch that was waiting for pause to complete.
1155 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
1156 relaunchActivityLocked(prev, prev.configChangeFlags, false,
1157 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001158 } else if (wasStopping) {
1159 // We are also stopping, the stop request must have gone soon after the pause.
1160 // We can't clobber it, because the stop confirmation will not be handled.
1161 // We don't need to schedule another stop, we only need to let it happen.
1162 prev.state = ActivityState.STOPPING;
Wale Ogunwaled8026642016-02-09 20:40:18 -08001163 } else if ((!prev.visible && !hasVisibleBehindActivity())
1164 || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001165 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001166 // stopping.
Chong Zhang46b1ac62016-02-18 17:53:57 -08001167 addToStopping(prev, true /* immediate */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001168 }
1169 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001170 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001171 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001172 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001173 // It is possible the activity was freezing the screen before it was paused.
1174 // In that case go ahead and remove the freeze this activity has on the screen
1175 // since it is no longer visible.
1176 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001177 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001178 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001179
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001180 if (resumeNext) {
1181 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1182 if (!mService.isSleepingOrShuttingDown()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001183 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001184 } else {
1185 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001186 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001187 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001188 // If there are no more activities available to run, do resume anyway to start
1189 // something. Also if the top activity on the stack is not the just paused
1190 // activity, we need to go ahead and resume it to ensure we complete an
1191 // in-flight app switch.
1192 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001193 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001194 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001195 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001196
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001197 if (prev != null) {
1198 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001199
Craig Mautner525f3d92013-05-07 14:01:50 -07001200 if (prev.app != null && prev.cpuTimeAtResume > 0
1201 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001202 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1203 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001204 if (diff > 0) {
1205 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1206 synchronized (bsi) {
1207 BatteryStatsImpl.Uid.Proc ps =
1208 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001209 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001210 if (ps != null) {
1211 ps.addForegroundTimeLocked(diff);
1212 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001213 }
1214 }
1215 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001216 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001217 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001218
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001219 // Notify when the task stack has changed, but only if visibilities changed (not just focus)
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001220 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause) {
1221 mService.notifyTaskStackChangedLocked();
1222 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1223 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001224
1225 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001226 }
1227
Chong Zhang46b1ac62016-02-18 17:53:57 -08001228 private void addToStopping(ActivityRecord r, boolean immediate) {
1229 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1230 mStackSupervisor.mStoppingActivities.add(r);
1231 }
1232
1233 // If we already have a few activities waiting to stop, then give up
1234 // on things going idle and start clearing them out. Or if r is the
1235 // last of activity of the last task the stack will be empty and must
1236 // be cleared immediately.
1237 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1238 || (r.frontOfTask && mTaskHistory.size() <= 1);
1239
1240 if (immediate || forceIdle) {
1241 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
1242 + forceIdle + "immediate=" + immediate);
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001243 mStackSupervisor.scheduleIdleLocked();
1244 } else {
1245 mStackSupervisor.checkReadyForSleepLocked();
1246 }
1247 }
1248
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001249 /**
1250 * Once we know that we have asked an application to put an activity in
1251 * the resumed state (either by launching it or explicitly telling it),
1252 * this function updates the rest of our state to match that fact.
1253 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001254 private void completeResumeLocked(ActivityRecord next) {
Chong Zhang2b79af12016-02-10 18:47:06 -08001255 next.visible = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001256 next.idle = false;
1257 next.results = null;
1258 next.newIntents = null;
Chong Zhang2b79af12016-02-10 18:47:06 -08001259 next.stopped = false;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001260
Chong Zhang85ee6542015-10-02 13:36:38 -07001261 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001262 ProcessRecord app = next.task.mActivities.get(0).app;
1263 if (app != null && app != mService.mHomeProcess) {
1264 mService.mHomeProcess = app;
1265 }
1266 }
1267
Craig Mautner07566322013-09-26 16:42:55 -07001268 if (next.nowVisible) {
1269 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001270 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001271 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001272
1273 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001274 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001275
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001276 mStackSupervisor.reportResumedActivityLocked(next);
1277
1278 next.resumeKeyDispatchingLocked();
1279 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001280
1281 // Mark the point when the activity is resuming
1282 // TODO: To be more accurate, the mark should be before the onCreate,
1283 // not after the onResume. But for subsequent starts, onResume is fine.
1284 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001285 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001286 } else {
1287 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1288 }
Winson Chung376543b2014-05-21 17:43:28 -07001289
George Mount6ba042b2014-07-28 11:12:28 -07001290 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001291
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001292 if (getVisibleBehindActivity() == next) {
Craig Mautner64ccb702014-10-01 09:38:40 -07001293 // When resuming an activity, require it to call requestVisibleBehind() again.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001294 setVisibleBehindActivity(null);
Craig Mautner64ccb702014-10-01 09:38:40 -07001295 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001296 }
1297
Craig Mautner2568c3a2015-03-26 14:22:34 -07001298 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001299 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001300 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1301 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1302 r.mUpdateTaskThumbnailWhenHidden = false;
1303 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001304 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001305 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001306 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001307 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001308 container.setVisible(visible);
1309 }
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001310 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001311 }
1312
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001313 // Find the first visible activity above the passed activity and if it is translucent return it
1314 // otherwise return null;
1315 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1316 TaskRecord task = r.task;
1317 if (task == null) {
1318 return null;
1319 }
1320
1321 ActivityStack stack = task.stack;
1322 if (stack == null) {
1323 return null;
1324 }
1325
1326 int stackNdx = mStacks.indexOf(stack);
1327
1328 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1329 int taskNdx = tasks.indexOf(task);
1330
1331 ArrayList<ActivityRecord> activities = task.mActivities;
1332 int activityNdx = activities.indexOf(r) + 1;
1333
1334 final int numStacks = mStacks.size();
1335 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001336 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001337 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001338 final int numTasks = tasks.size();
1339 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001340 final TaskRecord currentTask = tasks.get(taskNdx);
1341 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001342 final int numActivities = activities.size();
1343 while (activityNdx < numActivities) {
1344 final ActivityRecord activity = activities.get(activityNdx);
1345 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001346 return historyStack.mFullscreen
1347 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001348 }
1349 ++activityNdx;
1350 }
1351 activityNdx = 0;
1352 ++taskNdx;
1353 }
1354 taskNdx = 0;
1355 ++stackNdx;
1356 }
1357
1358 return null;
1359 }
1360
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001361 ActivityStack getNextFocusableStackLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001362 ArrayList<ActivityStack> stacks = mStacks;
1363 final ActivityRecord parent = mActivityContainer.mParentActivity;
1364 if (parent != null) {
1365 stacks = parent.task.stack.mStacks;
1366 }
1367 if (stacks != null) {
1368 for (int i = stacks.size() - 1; i >= 0; --i) {
1369 ActivityStack stack = stacks.get(i);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001370 if (stack != this && stack.isFocusable()
1371 && stack.getStackVisibilityLocked() != STACK_INVISIBLE) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001372 return stack;
1373 }
1374 }
1375 }
1376 return null;
1377 }
1378
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001379 /** Returns true if the stack contains a fullscreen task. */
1380 private boolean hasFullscreenTask() {
1381 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1382 final TaskRecord task = mTaskHistory.get(i);
1383 if (task.mFullscreen) {
1384 return true;
1385 }
1386 }
1387 return false;
1388 }
1389
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001390 private boolean hasTranslucentActivity(ActivityStack stack) {
1391 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1392 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1393 final TaskRecord task = tasks.get(taskNdx);
1394 final ArrayList<ActivityRecord> activities = task.mActivities;
1395 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1396 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001397
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001398 // Conditions for an activity to obscure the stack we're
1399 // examining:
1400 // 1. Not Finishing AND Visible AND:
1401 // 2. Either:
1402 // - Full Screen Activity OR
1403 // - On top of Home and our stack is NOT home
1404 if (!r.finishing && r.visible && (r.fullscreen ||
1405 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1406 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001407 }
1408 }
1409 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001410 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001411 }
1412
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001413 /**
1414 * Returns stack's visibility: {@link #STACK_INVISIBLE}, {@link #STACK_VISIBLE} or
1415 * {@link #STACK_VISIBLE_ACTIVITY_BEHIND}.
1416 */
1417 int getStackVisibilityLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001418 if (!isAttached()) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001419 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001420 }
1421
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001422 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001423 return STACK_VISIBLE;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001424 }
1425
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001426 final int stackIndex = mStacks.indexOf(this);
1427
1428 if (stackIndex == mStacks.size() - 1) {
1429 Slog.wtf(TAG,
1430 "Stack=" + this + " isn't front stack but is at the top of the stack list");
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001431 return STACK_INVISIBLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001432 }
1433
Chong Zhang75b37202015-12-04 14:16:36 -08001434 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1435 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001436 return STACK_INVISIBLE;
Chong Zhang75b37202015-12-04 14:16:36 -08001437 }
1438
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001439 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1440 final int focusedStackId = focusedStack.mStackId;
1441
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001442 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001443 && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID
1444 && !focusedStack.topActivity().fullscreen) {
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001445 // The fullscreen stack should be visible if it has a visible behind activity behind
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001446 // the home stack that is translucent.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001447 return STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001448 }
1449
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001450 if (mStackId == DOCKED_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001451 // Docked stack is always visible, except in the case where the top running activity
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001452 // task in the focus stack doesn't support any form of resizing but we show it for the
1453 // home task even though it's not resizable.
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001454 final ActivityRecord r = focusedStack.topRunningActivityLocked();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001455 final TaskRecord task = r != null ? r.task : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001456 return task == null || task.canGoInDockedStack() || task.isHomeTask() ? STACK_VISIBLE
1457 : STACK_INVISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001458 }
1459
Chong Zhangb16cf342015-11-12 17:22:40 -08001460 // Find the first stack below focused stack that actually got something visible.
1461 int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1462 while (belowFocusedIndex >= 0 &&
1463 mStacks.get(belowFocusedIndex).topRunningActivityLocked() == null) {
1464 belowFocusedIndex--;
1465 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001466 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1467 && stackIndex == belowFocusedIndex) {
1468 // Stacks directly behind the docked or pinned stack are always visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001469 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001470 }
1471
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001472 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1473 && hasTranslucentActivity(focusedStack)) {
1474 // Stacks behind the fullscreen stack with a translucent activity are always
1475 // visible so they can act as a backdrop to the translucent activity.
1476 // For example, dialog activities
1477 if (stackIndex == belowFocusedIndex) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001478 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001479 }
1480 if (belowFocusedIndex >= 0) {
1481 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001482 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1483 && stackIndex == (belowFocusedIndex - 1)) {
1484 // The stack behind the docked or pinned stack is also visible so we can have a
1485 // complete backdrop to the translucent activity when the docked stack is up.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001486 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001487 }
1488 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001489 }
1490
Wale Ogunwale3797c222015-10-27 14:21:58 -07001491 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001492 // Visibility of any static stack should have been determined by the conditions above.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001493 return STACK_INVISIBLE;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001494 }
1495
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001496 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001497 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001498
1499 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1500 continue;
1501 }
1502
Wale Ogunwale3797c222015-10-27 14:21:58 -07001503 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001504 // These stacks can't have any dynamic stacks visible behind them.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001505 return STACK_INVISIBLE;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001506 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001507
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001508 if (!hasTranslucentActivity(stack)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001509 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001510 }
1511 }
1512
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001513 return STACK_VISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001514 }
1515
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001516 final int rankTaskLayers(int baseLayer) {
1517 int layer = 0;
1518 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1519 final TaskRecord task = mTaskHistory.get(taskNdx);
1520 ActivityRecord r = task.topRunningActivityLocked();
1521 if (r == null || r.finishing || !r.visible) {
1522 task.mLayerRank = -1;
1523 } else {
1524 task.mLayerRank = baseLayer + layer++;
1525 }
1526 }
1527 return layer;
1528 }
1529
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001530 /**
1531 * Make sure that all activities that need to be visible (that is, they
1532 * currently can be seen by the user) actually are.
1533 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001534 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1535 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001536 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001537 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001538 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001539 if (top != null) {
1540 checkTranslucentActivityWaiting(top);
1541 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001542
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001543 // If the top activity is not fullscreen, then we need to
1544 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001545 boolean aboveTop = top != null;
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001546 final int stackVisibility = getStackVisibilityLocked();
1547 final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
1548 final boolean stackVisibleBehind = stackVisibility == STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001549 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaled046a012015-12-24 13:05:59 -08001550 boolean resumeNextActivity = isFocusable() && (isInStackLocked(starting) == null);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001551 boolean behindTranslucentActivity = false;
1552 final ActivityRecord visibleBehind = getVisibleBehindActivity();
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001553 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001554 final TaskRecord task = mTaskHistory.get(taskNdx);
1555 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001556 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1557 final ActivityRecord r = activities.get(activityNdx);
1558 if (r.finishing) {
1559 continue;
1560 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001561 final boolean isTop = r == top;
1562 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001563 continue;
1564 }
1565 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001566 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1567 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001568 final boolean activityVisibleBehind =
1569 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == r;
1570 final boolean isVisible = (!behindFullscreenActivity || r.mLaunchTaskBehind
1571 || activityVisibleBehind) && okToShowLocked(r);
1572 if (isVisible) {
1573 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1574 + " finishing=" + r.finishing + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001575 // First: if this is not the current activity being started, make
1576 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001577 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001578 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001579 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001580
1581 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001582 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001583 resumeNextActivity, r)) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001584 if (activityNdx >= activities.size()) {
1585 // Record may be removed if its process needs to restart.
1586 activityNdx = activities.size() - 1;
1587 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001588 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001589 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001590 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001591 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001592 // If this activity is already visible, then there is nothing to do here.
1593 if (handleAlreadyVisible(r)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001594 resumeNextActivity = false;
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001595 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001596 } else {
Jorim Jaggie66edb12016-02-05 12:41:17 -08001597 makeVisibleIfNeeded(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001598 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001599 // Aggregate current change flags.
1600 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001601 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1602 behindFullscreenActivity, task, r);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001603 if (behindFullscreenActivity && !r.fullscreen) {
1604 behindTranslucentActivity = true;
1605 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001606 } else {
Wale Ogunwale834c2362016-01-23 18:14:58 -08001607 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001608 + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1609 + stackInvisible + " behindFullscreenActivity="
1610 + behindFullscreenActivity + " mLaunchTaskBehind="
1611 + r.mLaunchTaskBehind);
1612 makeInvisible(r, visibleBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001613 }
1614 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001615 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1616 // The visibility of tasks and the activities they contain in freeform stack are
1617 // determined individually unlike other stacks where the visibility or fullscreen
1618 // status of an activity in a previous task affects other.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001619 behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001620 } else if (mStackId == HOME_STACK_ID) {
1621 if (task.isHomeTask()) {
1622 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
1623 + " stackInvisible=" + stackInvisible
1624 + " behindFullscreenActivity=" + behindFullscreenActivity);
1625 // No other task in the home stack should be visible behind the home activity.
1626 // Home activities is usually a translucent activity with the wallpaper behind
1627 // them. However, when they don't have the wallpaper behind them, we want to
1628 // show activities in the next application stack behind them vs. another
1629 // task in the home stack like recents.
1630 behindFullscreenActivity = true;
1631 } else if (task.isRecentsTask()
1632 && task.getTaskToReturnTo() == APPLICATION_ACTIVITY_TYPE) {
1633 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1634 "Recents task returning to app: at " + task
1635 + " stackInvisible=" + stackInvisible
1636 + " behindFullscreenActivity=" + behindFullscreenActivity);
1637 // We don't want any other tasks in the home stack visible if the recents
1638 // activity is going to be returning to an application activity type.
1639 // We do this to preserve the visible order the user used to get into the
1640 // recents activity. The recents activity is normally translucent and if it
1641 // doesn't have the wallpaper behind it the next activity in the home stack
1642 // shouldn't be visible when the home stack is brought to the front to display
1643 // the recents activity from an app.
1644 behindFullscreenActivity = true;
1645 }
1646
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001647 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001648 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001649
1650 if (mTranslucentActivityWaiting != null &&
1651 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1652 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1653 notifyActivityDrawnLocked(null);
1654 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001655 }
Craig Mautner58547802013-03-05 08:23:53 -08001656
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001657 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1658 if (mTranslucentActivityWaiting != top) {
1659 mUndrawnActivitiesBelowTopTranslucent.clear();
1660 if (mTranslucentActivityWaiting != null) {
1661 // Call the callback with a timeout indication.
1662 notifyActivityDrawnLocked(null);
1663 mTranslucentActivityWaiting = null;
1664 }
1665 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1666 }
1667 }
1668
1669 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001670 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001671 // We need to make sure the app is running if it's the top, or it is just made visible from
1672 // invisible. If the app is already visible, it must have died while it was visible. In this
1673 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1674 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001675 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001676 // This activity needs to be visible, but isn't even running...
1677 // get it started and resume if no other stack in this stack is resumed.
1678 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1679 if (r != starting) {
1680 r.startFreezingScreenLocked(r.app, configChanges);
1681 }
1682 if (!r.visible || r.mLaunchTaskBehind) {
1683 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1684 setVisible(r, true);
1685 }
1686 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001687 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001688 return true;
1689 }
1690 }
1691 return false;
1692 }
1693
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001694 private void makeInvisible(ActivityRecord r, ActivityRecord visibleBehind) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001695 if (!r.visible) {
1696 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1697 return;
1698 }
1699 // Now for any activities that aren't visible to the user, make sure they no longer are
1700 // keeping the screen frozen.
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001701 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001702 try {
1703 setVisible(r, false);
1704 switch (r.state) {
1705 case STOPPING:
1706 case STOPPED:
1707 if (r.app != null && r.app.thread != null) {
1708 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1709 "Scheduling invisibility: " + r);
1710 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1711 }
1712 break;
1713
1714 case INITIALIZING:
1715 case RESUMED:
1716 case PAUSING:
1717 case PAUSED:
1718 // This case created for transitioning activities from
1719 // translucent to opaque {@link Activity#convertToOpaque}.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001720 if (visibleBehind == r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001721 releaseBackgroundResources(r);
1722 } else {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001723 addToStopping(r, true /* immediate */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001724 }
1725 break;
1726
1727 default:
1728 break;
1729 }
1730 } catch (Exception e) {
1731 // Just skip on any failure; we'll make it visible when it next restarts.
1732 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1733 }
1734 }
1735
1736 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1737 TaskRecord task, ActivityRecord r) {
1738 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001739 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08001740 + " stackInvisible=" + stackInvisible
1741 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001742 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001743 behindFullscreenActivity = true;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001744 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001745 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1746 + " stackInvisible=" + stackInvisible
1747 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001748 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001749 }
1750 return behindFullscreenActivity;
1751 }
1752
Jorim Jaggie66edb12016-02-05 12:41:17 -08001753 private void makeVisibleIfNeeded(ActivityRecord starting, ActivityRecord r) {
1754
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001755 // This activity is not currently visible, but is running. Tell it to become visible.
Jorim Jaggie66edb12016-02-05 12:41:17 -08001756 if (r.state == ActivityState.RESUMED || r == starting) {
Chong Zhange05db742016-02-16 16:58:37 -08001757 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1758 "Not making visible, r=" + r + " state=" + r.state + " starting=" + starting);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001759 return;
1760 }
1761
1762 // If this activity is paused, tell it to now show its window.
1763 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1764 "Making visible and scheduling visibility: " + r);
1765 try {
1766 if (mTranslucentActivityWaiting != null) {
1767 r.updateOptionsLocked(r.returningOptions);
1768 mUndrawnActivitiesBelowTopTranslucent.add(r);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001769 }
Jorim Jaggie66edb12016-02-05 12:41:17 -08001770 setVisible(r, true);
1771 r.sleeping = false;
1772 r.app.pendingUiClean = true;
1773 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1774 r.stopFreezingScreenLocked(false);
1775 } catch (Exception e) {
1776 // Just skip on any failure; we'll make it
1777 // visible when it next restarts.
1778 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001779 }
1780 }
1781
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001782 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001783 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1784 r.stopFreezingScreenLocked(false);
1785 try {
1786 if (r.returningOptions != null) {
1787 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1788 }
1789 } catch(RemoteException e) {
1790 }
1791 return r.state == ActivityState.RESUMED;
1792 }
1793
Todd Kennedyaab56db2015-01-30 09:39:53 -08001794 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001795 mTranslucentActivityWaiting = r;
1796 mUndrawnActivitiesBelowTopTranslucent.clear();
1797 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1798 }
1799
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001800 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1801 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1802 final TaskRecord task = mTaskHistory.get(taskNdx);
1803 final ArrayList<ActivityRecord> activities = task.mActivities;
1804 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1805 final ActivityRecord r = activities.get(activityNdx);
1806 if ( r.appTimeTracker != except) {
1807 r.appTimeTracker = null;
1808 }
1809 }
1810 }
1811 }
1812
Craig Mautner5eda9b32013-07-02 11:58:16 -07001813 /**
1814 * Called as activities below the top translucent activity are redrawn. When the last one is
1815 * redrawn notify the top activity by calling
1816 * {@link Activity#onTranslucentConversionComplete}.
1817 *
1818 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1819 * occurred and the activity will be notified immediately.
1820 */
1821 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001822 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001823 if ((r == null)
1824 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1825 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1826 // The last undrawn activity below the top has just been drawn. If there is an
1827 // opaque activity at the top, notify it that it can become translucent safely now.
1828 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1829 mTranslucentActivityWaiting = null;
1830 mUndrawnActivitiesBelowTopTranslucent.clear();
1831 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1832
Craig Mautner71dd1b62014-02-18 15:48:52 -08001833 if (waitingActivity != null) {
1834 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1835 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1836 try {
1837 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1838 waitingActivity.appToken, r != null);
1839 } catch (RemoteException e) {
1840 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001841 }
1842 }
1843 }
1844 }
1845
Craig Mautnera61bc652013-10-28 15:43:18 -07001846 /** If any activities below the top running one are in the INITIALIZING state and they have a
1847 * starting window displayed then remove that starting window. It is possible that the activity
1848 * in this state will never resumed in which case that starting window will be orphaned. */
1849 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001850 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001851 boolean aboveTop = true;
1852 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1853 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1854 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1855 final ActivityRecord r = activities.get(activityNdx);
1856 if (aboveTop) {
1857 if (r == topActivity) {
1858 aboveTop = false;
1859 }
1860 continue;
1861 }
1862
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08001863 if (r.state == ActivityState.INITIALIZING
1864 && r.mStartingWindowState == STARTING_WINDOW_SHOWN) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001865 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1866 "Found orphaned starting window " + r);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08001867 r.mStartingWindowState = STARTING_WINDOW_REMOVED;
Craig Mautnera61bc652013-10-28 15:43:18 -07001868 mWindowManager.removeAppStartingWindow(r.appToken);
1869 }
1870 }
1871 }
1872 }
1873
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001874 /**
1875 * Ensure that the top activity in the stack is resumed.
1876 *
1877 * @param prev The previously resumed activity, for when in the process
1878 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001879 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001880 *
1881 * @return Returns true if something is being resumed, or false if
1882 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001883 *
1884 * NOTE: It is not safe to call this method directly as it can cause an activity in a
1885 * non-focused stack to be resumed.
1886 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
1887 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001888 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08001889 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001890 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001891 // Don't even start recursing.
1892 return false;
1893 }
1894
1895 boolean result = false;
1896 try {
1897 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001898 mStackSupervisor.inResumeTopActivity = true;
1899 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1900 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001901 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001902 }
Craig Mautner544efa72014-09-04 16:41:20 -07001903 result = resumeTopActivityInnerLocked(prev, options);
1904 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001905 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001906 }
1907 return result;
1908 }
1909
Chong Zhang280d3322015-11-03 17:27:26 -08001910 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001911 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001912
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001913 if (!mService.mBooting && !mService.mBooted) {
1914 // Not ready yet!
1915 return false;
1916 }
1917
Craig Mautnerdf88d732014-01-27 09:21:32 -08001918 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001919 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001920 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001921 // Do not resume this stack if its parent is not resumed.
1922 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1923 return false;
1924 }
1925
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001926 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07001927
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001928 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001929 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001930
1931 // Remember how we'll process this pause/resume situation, and ensure
1932 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001933 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1934 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001935
Craig Mautner84984fa2014-06-19 11:19:20 -07001936 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001937 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001938 // There are no more activities!
1939 final String reason = "noMoreActivities";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001940 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(reason)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001941 // Try to move focus to the next visible stack with a running activity if this
1942 // stack is not covering the entire screen.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001943 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
1944 mStackSupervisor.getFocusedStack(), prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001945 }
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001946
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001947 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001948 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001949 if (DEBUG_STATES) Slog.d(TAG_STATES,
1950 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001951 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001952 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001953 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1954 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1955 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001956 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001957 }
1958
1959 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001960
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001961 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001962 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1963 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001964 // Make sure we have executed any pending transitions, since there
1965 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001966 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001967 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001968 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001969 if (DEBUG_STATES) Slog.d(TAG_STATES,
1970 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001971 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001972 return false;
1973 }
1974
Craig Mautner525f3d92013-05-07 14:01:50 -07001975 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001976 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001977 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001978 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001979 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001980 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001981 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001982 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001983 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001984 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001985 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001986 } else if (!isOnHomeDisplay()) {
1987 return false;
1988 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001989 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001990 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001991 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1992 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1993 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001994 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001995 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001996 }
1997
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001998 // If we are sleeping, and there is no resumed activity, and the top
1999 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07002000 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09002001 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002002 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002003 // Make sure we have executed any pending transitions, since there
2004 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002005 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002006 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002007 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002008 if (DEBUG_STATES) Slog.d(TAG_STATES,
2009 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002010 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002011 return false;
2012 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002013
2014 // Make sure that the user who owns this activity is started. If not,
2015 // we will just leave it as is because someone should be bringing
2016 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002017 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002018 Slog.w(TAG, "Skipping resume of top activity " + next
2019 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002020 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002021 return false;
2022 }
2023
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002024 // The activity may be waiting for stop, but that is no longer
2025 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002026 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002027 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002028 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002029 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002030
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002031 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002032
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002033 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002034 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002035 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002036 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002037 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002038 return false;
2039 }
2040
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002041 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2042
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002043 // We need to start pausing the current activity so the top one can be resumed...
2044 final boolean dontWaitForPause = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002045 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002046 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002047 if (DEBUG_STATES) Slog.d(TAG_STATES,
2048 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08002049 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002050 }
2051 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002052 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002053 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002054 // At this point we want to put the upcoming activity's process
2055 // at the top of the LRU list, since we know we will be needing it
2056 // very soon and it would be a waste to let it get killed if it
2057 // happens to be sitting towards the end.
2058 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002059 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002060 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002061 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002062 return true;
2063 }
2064
Christopher Tated3f175c2012-06-14 14:16:54 -07002065 // If the most recent activity was noHistory but was only stopped rather
2066 // than stopped+finished because the device went to sleep, we need to make
2067 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07002068 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002069 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002070 if (DEBUG_STATES) Slog.d(TAG_STATES,
2071 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002072 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002073 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002074 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002075 }
2076
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002077 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002078 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2079 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002080 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002081 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2082 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002083 } else {
2084 // The next activity is already visible, so hide the previous
2085 // activity's windows right now so we can show the new one ASAP.
2086 // We only do this if the previous is finishing, which should mean
2087 // it is on top of the one being resumed so hiding it quickly
2088 // is good. Otherwise, we want to do the normal route of allowing
2089 // the resumed activity to be shown so we can decide if the
2090 // previous should actually be hidden depending on whether the
2091 // new one is found to be full-screen or not.
2092 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002093 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002094 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2095 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002096 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002097 + ", nowVisible=" + next.nowVisible);
2098 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002099 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002100 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002101 + ", waitingVisible="
2102 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2103 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 }
2105 }
2106 }
2107
Dianne Hackborne7f97212011-02-24 14:40:20 -08002108 // Launching this app's activity, make sure the app is no longer
2109 // considered stopped.
2110 try {
2111 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002112 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002113 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002114 } catch (IllegalArgumentException e) {
2115 Slog.w(TAG, "Failed trying to unstop package "
2116 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002117 }
2118
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002119 // We are starting up the next activity, so tell the window manager
2120 // that the previous one will be hidden soon. This way it can know
2121 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002122 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002123 if (prev != null) {
2124 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002125 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002126 "Prepare close transition: prev=" + prev);
2127 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002128 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002129 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002130 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002131 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002132 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2133 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002134 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002135 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002136 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002137 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2138 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002139 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002140 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002141 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002142 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002143 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002144 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002145 : next.mLaunchTaskBehind
2146 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2147 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002148 }
2149 }
Craig Mautner967212c2013-04-13 21:10:58 -07002150 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002151 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002152 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002153 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002154 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002155 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002156 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002157 }
2158 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002159
2160 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002161 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002162 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2163 if (opts != null) {
2164 resumeAnimOptions = opts.toBundle();
2165 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002166 next.applyOptionsLocked();
2167 } else {
2168 next.clearOptionsLocked();
2169 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002170
Craig Mautnercf910b02013-04-23 11:23:27 -07002171 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002172 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002173 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002174
2175 // This activity is now becoming visible.
Jorim Jaggi1b025a62016-02-03 19:27:49 -08002176 if (!next.visible) {
2177 mWindowManager.setAppVisibility(next.appToken, true);
2178 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002179
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002180 // schedule launch ticks to collect information about slow apps.
2181 next.startLaunchTickingLocked();
2182
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002183 ActivityRecord lastResumedActivity =
2184 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002185 ActivityState lastState = next.state;
2186
2187 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002188
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002189 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002190 next.state = ActivityState.RESUMED;
2191 mResumedActivity = next;
2192 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002193 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002194 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002195 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002196 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002197
2198 // Have the window manager re-evaluate the orientation of
2199 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002200 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002201 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002202 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002203 mService.mConfiguration,
2204 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2205 if (config != null) {
2206 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002207 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002208 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002209 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002210
Craig Mautner525f3d92013-05-07 14:01:50 -07002211 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002212 // The configuration update wasn't able to keep the existing
2213 // instance of the activity, and instead started a new one.
2214 // We should be all done, but let's just make sure our activity
2215 // is still at the top and schedule another run if something
2216 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002217 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002218 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002219 "Activity config changed during resume: " + next
2220 + ", new next: " + nextNext);
2221 if (nextNext != next) {
2222 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002223 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002224 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002225 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002226 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002227 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002228 return true;
2229 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002230 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002231 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002232 }
Craig Mautner58547802013-03-05 08:23:53 -08002233
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002234 try {
2235 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002236 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002237 if (a != null) {
2238 final int N = a.size();
2239 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002240 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2241 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002242 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002243 }
2244 }
2245
2246 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002247 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002248 }
2249
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002250 // Well the app will no longer be stopped.
2251 // Clear app token stopped state in window manager if needed.
2252 mWindowManager.notifyAppStopped(next.appToken, false);
2253
Craig Mautner299f9602015-01-26 09:47:33 -08002254 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2255 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002256
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002257 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002258 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002259 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002260 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002261 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002262 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002263 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002264
Craig Mautner0eea92c2013-05-16 13:35:39 -07002265 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002266
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002267 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002268 } catch (Exception e) {
2269 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002270 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002271 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002272 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002273 if (lastStack != null) {
2274 lastStack.mResumedActivity = lastResumedActivity;
2275 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002276 Slog.i(TAG, "Restarting because process died: " + next);
2277 if (!next.hasBeenLaunched) {
2278 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002279 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2280 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002281 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002282 next.appToken, next.packageName, next.theme,
2283 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002284 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2285 next.windowFlags, null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002286 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002287 }
George Mount2c92c972014-03-20 09:38:23 -07002288 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002289 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002290 return true;
2291 }
2292
2293 // From this point on, if something goes wrong there is no way
2294 // to recover the activity.
2295 try {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002296 completeResumeLocked(next);
2297 } catch (Exception e) {
2298 // If any exception gets thrown, toss away this
2299 // activity and try the next one.
2300 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002301 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002302 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002303 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002304 return true;
2305 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002306 } else {
2307 // Whoops, need to restart this activity!
2308 if (!next.hasBeenLaunched) {
2309 next.hasBeenLaunched = true;
2310 } else {
2311 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002312 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002313 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002314 mService.compatibilityInfoForPackageLocked(
2315 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002316 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002317 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002318 null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002319 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002320 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002321 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002322 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002323 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002324 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002325 }
2326
Craig Mautnercf910b02013-04-23 11:23:27 -07002327 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002328 return true;
2329 }
2330
riddle_hsuc215a4f2014-12-27 12:10:45 +08002331 private TaskRecord getNextTask(TaskRecord targetTask) {
2332 final int index = mTaskHistory.indexOf(targetTask);
2333 if (index >= 0) {
2334 final int numTasks = mTaskHistory.size();
2335 for (int i = index + 1; i < numTasks; ++i) {
2336 TaskRecord task = mTaskHistory.get(i);
2337 if (task.userId == targetTask.userId) {
2338 return task;
2339 }
2340 }
2341 }
2342 return null;
2343 }
2344
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002345 private void insertTaskAtPosition(TaskRecord task, int position) {
2346 if (position >= mTaskHistory.size()) {
2347 insertTaskAtTop(task, null);
2348 return;
2349 }
2350 // Calculate maximum possible position for this task.
2351 int maxPosition = mTaskHistory.size();
2352 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002353 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002354 // Put non-current user tasks below current user tasks.
2355 while (maxPosition > 0) {
2356 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2357 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002358 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002359 break;
2360 }
2361 maxPosition--;
2362 }
2363 }
2364 position = Math.min(position, maxPosition);
2365 mTaskHistory.remove(task);
2366 mTaskHistory.add(position, task);
2367 updateTaskMovement(task, true);
2368 }
2369
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002370 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002371 // If the moving task is over home stack, transfer its return type to next task
2372 if (task.isOverHomeStack()) {
2373 final TaskRecord nextTask = getNextTask(task);
2374 if (nextTask != null) {
2375 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2376 }
2377 }
2378
Craig Mautner9c85c202013-10-04 20:11:26 -07002379 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002380 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002381 if (isOnHomeDisplay()) {
2382 ActivityStack lastStack = mStackSupervisor.getLastStack();
2383 final boolean fromHome = lastStack.isHomeStack();
2384 if (!isHomeStack() && (fromHome || topTask() != task)) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002385 int returnToType = APPLICATION_ACTIVITY_TYPE;
2386 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2387 returnToType = lastStack.topTask() == null
2388 ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2389 }
2390 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002391 }
2392 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002393 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002394 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002395
Craig Mautnerac6f8432013-07-17 13:24:59 -07002396 mTaskHistory.remove(task);
2397 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002398 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002399 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002400 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002401 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002402 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002403 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002404 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002405 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002406 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002407 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002408 break;
2409 }
2410 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002411 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002412 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002413 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002414 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002415 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002416
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002417 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2418 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002419 TaskRecord rTask = r.task;
2420 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002421 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2422 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002423 // Last activity in task had been removed or ActivityManagerService is reusing task.
2424 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002425 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002426 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002427 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002428 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002429 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002430 if (!newTask) {
2431 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002432 boolean startIt = true;
2433 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2434 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002435 if (task.getTopActivity() == null) {
2436 // All activities in task are finishing.
2437 continue;
2438 }
Craig Mautner70a86932013-02-28 22:37:44 -08002439 if (task == r.task) {
2440 // Here it is! Now, if this is not yet visible to the
2441 // user, then just add it without starting; it will
2442 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002443 if (!startIt) {
2444 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2445 + task, new RuntimeException("here").fillInStackTrace());
2446 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002447 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002448 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002449 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002450 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002451 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002452 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002453 return;
2454 }
2455 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002456 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002457 startIt = false;
2458 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002459 }
2460 }
2461
2462 // Place a new activity at top of stack, so it is next to interact
2463 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002464
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002465 // If we are not placing the new activity frontmost, we do not want
2466 // to deliver the onUserLeaving callback to the actual frontmost
2467 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002468 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002469 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002470 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002471 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002472 }
Craig Mautner70a86932013-02-28 22:37:44 -08002473
2474 task = r.task;
2475
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002476 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002477 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002478 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002479 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002480 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002481
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002482 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002483 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002484 // We want to show the starting preview window if we are
2485 // switching to a new task, or the next activity's process is
2486 // not currently running.
2487 boolean showStartingIcon = newTask;
2488 ProcessRecord proc = r.app;
2489 if (proc == null) {
2490 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2491 }
2492 if (proc == null || proc.thread == null) {
2493 showStartingIcon = true;
2494 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002495 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002496 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002497 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002498 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002499 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002500 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002501 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002502 ? r.mLaunchTaskBehind
2503 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2504 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002505 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002506 mNoAnimActivities.remove(r);
2507 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002508 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002509 boolean doShow = true;
2510 if (newTask) {
2511 // Even though this activity is starting fresh, we still need
2512 // to reset it to make sure we apply affinities to move any
2513 // existing activities from other tasks in to it.
2514 // If the caller has requested that the target task be
2515 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002516 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002517 resetTaskIfNeededLocked(r, r);
2518 doShow = topRunningNonDelayedActivityLocked(null) == r;
2519 }
Chong Zhang280d3322015-11-03 17:27:26 -08002520 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002521 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2522 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002523 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002524 if (r.mLaunchTaskBehind) {
2525 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2526 // tell WindowManager that r is visible even though it is at the back of the stack.
2527 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002528 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002529 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002530 // Figure out if we are transitioning from another activity that is
2531 // "has the same starting icon" as the next one. This allows the
2532 // window manager to keep the previous window it had previously
2533 // created, if it still had one.
2534 ActivityRecord prev = mResumedActivity;
2535 if (prev != null) {
2536 // We don't want to reuse the previous starting preview if:
2537 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002538 if (prev.task != r.task) {
2539 prev = null;
2540 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002541 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002542 else if (prev.nowVisible) {
2543 prev = null;
2544 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002545 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002546 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002547 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002548 mService.compatibilityInfoForPackageLocked(
2549 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002550 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002551 prev != null ? prev.appToken : null, showStartingIcon);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002552 r.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002553 }
2554 } else {
2555 // If this is the first activity, don't do any fancy animations,
2556 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002557 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002558 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002559 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002560 }
2561 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002562 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002563 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002564 }
2565
Dianne Hackbornbe707852011-11-11 14:32:10 -08002566 final void validateAppTokensLocked() {
2567 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002568 mValidateAppTokens.ensureCapacity(numActivities());
2569 final int numTasks = mTaskHistory.size();
2570 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2571 TaskRecord task = mTaskHistory.get(taskNdx);
2572 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002573 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002574 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002575 }
Craig Mautner000f0022013-02-26 15:04:29 -08002576 TaskGroup group = new TaskGroup();
2577 group.taskId = task.taskId;
2578 mValidateAppTokens.add(group);
2579 final int numActivities = activities.size();
2580 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2581 final ActivityRecord r = activities.get(activityNdx);
2582 group.tokens.add(r.appToken);
2583 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002584 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002585 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002586 }
2587
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002588 /**
2589 * Perform a reset of the given task, if needed as part of launching it.
2590 * Returns the new HistoryRecord at the top of the task.
2591 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002592 /**
2593 * Helper method for #resetTaskIfNeededLocked.
2594 * We are inside of the task being reset... we'll either finish this activity, push it out
2595 * for another task, or leave it as-is.
2596 * @param task The task containing the Activity (taskTop) that might be reset.
2597 * @param forceReset
2598 * @return An ActivityOptions that needs to be processed.
2599 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002600 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002601 ActivityOptions topOptions = null;
2602
2603 int replyChainEnd = -1;
2604 boolean canMoveOptions = true;
2605
2606 // We only do this for activities that are not the root of the task (since if we finish
2607 // the root, we may no longer have the task!).
2608 final ArrayList<ActivityRecord> activities = task.mActivities;
2609 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002610 final int rootActivityNdx = task.findEffectiveRootIndex();
2611 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002612 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002613 if (target.frontOfTask)
2614 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002615
2616 final int flags = target.info.flags;
2617 final boolean finishOnTaskLaunch =
2618 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2619 final boolean allowTaskReparenting =
2620 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2621 final boolean clearWhenTaskReset =
2622 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2623
2624 if (!finishOnTaskLaunch
2625 && !clearWhenTaskReset
2626 && target.resultTo != null) {
2627 // If this activity is sending a reply to a previous
2628 // activity, we can't do anything with it now until
2629 // we reach the start of the reply chain.
2630 // XXX note that we are assuming the result is always
2631 // to the previous activity, which is almost always
2632 // the case but we really shouldn't count on.
2633 if (replyChainEnd < 0) {
2634 replyChainEnd = i;
2635 }
2636 } else if (!finishOnTaskLaunch
2637 && !clearWhenTaskReset
2638 && allowTaskReparenting
2639 && target.taskAffinity != null
2640 && !target.taskAffinity.equals(task.affinity)) {
2641 // If this activity has an affinity for another
2642 // task, then we need to move it out of here. We will
2643 // move it as far out of the way as possible, to the
2644 // bottom of the activity stack. This also keeps it
2645 // correctly ordered with any activities we previously
2646 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002647 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002648 final ActivityRecord bottom =
2649 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002650 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002651 if (bottom != null && target.taskAffinity != null
2652 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002653 // If the activity currently at the bottom has the
2654 // same task affinity as the one we are moving,
2655 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002656 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002657 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002658 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002659 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002660 targetTask = createTaskRecord(
2661 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
2662 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002663 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002664 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002665 + " out to new task " + target.task);
2666 }
2667
Wale Ogunwale706ed792015-08-02 10:29:44 -07002668 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002669
Craig Mautner525f3d92013-05-07 14:01:50 -07002670 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002671 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2672 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002673 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002674 if (p.finishing) {
2675 continue;
2676 }
2677
Craig Mautnere3a74d52013-02-22 14:14:58 -08002678 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002679 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002680 topOptions = p.takeOptionsLocked();
2681 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002682 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002683 }
2684 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002685 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2686 "Removing activity " + p + " from task=" + task + " adding to task="
2687 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002688 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2689 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002690 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002691 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002692
Wale Ogunwale706ed792015-08-02 10:29:44 -07002693 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002694 }
2695
Wale Ogunwale706ed792015-08-02 10:29:44 -07002696 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002697 if (VALIDATE_TOKENS) {
2698 validateAppTokensLocked();
2699 }
2700
2701 replyChainEnd = -1;
2702 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2703 // If the activity should just be removed -- either
2704 // because it asks for it, or the task should be
2705 // cleared -- then finish it and anything that is
2706 // part of its reply chain.
2707 int end;
2708 if (clearWhenTaskReset) {
2709 // In this case, we want to finish this activity
2710 // and everything above it, so be sneaky and pretend
2711 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002712 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002713 } else if (replyChainEnd < 0) {
2714 end = i;
2715 } else {
2716 end = replyChainEnd;
2717 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002718 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002719 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002720 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002721 if (p.finishing) {
2722 continue;
2723 }
2724 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002725 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002726 topOptions = p.takeOptionsLocked();
2727 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002728 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002729 }
2730 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002731 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002732 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002733 if (finishActivityLocked(
2734 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002735 end--;
2736 srcPos--;
2737 }
2738 }
2739 replyChainEnd = -1;
2740 } else {
2741 // If we were in the middle of a chain, well the
2742 // activity that started it all doesn't want anything
2743 // special, so leave it all as-is.
2744 replyChainEnd = -1;
2745 }
2746 }
2747
2748 return topOptions;
2749 }
2750
2751 /**
2752 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2753 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2754 * @param affinityTask The task we are looking for an affinity to.
2755 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2756 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2757 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2758 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002759 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002760 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002761 int replyChainEnd = -1;
2762 final int taskId = task.taskId;
2763 final String taskAffinity = task.affinity;
2764
2765 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2766 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002767 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2768
2769 // Do not operate on or below the effective root Activity.
2770 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002771 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002772 if (target.frontOfTask)
2773 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002774
2775 final int flags = target.info.flags;
2776 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2777 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2778
2779 if (target.resultTo != null) {
2780 // If this activity is sending a reply to a previous
2781 // activity, we can't do anything with it now until
2782 // we reach the start of the reply chain.
2783 // XXX note that we are assuming the result is always
2784 // to the previous activity, which is almost always
2785 // the case but we really shouldn't count on.
2786 if (replyChainEnd < 0) {
2787 replyChainEnd = i;
2788 }
2789 } else if (topTaskIsHigher
2790 && allowTaskReparenting
2791 && taskAffinity != null
2792 && taskAffinity.equals(target.taskAffinity)) {
2793 // This activity has an affinity for our task. Either remove it if we are
2794 // clearing or move it over to our task. Note that
2795 // we currently punt on the case where we are resetting a
2796 // task that is not at the top but who has activities above
2797 // with an affinity to it... this is really not a normal
2798 // case, and we will need to later pull that task to the front
2799 // and usually at that point we will do the reset and pick
2800 // up those remaining activities. (This only happens if
2801 // someone starts an activity in a new task from an activity
2802 // in a task that is not currently on top.)
2803 if (forceReset || finishOnTaskLaunch) {
2804 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002805 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2806 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002807 for (int srcPos = start; srcPos >= i; --srcPos) {
2808 final ActivityRecord p = activities.get(srcPos);
2809 if (p.finishing) {
2810 continue;
2811 }
Todd Kennedy539db512014-12-15 09:57:55 -08002812 finishActivityLocked(
2813 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002814 }
2815 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002816 if (taskInsertionPoint < 0) {
2817 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002818
Craig Mautner77878772013-03-04 19:46:24 -08002819 }
Craig Mautner77878772013-03-04 19:46:24 -08002820
2821 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002822 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2823 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2824 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002825 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002826 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002827 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002828 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002829
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002830 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2831 "Removing and adding activity " + p + " to stack at " + task
2832 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002833 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2834 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002835 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002836 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002837 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002838 if (VALIDATE_TOKENS) {
2839 validateAppTokensLocked();
2840 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002841
2842 // Now we've moved it in to place... but what if this is
2843 // a singleTop activity and we have put it on top of another
2844 // instance of the same activity? Then we drop the instance
2845 // below so it remains singleTop.
2846 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2847 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002848 int targetNdx = taskActivities.indexOf(target);
2849 if (targetNdx > 0) {
2850 ActivityRecord p = taskActivities.get(targetNdx - 1);
2851 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002852 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2853 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002854 }
2855 }
2856 }
2857 }
2858
2859 replyChainEnd = -1;
2860 }
2861 }
Craig Mautner77878772013-03-04 19:46:24 -08002862 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002863 }
2864
Craig Mautner8849a5e2013-04-02 16:41:03 -07002865 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002866 ActivityRecord newActivity) {
2867 boolean forceReset =
2868 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2869 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2870 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2871 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2872 forceReset = true;
2873 }
2874 }
2875
2876 final TaskRecord task = taskTop.task;
2877
2878 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2879 * for remaining tasks. Used for later tasks to reparent to task. */
2880 boolean taskFound = false;
2881
2882 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2883 ActivityOptions topOptions = null;
2884
Craig Mautner77878772013-03-04 19:46:24 -08002885 // Preserve the location for reparenting in the new task.
2886 int reparentInsertionPoint = -1;
2887
Craig Mautnere3a74d52013-02-22 14:14:58 -08002888 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2889 final TaskRecord targetTask = mTaskHistory.get(i);
2890
2891 if (targetTask == task) {
2892 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2893 taskFound = true;
2894 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002895 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2896 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002897 }
2898 }
2899
Craig Mautner70a86932013-02-28 22:37:44 -08002900 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002901 if (taskNdx >= 0) {
2902 do {
2903 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2904 } while (taskTop == null && taskNdx >= 0);
2905 }
Craig Mautner70a86932013-02-28 22:37:44 -08002906
Craig Mautnere3a74d52013-02-22 14:14:58 -08002907 if (topOptions != null) {
2908 // If we got some ActivityOptions from an activity on top that
2909 // was removed from the task, propagate them to the new real top.
2910 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002911 taskTop.updateOptionsLocked(topOptions);
2912 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002913 topOptions.abort();
2914 }
2915 }
2916
2917 return taskTop;
2918 }
2919
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002920 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2921 String resultWho, int requestCode, int resultCode, Intent data) {
2922
2923 if (callingUid > 0) {
2924 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002925 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002926 }
2927
2928 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2929 + " : who=" + resultWho + " req=" + requestCode
2930 + " res=" + resultCode + " data=" + data);
2931 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2932 try {
2933 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2934 list.add(new ResultInfo(resultWho, requestCode,
2935 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002936 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002937 return;
2938 } catch (Exception e) {
2939 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2940 }
2941 }
2942
2943 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2944 }
2945
Craig Mautner299f9602015-01-26 09:47:33 -08002946 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002947 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2948 return;
2949 }
2950
2951 final ActivityRecord next = topRunningActivityLocked();
2952 final String myReason = reason + " adjustFocus";
2953 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002954 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002955 // For freeform, docked, and pinned stacks we always keep the focus within the
2956 // stack as long as there is a running activity in the stack that we can adjust
2957 // focus to.
2958 mService.setFocusedActivityLocked(next, myReason);
2959 return;
2960 } else {
2961 final TaskRecord task = r.task;
2962 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2963 // For non-fullscreen stack, we want to move the focus to the next visible
2964 // stack to prevent the home screen from moving to the top and obscuring
2965 // other visible stacks.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002966 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002967 return;
2968 }
2969 // Move the home stack to the top if this stack is fullscreen or there is no
2970 // other visible stack.
2971 if (mStackSupervisor.moveHomeStackTaskToTop(
2972 task.getTaskToReturnTo(), myReason)) {
2973 // Activity focus was already adjusted. Nothing else to do...
2974 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002975 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002976 }
2977 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08002978 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002979
Wale Ogunwaled046a012015-12-24 13:05:59 -08002980 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002981 }
2982
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002983 private boolean adjustFocusToNextFocusableStackLocked(String reason) {
2984 final ActivityStack stack = getNextFocusableStackLocked();
2985 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002986 if (stack == null) {
2987 return false;
2988 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002989 return mService.setFocusedActivityLocked(stack.topRunningActivityLocked(), myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002990 }
2991
Craig Mautnerf3333272013-04-22 10:55:53 -07002992 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002993 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002994 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2995 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2996 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002997 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002998 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002999 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3000 "stop-no-history", false)) {
3001 // Activity was finished, no need to continue trying to schedule stop.
3002 adjustFocusedActivityLocked(r, "stopActivityFinished");
3003 r.resumeKeyDispatchingLocked();
3004 return;
3005 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003006 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003007 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003008 + " on stop because we're just sleeping");
3009 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003010 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003011 }
3012
3013 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08003014 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003015 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003016 try {
3017 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003018 if (DEBUG_STATES) Slog.v(TAG_STATES,
3019 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003020 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003021 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3022 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003023 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003024 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003025 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003026 EventLogTags.writeAmStopActivity(
3027 r.userId, System.identityHashCode(r), r.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003028 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07003029 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003030 r.setSleeping(true);
3031 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003032 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003033 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003034 } catch (Exception e) {
3035 // Maybe just ignore exceptions here... if the process
3036 // has crashed, our death notification will clean things
3037 // up.
3038 Slog.w(TAG, "Exception thrown during pause", e);
3039 // Just in case, assume it to be stopped.
3040 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003041 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003042 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003043 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003044 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003045 }
3046 }
3047 }
3048 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003049
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003050 /**
3051 * @return Returns true if the activity is being finished, false if for
3052 * some reason it is being left as-is.
3053 */
3054 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003055 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003056 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003057 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3058 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003059 + ", result=" + resultCode + ", data=" + resultData
3060 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003061 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003062 return false;
3063 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003064
Craig Mautnerd44711d2013-02-23 11:24:36 -08003065 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003066 return true;
3067 }
3068
Craig Mautnerd2328952013-03-05 12:46:26 -08003069 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003070 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3071 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3072 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3073 ActivityRecord r = activities.get(activityNdx);
3074 if (r.resultTo == self && r.requestCode == requestCode) {
3075 if ((r.resultWho == null && resultWho == null) ||
3076 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3077 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3078 false);
3079 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003080 }
3081 }
3082 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003083 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003084 }
3085
Adrian Roos20d7df32016-01-12 18:59:43 +01003086 final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003087 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003088 TaskRecord finishedTask = null;
3089 if (r == null || r.app != app) {
3090 return null;
3091 }
3092 Slog.w(TAG, " Force finishing activity "
3093 + r.intent.getComponent().flattenToShortString());
3094 int taskNdx = mTaskHistory.indexOf(r.task);
3095 int activityNdx = r.task.mActivities.indexOf(r);
3096 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3097 finishedTask = r.task;
3098 // Also terminate any activities below it that aren't yet
3099 // stopped, to avoid a situation where one will get
3100 // re-start our crashing activity once it gets resumed again.
3101 --activityNdx;
3102 if (activityNdx < 0) {
3103 do {
3104 --taskNdx;
3105 if (taskNdx < 0) {
3106 break;
3107 }
3108 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3109 } while (activityNdx < 0);
3110 }
3111 if (activityNdx >= 0) {
3112 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3113 if (r.state == ActivityState.RESUMED
3114 || r.state == ActivityState.PAUSING
3115 || r.state == ActivityState.PAUSED) {
3116 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3117 Slog.w(TAG, " Force finishing activity "
3118 + r.intent.getComponent().flattenToShortString());
3119 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003120 }
3121 }
3122 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003123 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003124 }
3125
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003126 final void finishVoiceTask(IVoiceInteractionSession session) {
3127 IBinder sessionBinder = session.asBinder();
3128 boolean didOne = false;
3129 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3130 TaskRecord tr = mTaskHistory.get(taskNdx);
3131 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3132 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3133 ActivityRecord r = tr.mActivities.get(activityNdx);
3134 if (!r.finishing) {
3135 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3136 false);
3137 didOne = true;
3138 }
3139 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003140 } else {
3141 // Check if any of the activities are using voice
3142 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3143 ActivityRecord r = tr.mActivities.get(activityNdx);
3144 if (r.voiceSession != null
3145 && r.voiceSession.asBinder() == sessionBinder) {
3146 // Inform of cancellation
3147 r.clearVoiceSessionLocked();
3148 try {
3149 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3150 null);
3151 } catch (RemoteException re) {
3152 // Ok
3153 }
3154 // TODO: VI This is redundant in some cases
3155 mService.finishRunningVoiceLocked();
3156 break;
3157 }
3158 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003159 }
3160 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003161 Slog.d(TAG, "ActivityStack.finishVoiceTask()");
3162
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003163 if (didOne) {
3164 mService.updateOomAdjLocked();
3165 }
3166 }
3167
Craig Mautnerd2328952013-03-05 12:46:26 -08003168 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003169 ArrayList<ActivityRecord> activities = r.task.mActivities;
3170 for (int index = activities.indexOf(r); index >= 0; --index) {
3171 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003172 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003173 break;
3174 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003175 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003176 }
3177 return true;
3178 }
3179
Dianne Hackborn5c607432012-02-28 14:44:19 -08003180 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3181 // send the result
3182 ActivityRecord resultTo = r.resultTo;
3183 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003184 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003185 + " who=" + r.resultWho + " req=" + r.requestCode
3186 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003187 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003188 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003189 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003190 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003191 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003192 if (r.info.applicationInfo.uid > 0) {
3193 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3194 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003195 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003196 }
3197 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3198 resultData);
3199 r.resultTo = null;
3200 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003201 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003202
3203 // Make sure this HistoryRecord is not holding on to other resources,
3204 // because clients have remote IPC references to this object so we
3205 // can't assume that will go away and want to avoid circular IPC refs.
3206 r.results = null;
3207 r.pendingResults = null;
3208 r.newIntents = null;
3209 r.icicle = null;
3210 }
3211
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003212 /**
3213 * @return Returns true if this activity has been removed from the history
3214 * list, or false if it is still in the list and will be removed later.
3215 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003216 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3217 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003218 if (r.finishing) {
3219 Slog.w(TAG, "Duplicate finish request for " + r);
3220 return false;
3221 }
3222
Wale Ogunwale7d701172015-03-11 15:36:30 -07003223 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003224 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003225 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003226 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003227 task.taskId, r.shortComponentName, reason);
3228 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003229 final int index = activities.indexOf(r);
3230 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003231 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003232 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003233 // If the caller asked that this activity (and all above it)
3234 // be cleared when the task is reset, don't lose that information,
3235 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003236 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003237 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003238 }
3239 }
3240
3241 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003242
Craig Mautner299f9602015-01-26 09:47:33 -08003243 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003244
Dianne Hackborn5c607432012-02-28 14:44:19 -08003245 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003246
Craig Mautnerde4ef022013-04-07 19:01:33 -07003247 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003248 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003249 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003250 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003251 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003252 ? AppTransition.TRANSIT_TASK_CLOSE
3253 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003254
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003255 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003256 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003257
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003258 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003259 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3260 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3261 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003262 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003263 }
3264
Craig Mautneraea74a52014-03-08 14:23:10 -08003265 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003266 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003267 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003268 } else if (r.state != ActivityState.PAUSING) {
3269 // If the activity is PAUSING, we will complete the finish once
3270 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003271 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003272 if (r.visible) {
3273 mWindowManager.setAppVisibility(r.appToken, false);
3274 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003275 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003276 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003277 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003278 }
3279
3280 return false;
3281 }
3282
Craig Mautnerf3333272013-04-22 10:55:53 -07003283 static final int FINISH_IMMEDIATELY = 0;
3284 static final int FINISH_AFTER_PAUSE = 1;
3285 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003286
Craig Mautnerf3333272013-04-22 10:55:53 -07003287 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003288 // First things first: if this activity is currently visible,
3289 // and the resumed activity is not yet visible, then hold off on
3290 // finishing until the resumed one becomes visible.
3291 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003292 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08003293 addToStopping(r, false /* immediate */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003294 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003295 if (DEBUG_STATES) Slog.v(TAG_STATES,
3296 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003297 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003298 if (oomAdj) {
3299 mService.updateOomAdjLocked();
3300 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003301 return r;
3302 }
3303
3304 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003305 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003306 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003307 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003308 if (mResumedActivity == r) {
3309 mResumedActivity = null;
3310 }
3311 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003312 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003313 r.state = ActivityState.FINISHING;
3314
3315 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003316 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003317 || prevState == ActivityState.STOPPED
3318 || prevState == ActivityState.INITIALIZING) {
3319 // If this activity is already stopped, we can just finish
3320 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003321 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003322 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003323 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003324 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003325 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003326 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003327 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3328 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003329 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003330 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003331
3332 // Need to go through the full pause cycle to get this
3333 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003334 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003335 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003336 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003337 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003338 return r;
3339 }
3340
Craig Mautneree36c772014-07-16 14:56:05 -07003341 void finishAllActivitiesLocked(boolean immediately) {
3342 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003343 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3344 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3345 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3346 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003347 noActivitiesInStack = false;
3348 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003349 continue;
3350 }
Craig Mautneree36c772014-07-16 14:56:05 -07003351 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003352 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3353 }
3354 }
Craig Mautneree36c772014-07-16 14:56:05 -07003355 if (noActivitiesInStack) {
3356 mActivityContainer.onTaskListEmptyLocked();
3357 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003358 }
3359
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003360 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3361 // Basic case: for simple app-centric recents, we need to recreate
3362 // the task if the affinity has changed.
3363 if (srec == null || srec.task.affinity == null ||
3364 !srec.task.affinity.equals(destAffinity)) {
3365 return true;
3366 }
3367 // Document-centric case: an app may be split in to multiple documents;
3368 // they need to re-create their task if this current activity is the root
3369 // of a document, unless simply finishing it will return them to the the
3370 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003371 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3372 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003373 // Okay, this activity is at the root of its task. What to do, what to do...
3374 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3375 // Finishing won't return to an application, so we need to recreate.
3376 return true;
3377 }
3378 // We now need to get the task below it to determine what to do.
3379 int taskIdx = mTaskHistory.indexOf(srec.task);
3380 if (taskIdx <= 0) {
3381 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3382 return false;
3383 }
3384 if (taskIdx == 0) {
3385 // At the bottom of the stack, nothing to go back to.
3386 return true;
3387 }
3388 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3389 if (!srec.task.affinity.equals(prevTask.affinity)) {
3390 // These are different apps, so need to recreate.
3391 return true;
3392 }
3393 }
3394 return false;
3395 }
3396
Wale Ogunwale7d701172015-03-11 15:36:30 -07003397 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003398 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003399 final TaskRecord task = srec.task;
3400 final ArrayList<ActivityRecord> activities = task.mActivities;
3401 final int start = activities.indexOf(srec);
3402 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003403 return false;
3404 }
3405 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003406 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003407 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003408 final ComponentName dest = destIntent.getComponent();
3409 if (start > 0 && dest != null) {
3410 for (int i = finishTo; i >= 0; i--) {
3411 ActivityRecord r = activities.get(i);
3412 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003413 r.info.name.equals(dest.getClassName())) {
3414 finishTo = i;
3415 parent = r;
3416 foundParentInTask = true;
3417 break;
3418 }
3419 }
3420 }
3421
3422 IActivityController controller = mService.mController;
3423 if (controller != null) {
3424 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3425 if (next != null) {
3426 // ask watcher if this is allowed
3427 boolean resumeOK = true;
3428 try {
3429 resumeOK = controller.activityResuming(next.packageName);
3430 } catch (RemoteException e) {
3431 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003432 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003433 }
3434
3435 if (!resumeOK) {
3436 return false;
3437 }
3438 }
3439 }
3440 final long origId = Binder.clearCallingIdentity();
3441 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003442 ActivityRecord r = activities.get(i);
3443 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003444 // Only return the supplied result for the first activity finished
3445 resultCode = Activity.RESULT_CANCELED;
3446 resultData = null;
3447 }
3448
3449 if (parent != null && foundParentInTask) {
3450 final int parentLaunchMode = parent.info.launchMode;
3451 final int destIntentFlags = destIntent.getFlags();
3452 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3453 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3454 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3455 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003456 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3457 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003458 } else {
3459 try {
3460 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3461 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003462 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3463 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3464 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003465 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3466 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003467 foundParentInTask = res == ActivityManager.START_SUCCESS;
3468 } catch (RemoteException e) {
3469 foundParentInTask = false;
3470 }
3471 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003472 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003473 }
3474 }
3475 Binder.restoreCallingIdentity(origId);
3476 return foundParentInTask;
3477 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003478 /**
3479 * Perform the common clean-up of an activity record. This is called both
3480 * as part of destroyActivityLocked() (when destroying the client-side
3481 * representation) and cleaning things up as a result of its hosting
3482 * processing going away, in which case there is no remaining client-side
3483 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003484 *
3485 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003486 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003487 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3488 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003489 if (mResumedActivity == r) {
3490 mResumedActivity = null;
3491 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003492 if (mPausingActivity == r) {
3493 mPausingActivity = null;
3494 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003495 mService.resetFocusedActivityIfNeededLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003496
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003497 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003498 r.frozenBeforeDestroy = false;
3499
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003500 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003501 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003502 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003503 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003504 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003505 }
3506
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003507 // Make sure this record is no longer in the pending finishes list.
3508 // This could happen, for example, if we are trimming activities
3509 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003510 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003511 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003512
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003513 // Remove any pending results.
3514 if (r.finishing && r.pendingResults != null) {
3515 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3516 PendingIntentRecord rec = apr.get();
3517 if (rec != null) {
3518 mService.cancelIntentSenderLocked(rec, false);
3519 }
3520 }
3521 r.pendingResults = null;
3522 }
3523
3524 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003525 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003526 }
3527
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003528 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003529 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003530 if (getVisibleBehindActivity() == r) {
3531 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003532 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003533 }
3534
3535 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003536 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003537 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003538 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003539 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003540 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003541 }
3542
Craig Mautner299f9602015-01-26 09:47:33 -08003543 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003544 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003545 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003546 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003547 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3548 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3549
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003550 r.takeFromHistory();
3551 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003552 if (DEBUG_STATES) Slog.v(TAG_STATES,
3553 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003554 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003555 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003556 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003557 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003558 if (VALIDATE_TOKENS) {
3559 validateAppTokensLocked();
3560 }
Craig Mautner312ba862014-02-10 17:55:01 -08003561 final TaskRecord task = r.task;
3562 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003563 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003564 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003565 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003566 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003567 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003568 }
Craig Mautner299f9602015-01-26 09:47:33 -08003569 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003570 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003571 cleanUpActivityServicesLocked(r);
3572 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003573 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003574
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003575 /**
3576 * Perform clean-up of service connections in an activity record.
3577 */
3578 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3579 // Throw away any services that have been bound by this activity.
3580 if (r.connections != null) {
3581 Iterator<ConnectionRecord> it = r.connections.iterator();
3582 while (it.hasNext()) {
3583 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003584 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003585 }
3586 r.connections = null;
3587 }
3588 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003589
Craig Mautneree2e45a2014-06-27 12:10:03 -07003590 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003591 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003592 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003593 mHandler.sendMessage(msg);
3594 }
3595
Craig Mautneree2e45a2014-06-27 12:10:03 -07003596 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003597 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003598 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003599 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3600 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3601 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3602 final ActivityRecord r = activities.get(activityNdx);
3603 if (r.finishing) {
3604 continue;
3605 }
3606 if (r.fullscreen) {
3607 lastIsOpaque = true;
3608 }
3609 if (owner != null && r.app != owner) {
3610 continue;
3611 }
3612 if (!lastIsOpaque) {
3613 continue;
3614 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003615 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003616 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003617 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003618 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003619 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003620 activityRemoved = true;
3621 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003622 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003623 }
3624 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003625 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003626 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003627 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003628 }
3629
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003630 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3631 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003632 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3633 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003634 + " pausing=" + mPausingActivity + " for reason " + reason);
3635 return destroyActivityLocked(r, true, reason);
3636 }
3637 return false;
3638 }
3639
3640 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3641 String reason) {
3642 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003643 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003644 int maxTasks = tasks.size() / 4;
3645 if (maxTasks < 1) {
3646 maxTasks = 1;
3647 }
3648 int numReleased = 0;
3649 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3650 final TaskRecord task = mTaskHistory.get(taskNdx);
3651 if (!tasks.contains(task)) {
3652 continue;
3653 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003654 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003655 int curNum = 0;
3656 final ArrayList<ActivityRecord> activities = task.mActivities;
3657 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3658 final ActivityRecord activity = activities.get(actNdx);
3659 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003660 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003661 + " in state " + activity.state + " resumed=" + mResumedActivity
3662 + " pausing=" + mPausingActivity + " for reason " + reason);
3663 destroyActivityLocked(activity, true, reason);
3664 if (activities.get(actNdx) != activity) {
3665 // Was removed from list, back up so we don't miss the next one.
3666 actNdx--;
3667 }
3668 curNum++;
3669 }
3670 }
3671 if (curNum > 0) {
3672 numReleased += curNum;
3673 maxTasks--;
3674 if (mTaskHistory.get(taskNdx) != task) {
3675 // The entire task got removed, back up so we don't miss the next one.
3676 taskNdx--;
3677 }
3678 }
3679 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003680 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3681 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003682 return numReleased;
3683 }
3684
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003685 /**
3686 * Destroy the current CLIENT SIDE instance of an activity. This may be
3687 * called both when actually finishing an activity, or when performing
3688 * a configuration switch where we destroy the current client-side object
3689 * but then create a new client-side object for this same HistoryRecord.
3690 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003691 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003692 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3693 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003694 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003695 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003696 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003697 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003698
3699 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003700
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003701 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003702
3703 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003704
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003705 if (hadApp) {
3706 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003707 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003708 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3709 mService.mHeavyWeightProcess = null;
3710 mService.mHandler.sendEmptyMessage(
3711 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3712 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003713 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003714 // Update any services we are bound to that might care about whether
3715 // their client may have activities.
3716 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003717 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003718 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003719 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003720 }
3721 }
3722
3723 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003724
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003725 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003726 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003727 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003728 r.configChangeFlags);
3729 } catch (Exception e) {
3730 // We can just ignore exceptions here... if the process
3731 // has crashed, our death notification will clean things
3732 // up.
3733 //Slog.w(TAG, "Exception thrown during finish", e);
3734 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003735 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003736 removedFromHistory = true;
3737 skipDestroy = true;
3738 }
3739 }
3740
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003741 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003742
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003743 // If the activity is finishing, we need to wait on removing it
3744 // from the list to give it a chance to do its cleanup. During
3745 // that time it may make calls back with its token so we need to
3746 // be able to find it on the list and so we don't want to remove
3747 // it from the list yet. Otherwise, we can just immediately put
3748 // it in the destroyed state since we are not removing it from the
3749 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003750 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003751 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003752 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003753 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003754 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003755 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3756 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003757 if (DEBUG_STATES) Slog.v(TAG_STATES,
3758 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003759 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003760 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003761 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003762 }
3763 } else {
3764 // remove this record from the history.
3765 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003766 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003767 removedFromHistory = true;
3768 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003769 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003770 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003771 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003772 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003773 }
3774 }
3775
3776 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003777
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003778 if (!mLRUActivities.remove(r) && hadApp) {
3779 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3780 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003781
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003782 return removedFromHistory;
3783 }
3784
Craig Mautner299f9602015-01-26 09:47:33 -08003785 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003786 final long origId = Binder.clearCallingIdentity();
3787 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003788 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003789 if (r != null) {
3790 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003791 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003792 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003793
Wale Ogunwale60454db2015-01-23 16:05:07 -08003794 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003795 if (r.state == ActivityState.DESTROYING) {
3796 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003797 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003798 }
3799 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003800 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003801 } finally {
3802 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003803 }
3804 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003805
Todd Kennedyaab56db2015-01-30 09:39:53 -08003806 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003807 if (hasVisibleBehindActivity() &&
3808 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08003809 if (r == topRunningActivityLocked() && getStackVisibilityLocked() == STACK_VISIBLE) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003810 // Don't release the top activity if it has requested to run behind the next
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08003811 // activity and the stack is currently visible.
Craig Mautner64ccb702014-10-01 09:38:40 -07003812 return;
3813 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003814 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003815 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003816 " thread=" + r.app.thread);
3817 if (r != null && r.app != null && r.app.thread != null) {
3818 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003819 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003820 } catch (RemoteException e) {
3821 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003822 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003823 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003824 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003825 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003826 }
3827 }
3828 }
3829
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003830 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003831 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3832 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003833 if (r != null) {
3834 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003835 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003836 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003837 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003838 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003839 }
3840
Jose Lima4b6c6692014-08-12 17:41:12 -07003841 boolean hasVisibleBehindActivity() {
3842 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003843 }
3844
Jose Lima4b6c6692014-08-12 17:41:12 -07003845 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003846 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003847 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003848 }
3849 }
3850
Jose Lima4b6c6692014-08-12 17:41:12 -07003851 ActivityRecord getVisibleBehindActivity() {
3852 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003853 }
3854
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003855 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3856 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003857 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003858 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3859 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003860 while (i > 0) {
3861 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003862 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003863 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003864 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003865 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003866 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003867 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003868 }
3869 }
3870 }
3871
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003872 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3873 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003874 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3875 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003876 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3877 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003878 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003879 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003880 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3881 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003882
3883 boolean hasVisibleActivities = false;
3884
3885 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003886 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003887 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3888 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003889 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3890 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3891 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3892 final ActivityRecord r = activities.get(activityNdx);
3893 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003894 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3895 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003896 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003897 if (r.visible) {
3898 hasVisibleActivities = true;
3899 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003900 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003901 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3902 // Don't currently have state for the activity, or
3903 // it is finishing -- always remove it.
3904 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003905 } else if (!r.visible && r.launchCount > 2 &&
3906 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003907 // We have launched this activity too many times since it was
3908 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003909 // (Note if the activity is visible, we don't remove the record.
3910 // We leave the dead window on the screen but the process will
3911 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003912 remove = true;
3913 } else {
3914 // The process may be gone, but the activity lives on!
3915 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003916 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003917 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003918 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3919 "Removing activity " + r + " from stack at " + i
3920 + ": haveState=" + r.haveState
3921 + " stateNotNeeded=" + r.stateNotNeeded
3922 + " finishing=" + r.finishing
3923 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003924 if (!r.finishing) {
3925 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3926 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3927 r.userId, System.identityHashCode(r),
3928 r.task.taskId, r.shortComponentName,
3929 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003930 if (r.state == ActivityState.RESUMED) {
3931 mService.updateUsageStats(r, false);
3932 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003933 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003934 } else {
3935 // We have the current state for this activity, so
3936 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003937 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3938 if (DEBUG_APP) Slog.v(TAG_APP,
3939 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003940 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003941 // Set nowVisible to previous visible state. If the app was visible while
3942 // it died, we leave the dead window on screen so it's basically visible.
3943 // This is needed when user later tap on the dead window, we need to stop
3944 // other apps when user transfers focus to the restarted activity.
3945 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003946 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003947 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003948 "App died, clearing saved state of " + r);
3949 r.icicle = null;
3950 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003951 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003952 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003953 if (remove) {
3954 removeActivityFromHistoryLocked(r, "appDied");
3955 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003956 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003957 }
3958 }
3959
3960 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003961 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003962
Chong Zhang280d3322015-11-03 17:27:26 -08003963 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003964 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003965 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003966 if (r != null && r.state != ActivityState.RESUMED) {
3967 r.updateOptionsLocked(options);
3968 } else {
3969 ActivityOptions.abort(options);
3970 }
3971 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003972 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003973 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003974
Craig Mautner21d24a22014-04-23 11:45:37 -07003975 void updateTaskMovement(TaskRecord task, boolean toFront) {
3976 if (task.isPersistable) {
3977 task.mLastTimeMoved = System.currentTimeMillis();
3978 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3979 // recently will be most negative, tasks sent to the bottom before that will be less
3980 // negative. Similarly for recent tasks moved to the top which will be most positive.
3981 if (!toFront) {
3982 task.mLastTimeMoved *= -1;
3983 }
3984 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003985 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003986 }
3987
Craig Mautner84984fa2014-06-19 11:19:20 -07003988 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003989 final int top = mTaskHistory.size() - 1;
3990 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3991 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003992 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003993 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3994 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003995 mTaskHistory.remove(taskNdx);
3996 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003997 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003998 return;
3999 }
4000 }
4001 }
4002
Chong Zhang280d3322015-11-03 17:27:26 -08004003 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004004 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004005 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004006
Craig Mautner11bf9a52013-02-19 14:08:51 -08004007 final int numTasks = mTaskHistory.size();
4008 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004009 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004010 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004011 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004012 ActivityOptions.abort(options);
4013 } else {
4014 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
4015 }
4016 return;
4017 }
4018
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004019 if (timeTracker != null) {
4020 // The caller wants a time tracker associated with this task.
4021 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4022 tr.mActivities.get(i).appTimeTracker = timeTracker;
4023 }
4024 }
4025
Craig Mautner11bf9a52013-02-19 14:08:51 -08004026 // Shift all activities with this task up to the top
4027 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004028 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004029
Chong Zhang45c25ce2015-08-10 22:18:26 -07004030 // Don't refocus if invisible to current user
4031 ActivityRecord top = tr.getTopActivity();
4032 if (!okToShowLocked(top)) {
4033 addRecentActivityLocked(top);
4034 ActivityOptions.abort(options);
4035 return;
4036 }
4037
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004038 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004039 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004040 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004041
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004042 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004043 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004044 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004045 if (r != null) {
4046 mNoAnimActivities.add(r);
4047 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004048 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004049 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08004050 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004051 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004052
Wale Ogunwaled046a012015-12-24 13:05:59 -08004053 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004054 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004055
4056 if (VALIDATE_TOKENS) {
4057 validateAppTokensLocked();
4058 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004059 }
4060
4061 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004062 * Worker method for rearranging history stack. Implements the function of moving all
4063 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004064 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004065 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004066 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4067 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004068 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004069 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004070 * @return Returns true if the move completed, false if not.
4071 */
Craig Mautner299f9602015-01-26 09:47:33 -08004072 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004073 final TaskRecord tr = taskForIdLocked(taskId);
4074 if (tr == null) {
4075 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4076 return false;
4077 }
4078
4079 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07004080 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004081
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004082 // If we have a watcher, preflight the move before committing to it. First check
4083 // for *other* available tasks, but if none are available, then try again allowing the
4084 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004085 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004086 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004087 if (next == null) {
4088 next = topRunningActivityLocked(null, 0);
4089 }
4090 if (next != null) {
4091 // ask watcher if this is allowed
4092 boolean moveOK = true;
4093 try {
4094 moveOK = mService.mController.activityResuming(next.packageName);
4095 } catch (RemoteException e) {
4096 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004097 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004098 }
4099 if (!moveOK) {
4100 return false;
4101 }
4102 }
4103 }
4104
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004105 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004106
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004107 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4108 // For the case where we are moving the home task back and there is an activity visible
4109 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4110 // activity to maintain order with what the user is seeing.
4111 final ActivityStack fullscreenStack =
4112 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4113 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4114 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4115 mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004116 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004117 return true;
4118 }
4119 }
4120
riddle_hsuc215a4f2014-12-27 12:10:45 +08004121 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004122
4123 // If true, we should resume the home activity next if the task we are moving to the
4124 // back is over the home stack. We force to false if the task we are moving to back
4125 // is the home task and we don't want it resumed after moving to the back.
4126 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4127 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004128 final TaskRecord nextTask = getNextTask(tr);
4129 if (nextTask != null) {
4130 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4131 } else {
4132 prevIsHome = true;
4133 }
4134 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004135 mTaskHistory.remove(tr);
4136 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004137 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004138
Craig Mautnerc8143c62013-09-03 12:15:57 -07004139 // There is an assumption that moving a task to the back moves it behind the home activity.
4140 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004141 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004142 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4143 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004144 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004145 break;
4146 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004147 if (taskNdx == 1) {
4148 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004149 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004150 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004151 }
4152
Craig Mautner299f9602015-01-26 09:47:33 -08004153 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004154 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004155
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004156 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004157 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004158 }
4159
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004160 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004161 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004162 if (!mService.mBooting && !mService.mBooted) {
4163 // Not ready yet!
4164 return false;
4165 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004166 final int taskToReturnTo = tr.getTaskToReturnTo();
4167 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004168 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004169 }
4170
Wale Ogunwaled046a012015-12-24 13:05:59 -08004171 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004172 return true;
4173 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004174
Craig Mautner8849a5e2013-04-02 16:41:03 -07004175 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004176 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004177 final Uri data = r.intent.getData();
4178 final String strData = data != null ? data.toSafeString() : null;
4179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004180 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004181 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004182 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004183 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004184 }
4185
4186 /**
4187 * Make sure the given activity matches the current configuration. Returns
4188 * false if the activity had to be destroyed. Returns true if the
4189 * configuration is the same, or the activity will remain running as-is
4190 * for whatever reason. Ensures the HistoryRecord is updated with the
4191 * correct configuration and all other bookkeeping is handled.
4192 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004193 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4194 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004195 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004196 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004197 "Skipping config check (will change): " + r);
4198 return true;
4199 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004200
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004201 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004202 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004203
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004204 // Short circuit: if the two configurations are the exact same
4205 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004206 final Configuration newConfig = mService.mConfiguration;
4207 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004208 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004209 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004210 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004211 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004212 "Configuration unchanged in " + r);
4213 return true;
4214 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004215
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004216 // We don't worry about activities that are finishing.
4217 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004218 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004219 "Configuration doesn't matter in finishing " + r);
4220 r.stopFreezingScreenLocked(false);
4221 return true;
4222 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004223
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004224 // Okay we now are going to make this activity have the new config.
4225 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004226 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004227 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004228 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004229 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004230
Filip Gruszczynski23493322015-07-29 17:02:59 -07004231 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004232 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004233 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004234 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004235 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004236 // There are no significant differences, so we won't relaunch but should still deliver
4237 // the new configuration to the client process.
4238 r.scheduleConfigurationChanged(taskConfig, false);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004239 return true;
4240 }
4241
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004242 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4243 "Configuration changes for " + r + " ; taskChanges="
4244 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4245 + Configuration.configurationDiffToString(changes));
4246
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004247 // If the activity isn't currently running, just leave the new
4248 // configuration and it will pick that up next time it starts.
4249 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004250 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004251 "Configuration doesn't matter not running " + r);
4252 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004253 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004254 return true;
4255 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004256
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004257 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004258 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4259 "Checking to restart " + r.info.name + ": changed=0x"
4260 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004261 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4262 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004263
Dianne Hackborne6676352011-06-01 16:51:20 -07004264 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004265 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4266 r.configChangeFlags |= changes;
4267 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004268 r.forceNewConfig = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004269 preserveWindow &= isResizeOnlyChange(changes);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004270 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004271 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004272 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004273 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004274 } else if (r.state == ActivityState.PAUSING) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004275 // A little annoying: we are waiting for this activity to finish pausing. Let's not
4276 // do anything now, but just flag that it needs to be restarted when done pausing.
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 skipping already pausing " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004279 r.deferRelaunchUntilPaused = true;
4280 r.preserveWindowOnDeferredRelaunch = preserveWindow;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004281 return true;
4282 } else if (r.state == ActivityState.RESUMED) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004283 // Try to optimize this case: the configuration is changing and we need to restart
4284 // the top, resumed activity. Instead of doing the normal handshaking, just say
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004285 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004286 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004287 "Config is relaunching resumed " + r);
Chong Zhang2b79af12016-02-10 18:47:06 -08004288
4289 if (DEBUG_STATES && !r.visible) {
4290 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + r
4291 + " called by " + Debug.getCallers(4));
4292 }
4293
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004294 relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004295 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004296 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004297 "Config is relaunching non-resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004298 relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004299 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004300
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004301 // All done... tell the caller we weren't able to keep this activity around.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004302 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004303 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004304
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004305 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004306 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004307 // changes is always sent to all processes when they happen so it can just use whatever
4308 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004309 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004310 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004311
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004312 return true;
4313 }
4314
Filip Gruszczynski23493322015-07-29 17:02:59 -07004315 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4316 Configuration oldTaskOverride) {
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08004317
4318 // If we went from full-screen to non-full-screen, make sure to use the correct
4319 // configuration task diff, so the diff stays as small as possible.
4320 if (Configuration.EMPTY.equals(oldTaskOverride)
4321 && !Configuration.EMPTY.equals(taskConfig)) {
4322 oldTaskOverride = record.task.extractOverrideConfig(record.configuration);
4323 }
4324
Jorim Jaggi1b025a62016-02-03 19:27:49 -08004325 // Conversely, do the same when going the other direction.
4326 if (Configuration.EMPTY.equals(taskConfig)
4327 && !Configuration.EMPTY.equals(oldTaskOverride)) {
4328 taskConfig = record.task.extractOverrideConfig(record.configuration);
4329 }
4330
Filip Gruszczynski23493322015-07-29 17:02:59 -07004331 // Determine what has changed. May be nothing, if this is a config
4332 // that has come back from the app after going idle. In that case
4333 // we just want to leave the official config object now in the
4334 // activity and do nothing else.
4335 int taskChanges = oldTaskOverride.diff(taskConfig);
4336 // We don't want to use size changes if they don't cross boundaries that are important to
4337 // the app.
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004338 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004339 final boolean crosses = record.crossesHorizontalSizeThreshold(
4340 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4341 || record.crossesVerticalSizeThreshold(
4342 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4343 if (!crosses) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004344 taskChanges &= ~CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004345 }
4346 }
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004347 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004348 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4349 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004350 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004351 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004352 }
4353 }
4354 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4355 }
4356
4357 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4358 Configuration oldTaskOverride, int taskChanges) {
4359 if (taskChanges == 0) {
4360 // {@link Configuration#diff} doesn't catch changes from unset values.
4361 // Check for changes we care about.
4362 if (oldTaskOverride.orientation != taskConfig.orientation) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004363 taskChanges |= CONFIG_ORIENTATION;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004364 }
4365 // We want to explicitly track situations where the size configuration goes from
4366 // undefined to defined. We don't care about crossing the threshold in that case,
4367 // because there is no threshold.
4368 final int oldHeight = oldTaskOverride.screenHeightDp;
4369 final int newHeight = taskConfig.screenHeightDp;
4370 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4371 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4372 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004373 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004374 }
4375 final int oldWidth = oldTaskOverride.screenWidthDp;
4376 final int newWidth = taskConfig.screenWidthDp;
4377 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4378 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4379 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004380 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004381 }
4382 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4383 final int newSmallest = taskConfig.smallestScreenWidthDp;
4384 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4385 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4386 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004387 taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4388 }
4389 final int oldLayout = oldTaskOverride.screenLayout;
4390 final int newLayout = taskConfig.screenLayout;
4391 if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4392 || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4393 taskChanges |= CONFIG_SCREEN_LAYOUT;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004394 }
4395 }
4396 return taskChanges;
4397 }
4398
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004399 private static boolean isResizeOnlyChange(int change) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004400 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4401 | CONFIG_SCREEN_LAYOUT)) == 0;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004402 }
4403
Wale Ogunwale83301a92015-09-24 15:54:08 -07004404 private void relaunchActivityLocked(
4405 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4406 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004407 r.configChangeFlags = 0;
Wale Ogunwale83301a92015-09-24 15:54:08 -07004408 return;
4409 }
4410
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004411 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004412 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004413 if (andResume) {
4414 results = r.results;
4415 newIntents = r.newIntents;
4416 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004417 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4418 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004419 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004420 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004421 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004422 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004423
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004424 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004425
Craig Mautner34b73df2014-01-12 21:11:08 -08004426 mStackSupervisor.removeChildActivityContainers(r);
4427
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004428 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004429 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08004430 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r
4431 + " callers=" + Debug.getCallers(6));
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004432 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004433 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4434 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004435 new Configuration(r.task.mOverrideConfig), preserveWindow);
Jorim Jaggife89d122015-12-22 16:28:44 +01004436 mStackSupervisor.activityRelaunchingLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004437 // Note: don't need to call pauseIfSleepingLocked() here, because
4438 // the caller will only pass in 'andResume' if this activity is
4439 // currently resumed, which implies we aren't sleeping.
4440 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004441 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004442 }
4443
4444 if (andResume) {
Chong Zhang2b79af12016-02-10 18:47:06 -08004445 if (DEBUG_STATES) {
4446 Slog.d(TAG_STATES, "Resumed after relaunch " + r);
4447 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004448 r.state = ActivityState.RESUMED;
Chong Zhang2b79af12016-02-10 18:47:06 -08004449 // Relaunch-resume could happen either when the app is already in the front,
4450 // or while it's being brought to front. In the latter case, it's marked RESUMED
4451 // but not yet visible (or stopped). We need to complete the resume here as the
4452 // code in resumeTopActivityInnerLocked to complete the resume might be skipped.
4453 if (!r.visible || r.stopped) {
4454 mWindowManager.setAppVisibility(r.appToken, true);
4455 completeResumeLocked(r);
4456 } else {
4457 r.results = null;
4458 r.newIntents = null;
4459 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004460 } else {
4461 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4462 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004463 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004464
4465 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004466 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004467
4468 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004469 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4470 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4471 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4472 final ActivityRecord r = activities.get(activityNdx);
4473 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004474 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004475 }
4476 if (r.fullscreen && !r.finishing) {
4477 return false;
4478 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004479 }
4480 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004481 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004482 if (r == null) {
4483 return false;
4484 }
4485 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4486 + " would have returned true for r=" + r);
4487 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004488 }
4489
4490 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004491 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4492 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4493 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4494 final ActivityRecord r = activities.get(activityNdx);
4495 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004496 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004497 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004498 }
4499 }
4500 }
4501
Wale Ogunwale540e1232015-05-01 15:35:39 -07004502 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4503 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004504 boolean didSomething = false;
4505 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004506 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004507 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4508 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4509 int numActivities = activities.size();
4510 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4511 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004512 final boolean sameComponent =
4513 (r.packageName.equals(packageName) && (filterByClasses == null
4514 || filterByClasses.contains(r.realActivity.getClassName())))
4515 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004516 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004517 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004518 && (r.app == null || evenPersistent || !r.app.persistent)) {
4519 if (!doit) {
4520 if (r.finishing) {
4521 // If this activity is just finishing, then it is not
4522 // interesting as far as something to stop.
4523 continue;
4524 }
4525 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004526 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004527 if (r.isHomeActivity()) {
4528 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4529 Slog.i(TAG, "Skip force-stop again " + r);
4530 continue;
4531 } else {
4532 homeActivity = r.realActivity;
4533 }
4534 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004535 didSomething = true;
4536 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004537 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004538 if (r.app != null) {
4539 r.app.removed = true;
4540 }
4541 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004542 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004543 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004544 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4545 true)) {
4546 // r has been deleted from mActivities, accommodate.
4547 --numActivities;
4548 --activityNdx;
4549 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004550 }
4551 }
4552 }
4553 return didSomething;
4554 }
4555
Dianne Hackborn09233282014-04-30 11:33:59 -07004556 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004557 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4558 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004559 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004560 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004561 if (task.getTopActivity() == null) {
4562 continue;
4563 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004564 ActivityRecord r = null;
4565 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004566 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004567 int numActivities = 0;
4568 int numRunning = 0;
4569 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004570 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004571 continue;
4572 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004573 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004574 tmp = activities.get(activityNdx);
4575 if (tmp.finishing) {
4576 continue;
4577 }
4578 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004579
Craig Mautneraab647e2013-02-28 16:31:36 -08004580 // Initialize state for next task if needed.
4581 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4582 top = r;
4583 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004584 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004585
4586 // Add 'r' into the current task.
4587 numActivities++;
4588 if (r.app != null && r.app.thread != null) {
4589 numRunning++;
4590 }
4591
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004592 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004593 TAG, r.intent.getComponent().flattenToShortString()
4594 + ": task=" + r.task);
4595 }
4596
4597 RunningTaskInfo ci = new RunningTaskInfo();
4598 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004599 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004600 ci.baseActivity = r.intent.getComponent();
4601 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004602 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004603 if (focusedStack && topTask) {
4604 // Give the latest time to ensure foreground task can be sorted
4605 // at the first, because lastActiveTime of creating task is 0.
4606 ci.lastActiveTime = System.currentTimeMillis();
4607 topTask = false;
4608 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004609
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004610 if (top.task != null) {
4611 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004612 }
4613 ci.numActivities = numActivities;
4614 ci.numRunning = numRunning;
Wale Ogunwale21b60582016-01-27 12:34:16 -08004615 ci.isDockable = task.canGoInDockedStack();
Craig Mautneraab647e2013-02-28 16:31:36 -08004616 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004617 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004618 }
4619
4620 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004621 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004622 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004623 if (top >= 0) {
4624 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4625 int activityTop = activities.size() - 1;
4626 if (activityTop > 0) {
4627 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4628 "unhandled-back", true);
4629 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004630 }
4631 }
4632
Craig Mautner6b74cb52013-09-27 17:02:21 -07004633 /**
4634 * Reset local parameters because an app's activity died.
4635 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004636 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004637 */
4638 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004639 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004640 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004641 "App died while pausing: " + mPausingActivity);
4642 mPausingActivity = null;
4643 }
4644 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4645 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004646 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004647 }
4648
Craig Mautner19091252013-10-05 00:03:53 -07004649 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004650 }
4651
Craig Mautnercae015f2013-02-08 14:31:27 -08004652 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004653 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4654 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4655 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4656 final ActivityRecord r = activities.get(activityNdx);
4657 if (r.app == app) {
4658 Slog.w(TAG, " Force finishing activity "
4659 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004660 // Force the destroy to skip right to removal.
4661 r.app = null;
4662 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004663 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004664 }
4665 }
4666 }
4667
Dianne Hackborn390517b2013-05-30 15:03:32 -07004668 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004669 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004670 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004671 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4672 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004673 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4674 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004675 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004676 " Task id #" + task.taskId + "\n" +
4677 " mFullscreen=" + task.mFullscreen + "\n" +
4678 " mBounds=" + task.mBounds + "\n" +
4679 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004680 if (printed) {
4681 header = null;
4682 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004683 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004684 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004685 }
4686
4687 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4688 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4689
4690 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004691 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4692 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004693 }
4694 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004695 final int top = mTaskHistory.size() - 1;
4696 if (top >= 0) {
4697 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4698 int listTop = list.size() - 1;
4699 if (listTop >= 0) {
4700 activities.add(list.get(listTop));
4701 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004702 }
4703 } else {
4704 ItemMatcher matcher = new ItemMatcher();
4705 matcher.build(name);
4706
Craig Mautneraab647e2013-02-28 16:31:36 -08004707 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4708 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4709 if (matcher.match(r1, r1.intent.getComponent())) {
4710 activities.add(r1);
4711 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004712 }
4713 }
4714 }
4715
4716 return activities;
4717 }
4718
4719 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004720 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004721
4722 // All activities that came from the package must be
4723 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004724 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4725 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4726 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4727 final ActivityRecord a = activities.get(activityNdx);
4728 if (a.info.packageName.equals(packageName)) {
4729 a.forceNewConfig = true;
4730 if (starting != null && a == starting && a.visible) {
4731 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004732 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08004733 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004734 }
4735 }
4736 }
4737
4738 return starting;
4739 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004740
Craig Mautner299f9602015-01-26 09:47:33 -08004741 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004742 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004743 }
4744
Wale Ogunwale000957c2015-04-03 08:19:12 -07004745 /**
4746 * Removes the input task from this stack.
4747 * @param task to remove.
4748 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004749 * @param moving task to another stack. In the case we are moving we don't want to perform
4750 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004751 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004752 void removeTask(TaskRecord task, String reason, boolean moving) {
4753 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004754 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004755 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004756 if (!StackId.persistTaskBounds(mStackId)) {
4757 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4758 // default configuration the next time it launches.
4759 task.updateOverrideConfiguration(null);
4760 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004761 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004762
Craig Mautner04a0ea62014-01-13 12:51:26 -08004763 final ActivityRecord r = mResumedActivity;
4764 if (r != null && r.task == task) {
4765 mResumedActivity = null;
4766 }
4767
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004768 final int taskNdx = mTaskHistory.indexOf(task);
4769 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004770 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4771 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4772 if (!nextTask.isOverHomeStack()) {
4773 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4774 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004775 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004776 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004777 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004778
Wale Ogunwale040b4702015-08-06 18:10:50 -07004779 if (!moving && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004780 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07004781 final boolean isVoiceSession = task.voiceSession != null;
4782 if (isVoiceSession) {
4783 try {
4784 task.voiceSession.taskFinished(task.intent, task.taskId);
4785 } catch (RemoteException e) {
4786 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004787 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004788 if (task.autoRemoveFromRecents() || isVoiceSession) {
4789 // Task creator asked to remove this when done, or this task was a voice
4790 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004791 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004792 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004793 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004794 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004795
4796 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004797 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004798 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004799 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004800 String myReason = reason + " leftTaskHistoryEmpty";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08004801 if (mFullscreen || !adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004802 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004803 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004804 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004805 if (mStacks != null) {
4806 mStacks.remove(this);
4807 mStacks.add(0, this);
4808 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004809 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004810 mActivityContainer.onTaskListEmptyLocked();
4811 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004812 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004813
4814 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004815 }
4816
Dianne Hackborn91097de2014-04-04 18:02:06 -07004817 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4818 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4819 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004820 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4821 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004822 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08004823 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08004824 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004825 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.isResizeable()
Chong Zhang75b37202015-12-04 14:16:36 -08004826 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004827 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004828 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004829 return task;
4830 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004831
Wale Ogunwale935e5022015-11-10 12:36:10 -08004832 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4833 if (mTaskPositioner == null) {
4834 return false;
4835 }
4836 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4837 return true;
4838 }
4839
Craig Mautnerc00204b2013-03-05 15:02:14 -08004840 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004841 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004842 }
4843
Wale Ogunwale5f986092015-12-04 15:35:38 -08004844 void addTask(final TaskRecord task, final boolean toTop, String reason) {
4845 final ActivityStack prevStack = preAddTask(task, reason);
4846
Craig Mautnerc00204b2013-03-05 15:02:14 -08004847 task.stack = this;
4848 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004849 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004850 } else {
4851 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004852 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004853 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08004854 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004855 }
4856
Wale Ogunwale5f986092015-12-04 15:35:38 -08004857 void positionTask(final TaskRecord task, int position) {
4858 final ActivityStack prevStack = preAddTask(task, "positionTask");
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004859 task.stack = this;
4860 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004861 postAddTask(task, prevStack);
4862 }
4863
4864 private ActivityStack preAddTask(TaskRecord task, String reason) {
4865 final ActivityStack prevStack = task.stack;
4866 if (prevStack != null && prevStack != this) {
4867 prevStack.removeTask(task, reason, MOVING);
4868 }
4869 return prevStack;
4870 }
4871
4872 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
4873 if (prevStack != null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004874 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004875 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004876 try {
4877 task.voiceSession.taskStarted(task.intent, task.taskId);
4878 } catch (RemoteException e) {
4879 }
4880 }
4881 }
4882
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004883 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004884 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004885 // TODO: VI deal with activity
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004886 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4887 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6cae7652015-12-26 07:36:26 -08004888 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
4889 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004890 task.mResizeMode, r.isAlwaysFocusable(), task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004891 r.taskConfigOverride = task.mOverrideConfig;
4892 }
4893
Wale Ogunwaled046a012015-12-24 13:05:59 -08004894 void moveToFrontAndResumeStateIfNeeded(
4895 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
4896 if (!moveToFront) {
4897 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07004898 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004899
4900 // If the activity owns the last resumed activity, transfer that together,
4901 // so that we don't resume the same activity again in the new stack.
4902 // Apps may depend on onResume()/onPause() being called in pairs.
4903 if (setResume) {
4904 mResumedActivity = r;
4905 }
4906 // Move the stack in which we are placing the activity to the front. The call will also
4907 // make sure the activity focus is set.
4908 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004909 }
4910
4911 /**
4912 * Moves the input activity from its current stack to this one.
4913 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4914 * created on this stack which the activity is added to.
4915 * */
4916 void moveActivityToStack(ActivityRecord r) {
4917 final ActivityStack prevStack = r.task.stack;
4918 if (prevStack.mStackId == mStackId) {
4919 // You are already in the right stack silly...
4920 return;
4921 }
4922
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004923 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004924 && (mStackSupervisor.topRunningActivityLocked() == r);
4925 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4926
4927 final TaskRecord task = createTaskRecord(
Suprabh Shukla09a88f52015-12-02 14:36:31 -08004928 mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
4929 r.info, r.intent, null, null, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004930 r.setTask(task, null);
4931 task.addActivityToTop(r);
4932 setAppTask(r, task);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004933 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004934 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4935 if (wasResumed) {
4936 prevStack.mResumedActivity = null;
4937 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07004938 }
4939
Wale Ogunwale706ed792015-08-02 10:29:44 -07004940 private void setAppTask(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004941 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004942 mWindowManager.setAppTask(r.appToken, task.taskId, mStackId, bounds, task.mOverrideConfig,
4943 task.mResizeMode, task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004944 r.taskConfigOverride = task.mOverrideConfig;
4945 }
4946
Craig Mautnerc00204b2013-03-05 15:02:14 -08004947 public int getStackId() {
4948 return mStackId;
4949 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004950
4951 @Override
4952 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004953 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4954 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004955 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004956
Craig Mautner15df08a2015-04-01 12:17:18 -07004957 void onLockTaskPackagesUpdatedLocked() {
4958 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4959 mTaskHistory.get(taskNdx).setLockTaskAuth();
4960 }
4961 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004962}