blob: 5561456ceca663f4f498ea5634584dd99370fea9 [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;
Wale Ogunwale56d75cf2016-03-09 15:14:47 -080023import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070024import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080025import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
26import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
27import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
28import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080029import static android.content.pm.ActivityInfo.FLAG_RESUME_WHILE_PAUSING;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070030import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070031
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080032import static android.content.res.Configuration.SCREENLAYOUT_UNDEFINED;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080033import static com.android.server.am.ActivityManagerDebugConfig.*;
Chong Zhang75b37202015-12-04 14:16:36 -080034import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
Craig Mautner84984fa2014-06-19 11:19:20 -070035import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
36import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070037
Wale Ogunwalef40c11b2016-02-26 08:16:02 -080038import static com.android.server.am.ActivityRecord.STARTING_WINDOW_REMOVED;
39import static com.android.server.am.ActivityRecord.STARTING_WINDOW_SHOWN;
Wale Ogunwale39381972015-12-17 17:15:29 -080040import static com.android.server.am.ActivityStackSupervisor.FindTaskResult;
Wale Ogunwale040b4702015-08-06 18:10:50 -070041import static com.android.server.am.ActivityStackSupervisor.MOVING;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070042import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwale040b4702015-08-06 18:10:50 -070043
Winsonc809cbb2015-11-02 12:06:15 -080044import android.graphics.Point;
Wale Ogunwale706ed792015-08-02 10:29:44 -070045import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070046import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070047
Dianne Hackborn91097de2014-04-04 18:02:06 -070048import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080049import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070050import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070051import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080052import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080053import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080054import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080055import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070056import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070057
58import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070059import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070060import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070061import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080063import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080065import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070068import android.content.pm.ActivityInfo;
Todd Kennedy39bfee52016-02-24 10:28:21 -080069import android.content.pm.ApplicationInfo;
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);
Jorim Jaggi899327f2016-02-25 20:44:18 -0500418 if (mStackId == DOCKED_STACK_ID) {
419 // If we removed a docked stack we want to resize it so it resizes all other stacks
420 // in the system to fullscreen.
421 mStackSupervisor.resizeDockedStackLocked(
422 null, null, null, null, null, PRESERVE_WINDOWS);
423 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700424 }
425
Winsonc809cbb2015-11-02 12:06:15 -0800426 public void getDisplaySize(Point out) {
427 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
428 }
429
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700430 void setBounds(Rect bounds) {
431 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700432 if (mTaskPositioner != null) {
433 mTaskPositioner.configure(bounds);
434 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700435 }
Craig Mautner5962b122012-10-05 14:45:52 -0700436
Amith Yamasani734983f2014-03-04 16:48:05 -0800437 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700438 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700439 }
440
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700441 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800442 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700443 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700444 if (r != null) {
445 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800446 }
447 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700448 return null;
449 }
450
451 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800452 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
453 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800454 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800455 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
456 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800457 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800458 return r;
459 }
460 }
461 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700462 return null;
463 }
464
465 /**
466 * This is a simplified version of topRunningActivityLocked that provides a number of
467 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800468 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700469 * @param token If non-null, any history records matching this token will be skipped.
470 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800471 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700472 * @return Returns the HistoryRecord of the next activity on the stack.
473 */
474 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800475 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
476 TaskRecord task = mTaskHistory.get(taskNdx);
477 if (task.taskId == taskId) {
478 continue;
479 }
480 ArrayList<ActivityRecord> activities = task.mActivities;
481 for (int i = activities.size() - 1; i >= 0; --i) {
482 final ActivityRecord r = activities.get(i);
483 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800484 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800485 return r;
486 }
487 }
488 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700489 return null;
490 }
491
Craig Mautner8849a5e2013-04-02 16:41:03 -0700492 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700493 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
494 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700495 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
496 final ActivityRecord r = activities.get(activityNdx);
497 if (!r.finishing) {
498 return r;
499 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700500 }
501 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700502 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700503 }
504
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700505 final TaskRecord topTask() {
506 final int size = mTaskHistory.size();
507 if (size > 0) {
508 return mTaskHistory.get(size - 1);
509 }
510 return null;
511 }
512
Craig Mautnerd2328952013-03-05 12:46:26 -0800513 TaskRecord taskForIdLocked(int id) {
514 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
515 final TaskRecord task = mTaskHistory.get(taskNdx);
516 if (task.taskId == id) {
517 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800518 }
519 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700520 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700521 }
522
Craig Mautnerd2328952013-03-05 12:46:26 -0800523 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700524 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800525 return isInStackLocked(r);
526 }
527
528 ActivityRecord isInStackLocked(ActivityRecord r) {
529 if (r == null) {
530 return null;
531 }
532 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700533 if (task != null && task.stack != null
534 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800535 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800536 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800537 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800538 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800539 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800540 }
541
Craig Mautner2420ead2013-04-01 17:13:20 -0700542 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700543 final boolean hadit = mLRUActivities.remove(r);
544 mLRUActivities.add(r);
545 return hadit;
546 }
547
Craig Mautnerde4ef022013-04-07 19:01:33 -0700548 final boolean isHomeStack() {
549 return mStackId == HOME_STACK_ID;
550 }
551
Winson Chung0583d3d2015-12-18 10:03:12 -0500552 final boolean isDockedStack() {
553 return mStackId == DOCKED_STACK_ID;
554 }
555
556 final boolean isPinnedStack() {
557 return mStackId == PINNED_STACK_ID;
558 }
559
Craig Mautnere0a38842013-12-16 16:14:02 -0800560 final boolean isOnHomeDisplay() {
561 return isAttached() &&
562 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
563 }
564
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700565 void moveToFront(String reason) {
566 moveToFront(reason, null);
567 }
568
569 /**
570 * @param reason The reason for moving the stack to the front.
571 * @param task If non-null, the task will be moved to the top of the stack.
572 * */
573 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700574 if (!isAttached()) {
575 return;
576 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700577
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700578 mStacks.remove(this);
579 int addIndex = mStacks.size();
580
581 if (addIndex > 0) {
582 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800583 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
584 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700585 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800586 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700587 }
588
589 mStacks.add(addIndex, this);
590
591 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
592 if (isOnHomeDisplay()) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800593 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700594 }
595 if (task != null) {
596 insertTaskAtTop(task, null);
597 } else {
598 task = topTask();
599 }
600 if (task != null) {
601 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800602 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800603 }
604
Wale Ogunwaled046a012015-12-24 13:05:59 -0800605 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800606 if (StackId.canReceiveKeys(mStackId)) {
607 return true;
608 }
609 // The stack isn't focusable. See if its top activity is focusable to force focus on the
610 // stack.
611 final ActivityRecord r = topRunningActivityLocked();
612 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800613 }
614
Craig Mautnere0a38842013-12-16 16:14:02 -0800615 final boolean isAttached() {
616 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800617 }
618
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700619 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800620 * Returns the top activity in any existing task matching the given Intent in the input result.
621 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700622 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800623 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700624 Intent intent = target.intent;
625 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700626 ComponentName cls = intent.getComponent();
627 if (info.targetActivity != null) {
628 cls = new ComponentName(info.packageName, info.targetActivity);
629 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700630 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700631 boolean isDocument = intent != null & intent.isDocument();
632 // If documentData is non-null then it must match the existing task data.
633 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800634
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700635 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800636 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
637 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700638 if (task.voiceSession != null) {
639 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700640 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700641 continue;
642 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700643 if (task.userId != userId) {
644 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700645 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700646 continue;
647 }
Craig Mautner000f0022013-02-26 15:04:29 -0800648 final ActivityRecord r = task.getTopActivity();
649 if (r == null || r.finishing || r.userId != userId ||
650 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700651 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800652 continue;
653 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700654 if (r.mActivityType != target.mActivityType) {
655 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
656 continue;
657 }
Craig Mautner000f0022013-02-26 15:04:29 -0800658
Craig Mautnerd00f4742014-03-12 14:17:26 -0700659 final Intent taskIntent = task.intent;
660 final Intent affinityIntent = task.affinityIntent;
661 final boolean taskIsDocument;
662 final Uri taskDocumentData;
663 if (taskIntent != null && taskIntent.isDocument()) {
664 taskIsDocument = true;
665 taskDocumentData = taskIntent.getData();
666 } else if (affinityIntent != null && affinityIntent.isDocument()) {
667 taskIsDocument = true;
668 taskDocumentData = affinityIntent.getData();
669 } else {
670 taskIsDocument = false;
671 taskDocumentData = null;
672 }
673
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700674 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700675 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700676 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700677 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Wale Ogunwale39381972015-12-17 17:15:29 -0800678 if (!isDocument && !taskIsDocument
679 && result.r == null && task.canMatchRootAffinity()) {
Dianne Hackborn79228822014-09-16 11:11:23 -0700680 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwale39381972015-12-17 17:15:29 -0800681 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
682 // It is possible for multiple tasks to have the same root affinity especially
683 // if they are in separate stacks. We save off this candidate, but keep looking
684 // to see if there is a better candidate.
685 result.r = r;
686 result.matchedByRootAffinity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700687 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700688 } else if (taskIntent != null && taskIntent.getComponent() != null &&
689 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700690 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700691 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800692 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700693 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
694 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800695 result.r = r;
696 result.matchedByRootAffinity = false;
697 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700698 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
699 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700700 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700701 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800702 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700703 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
704 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800705 result.r = r;
706 result.matchedByRootAffinity = false;
707 break;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700708 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700709 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700710 }
711
712 /**
713 * Returns the first activity (starting from the top of the stack) that
714 * is the same as the given activity. Returns null if no such activity
715 * is found.
716 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700717 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700718 ComponentName cls = intent.getComponent();
719 if (info.targetActivity != null) {
720 cls = new ComponentName(info.packageName, info.targetActivity);
721 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700722 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700723
Craig Mautner000f0022013-02-26 15:04:29 -0800724 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700725 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700726 final boolean notCurrentUserTask =
727 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700728 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700729
Craig Mautner000f0022013-02-26 15:04:29 -0800730 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
731 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700732 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700733 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700734 }
Craig Mautner000f0022013-02-26 15:04:29 -0800735 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700736 return r;
737 }
738 }
739 }
740
741 return null;
742 }
743
Amith Yamasani742a6712011-05-04 14:49:28 -0700744 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700745 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700746 */
Craig Mautner93529a42013-10-04 15:03:13 -0700747 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800748 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700749 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800750 }
751 mCurrentUser = userId;
752
753 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800754 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800755 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700756 final TaskRecord task = mTaskHistory.get(i);
757
758 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
759 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700760 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700761 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700762 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700763 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800764 mTaskHistory.remove(i);
765 mTaskHistory.add(task);
766 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800767 // Use same value for i.
768 } else {
769 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800770 }
771 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700772 if (VALIDATE_TOKENS) {
773 validateAppTokensLocked();
774 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700775 }
776
Craig Mautner2420ead2013-04-01 17:13:20 -0700777 void minimalResumeActivityLocked(ActivityRecord r) {
778 r.state = ActivityState.RESUMED;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800779 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
780 + " callers=" + Debug.getCallers(5));
Craig Mautner2420ead2013-04-01 17:13:20 -0700781 mResumedActivity = r;
782 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800783 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700784 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700785 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700786 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700787 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
788 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700789 }
790
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800791 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700792 if (r != null) {
793 mRecentTasks.addLocked(r.task);
794 r.task.touchActiveTime();
795 }
796 }
797
Narayan Kamath7829c812015-06-08 17:39:43 +0100798 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700799 if (mFullyDrawnStartTime != 0) {
800 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
801 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100802 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700803 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
804 }
805
806 private void stopFullyDrawnTraceIfNeeded() {
807 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
808 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
809 mFullyDrawnStartTime = 0;
810 }
811 }
812
Craig Mautnere79d42682013-04-01 19:01:53 -0700813 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700814 if (r.displayStartTime == 0) {
815 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
816 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100817 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700818 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700819 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700820 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100821 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700822 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700823 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700824 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800825
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700826 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700827 // Make sure that there is no activity waiting for this to launch.
828 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
829 r.displayStartTime = r.fullyDrawnStartTime = 0;
830 } else {
831 mStackSupervisor.removeTimeoutsForActivityLocked(r);
832 mStackSupervisor.scheduleIdleTimeoutLocked(r);
833 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700834 }
835
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800836 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800837 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800838 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
839 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
840 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
841 activities.get(activityNdx).setSleeping(false);
842 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800843 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800844 if (mPausingActivity != null) {
845 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
846 activityPausedLocked(mPausingActivity.appToken, true);
847 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800848 }
849
Todd Kennedy39bfee52016-02-24 10:28:21 -0800850 void updateActivityApplicationInfoLocked(ApplicationInfo aInfo) {
851 final String packageName = aInfo.packageName;
852 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
853 final List<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
854 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
855 if (packageName.equals(activities.get(activityNdx).packageName)) {
856 activities.get(activityNdx).info.applicationInfo = aInfo;
857 }
858 }
859 }
860 }
861
Craig Mautner0eea92c2013-05-16 13:35:39 -0700862 /**
863 * @return true if something must be done before going to sleep.
864 */
865 boolean checkReadyForSleepLocked() {
866 if (mResumedActivity != null) {
867 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700868 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
869 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
870 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700871 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700872 return true;
873 }
874 if (mPausingActivity != null) {
875 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700876 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700877 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800878 }
879
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700880 if (hasVisibleBehindActivity()) {
881 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800882 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700883 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800884 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700885 return true;
886 }
887
Craig Mautner0eea92c2013-05-16 13:35:39 -0700888 return false;
889 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800890
Craig Mautner0eea92c2013-05-16 13:35:39 -0700891 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700892 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800893
Craig Mautner0eea92c2013-05-16 13:35:39 -0700894 // Make sure any stopped but visible activities are now sleeping.
895 // This ensures that the activity's onStop() is called.
896 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
897 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
898 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
899 final ActivityRecord r = activities.get(activityNdx);
900 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
901 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800902 }
903 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800904 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700905 }
Craig Mautner59c00972012-07-30 12:10:24 -0700906
Winson8b1871d2015-11-20 09:56:20 -0800907 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
908 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800909 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700910 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800911 return null;
912 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800913
Winson Chung083baf92014-07-11 10:32:42 -0700914 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700915 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800916 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700917 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700918 return null;
919 }
920
Winson Chung48a10a52014-08-27 14:36:51 -0700921 int w = mService.mThumbnailWidth;
922 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800923 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800924 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700925 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800926
927 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800928 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
929 // use within SystemUI while keeping memory usage low.
Winson Chungead5c0f2015-12-14 11:18:57 -0500930 if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
Winson8b1871d2015-11-20 09:56:20 -0800931 w = h = -1;
Winson387aac62015-11-25 11:18:56 -0800932 scale = 0.5f;
Winson8b1871d2015-11-20 09:56:20 -0800933 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800934 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800935 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800936 }
Winson Chung376543b2014-05-21 17:43:28 -0700937 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800938 return null;
939 }
940
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700941 /**
942 * Start pausing the currently resumed activity. It is an error to call this if there
943 * is already an activity being paused or there is no resumed activity.
944 *
945 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
946 * @param uiSleeping True if this is happening with the user interface going to sleep (the
947 * screen turning off).
948 * @param resuming True if this is being called as part of resuming the top activity, so
949 * we shouldn't try to instigate a resume here.
950 * @param dontWait True if the caller does not want to wait for the pause to complete. If
951 * set to true, we will immediately complete the pause here before returning.
952 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
953 * it to tell us when it is done.
954 */
955 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
956 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800957 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800958 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
959 + " state=" + mPausingActivity.state);
960 if (!mService.isSleeping()) {
961 // Avoid recursion among check for sleep and complete pause during sleeping.
962 // Because activity will be paused immediately after resume, just let pause
963 // be completed by the order of activity paused from clients.
964 completePauseLocked(false);
965 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800966 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700967 ActivityRecord prev = mResumedActivity;
968 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700969 if (!resuming) {
970 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -0800971 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700972 }
973 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700974 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800975
976 if (mActivityContainer.mParentActivity == null) {
977 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700978 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800979 }
980
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700981 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700982 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700983 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800984 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700985 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700986 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
987 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700988 prev.state = ActivityState.PAUSING;
989 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700990 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700991 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700992 if (mService.mHasRecents
993 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800994 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700995 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700996 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700997
998 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800999
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001000 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001001 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001002 try {
1003 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07001004 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001005 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -07001006 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -08001007 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001008 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001009 } catch (Exception e) {
1010 // Ignore exception, if process died other code will cleanup.
1011 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001012 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001013 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001014 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001015 }
1016 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001017 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001018 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07001019 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001020 }
1021
1022 // If we are not going to sleep, we want to ensure the device is
1023 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001024 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001025 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001026 }
1027
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001028 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001029 // Have the window manager pause its key dispatching until the new
1030 // activity has started. If we're pausing the activity just because
1031 // the screen is being turned off and the UI is sleeping, don't interrupt
1032 // key dispatch; the same activity will pick it up again on wakeup.
1033 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001034 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001035 } else if (DEBUG_PAUSE) {
1036 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001037 }
1038
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001039 if (dontWait) {
1040 // If the caller said they don't want to wait for the pause, then complete
1041 // the pause now.
1042 completePauseLocked(false);
1043 return false;
1044
1045 } else {
1046 // Schedule a pause timeout in case the app doesn't respond.
1047 // We don't give it much time because this directly impacts the
1048 // responsiveness seen by the user.
1049 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1050 msg.obj = prev;
1051 prev.pauseTime = SystemClock.uptimeMillis();
1052 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001053 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001054 return true;
1055 }
1056
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001057 } else {
1058 // This activity failed to schedule the
1059 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001060 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001061 if (!resuming) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001062 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001063 }
1064 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001065 }
1066 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001067
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001068 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001069 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1070 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001071
Craig Mautnerd2328952013-03-05 12:46:26 -08001072 final ActivityRecord r = isInStackLocked(token);
1073 if (r != null) {
1074 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1075 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001076 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001077 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001078 completePauseLocked(true);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001079 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001080 } else {
1081 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1082 r.userId, System.identityHashCode(r), r.shortComponentName,
1083 mPausingActivity != null
1084 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001085 if (r.state == ActivityState.PAUSING) {
1086 r.state = ActivityState.PAUSED;
1087 if (r.finishing) {
1088 if (DEBUG_PAUSE) Slog.v(TAG,
1089 "Executing finish of failed to pause activity: " + r);
1090 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1091 }
louis_chang047dfd42015-04-08 16:35:55 +08001092 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001093 }
1094 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001095 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001096 }
1097
Chong Zhangd78ddb42016-03-02 17:01:14 -08001098 final void activityResumedLocked(IBinder token) {
1099 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
1100 if (DEBUG_SAVED_STATE) Slog.i(TAG_STATES, "Resumed activity; dropping state of: " + r);
1101 r.icicle = null;
1102 r.haveState = false;
1103 }
1104
Craig Mautnera0026042014-04-23 11:45:37 -07001105 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1106 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001107 if (r.state != ActivityState.STOPPING) {
1108 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1109 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1110 return;
1111 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001112 if (persistentState != null) {
1113 r.persistentState = persistentState;
1114 mService.notifyTaskPersisterLocked(r.task, false);
1115 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001116 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001117 if (icicle != null) {
1118 // If icicle is null, this is happening due to a timeout, so we
1119 // haven't really saved the state.
1120 r.icicle = icicle;
1121 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001122 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001123 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001124 }
1125 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001126 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001127 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1128 r.stopped = true;
1129 r.state = ActivityState.STOPPED;
Robert Carre12aece2016-02-02 22:43:27 -08001130
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08001131 mWindowManager.notifyAppStopped(r.appToken, true);
Robert Carre12aece2016-02-02 22:43:27 -08001132
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001133 if (getVisibleBehindActivity() == r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001134 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001135 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001136 if (r.finishing) {
1137 r.clearOptionsLocked();
1138 } else {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001139 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001140 destroyActivityLocked(r, true, "stop-config");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001141 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001142 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001143 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001144 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001145 }
1146 }
1147 }
1148
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001149 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001150 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001151 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001152
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001153 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001154 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001155 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001156 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001157 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001158 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001159 } else if (prev.app != null) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001160 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1161 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Craig Mautner8c14c152015-01-15 17:32:07 -08001162 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001163 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1164 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001165 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001166 if (prev.deferRelaunchUntilPaused) {
1167 // Complete the deferred relaunch that was waiting for pause to complete.
1168 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
1169 relaunchActivityLocked(prev, prev.configChangeFlags, false,
1170 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001171 } else if (wasStopping) {
1172 // We are also stopping, the stop request must have gone soon after the pause.
1173 // We can't clobber it, because the stop confirmation will not be handled.
1174 // We don't need to schedule another stop, we only need to let it happen.
1175 prev.state = ActivityState.STOPPING;
Wale Ogunwaled8026642016-02-09 20:40:18 -08001176 } else if ((!prev.visible && !hasVisibleBehindActivity())
1177 || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001178 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001179 // stopping.
Chong Zhang46b1ac62016-02-18 17:53:57 -08001180 addToStopping(prev, true /* immediate */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001181 }
1182 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001183 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001184 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001185 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001186 // It is possible the activity was freezing the screen before it was paused.
1187 // In that case go ahead and remove the freeze this activity has on the screen
1188 // since it is no longer visible.
1189 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001190 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001191 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001192
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001193 if (resumeNext) {
1194 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1195 if (!mService.isSleepingOrShuttingDown()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001196 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001197 } else {
1198 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001199 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001200 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001201 // If there are no more activities available to run, do resume anyway to start
1202 // something. Also if the top activity on the stack is not the just paused
1203 // activity, we need to go ahead and resume it to ensure we complete an
1204 // in-flight app switch.
1205 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001206 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001207 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001208 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001209
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001210 if (prev != null) {
1211 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001212
Craig Mautner525f3d92013-05-07 14:01:50 -07001213 if (prev.app != null && prev.cpuTimeAtResume > 0
1214 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001215 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1216 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001217 if (diff > 0) {
1218 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1219 synchronized (bsi) {
1220 BatteryStatsImpl.Uid.Proc ps =
1221 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001222 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001223 if (ps != null) {
1224 ps.addForegroundTimeLocked(diff);
1225 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001226 }
1227 }
1228 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001229 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001230 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001231
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001232 // Notify when the task stack has changed, but only if visibilities changed (not just focus)
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001233 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause) {
1234 mService.notifyTaskStackChangedLocked();
1235 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1236 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001237
1238 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001239 }
1240
Chong Zhang46b1ac62016-02-18 17:53:57 -08001241 private void addToStopping(ActivityRecord r, boolean immediate) {
1242 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1243 mStackSupervisor.mStoppingActivities.add(r);
1244 }
1245
1246 // If we already have a few activities waiting to stop, then give up
1247 // on things going idle and start clearing them out. Or if r is the
1248 // last of activity of the last task the stack will be empty and must
1249 // be cleared immediately.
1250 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1251 || (r.frontOfTask && mTaskHistory.size() <= 1);
1252
1253 if (immediate || forceIdle) {
1254 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
1255 + forceIdle + "immediate=" + immediate);
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001256 mStackSupervisor.scheduleIdleLocked();
1257 } else {
1258 mStackSupervisor.checkReadyForSleepLocked();
1259 }
1260 }
1261
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001262 /**
1263 * Once we know that we have asked an application to put an activity in
1264 * the resumed state (either by launching it or explicitly telling it),
1265 * this function updates the rest of our state to match that fact.
1266 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001267 private void completeResumeLocked(ActivityRecord next) {
Chong Zhang2b79af12016-02-10 18:47:06 -08001268 next.visible = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001269 next.idle = false;
1270 next.results = null;
1271 next.newIntents = null;
Chong Zhang2b79af12016-02-10 18:47:06 -08001272 next.stopped = false;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001273
Chong Zhang85ee6542015-10-02 13:36:38 -07001274 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001275 ProcessRecord app = next.task.mActivities.get(0).app;
1276 if (app != null && app != mService.mHomeProcess) {
1277 mService.mHomeProcess = app;
1278 }
1279 }
1280
Craig Mautner07566322013-09-26 16:42:55 -07001281 if (next.nowVisible) {
1282 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001283 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001284 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001285
1286 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001287 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001288
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001289 mStackSupervisor.reportResumedActivityLocked(next);
1290
1291 next.resumeKeyDispatchingLocked();
1292 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001293
1294 // Mark the point when the activity is resuming
1295 // TODO: To be more accurate, the mark should be before the onCreate,
1296 // not after the onResume. But for subsequent starts, onResume is fine.
1297 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001298 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001299 } else {
1300 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1301 }
Winson Chung376543b2014-05-21 17:43:28 -07001302
George Mount6ba042b2014-07-28 11:12:28 -07001303 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001304
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001305 if (getVisibleBehindActivity() == next) {
Craig Mautner64ccb702014-10-01 09:38:40 -07001306 // When resuming an activity, require it to call requestVisibleBehind() again.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001307 setVisibleBehindActivity(null);
Craig Mautner64ccb702014-10-01 09:38:40 -07001308 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001309 }
1310
Craig Mautner2568c3a2015-03-26 14:22:34 -07001311 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001312 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001313 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1314 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1315 r.mUpdateTaskThumbnailWhenHidden = false;
1316 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001317 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001318 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001319 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001320 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001321 container.setVisible(visible);
1322 }
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001323 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001324 }
1325
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001326 // Find the first visible activity above the passed activity and if it is translucent return it
1327 // otherwise return null;
1328 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1329 TaskRecord task = r.task;
1330 if (task == null) {
1331 return null;
1332 }
1333
1334 ActivityStack stack = task.stack;
1335 if (stack == null) {
1336 return null;
1337 }
1338
1339 int stackNdx = mStacks.indexOf(stack);
1340
1341 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1342 int taskNdx = tasks.indexOf(task);
1343
1344 ArrayList<ActivityRecord> activities = task.mActivities;
1345 int activityNdx = activities.indexOf(r) + 1;
1346
1347 final int numStacks = mStacks.size();
1348 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001349 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001350 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001351 final int numTasks = tasks.size();
1352 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001353 final TaskRecord currentTask = tasks.get(taskNdx);
1354 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001355 final int numActivities = activities.size();
1356 while (activityNdx < numActivities) {
1357 final ActivityRecord activity = activities.get(activityNdx);
1358 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001359 return historyStack.mFullscreen
1360 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001361 }
1362 ++activityNdx;
1363 }
1364 activityNdx = 0;
1365 ++taskNdx;
1366 }
1367 taskNdx = 0;
1368 ++stackNdx;
1369 }
1370
1371 return null;
1372 }
1373
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001374 ActivityStack getNextFocusableStackLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001375 ArrayList<ActivityStack> stacks = mStacks;
1376 final ActivityRecord parent = mActivityContainer.mParentActivity;
1377 if (parent != null) {
1378 stacks = parent.task.stack.mStacks;
1379 }
1380 if (stacks != null) {
1381 for (int i = stacks.size() - 1; i >= 0; --i) {
1382 ActivityStack stack = stacks.get(i);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001383 if (stack != this && stack.isFocusable()
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001384 && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001385 return stack;
1386 }
1387 }
1388 }
1389 return null;
1390 }
1391
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001392 /** Returns true if the stack contains a fullscreen task. */
1393 private boolean hasFullscreenTask() {
1394 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1395 final TaskRecord task = mTaskHistory.get(i);
1396 if (task.mFullscreen) {
1397 return true;
1398 }
1399 }
1400 return false;
1401 }
1402
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001403 /**
1404 * Returns true if the stack is translucent and can have other contents visible behind it if
1405 * needed. A stack is considered translucent if it don't contain a visible or
1406 * starting (about to be visible) activity that is fullscreen (opaque).
1407 * @param starting The currently starting activity or null if there is none.
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001408 * @param stackBehindId The id of the stack directly behind this one.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001409 */
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001410 private boolean isStackTranslucent(ActivityRecord starting, int stackBehindId) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001411 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1412 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001413 final ArrayList<ActivityRecord> activities = task.mActivities;
1414 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1415 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001416
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001417 if (r.finishing) {
1418 // We don't factor in finishing activities when determining translucency since
1419 // they will be gone soon.
1420 continue;
1421 }
1422
1423 if (!r.visible && r != starting) {
1424 // Also ignore invisible activities that are not the currently starting
1425 // activity (about to be visible).
1426 continue;
1427 }
1428
1429 if (r.fullscreen) {
1430 // Stack isn't translucent if it has at least one fullscreen activity
1431 // that is visible.
1432 return false;
1433 }
1434
1435 if (!isHomeStack() && r.frontOfTask
1436 && task.isOverHomeStack() && stackBehindId != HOME_STACK_ID) {
1437 // Stack isn't translucent if it's top activity should have the home stack
1438 // behind it and the stack currently behind it isn't the home stack.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001439 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001440 }
1441 }
1442 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001443 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001444 }
1445
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001446 /**
1447 * Returns stack's visibility: {@link #STACK_INVISIBLE}, {@link #STACK_VISIBLE} or
1448 * {@link #STACK_VISIBLE_ACTIVITY_BEHIND}.
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001449 * @param starting The currently starting activity or null if there is none.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001450 */
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001451 int getStackVisibilityLocked(ActivityRecord starting) {
Jose Lima7ba71252014-04-30 12:59:27 -07001452 if (!isAttached()) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001453 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001454 }
1455
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001456 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001457 return STACK_VISIBLE;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001458 }
1459
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001460 final int stackIndex = mStacks.indexOf(this);
1461
1462 if (stackIndex == mStacks.size() - 1) {
1463 Slog.wtf(TAG,
1464 "Stack=" + this + " isn't front stack but is at the top of the stack list");
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001465 return STACK_INVISIBLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001466 }
1467
Chong Zhang75b37202015-12-04 14:16:36 -08001468 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1469 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001470 return STACK_INVISIBLE;
Chong Zhang75b37202015-12-04 14:16:36 -08001471 }
1472
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001473 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1474 final int focusedStackId = focusedStack.mStackId;
1475
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001476 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001477 && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID
1478 && !focusedStack.topActivity().fullscreen) {
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001479 // The fullscreen stack should be visible if it has a visible behind activity behind
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001480 // the home stack that is translucent.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001481 return STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001482 }
1483
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001484 if (mStackId == DOCKED_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001485 // Docked stack is always visible, except in the case where the top running activity
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001486 // task in the focus stack doesn't support any form of resizing but we show it for the
1487 // home task even though it's not resizable.
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001488 final ActivityRecord r = focusedStack.topRunningActivityLocked();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001489 final TaskRecord task = r != null ? r.task : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001490 return task == null || task.canGoInDockedStack() || task.isHomeTask() ? STACK_VISIBLE
1491 : STACK_INVISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001492 }
1493
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001494 // Find the first stack behind focused stack that actually got something visible.
1495 int stackBehindFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1496 while (stackBehindFocusedIndex >= 0 &&
1497 mStacks.get(stackBehindFocusedIndex).topRunningActivityLocked() == null) {
1498 stackBehindFocusedIndex--;
Chong Zhangb16cf342015-11-12 17:22:40 -08001499 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001500 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001501 && stackIndex == stackBehindFocusedIndex) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001502 // Stacks directly behind the docked or pinned stack are always visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001503 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001504 }
1505
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001506 final int stackBehindFocusedId = (stackBehindFocusedIndex >= 0)
1507 ? mStacks.get(stackBehindFocusedIndex).mStackId : INVALID_STACK_ID;
1508
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001509 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001510 && focusedStack.isStackTranslucent(starting, stackBehindFocusedId)) {
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001511 // Stacks behind the fullscreen stack with a translucent activity are always
1512 // visible so they can act as a backdrop to the translucent activity.
1513 // For example, dialog activities
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001514 if (stackIndex == stackBehindFocusedIndex) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001515 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001516 }
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001517 if (stackBehindFocusedIndex >= 0) {
1518 if ((stackBehindFocusedId == DOCKED_STACK_ID
1519 || stackBehindFocusedId == PINNED_STACK_ID)
1520 && stackIndex == (stackBehindFocusedIndex - 1)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001521 // The stack behind the docked or pinned stack is also visible so we can have a
1522 // complete backdrop to the translucent activity when the docked stack is up.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001523 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001524 }
1525 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001526 }
1527
Wale Ogunwale3797c222015-10-27 14:21:58 -07001528 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001529 // Visibility of any static stack should have been determined by the conditions above.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001530 return STACK_INVISIBLE;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001531 }
1532
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001533 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001534 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001535
1536 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1537 continue;
1538 }
1539
Wale Ogunwale3797c222015-10-27 14:21:58 -07001540 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001541 // These stacks can't have any dynamic stacks visible behind them.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001542 return STACK_INVISIBLE;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001543 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001544
Wale Ogunwale56d75cf2016-03-09 15:14:47 -08001545 if (!stack.isStackTranslucent(starting, INVALID_STACK_ID)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001546 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001547 }
1548 }
1549
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001550 return STACK_VISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001551 }
1552
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001553 final int rankTaskLayers(int baseLayer) {
1554 int layer = 0;
1555 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1556 final TaskRecord task = mTaskHistory.get(taskNdx);
1557 ActivityRecord r = task.topRunningActivityLocked();
1558 if (r == null || r.finishing || !r.visible) {
1559 task.mLayerRank = -1;
1560 } else {
1561 task.mLayerRank = baseLayer + layer++;
1562 }
1563 }
1564 return layer;
1565 }
1566
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001567 /**
1568 * Make sure that all activities that need to be visible (that is, they
1569 * currently can be seen by the user) actually are.
1570 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001571 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1572 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001573 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001574 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001575 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001576 if (top != null) {
1577 checkTranslucentActivityWaiting(top);
1578 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001579
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001580 // If the top activity is not fullscreen, then we need to
1581 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001582 boolean aboveTop = top != null;
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08001583 final int stackVisibility = getStackVisibilityLocked(starting);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001584 final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
1585 final boolean stackVisibleBehind = stackVisibility == STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001586 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaled046a012015-12-24 13:05:59 -08001587 boolean resumeNextActivity = isFocusable() && (isInStackLocked(starting) == null);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001588 boolean behindTranslucentActivity = false;
1589 final ActivityRecord visibleBehind = getVisibleBehindActivity();
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001590 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001591 final TaskRecord task = mTaskHistory.get(taskNdx);
1592 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001593 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1594 final ActivityRecord r = activities.get(activityNdx);
1595 if (r.finishing) {
1596 continue;
1597 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001598 final boolean isTop = r == top;
1599 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001600 continue;
1601 }
1602 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001603 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1604 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001605 final boolean activityVisibleBehind =
1606 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == r;
1607 final boolean isVisible = (!behindFullscreenActivity || r.mLaunchTaskBehind
1608 || activityVisibleBehind) && okToShowLocked(r);
1609 if (isVisible) {
1610 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1611 + " finishing=" + r.finishing + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001612 // First: if this is not the current activity being started, make
1613 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001614 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001615 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001616 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001617
1618 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001619 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001620 resumeNextActivity, r)) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001621 if (activityNdx >= activities.size()) {
1622 // Record may be removed if its process needs to restart.
1623 activityNdx = activities.size() - 1;
1624 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001625 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001626 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001627 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001628 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001629 // If this activity is already visible, then there is nothing to do here.
1630 if (handleAlreadyVisible(r)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001631 resumeNextActivity = false;
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001632 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001633 } else {
Jorim Jaggie66edb12016-02-05 12:41:17 -08001634 makeVisibleIfNeeded(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001635 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001636 // Aggregate current change flags.
1637 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001638 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1639 behindFullscreenActivity, task, r);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001640 if (behindFullscreenActivity && !r.fullscreen) {
1641 behindTranslucentActivity = true;
1642 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001643 } else {
Wale Ogunwale834c2362016-01-23 18:14:58 -08001644 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001645 + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1646 + stackInvisible + " behindFullscreenActivity="
1647 + behindFullscreenActivity + " mLaunchTaskBehind="
1648 + r.mLaunchTaskBehind);
1649 makeInvisible(r, visibleBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001650 }
1651 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001652 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1653 // The visibility of tasks and the activities they contain in freeform stack are
1654 // determined individually unlike other stacks where the visibility or fullscreen
1655 // status of an activity in a previous task affects other.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001656 behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001657 } else if (mStackId == HOME_STACK_ID) {
1658 if (task.isHomeTask()) {
1659 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
1660 + " stackInvisible=" + stackInvisible
1661 + " behindFullscreenActivity=" + behindFullscreenActivity);
1662 // No other task in the home stack should be visible behind the home activity.
1663 // Home activities is usually a translucent activity with the wallpaper behind
1664 // them. However, when they don't have the wallpaper behind them, we want to
1665 // show activities in the next application stack behind them vs. another
1666 // task in the home stack like recents.
1667 behindFullscreenActivity = true;
1668 } else if (task.isRecentsTask()
1669 && task.getTaskToReturnTo() == APPLICATION_ACTIVITY_TYPE) {
1670 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1671 "Recents task returning to app: at " + task
1672 + " stackInvisible=" + stackInvisible
1673 + " behindFullscreenActivity=" + behindFullscreenActivity);
1674 // We don't want any other tasks in the home stack visible if the recents
1675 // activity is going to be returning to an application activity type.
1676 // We do this to preserve the visible order the user used to get into the
1677 // recents activity. The recents activity is normally translucent and if it
1678 // doesn't have the wallpaper behind it the next activity in the home stack
1679 // shouldn't be visible when the home stack is brought to the front to display
1680 // the recents activity from an app.
1681 behindFullscreenActivity = true;
1682 }
1683
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001684 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001685 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001686
1687 if (mTranslucentActivityWaiting != null &&
1688 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1689 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1690 notifyActivityDrawnLocked(null);
1691 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001692 }
Craig Mautner58547802013-03-05 08:23:53 -08001693
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001694 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1695 if (mTranslucentActivityWaiting != top) {
1696 mUndrawnActivitiesBelowTopTranslucent.clear();
1697 if (mTranslucentActivityWaiting != null) {
1698 // Call the callback with a timeout indication.
1699 notifyActivityDrawnLocked(null);
1700 mTranslucentActivityWaiting = null;
1701 }
1702 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1703 }
1704 }
1705
1706 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001707 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001708 // We need to make sure the app is running if it's the top, or it is just made visible from
1709 // invisible. If the app is already visible, it must have died while it was visible. In this
1710 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1711 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001712 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001713 // This activity needs to be visible, but isn't even running...
1714 // get it started and resume if no other stack in this stack is resumed.
1715 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1716 if (r != starting) {
1717 r.startFreezingScreenLocked(r.app, configChanges);
1718 }
1719 if (!r.visible || r.mLaunchTaskBehind) {
1720 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1721 setVisible(r, true);
1722 }
1723 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001724 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001725 return true;
1726 }
1727 }
1728 return false;
1729 }
1730
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001731 private void makeInvisible(ActivityRecord r, ActivityRecord visibleBehind) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001732 if (!r.visible) {
1733 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1734 return;
1735 }
1736 // Now for any activities that aren't visible to the user, make sure they no longer are
1737 // keeping the screen frozen.
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001738 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001739 try {
1740 setVisible(r, false);
1741 switch (r.state) {
1742 case STOPPING:
1743 case STOPPED:
1744 if (r.app != null && r.app.thread != null) {
1745 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1746 "Scheduling invisibility: " + r);
1747 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1748 }
1749 break;
1750
1751 case INITIALIZING:
1752 case RESUMED:
1753 case PAUSING:
1754 case PAUSED:
1755 // This case created for transitioning activities from
1756 // translucent to opaque {@link Activity#convertToOpaque}.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001757 if (visibleBehind == r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001758 releaseBackgroundResources(r);
1759 } else {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001760 addToStopping(r, true /* immediate */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001761 }
1762 break;
1763
1764 default:
1765 break;
1766 }
1767 } catch (Exception e) {
1768 // Just skip on any failure; we'll make it visible when it next restarts.
1769 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1770 }
1771 }
1772
1773 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1774 TaskRecord task, ActivityRecord r) {
1775 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001776 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08001777 + " stackInvisible=" + stackInvisible
1778 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001779 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001780 behindFullscreenActivity = true;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001781 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001782 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1783 + " stackInvisible=" + stackInvisible
1784 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001785 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001786 }
1787 return behindFullscreenActivity;
1788 }
1789
Jorim Jaggie66edb12016-02-05 12:41:17 -08001790 private void makeVisibleIfNeeded(ActivityRecord starting, ActivityRecord r) {
1791
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001792 // This activity is not currently visible, but is running. Tell it to become visible.
Jorim Jaggie66edb12016-02-05 12:41:17 -08001793 if (r.state == ActivityState.RESUMED || r == starting) {
Chong Zhange05db742016-02-16 16:58:37 -08001794 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1795 "Not making visible, r=" + r + " state=" + r.state + " starting=" + starting);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001796 return;
1797 }
1798
1799 // If this activity is paused, tell it to now show its window.
1800 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1801 "Making visible and scheduling visibility: " + r);
1802 try {
1803 if (mTranslucentActivityWaiting != null) {
1804 r.updateOptionsLocked(r.returningOptions);
1805 mUndrawnActivitiesBelowTopTranslucent.add(r);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001806 }
Jorim Jaggie66edb12016-02-05 12:41:17 -08001807 setVisible(r, true);
1808 r.sleeping = false;
1809 r.app.pendingUiClean = true;
1810 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1811 r.stopFreezingScreenLocked(false);
1812 } catch (Exception e) {
1813 // Just skip on any failure; we'll make it
1814 // visible when it next restarts.
1815 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001816 }
1817 }
1818
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001819 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001820 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1821 r.stopFreezingScreenLocked(false);
1822 try {
1823 if (r.returningOptions != null) {
1824 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1825 }
1826 } catch(RemoteException e) {
1827 }
1828 return r.state == ActivityState.RESUMED;
1829 }
1830
Todd Kennedyaab56db2015-01-30 09:39:53 -08001831 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001832 mTranslucentActivityWaiting = r;
1833 mUndrawnActivitiesBelowTopTranslucent.clear();
1834 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1835 }
1836
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001837 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1838 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1839 final TaskRecord task = mTaskHistory.get(taskNdx);
1840 final ArrayList<ActivityRecord> activities = task.mActivities;
1841 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1842 final ActivityRecord r = activities.get(activityNdx);
1843 if ( r.appTimeTracker != except) {
1844 r.appTimeTracker = null;
1845 }
1846 }
1847 }
1848 }
1849
Craig Mautner5eda9b32013-07-02 11:58:16 -07001850 /**
1851 * Called as activities below the top translucent activity are redrawn. When the last one is
1852 * redrawn notify the top activity by calling
1853 * {@link Activity#onTranslucentConversionComplete}.
1854 *
1855 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1856 * occurred and the activity will be notified immediately.
1857 */
1858 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001859 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001860 if ((r == null)
1861 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1862 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1863 // The last undrawn activity below the top has just been drawn. If there is an
1864 // opaque activity at the top, notify it that it can become translucent safely now.
1865 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1866 mTranslucentActivityWaiting = null;
1867 mUndrawnActivitiesBelowTopTranslucent.clear();
1868 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1869
Craig Mautner71dd1b62014-02-18 15:48:52 -08001870 if (waitingActivity != null) {
1871 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1872 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1873 try {
1874 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1875 waitingActivity.appToken, r != null);
1876 } catch (RemoteException e) {
1877 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001878 }
1879 }
1880 }
1881 }
1882
Craig Mautnera61bc652013-10-28 15:43:18 -07001883 /** If any activities below the top running one are in the INITIALIZING state and they have a
1884 * starting window displayed then remove that starting window. It is possible that the activity
1885 * in this state will never resumed in which case that starting window will be orphaned. */
1886 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001887 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001888 boolean aboveTop = true;
1889 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1890 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1891 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1892 final ActivityRecord r = activities.get(activityNdx);
1893 if (aboveTop) {
1894 if (r == topActivity) {
1895 aboveTop = false;
1896 }
1897 continue;
1898 }
1899
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08001900 if (r.state == ActivityState.INITIALIZING
1901 && r.mStartingWindowState == STARTING_WINDOW_SHOWN) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001902 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1903 "Found orphaned starting window " + r);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08001904 r.mStartingWindowState = STARTING_WINDOW_REMOVED;
Craig Mautnera61bc652013-10-28 15:43:18 -07001905 mWindowManager.removeAppStartingWindow(r.appToken);
1906 }
1907 }
1908 }
1909 }
1910
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001911 /**
1912 * Ensure that the top activity in the stack is resumed.
1913 *
1914 * @param prev The previously resumed activity, for when in the process
1915 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001916 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001917 *
1918 * @return Returns true if something is being resumed, or false if
1919 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001920 *
1921 * NOTE: It is not safe to call this method directly as it can cause an activity in a
1922 * non-focused stack to be resumed.
1923 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
1924 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001925 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08001926 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001927 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001928 // Don't even start recursing.
1929 return false;
1930 }
1931
1932 boolean result = false;
1933 try {
1934 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001935 mStackSupervisor.inResumeTopActivity = true;
1936 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1937 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001938 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001939 }
Craig Mautner544efa72014-09-04 16:41:20 -07001940 result = resumeTopActivityInnerLocked(prev, options);
1941 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001942 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001943 }
1944 return result;
1945 }
1946
Chong Zhang280d3322015-11-03 17:27:26 -08001947 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001948 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001949
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001950 if (!mService.mBooting && !mService.mBooted) {
1951 // Not ready yet!
1952 return false;
1953 }
1954
Craig Mautnerdf88d732014-01-27 09:21:32 -08001955 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001956 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001957 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001958 // Do not resume this stack if its parent is not resumed.
1959 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1960 return false;
1961 }
1962
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001963 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07001964
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001965 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001966 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001967
1968 // Remember how we'll process this pause/resume situation, and ensure
1969 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001970 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1971 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001972
Craig Mautner84984fa2014-06-19 11:19:20 -07001973 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001974 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001975 // There are no more activities!
1976 final String reason = "noMoreActivities";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001977 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(reason)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001978 // Try to move focus to the next visible stack with a running activity if this
1979 // stack is not covering the entire screen.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001980 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
1981 mStackSupervisor.getFocusedStack(), prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001982 }
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001983
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001984 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001985 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001986 if (DEBUG_STATES) Slog.d(TAG_STATES,
1987 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001988 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001989 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001990 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1991 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1992 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001993 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001994 }
1995
1996 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001997
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001998 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001999 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2000 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002001 // Make sure we have executed any pending transitions, since there
2002 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002003 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002004 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002005 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002006 if (DEBUG_STATES) Slog.d(TAG_STATES,
2007 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07002008 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002009 return false;
2010 }
2011
Craig Mautner525f3d92013-05-07 14:01:50 -07002012 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07002013 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07002014 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002015 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07002016 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002017 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07002018 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07002019 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07002020 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07002021 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07002022 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08002023 } else if (!isOnHomeDisplay()) {
2024 return false;
2025 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002026 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08002027 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07002028 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
2029 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2030 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08002031 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07002032 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002033 }
2034
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002035 // If we are sleeping, and there is no resumed activity, and the top
2036 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07002037 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09002038 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002039 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002040 // Make sure we have executed any pending transitions, since there
2041 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002042 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002043 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002044 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002045 if (DEBUG_STATES) Slog.d(TAG_STATES,
2046 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002047 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002048 return false;
2049 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002050
2051 // Make sure that the user who owns this activity is started. If not,
2052 // we will just leave it as is because someone should be bringing
2053 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002054 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002055 Slog.w(TAG, "Skipping resume of top activity " + next
2056 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002057 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002058 return false;
2059 }
2060
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002061 // The activity may be waiting for stop, but that is no longer
2062 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002063 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002064 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002065 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002066 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002067
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002068 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002069
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002070 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002071 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002072 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002073 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002074 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002075 return false;
2076 }
2077
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002078 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2079
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002080 // We need to start pausing the current activity so the top one can be resumed...
2081 final boolean dontWaitForPause = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002082 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002083 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002084 if (DEBUG_STATES) Slog.d(TAG_STATES,
2085 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08002086 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002087 }
2088 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002089 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002090 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002091 // At this point we want to put the upcoming activity's process
2092 // at the top of the LRU list, since we know we will be needing it
2093 // very soon and it would be a waste to let it get killed if it
2094 // happens to be sitting towards the end.
2095 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002096 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002097 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002098 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002099 return true;
2100 }
2101
Christopher Tated3f175c2012-06-14 14:16:54 -07002102 // If the most recent activity was noHistory but was only stopped rather
2103 // than stopped+finished because the device went to sleep, we need to make
2104 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07002105 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002106 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002107 if (DEBUG_STATES) Slog.d(TAG_STATES,
2108 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002109 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002110 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002111 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002112 }
2113
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002114 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002115 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2116 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002117 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002118 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2119 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002120 } else {
2121 // The next activity is already visible, so hide the previous
2122 // activity's windows right now so we can show the new one ASAP.
2123 // We only do this if the previous is finishing, which should mean
2124 // it is on top of the one being resumed so hiding it quickly
2125 // is good. Otherwise, we want to do the normal route of allowing
2126 // the resumed activity to be shown so we can decide if the
2127 // previous should actually be hidden depending on whether the
2128 // new one is found to be full-screen or not.
2129 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002130 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002131 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2132 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002133 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002134 + ", nowVisible=" + next.nowVisible);
2135 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002136 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002137 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002138 + ", waitingVisible="
2139 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2140 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002141 }
2142 }
2143 }
2144
Dianne Hackborne7f97212011-02-24 14:40:20 -08002145 // Launching this app's activity, make sure the app is no longer
2146 // considered stopped.
2147 try {
2148 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002149 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002150 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002151 } catch (IllegalArgumentException e) {
2152 Slog.w(TAG, "Failed trying to unstop package "
2153 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002154 }
2155
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002156 // We are starting up the next activity, so tell the window manager
2157 // that the previous one will be hidden soon. This way it can know
2158 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002159 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002160 if (prev != null) {
2161 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002162 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002163 "Prepare close transition: prev=" + prev);
2164 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002165 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002166 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002167 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002168 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002169 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2170 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002171 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002172 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002173 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002174 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2175 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002176 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002177 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002178 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002179 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002180 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002181 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002182 : next.mLaunchTaskBehind
2183 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2184 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002185 }
2186 }
Craig Mautner967212c2013-04-13 21:10:58 -07002187 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002188 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002189 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002190 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002191 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002192 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002193 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002194 }
2195 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002196
2197 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002198 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002199 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2200 if (opts != null) {
2201 resumeAnimOptions = opts.toBundle();
2202 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002203 next.applyOptionsLocked();
2204 } else {
2205 next.clearOptionsLocked();
2206 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002207
Craig Mautnercf910b02013-04-23 11:23:27 -07002208 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002209 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002210 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002211
2212 // This activity is now becoming visible.
Jorim Jaggi1b025a62016-02-03 19:27:49 -08002213 if (!next.visible) {
2214 mWindowManager.setAppVisibility(next.appToken, true);
2215 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002216
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002217 // schedule launch ticks to collect information about slow apps.
2218 next.startLaunchTickingLocked();
2219
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002220 ActivityRecord lastResumedActivity =
2221 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002222 ActivityState lastState = next.state;
2223
2224 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002225
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002226 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002227 next.state = ActivityState.RESUMED;
2228 mResumedActivity = next;
2229 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002230 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002231 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002232 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002233 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002234
2235 // Have the window manager re-evaluate the orientation of
2236 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002237 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002238 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002239 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002240 mService.mConfiguration,
2241 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2242 if (config != null) {
2243 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002244 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002245 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002246 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002247
Craig Mautner525f3d92013-05-07 14:01:50 -07002248 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002249 // The configuration update wasn't able to keep the existing
2250 // instance of the activity, and instead started a new one.
2251 // We should be all done, but let's just make sure our activity
2252 // is still at the top and schedule another run if something
2253 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002254 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002255 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002256 "Activity config changed during resume: " + next
2257 + ", new next: " + nextNext);
2258 if (nextNext != next) {
2259 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002260 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002261 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002262 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002263 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002264 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002265 return true;
2266 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002267 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002268 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002269 }
Craig Mautner58547802013-03-05 08:23:53 -08002270
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002271 try {
2272 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002273 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002274 if (a != null) {
2275 final int N = a.size();
2276 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002277 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2278 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002279 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002280 }
2281 }
2282
2283 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002284 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002285 }
2286
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002287 // Well the app will no longer be stopped.
2288 // Clear app token stopped state in window manager if needed.
2289 mWindowManager.notifyAppStopped(next.appToken, false);
2290
Craig Mautner299f9602015-01-26 09:47:33 -08002291 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2292 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002293
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002294 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002295 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002296 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002297 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002298 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002299 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002300 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002301
Craig Mautner0eea92c2013-05-16 13:35:39 -07002302 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002303
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002304 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002305 } catch (Exception e) {
2306 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002307 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002308 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002309 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002310 if (lastStack != null) {
2311 lastStack.mResumedActivity = lastResumedActivity;
2312 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002313 Slog.i(TAG, "Restarting because process died: " + next);
2314 if (!next.hasBeenLaunched) {
2315 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002316 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2317 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002318 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002319 next.appToken, next.packageName, next.theme,
2320 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002321 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2322 next.windowFlags, null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002323 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002324 }
George Mount2c92c972014-03-20 09:38:23 -07002325 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002326 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002327 return true;
2328 }
2329
2330 // From this point on, if something goes wrong there is no way
2331 // to recover the activity.
2332 try {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002333 completeResumeLocked(next);
2334 } catch (Exception e) {
2335 // If any exception gets thrown, toss away this
2336 // activity and try the next one.
2337 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002338 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002339 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002340 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002341 return true;
2342 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002343 } else {
2344 // Whoops, need to restart this activity!
2345 if (!next.hasBeenLaunched) {
2346 next.hasBeenLaunched = true;
2347 } else {
2348 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002349 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002350 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002351 mService.compatibilityInfoForPackageLocked(
2352 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002353 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002354 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002355 null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002356 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002357 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002358 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002359 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002360 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002361 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002362 }
2363
Craig Mautnercf910b02013-04-23 11:23:27 -07002364 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002365 return true;
2366 }
2367
riddle_hsuc215a4f2014-12-27 12:10:45 +08002368 private TaskRecord getNextTask(TaskRecord targetTask) {
2369 final int index = mTaskHistory.indexOf(targetTask);
2370 if (index >= 0) {
2371 final int numTasks = mTaskHistory.size();
2372 for (int i = index + 1; i < numTasks; ++i) {
2373 TaskRecord task = mTaskHistory.get(i);
2374 if (task.userId == targetTask.userId) {
2375 return task;
2376 }
2377 }
2378 }
2379 return null;
2380 }
2381
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002382 private void insertTaskAtPosition(TaskRecord task, int position) {
2383 if (position >= mTaskHistory.size()) {
2384 insertTaskAtTop(task, null);
2385 return;
2386 }
2387 // Calculate maximum possible position for this task.
2388 int maxPosition = mTaskHistory.size();
2389 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002390 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002391 // Put non-current user tasks below current user tasks.
2392 while (maxPosition > 0) {
2393 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2394 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002395 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002396 break;
2397 }
2398 maxPosition--;
2399 }
2400 }
2401 position = Math.min(position, maxPosition);
2402 mTaskHistory.remove(task);
2403 mTaskHistory.add(position, task);
2404 updateTaskMovement(task, true);
2405 }
2406
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002407 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002408 // If the moving task is over home stack, transfer its return type to next task
2409 if (task.isOverHomeStack()) {
2410 final TaskRecord nextTask = getNextTask(task);
2411 if (nextTask != null) {
2412 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2413 }
2414 }
2415
Craig Mautner9c85c202013-10-04 20:11:26 -07002416 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002417 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002418 if (isOnHomeDisplay()) {
2419 ActivityStack lastStack = mStackSupervisor.getLastStack();
2420 final boolean fromHome = lastStack.isHomeStack();
2421 if (!isHomeStack() && (fromHome || topTask() != task)) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002422 int returnToType = APPLICATION_ACTIVITY_TYPE;
2423 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2424 returnToType = lastStack.topTask() == null
2425 ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2426 }
2427 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002428 }
2429 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002430 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002431 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002432
Craig Mautnerac6f8432013-07-17 13:24:59 -07002433 mTaskHistory.remove(task);
2434 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002435 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002436 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002437 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002438 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002439 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002440 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002441 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002442 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002443 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002444 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002445 break;
2446 }
2447 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002448 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002449 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002450 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002451 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002452 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002453
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002454 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2455 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002456 TaskRecord rTask = r.task;
2457 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002458 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2459 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002460 // Last activity in task had been removed or ActivityManagerService is reusing task.
2461 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002462 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002463 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002464 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002465 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002466 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002467 if (!newTask) {
2468 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002469 boolean startIt = true;
2470 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2471 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002472 if (task.getTopActivity() == null) {
2473 // All activities in task are finishing.
2474 continue;
2475 }
Craig Mautner70a86932013-02-28 22:37:44 -08002476 if (task == r.task) {
2477 // Here it is! Now, if this is not yet visible to the
2478 // user, then just add it without starting; it will
2479 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002480 if (!startIt) {
2481 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2482 + task, new RuntimeException("here").fillInStackTrace());
2483 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002484 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002485 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002486 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002487 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002488 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002489 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002490 return;
2491 }
2492 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002493 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002494 startIt = false;
2495 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002496 }
2497 }
2498
2499 // Place a new activity at top of stack, so it is next to interact
2500 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002501
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002502 // If we are not placing the new activity frontmost, we do not want
2503 // to deliver the onUserLeaving callback to the actual frontmost
2504 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002505 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002506 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002507 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002508 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002509 }
Craig Mautner70a86932013-02-28 22:37:44 -08002510
2511 task = r.task;
2512
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002513 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002514 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002515 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002516 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002517 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002518
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002519 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002520 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002521 // We want to show the starting preview window if we are
2522 // switching to a new task, or the next activity's process is
2523 // not currently running.
2524 boolean showStartingIcon = newTask;
2525 ProcessRecord proc = r.app;
2526 if (proc == null) {
2527 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2528 }
2529 if (proc == null || proc.thread == null) {
2530 showStartingIcon = true;
2531 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002532 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002533 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002534 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002535 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002536 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002537 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002538 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002539 ? r.mLaunchTaskBehind
2540 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2541 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002542 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002543 mNoAnimActivities.remove(r);
2544 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002545 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002546 boolean doShow = true;
2547 if (newTask) {
2548 // Even though this activity is starting fresh, we still need
2549 // to reset it to make sure we apply affinities to move any
2550 // existing activities from other tasks in to it.
2551 // If the caller has requested that the target task be
2552 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002553 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002554 resetTaskIfNeededLocked(r, r);
2555 doShow = topRunningNonDelayedActivityLocked(null) == r;
2556 }
Chong Zhang280d3322015-11-03 17:27:26 -08002557 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002558 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2559 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002560 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002561 if (r.mLaunchTaskBehind) {
2562 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2563 // tell WindowManager that r is visible even though it is at the back of the stack.
2564 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002565 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002566 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002567 // Figure out if we are transitioning from another activity that is
2568 // "has the same starting icon" as the next one. This allows the
2569 // window manager to keep the previous window it had previously
2570 // created, if it still had one.
2571 ActivityRecord prev = mResumedActivity;
2572 if (prev != null) {
2573 // We don't want to reuse the previous starting preview if:
2574 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002575 if (prev.task != r.task) {
2576 prev = null;
2577 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002578 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002579 else if (prev.nowVisible) {
2580 prev = null;
2581 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002582 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002583 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002584 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002585 mService.compatibilityInfoForPackageLocked(
2586 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002587 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002588 prev != null ? prev.appToken : null, showStartingIcon);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002589 r.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002590 }
2591 } else {
2592 // If this is the first activity, don't do any fancy animations,
2593 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002594 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002595 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002596 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002597 }
2598 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002599 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002600 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002601 }
2602
Dianne Hackbornbe707852011-11-11 14:32:10 -08002603 final void validateAppTokensLocked() {
2604 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002605 mValidateAppTokens.ensureCapacity(numActivities());
2606 final int numTasks = mTaskHistory.size();
2607 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2608 TaskRecord task = mTaskHistory.get(taskNdx);
2609 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002610 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002611 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002612 }
Craig Mautner000f0022013-02-26 15:04:29 -08002613 TaskGroup group = new TaskGroup();
2614 group.taskId = task.taskId;
2615 mValidateAppTokens.add(group);
2616 final int numActivities = activities.size();
2617 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2618 final ActivityRecord r = activities.get(activityNdx);
2619 group.tokens.add(r.appToken);
2620 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002621 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002622 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002623 }
2624
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002625 /**
2626 * Perform a reset of the given task, if needed as part of launching it.
2627 * Returns the new HistoryRecord at the top of the task.
2628 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002629 /**
2630 * Helper method for #resetTaskIfNeededLocked.
2631 * We are inside of the task being reset... we'll either finish this activity, push it out
2632 * for another task, or leave it as-is.
2633 * @param task The task containing the Activity (taskTop) that might be reset.
2634 * @param forceReset
2635 * @return An ActivityOptions that needs to be processed.
2636 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002637 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002638 ActivityOptions topOptions = null;
2639
2640 int replyChainEnd = -1;
2641 boolean canMoveOptions = true;
2642
2643 // We only do this for activities that are not the root of the task (since if we finish
2644 // the root, we may no longer have the task!).
2645 final ArrayList<ActivityRecord> activities = task.mActivities;
2646 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002647 final int rootActivityNdx = task.findEffectiveRootIndex();
2648 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002649 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002650 if (target.frontOfTask)
2651 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002652
2653 final int flags = target.info.flags;
2654 final boolean finishOnTaskLaunch =
2655 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2656 final boolean allowTaskReparenting =
2657 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2658 final boolean clearWhenTaskReset =
2659 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2660
2661 if (!finishOnTaskLaunch
2662 && !clearWhenTaskReset
2663 && target.resultTo != null) {
2664 // If this activity is sending a reply to a previous
2665 // activity, we can't do anything with it now until
2666 // we reach the start of the reply chain.
2667 // XXX note that we are assuming the result is always
2668 // to the previous activity, which is almost always
2669 // the case but we really shouldn't count on.
2670 if (replyChainEnd < 0) {
2671 replyChainEnd = i;
2672 }
2673 } else if (!finishOnTaskLaunch
2674 && !clearWhenTaskReset
2675 && allowTaskReparenting
2676 && target.taskAffinity != null
2677 && !target.taskAffinity.equals(task.affinity)) {
2678 // If this activity has an affinity for another
2679 // task, then we need to move it out of here. We will
2680 // move it as far out of the way as possible, to the
2681 // bottom of the activity stack. This also keeps it
2682 // correctly ordered with any activities we previously
2683 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002684 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002685 final ActivityRecord bottom =
2686 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002687 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002688 if (bottom != null && target.taskAffinity != null
2689 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002690 // If the activity currently at the bottom has the
2691 // same task affinity as the one we are moving,
2692 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002693 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002694 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002695 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002696 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002697 targetTask = createTaskRecord(
2698 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
2699 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002700 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002701 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002702 + " out to new task " + target.task);
2703 }
2704
Wale Ogunwale706ed792015-08-02 10:29:44 -07002705 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002706
Craig Mautner525f3d92013-05-07 14:01:50 -07002707 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002708 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2709 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002710 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002711 if (p.finishing) {
2712 continue;
2713 }
2714
Craig Mautnere3a74d52013-02-22 14:14:58 -08002715 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002716 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002717 topOptions = p.takeOptionsLocked();
2718 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002719 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002720 }
2721 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002722 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2723 "Removing activity " + p + " from task=" + task + " adding to task="
2724 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002725 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2726 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002727 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002728 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002729
Wale Ogunwale706ed792015-08-02 10:29:44 -07002730 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002731 }
2732
Wale Ogunwale706ed792015-08-02 10:29:44 -07002733 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002734 if (VALIDATE_TOKENS) {
2735 validateAppTokensLocked();
2736 }
2737
2738 replyChainEnd = -1;
2739 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2740 // If the activity should just be removed -- either
2741 // because it asks for it, or the task should be
2742 // cleared -- then finish it and anything that is
2743 // part of its reply chain.
2744 int end;
2745 if (clearWhenTaskReset) {
2746 // In this case, we want to finish this activity
2747 // and everything above it, so be sneaky and pretend
2748 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002749 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002750 } else if (replyChainEnd < 0) {
2751 end = i;
2752 } else {
2753 end = replyChainEnd;
2754 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002755 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002756 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002757 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002758 if (p.finishing) {
2759 continue;
2760 }
2761 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002762 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002763 topOptions = p.takeOptionsLocked();
2764 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002765 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002766 }
2767 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002768 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002769 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002770 if (finishActivityLocked(
2771 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002772 end--;
2773 srcPos--;
2774 }
2775 }
2776 replyChainEnd = -1;
2777 } else {
2778 // If we were in the middle of a chain, well the
2779 // activity that started it all doesn't want anything
2780 // special, so leave it all as-is.
2781 replyChainEnd = -1;
2782 }
2783 }
2784
2785 return topOptions;
2786 }
2787
2788 /**
2789 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2790 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2791 * @param affinityTask The task we are looking for an affinity to.
2792 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2793 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2794 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2795 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002796 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002797 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002798 int replyChainEnd = -1;
2799 final int taskId = task.taskId;
2800 final String taskAffinity = task.affinity;
2801
2802 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2803 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002804 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2805
2806 // Do not operate on or below the effective root Activity.
2807 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002808 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002809 if (target.frontOfTask)
2810 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002811
2812 final int flags = target.info.flags;
2813 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2814 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2815
2816 if (target.resultTo != null) {
2817 // If this activity is sending a reply to a previous
2818 // activity, we can't do anything with it now until
2819 // we reach the start of the reply chain.
2820 // XXX note that we are assuming the result is always
2821 // to the previous activity, which is almost always
2822 // the case but we really shouldn't count on.
2823 if (replyChainEnd < 0) {
2824 replyChainEnd = i;
2825 }
2826 } else if (topTaskIsHigher
2827 && allowTaskReparenting
2828 && taskAffinity != null
2829 && taskAffinity.equals(target.taskAffinity)) {
2830 // This activity has an affinity for our task. Either remove it if we are
2831 // clearing or move it over to our task. Note that
2832 // we currently punt on the case where we are resetting a
2833 // task that is not at the top but who has activities above
2834 // with an affinity to it... this is really not a normal
2835 // case, and we will need to later pull that task to the front
2836 // and usually at that point we will do the reset and pick
2837 // up those remaining activities. (This only happens if
2838 // someone starts an activity in a new task from an activity
2839 // in a task that is not currently on top.)
2840 if (forceReset || finishOnTaskLaunch) {
2841 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002842 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2843 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002844 for (int srcPos = start; srcPos >= i; --srcPos) {
2845 final ActivityRecord p = activities.get(srcPos);
2846 if (p.finishing) {
2847 continue;
2848 }
Todd Kennedy539db512014-12-15 09:57:55 -08002849 finishActivityLocked(
2850 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002851 }
2852 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002853 if (taskInsertionPoint < 0) {
2854 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002855
Craig Mautner77878772013-03-04 19:46:24 -08002856 }
Craig Mautner77878772013-03-04 19:46:24 -08002857
2858 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002859 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2860 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2861 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002862 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002863 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002864 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002865 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002866
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002867 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2868 "Removing and adding activity " + p + " to stack at " + task
2869 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002870 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2871 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002872 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002873 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002874 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002875 if (VALIDATE_TOKENS) {
2876 validateAppTokensLocked();
2877 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002878
2879 // Now we've moved it in to place... but what if this is
2880 // a singleTop activity and we have put it on top of another
2881 // instance of the same activity? Then we drop the instance
2882 // below so it remains singleTop.
2883 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2884 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002885 int targetNdx = taskActivities.indexOf(target);
2886 if (targetNdx > 0) {
2887 ActivityRecord p = taskActivities.get(targetNdx - 1);
2888 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002889 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2890 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002891 }
2892 }
2893 }
2894 }
2895
2896 replyChainEnd = -1;
2897 }
2898 }
Craig Mautner77878772013-03-04 19:46:24 -08002899 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002900 }
2901
Craig Mautner8849a5e2013-04-02 16:41:03 -07002902 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002903 ActivityRecord newActivity) {
2904 boolean forceReset =
2905 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2906 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2907 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2908 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2909 forceReset = true;
2910 }
2911 }
2912
2913 final TaskRecord task = taskTop.task;
2914
2915 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2916 * for remaining tasks. Used for later tasks to reparent to task. */
2917 boolean taskFound = false;
2918
2919 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2920 ActivityOptions topOptions = null;
2921
Craig Mautner77878772013-03-04 19:46:24 -08002922 // Preserve the location for reparenting in the new task.
2923 int reparentInsertionPoint = -1;
2924
Craig Mautnere3a74d52013-02-22 14:14:58 -08002925 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2926 final TaskRecord targetTask = mTaskHistory.get(i);
2927
2928 if (targetTask == task) {
2929 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2930 taskFound = true;
2931 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002932 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2933 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002934 }
2935 }
2936
Craig Mautner70a86932013-02-28 22:37:44 -08002937 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002938 if (taskNdx >= 0) {
2939 do {
2940 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2941 } while (taskTop == null && taskNdx >= 0);
2942 }
Craig Mautner70a86932013-02-28 22:37:44 -08002943
Craig Mautnere3a74d52013-02-22 14:14:58 -08002944 if (topOptions != null) {
2945 // If we got some ActivityOptions from an activity on top that
2946 // was removed from the task, propagate them to the new real top.
2947 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002948 taskTop.updateOptionsLocked(topOptions);
2949 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002950 topOptions.abort();
2951 }
2952 }
2953
2954 return taskTop;
2955 }
2956
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002957 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2958 String resultWho, int requestCode, int resultCode, Intent data) {
2959
2960 if (callingUid > 0) {
2961 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002962 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002963 }
2964
2965 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2966 + " : who=" + resultWho + " req=" + requestCode
2967 + " res=" + resultCode + " data=" + data);
2968 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2969 try {
2970 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2971 list.add(new ResultInfo(resultWho, requestCode,
2972 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002973 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002974 return;
2975 } catch (Exception e) {
2976 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2977 }
2978 }
2979
2980 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2981 }
2982
Craig Mautner299f9602015-01-26 09:47:33 -08002983 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002984 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2985 return;
2986 }
2987
2988 final ActivityRecord next = topRunningActivityLocked();
2989 final String myReason = reason + " adjustFocus";
2990 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002991 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002992 // For freeform, docked, and pinned stacks we always keep the focus within the
2993 // stack as long as there is a running activity in the stack that we can adjust
2994 // focus to.
2995 mService.setFocusedActivityLocked(next, myReason);
2996 return;
2997 } else {
2998 final TaskRecord task = r.task;
2999 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
3000 // For non-fullscreen stack, we want to move the focus to the next visible
3001 // stack to prevent the home screen from moving to the top and obscuring
3002 // other visible stacks.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003003 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003004 return;
3005 }
3006 // Move the home stack to the top if this stack is fullscreen or there is no
3007 // other visible stack.
3008 if (mStackSupervisor.moveHomeStackTaskToTop(
3009 task.getTaskToReturnTo(), myReason)) {
3010 // Activity focus was already adjusted. Nothing else to do...
3011 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003012 }
Craig Mautner04f0b702013-10-22 12:31:01 -07003013 }
3014 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08003015 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003016
Wale Ogunwaled046a012015-12-24 13:05:59 -08003017 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07003018 }
3019
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003020 private boolean adjustFocusToNextFocusableStackLocked(String reason) {
3021 final ActivityStack stack = getNextFocusableStackLocked();
3022 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003023 if (stack == null) {
3024 return false;
3025 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003026 return mService.setFocusedActivityLocked(stack.topRunningActivityLocked(), myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003027 }
3028
Craig Mautnerf3333272013-04-22 10:55:53 -07003029 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003030 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003031 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3032 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3033 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07003034 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003035 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003036 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3037 "stop-no-history", false)) {
3038 // Activity was finished, no need to continue trying to schedule stop.
3039 adjustFocusedActivityLocked(r, "stopActivityFinished");
3040 r.resumeKeyDispatchingLocked();
3041 return;
3042 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003043 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003044 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003045 + " on stop because we're just sleeping");
3046 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003047 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003048 }
3049
3050 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08003051 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003052 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003053 try {
3054 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003055 if (DEBUG_STATES) Slog.v(TAG_STATES,
3056 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003057 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003058 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3059 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003060 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003061 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003062 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003063 EventLogTags.writeAmStopActivity(
3064 r.userId, System.identityHashCode(r), r.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003065 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07003066 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003067 r.setSleeping(true);
3068 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003069 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003070 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003071 } catch (Exception e) {
3072 // Maybe just ignore exceptions here... if the process
3073 // has crashed, our death notification will clean things
3074 // up.
3075 Slog.w(TAG, "Exception thrown during pause", e);
3076 // Just in case, assume it to be stopped.
3077 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003078 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003079 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003080 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003081 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003082 }
3083 }
3084 }
3085 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003086
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003087 /**
3088 * @return Returns true if the activity is being finished, false if for
3089 * some reason it is being left as-is.
3090 */
3091 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003092 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003093 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003094 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3095 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003096 + ", result=" + resultCode + ", data=" + resultData
3097 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003098 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003099 return false;
3100 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003101
Craig Mautnerd44711d2013-02-23 11:24:36 -08003102 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003103 return true;
3104 }
3105
Craig Mautnerd2328952013-03-05 12:46:26 -08003106 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003107 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3108 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3109 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3110 ActivityRecord r = activities.get(activityNdx);
3111 if (r.resultTo == self && r.requestCode == requestCode) {
3112 if ((r.resultWho == null && resultWho == null) ||
3113 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3114 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3115 false);
3116 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003117 }
3118 }
3119 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003120 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003121 }
3122
Adrian Roos20d7df32016-01-12 18:59:43 +01003123 final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003124 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003125 TaskRecord finishedTask = null;
3126 if (r == null || r.app != app) {
3127 return null;
3128 }
3129 Slog.w(TAG, " Force finishing activity "
3130 + r.intent.getComponent().flattenToShortString());
3131 int taskNdx = mTaskHistory.indexOf(r.task);
3132 int activityNdx = r.task.mActivities.indexOf(r);
3133 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3134 finishedTask = r.task;
3135 // Also terminate any activities below it that aren't yet
3136 // stopped, to avoid a situation where one will get
3137 // re-start our crashing activity once it gets resumed again.
3138 --activityNdx;
3139 if (activityNdx < 0) {
3140 do {
3141 --taskNdx;
3142 if (taskNdx < 0) {
3143 break;
3144 }
3145 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3146 } while (activityNdx < 0);
3147 }
3148 if (activityNdx >= 0) {
3149 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3150 if (r.state == ActivityState.RESUMED
3151 || r.state == ActivityState.PAUSING
3152 || r.state == ActivityState.PAUSED) {
3153 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3154 Slog.w(TAG, " Force finishing activity "
3155 + r.intent.getComponent().flattenToShortString());
3156 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003157 }
3158 }
3159 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003160 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003161 }
3162
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003163 final void finishVoiceTask(IVoiceInteractionSession session) {
3164 IBinder sessionBinder = session.asBinder();
3165 boolean didOne = false;
3166 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3167 TaskRecord tr = mTaskHistory.get(taskNdx);
3168 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3169 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3170 ActivityRecord r = tr.mActivities.get(activityNdx);
3171 if (!r.finishing) {
3172 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3173 false);
3174 didOne = true;
3175 }
3176 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003177 } else {
3178 // Check if any of the activities are using voice
3179 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3180 ActivityRecord r = tr.mActivities.get(activityNdx);
3181 if (r.voiceSession != null
3182 && r.voiceSession.asBinder() == sessionBinder) {
3183 // Inform of cancellation
3184 r.clearVoiceSessionLocked();
3185 try {
3186 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3187 null);
3188 } catch (RemoteException re) {
3189 // Ok
3190 }
3191 // TODO: VI This is redundant in some cases
3192 mService.finishRunningVoiceLocked();
3193 break;
3194 }
3195 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003196 }
3197 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003198 Slog.d(TAG, "ActivityStack.finishVoiceTask()");
3199
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003200 if (didOne) {
3201 mService.updateOomAdjLocked();
3202 }
3203 }
3204
Craig Mautnerd2328952013-03-05 12:46:26 -08003205 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003206 ArrayList<ActivityRecord> activities = r.task.mActivities;
3207 for (int index = activities.indexOf(r); index >= 0; --index) {
3208 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003209 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003210 break;
3211 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003212 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003213 }
3214 return true;
3215 }
3216
Dianne Hackborn5c607432012-02-28 14:44:19 -08003217 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3218 // send the result
3219 ActivityRecord resultTo = r.resultTo;
3220 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003221 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003222 + " who=" + r.resultWho + " req=" + r.requestCode
3223 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003224 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003225 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003226 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003227 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003228 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003229 if (r.info.applicationInfo.uid > 0) {
3230 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3231 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003232 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003233 }
3234 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3235 resultData);
3236 r.resultTo = null;
3237 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003238 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003239
3240 // Make sure this HistoryRecord is not holding on to other resources,
3241 // because clients have remote IPC references to this object so we
3242 // can't assume that will go away and want to avoid circular IPC refs.
3243 r.results = null;
3244 r.pendingResults = null;
3245 r.newIntents = null;
3246 r.icicle = null;
3247 }
3248
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003249 /**
3250 * @return Returns true if this activity has been removed from the history
3251 * list, or false if it is still in the list and will be removed later.
3252 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003253 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3254 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003255 if (r.finishing) {
3256 Slog.w(TAG, "Duplicate finish request for " + r);
3257 return false;
3258 }
3259
Wale Ogunwale7d701172015-03-11 15:36:30 -07003260 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003261 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003262 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003263 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003264 task.taskId, r.shortComponentName, reason);
3265 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003266 final int index = activities.indexOf(r);
3267 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003268 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003269 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003270 // If the caller asked that this activity (and all above it)
3271 // be cleared when the task is reset, don't lose that information,
3272 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003273 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003274 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003275 }
3276 }
3277
3278 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003279
Craig Mautner299f9602015-01-26 09:47:33 -08003280 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003281
Dianne Hackborn5c607432012-02-28 14:44:19 -08003282 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003283
Craig Mautnerde4ef022013-04-07 19:01:33 -07003284 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003285 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003286 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003287 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003288 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003289 ? AppTransition.TRANSIT_TASK_CLOSE
3290 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003291
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003292 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003293 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003294
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003295 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003296 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3297 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3298 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003299 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003300 }
3301
Craig Mautneraea74a52014-03-08 14:23:10 -08003302 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003303 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003304 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003305 } else if (r.state != ActivityState.PAUSING) {
3306 // If the activity is PAUSING, we will complete the finish once
3307 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003308 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003309 if (r.visible) {
3310 mWindowManager.setAppVisibility(r.appToken, false);
3311 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003312 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003313 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003314 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003315 }
3316
3317 return false;
3318 }
3319
Craig Mautnerf3333272013-04-22 10:55:53 -07003320 static final int FINISH_IMMEDIATELY = 0;
3321 static final int FINISH_AFTER_PAUSE = 1;
3322 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003323
Craig Mautnerf3333272013-04-22 10:55:53 -07003324 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003325 // First things first: if this activity is currently visible,
3326 // and the resumed activity is not yet visible, then hold off on
3327 // finishing until the resumed one becomes visible.
3328 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003329 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08003330 addToStopping(r, false /* immediate */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003331 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003332 if (DEBUG_STATES) Slog.v(TAG_STATES,
3333 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003334 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003335 if (oomAdj) {
3336 mService.updateOomAdjLocked();
3337 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003338 return r;
3339 }
3340
3341 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003342 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003343 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003344 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003345 if (mResumedActivity == r) {
3346 mResumedActivity = null;
3347 }
3348 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003349 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003350 r.state = ActivityState.FINISHING;
3351
3352 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003353 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003354 || prevState == ActivityState.STOPPED
3355 || prevState == ActivityState.INITIALIZING) {
3356 // If this activity is already stopped, we can just finish
3357 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003358 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003359 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003360 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003361 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003362 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003363 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003364 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3365 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003366 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003367 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003368
3369 // Need to go through the full pause cycle to get this
3370 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003371 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003372 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003373 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003374 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003375 return r;
3376 }
3377
Craig Mautneree36c772014-07-16 14:56:05 -07003378 void finishAllActivitiesLocked(boolean immediately) {
3379 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003380 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3381 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3382 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3383 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003384 noActivitiesInStack = false;
3385 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003386 continue;
3387 }
Craig Mautneree36c772014-07-16 14:56:05 -07003388 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003389 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3390 }
3391 }
Craig Mautneree36c772014-07-16 14:56:05 -07003392 if (noActivitiesInStack) {
3393 mActivityContainer.onTaskListEmptyLocked();
3394 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003395 }
3396
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003397 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3398 // Basic case: for simple app-centric recents, we need to recreate
3399 // the task if the affinity has changed.
3400 if (srec == null || srec.task.affinity == null ||
3401 !srec.task.affinity.equals(destAffinity)) {
3402 return true;
3403 }
3404 // Document-centric case: an app may be split in to multiple documents;
3405 // they need to re-create their task if this current activity is the root
3406 // of a document, unless simply finishing it will return them to the the
3407 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003408 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3409 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003410 // Okay, this activity is at the root of its task. What to do, what to do...
3411 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3412 // Finishing won't return to an application, so we need to recreate.
3413 return true;
3414 }
3415 // We now need to get the task below it to determine what to do.
3416 int taskIdx = mTaskHistory.indexOf(srec.task);
3417 if (taskIdx <= 0) {
3418 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3419 return false;
3420 }
3421 if (taskIdx == 0) {
3422 // At the bottom of the stack, nothing to go back to.
3423 return true;
3424 }
3425 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3426 if (!srec.task.affinity.equals(prevTask.affinity)) {
3427 // These are different apps, so need to recreate.
3428 return true;
3429 }
3430 }
3431 return false;
3432 }
3433
Wale Ogunwale7d701172015-03-11 15:36:30 -07003434 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003435 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003436 final TaskRecord task = srec.task;
3437 final ArrayList<ActivityRecord> activities = task.mActivities;
3438 final int start = activities.indexOf(srec);
3439 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003440 return false;
3441 }
3442 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003443 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003444 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003445 final ComponentName dest = destIntent.getComponent();
3446 if (start > 0 && dest != null) {
3447 for (int i = finishTo; i >= 0; i--) {
3448 ActivityRecord r = activities.get(i);
3449 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003450 r.info.name.equals(dest.getClassName())) {
3451 finishTo = i;
3452 parent = r;
3453 foundParentInTask = true;
3454 break;
3455 }
3456 }
3457 }
3458
3459 IActivityController controller = mService.mController;
3460 if (controller != null) {
3461 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3462 if (next != null) {
3463 // ask watcher if this is allowed
3464 boolean resumeOK = true;
3465 try {
3466 resumeOK = controller.activityResuming(next.packageName);
3467 } catch (RemoteException e) {
3468 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003469 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003470 }
3471
3472 if (!resumeOK) {
3473 return false;
3474 }
3475 }
3476 }
3477 final long origId = Binder.clearCallingIdentity();
3478 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003479 ActivityRecord r = activities.get(i);
3480 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003481 // Only return the supplied result for the first activity finished
3482 resultCode = Activity.RESULT_CANCELED;
3483 resultData = null;
3484 }
3485
3486 if (parent != null && foundParentInTask) {
3487 final int parentLaunchMode = parent.info.launchMode;
3488 final int destIntentFlags = destIntent.getFlags();
3489 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3490 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3491 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3492 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003493 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3494 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003495 } else {
3496 try {
3497 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3498 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003499 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3500 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3501 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003502 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3503 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003504 foundParentInTask = res == ActivityManager.START_SUCCESS;
3505 } catch (RemoteException e) {
3506 foundParentInTask = false;
3507 }
3508 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003509 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003510 }
3511 }
3512 Binder.restoreCallingIdentity(origId);
3513 return foundParentInTask;
3514 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003515 /**
3516 * Perform the common clean-up of an activity record. This is called both
3517 * as part of destroyActivityLocked() (when destroying the client-side
3518 * representation) and cleaning things up as a result of its hosting
3519 * processing going away, in which case there is no remaining client-side
3520 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003521 *
3522 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003523 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003524 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3525 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003526 if (mResumedActivity == r) {
3527 mResumedActivity = null;
3528 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003529 if (mPausingActivity == r) {
3530 mPausingActivity = null;
3531 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003532 mService.resetFocusedActivityIfNeededLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003533
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003534 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003535 r.frozenBeforeDestroy = false;
3536
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003537 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003538 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003539 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003540 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003541 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003542 }
3543
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003544 // Make sure this record is no longer in the pending finishes list.
3545 // This could happen, for example, if we are trimming activities
3546 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003547 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003548 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003549
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003550 // Remove any pending results.
3551 if (r.finishing && r.pendingResults != null) {
3552 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3553 PendingIntentRecord rec = apr.get();
3554 if (rec != null) {
3555 mService.cancelIntentSenderLocked(rec, false);
3556 }
3557 }
3558 r.pendingResults = null;
3559 }
3560
3561 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003562 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003563 }
3564
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003565 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003566 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003567 if (getVisibleBehindActivity() == r) {
3568 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003569 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003570 }
3571
3572 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003573 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003574 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003575 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003576 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003577 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003578 }
3579
Craig Mautner299f9602015-01-26 09:47:33 -08003580 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003581 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003582 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003583 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003584 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3585 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3586
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003587 r.takeFromHistory();
3588 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003589 if (DEBUG_STATES) Slog.v(TAG_STATES,
3590 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003591 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003592 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003593 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003594 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003595 if (VALIDATE_TOKENS) {
3596 validateAppTokensLocked();
3597 }
Craig Mautner312ba862014-02-10 17:55:01 -08003598 final TaskRecord task = r.task;
3599 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003600 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003601 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003602 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003603 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003604 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003605 }
Craig Mautner299f9602015-01-26 09:47:33 -08003606 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003607 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003608 cleanUpActivityServicesLocked(r);
3609 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003610 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003611
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003612 /**
3613 * Perform clean-up of service connections in an activity record.
3614 */
3615 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3616 // Throw away any services that have been bound by this activity.
3617 if (r.connections != null) {
3618 Iterator<ConnectionRecord> it = r.connections.iterator();
3619 while (it.hasNext()) {
3620 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003621 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003622 }
3623 r.connections = null;
3624 }
3625 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003626
Craig Mautneree2e45a2014-06-27 12:10:03 -07003627 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003628 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003629 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003630 mHandler.sendMessage(msg);
3631 }
3632
Craig Mautneree2e45a2014-06-27 12:10:03 -07003633 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003634 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003635 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003636 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3637 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3638 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3639 final ActivityRecord r = activities.get(activityNdx);
3640 if (r.finishing) {
3641 continue;
3642 }
3643 if (r.fullscreen) {
3644 lastIsOpaque = true;
3645 }
3646 if (owner != null && r.app != owner) {
3647 continue;
3648 }
3649 if (!lastIsOpaque) {
3650 continue;
3651 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003652 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003653 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003654 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003655 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003656 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003657 activityRemoved = true;
3658 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003659 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003660 }
3661 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003662 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003663 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003664 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003665 }
3666
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003667 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3668 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003669 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3670 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003671 + " pausing=" + mPausingActivity + " for reason " + reason);
3672 return destroyActivityLocked(r, true, reason);
3673 }
3674 return false;
3675 }
3676
3677 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3678 String reason) {
3679 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003680 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003681 int maxTasks = tasks.size() / 4;
3682 if (maxTasks < 1) {
3683 maxTasks = 1;
3684 }
3685 int numReleased = 0;
3686 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3687 final TaskRecord task = mTaskHistory.get(taskNdx);
3688 if (!tasks.contains(task)) {
3689 continue;
3690 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003691 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003692 int curNum = 0;
3693 final ArrayList<ActivityRecord> activities = task.mActivities;
3694 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3695 final ActivityRecord activity = activities.get(actNdx);
3696 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003697 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003698 + " in state " + activity.state + " resumed=" + mResumedActivity
3699 + " pausing=" + mPausingActivity + " for reason " + reason);
3700 destroyActivityLocked(activity, true, reason);
3701 if (activities.get(actNdx) != activity) {
3702 // Was removed from list, back up so we don't miss the next one.
3703 actNdx--;
3704 }
3705 curNum++;
3706 }
3707 }
3708 if (curNum > 0) {
3709 numReleased += curNum;
3710 maxTasks--;
3711 if (mTaskHistory.get(taskNdx) != task) {
3712 // The entire task got removed, back up so we don't miss the next one.
3713 taskNdx--;
3714 }
3715 }
3716 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003717 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3718 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003719 return numReleased;
3720 }
3721
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003722 /**
3723 * Destroy the current CLIENT SIDE instance of an activity. This may be
3724 * called both when actually finishing an activity, or when performing
3725 * a configuration switch where we destroy the current client-side object
3726 * but then create a new client-side object for this same HistoryRecord.
3727 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003728 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003729 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3730 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003731 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003732 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003733 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003734 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003735
3736 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003737
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003738 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003739
3740 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003741
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003742 if (hadApp) {
3743 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003744 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003745 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3746 mService.mHeavyWeightProcess = null;
3747 mService.mHandler.sendEmptyMessage(
3748 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3749 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003750 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003751 // Update any services we are bound to that might care about whether
3752 // their client may have activities.
3753 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003754 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003755 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003756 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003757 }
3758 }
3759
3760 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003761
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003762 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003763 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003764 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003765 r.configChangeFlags);
3766 } catch (Exception e) {
3767 // We can just ignore exceptions here... if the process
3768 // has crashed, our death notification will clean things
3769 // up.
3770 //Slog.w(TAG, "Exception thrown during finish", e);
3771 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003772 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003773 removedFromHistory = true;
3774 skipDestroy = true;
3775 }
3776 }
3777
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003778 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003779
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003780 // If the activity is finishing, we need to wait on removing it
3781 // from the list to give it a chance to do its cleanup. During
3782 // that time it may make calls back with its token so we need to
3783 // be able to find it on the list and so we don't want to remove
3784 // it from the list yet. Otherwise, we can just immediately put
3785 // it in the destroyed state since we are not removing it from the
3786 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003787 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003788 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003789 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003790 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003791 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003792 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3793 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003794 if (DEBUG_STATES) Slog.v(TAG_STATES,
3795 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003796 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003797 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003798 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003799 }
3800 } else {
3801 // remove this record from the history.
3802 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003803 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003804 removedFromHistory = true;
3805 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003806 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003807 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003808 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003809 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003810 }
3811 }
3812
3813 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003814
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003815 if (!mLRUActivities.remove(r) && hadApp) {
3816 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3817 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003818
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003819 return removedFromHistory;
3820 }
3821
Craig Mautner299f9602015-01-26 09:47:33 -08003822 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003823 final long origId = Binder.clearCallingIdentity();
3824 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003825 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003826 if (r != null) {
3827 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003828 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003829 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003830
Wale Ogunwale60454db2015-01-23 16:05:07 -08003831 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003832 if (r.state == ActivityState.DESTROYING) {
3833 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003834 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003835 }
3836 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003837 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003838 } finally {
3839 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003840 }
3841 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003842
Todd Kennedyaab56db2015-01-30 09:39:53 -08003843 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003844 if (hasVisibleBehindActivity() &&
3845 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08003846 if (r == topRunningActivityLocked()
3847 && getStackVisibilityLocked(null) == STACK_VISIBLE) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003848 // Don't release the top activity if it has requested to run behind the next
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08003849 // activity and the stack is currently visible.
Craig Mautner64ccb702014-10-01 09:38:40 -07003850 return;
3851 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003852 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003853 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003854 " thread=" + r.app.thread);
3855 if (r != null && r.app != null && r.app.thread != null) {
3856 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003857 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003858 } catch (RemoteException e) {
3859 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003860 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003861 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003862 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003863 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003864 }
3865 }
3866 }
3867
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003868 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003869 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3870 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003871 if (r != null) {
3872 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003873 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003874 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003875 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003876 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003877 }
3878
Jose Lima4b6c6692014-08-12 17:41:12 -07003879 boolean hasVisibleBehindActivity() {
3880 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003881 }
3882
Jose Lima4b6c6692014-08-12 17:41:12 -07003883 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003884 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003885 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003886 }
3887 }
3888
Jose Lima4b6c6692014-08-12 17:41:12 -07003889 ActivityRecord getVisibleBehindActivity() {
3890 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003891 }
3892
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003893 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3894 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003895 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003896 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3897 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003898 while (i > 0) {
3899 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003900 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003901 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003902 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003903 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003904 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003905 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003906 }
3907 }
3908 }
3909
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003910 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3911 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003912 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3913 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003914 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3915 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003916 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003917 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003918 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3919 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003920
3921 boolean hasVisibleActivities = false;
3922
3923 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003924 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003925 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3926 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003927 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3928 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3929 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3930 final ActivityRecord r = activities.get(activityNdx);
3931 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003932 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3933 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003934 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003935 if (r.visible) {
3936 hasVisibleActivities = true;
3937 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003938 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003939 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3940 // Don't currently have state for the activity, or
3941 // it is finishing -- always remove it.
3942 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003943 } else if (!r.visible && r.launchCount > 2 &&
3944 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003945 // We have launched this activity too many times since it was
3946 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003947 // (Note if the activity is visible, we don't remove the record.
3948 // We leave the dead window on the screen but the process will
3949 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003950 remove = true;
3951 } else {
3952 // The process may be gone, but the activity lives on!
3953 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003954 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003955 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003956 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3957 "Removing activity " + r + " from stack at " + i
3958 + ": haveState=" + r.haveState
3959 + " stateNotNeeded=" + r.stateNotNeeded
3960 + " finishing=" + r.finishing
3961 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003962 if (!r.finishing) {
3963 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3964 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3965 r.userId, System.identityHashCode(r),
3966 r.task.taskId, r.shortComponentName,
3967 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003968 if (r.state == ActivityState.RESUMED) {
3969 mService.updateUsageStats(r, false);
3970 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003971 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003972 } else {
3973 // We have the current state for this activity, so
3974 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003975 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3976 if (DEBUG_APP) Slog.v(TAG_APP,
3977 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003978 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003979 // Set nowVisible to previous visible state. If the app was visible while
3980 // it died, we leave the dead window on screen so it's basically visible.
3981 // This is needed when user later tap on the dead window, we need to stop
3982 // other apps when user transfers focus to the restarted activity.
3983 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003984 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003985 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003986 "App died, clearing saved state of " + r);
3987 r.icicle = null;
3988 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003989 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003990 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003991 if (remove) {
3992 removeActivityFromHistoryLocked(r, "appDied");
3993 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003994 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003995 }
3996 }
3997
3998 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003999 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004000
Chong Zhang280d3322015-11-03 17:27:26 -08004001 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004002 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004003 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004004 if (r != null && r.state != ActivityState.RESUMED) {
4005 r.updateOptionsLocked(options);
4006 } else {
4007 ActivityOptions.abort(options);
4008 }
4009 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004010 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004011 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004012
Craig Mautner21d24a22014-04-23 11:45:37 -07004013 void updateTaskMovement(TaskRecord task, boolean toFront) {
4014 if (task.isPersistable) {
4015 task.mLastTimeMoved = System.currentTimeMillis();
4016 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
4017 // recently will be most negative, tasks sent to the bottom before that will be less
4018 // negative. Similarly for recent tasks moved to the top which will be most positive.
4019 if (!toFront) {
4020 task.mLastTimeMoved *= -1;
4021 }
4022 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07004023 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07004024 }
4025
Craig Mautner84984fa2014-06-19 11:19:20 -07004026 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07004027 final int top = mTaskHistory.size() - 1;
4028 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
4029 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004030 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004031 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4032 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07004033 mTaskHistory.remove(taskNdx);
4034 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004035 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07004036 return;
4037 }
4038 }
4039 }
4040
Chong Zhang280d3322015-11-03 17:27:26 -08004041 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004042 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004043 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004044
Craig Mautner11bf9a52013-02-19 14:08:51 -08004045 final int numTasks = mTaskHistory.size();
4046 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004047 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004048 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004049 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004050 ActivityOptions.abort(options);
4051 } else {
4052 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
4053 }
4054 return;
4055 }
4056
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004057 if (timeTracker != null) {
4058 // The caller wants a time tracker associated with this task.
4059 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4060 tr.mActivities.get(i).appTimeTracker = timeTracker;
4061 }
4062 }
4063
Craig Mautner11bf9a52013-02-19 14:08:51 -08004064 // Shift all activities with this task up to the top
4065 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004066 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004067
Chong Zhang45c25ce2015-08-10 22:18:26 -07004068 // Don't refocus if invisible to current user
4069 ActivityRecord top = tr.getTopActivity();
4070 if (!okToShowLocked(top)) {
4071 addRecentActivityLocked(top);
4072 ActivityOptions.abort(options);
4073 return;
4074 }
4075
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004076 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004077 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004078 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004079
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004080 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004081 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004082 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004083 if (r != null) {
4084 mNoAnimActivities.add(r);
4085 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004086 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004087 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08004088 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004089 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004090
Wale Ogunwaled046a012015-12-24 13:05:59 -08004091 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004092 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004093
4094 if (VALIDATE_TOKENS) {
4095 validateAppTokensLocked();
4096 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004097 }
4098
4099 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004100 * Worker method for rearranging history stack. Implements the function of moving all
4101 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004102 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004103 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004104 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4105 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004106 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004107 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004108 * @return Returns true if the move completed, false if not.
4109 */
Craig Mautner299f9602015-01-26 09:47:33 -08004110 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004111 final TaskRecord tr = taskForIdLocked(taskId);
4112 if (tr == null) {
4113 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4114 return false;
4115 }
4116
4117 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07004118 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004119
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004120 // If we have a watcher, preflight the move before committing to it. First check
4121 // for *other* available tasks, but if none are available, then try again allowing the
4122 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004123 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004124 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004125 if (next == null) {
4126 next = topRunningActivityLocked(null, 0);
4127 }
4128 if (next != null) {
4129 // ask watcher if this is allowed
4130 boolean moveOK = true;
4131 try {
4132 moveOK = mService.mController.activityResuming(next.packageName);
4133 } catch (RemoteException e) {
4134 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004135 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004136 }
4137 if (!moveOK) {
4138 return false;
4139 }
4140 }
4141 }
4142
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004143 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004144
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004145 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4146 // For the case where we are moving the home task back and there is an activity visible
4147 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4148 // activity to maintain order with what the user is seeing.
4149 final ActivityStack fullscreenStack =
4150 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4151 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4152 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4153 mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004154 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004155 return true;
4156 }
4157 }
4158
riddle_hsuc215a4f2014-12-27 12:10:45 +08004159 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004160
4161 // If true, we should resume the home activity next if the task we are moving to the
4162 // back is over the home stack. We force to false if the task we are moving to back
4163 // is the home task and we don't want it resumed after moving to the back.
4164 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4165 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004166 final TaskRecord nextTask = getNextTask(tr);
4167 if (nextTask != null) {
4168 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4169 } else {
4170 prevIsHome = true;
4171 }
4172 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004173 mTaskHistory.remove(tr);
4174 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004175 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004176
Craig Mautnerc8143c62013-09-03 12:15:57 -07004177 // There is an assumption that moving a task to the back moves it behind the home activity.
4178 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004179 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004180 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4181 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004182 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004183 break;
4184 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004185 if (taskNdx == 1) {
4186 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004187 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004188 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004189 }
4190
Craig Mautner299f9602015-01-26 09:47:33 -08004191 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004192 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004193
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004194 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004195 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004196 }
4197
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004198 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004199 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004200 if (!mService.mBooting && !mService.mBooted) {
4201 // Not ready yet!
4202 return false;
4203 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004204 final int taskToReturnTo = tr.getTaskToReturnTo();
4205 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004206 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004207 }
4208
Wale Ogunwaled046a012015-12-24 13:05:59 -08004209 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004210 return true;
4211 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004212
Craig Mautner8849a5e2013-04-02 16:41:03 -07004213 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004214 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004215 final Uri data = r.intent.getData();
4216 final String strData = data != null ? data.toSafeString() : null;
4217
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004218 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004219 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004220 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004221 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004222 }
4223
4224 /**
4225 * Make sure the given activity matches the current configuration. Returns
4226 * false if the activity had to be destroyed. Returns true if the
4227 * configuration is the same, or the activity will remain running as-is
4228 * for whatever reason. Ensures the HistoryRecord is updated with the
4229 * correct configuration and all other bookkeeping is handled.
4230 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004231 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4232 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004233 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004234 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004235 "Skipping config check (will change): " + r);
4236 return true;
4237 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004238
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004239 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004240 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004241
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004242 // Short circuit: if the two configurations are the exact same
4243 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004244 final Configuration newConfig = mService.mConfiguration;
4245 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004246 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004247 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004248 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004249 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004250 "Configuration unchanged in " + r);
4251 return true;
4252 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004253
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004254 // We don't worry about activities that are finishing.
4255 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004256 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004257 "Configuration doesn't matter in finishing " + r);
4258 r.stopFreezingScreenLocked(false);
4259 return true;
4260 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004261
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004262 // Okay we now are going to make this activity have the new config.
4263 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004264 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004265 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004266 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004267 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004268
Filip Gruszczynski23493322015-07-29 17:02:59 -07004269 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004270 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004271 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004272 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004273 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004274 // There are no significant differences, so we won't relaunch but should still deliver
4275 // the new configuration to the client process.
4276 r.scheduleConfigurationChanged(taskConfig, false);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004277 return true;
4278 }
4279
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004280 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4281 "Configuration changes for " + r + " ; taskChanges="
4282 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4283 + Configuration.configurationDiffToString(changes));
4284
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004285 // If the activity isn't currently running, just leave the new
4286 // configuration and it will pick that up next time it starts.
4287 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004288 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004289 "Configuration doesn't matter not running " + r);
4290 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004291 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004292 return true;
4293 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004294
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004295 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004296 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4297 "Checking to restart " + r.info.name + ": changed=0x"
4298 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004299 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4300 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004301
Dianne Hackborne6676352011-06-01 16:51:20 -07004302 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004303 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4304 r.configChangeFlags |= changes;
4305 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004306 r.forceNewConfig = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004307 preserveWindow &= isResizeOnlyChange(changes);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004308 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004309 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004310 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004311 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004312 } else if (r.state == ActivityState.PAUSING) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004313 // A little annoying: we are waiting for this activity to finish pausing. Let's not
4314 // do anything now, but just flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004315 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004316 "Config is skipping already pausing " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004317 r.deferRelaunchUntilPaused = true;
4318 r.preserveWindowOnDeferredRelaunch = preserveWindow;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004319 return true;
4320 } else if (r.state == ActivityState.RESUMED) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004321 // Try to optimize this case: the configuration is changing and we need to restart
4322 // the top, resumed activity. Instead of doing the normal handshaking, just say
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004323 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004324 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004325 "Config is relaunching resumed " + r);
Chong Zhang2b79af12016-02-10 18:47:06 -08004326
4327 if (DEBUG_STATES && !r.visible) {
4328 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + r
4329 + " called by " + Debug.getCallers(4));
4330 }
4331
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004332 relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004333 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004334 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004335 "Config is relaunching non-resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004336 relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004337 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004338
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004339 // All done... tell the caller we weren't able to keep this activity around.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004340 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004341 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004342
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004343 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004344 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004345 // changes is always sent to all processes when they happen so it can just use whatever
4346 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004347 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004348 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004349
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004350 return true;
4351 }
4352
Filip Gruszczynski23493322015-07-29 17:02:59 -07004353 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4354 Configuration oldTaskOverride) {
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08004355
4356 // If we went from full-screen to non-full-screen, make sure to use the correct
4357 // configuration task diff, so the diff stays as small as possible.
4358 if (Configuration.EMPTY.equals(oldTaskOverride)
4359 && !Configuration.EMPTY.equals(taskConfig)) {
4360 oldTaskOverride = record.task.extractOverrideConfig(record.configuration);
4361 }
4362
Jorim Jaggi1b025a62016-02-03 19:27:49 -08004363 // Conversely, do the same when going the other direction.
4364 if (Configuration.EMPTY.equals(taskConfig)
4365 && !Configuration.EMPTY.equals(oldTaskOverride)) {
4366 taskConfig = record.task.extractOverrideConfig(record.configuration);
4367 }
4368
Filip Gruszczynski23493322015-07-29 17:02:59 -07004369 // Determine what has changed. May be nothing, if this is a config
4370 // that has come back from the app after going idle. In that case
4371 // we just want to leave the official config object now in the
4372 // activity and do nothing else.
4373 int taskChanges = oldTaskOverride.diff(taskConfig);
4374 // We don't want to use size changes if they don't cross boundaries that are important to
4375 // the app.
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004376 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004377 final boolean crosses = record.crossesHorizontalSizeThreshold(
4378 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4379 || record.crossesVerticalSizeThreshold(
4380 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4381 if (!crosses) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004382 taskChanges &= ~CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004383 }
4384 }
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004385 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004386 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4387 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004388 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004389 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004390 }
4391 }
4392 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4393 }
4394
4395 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4396 Configuration oldTaskOverride, int taskChanges) {
4397 if (taskChanges == 0) {
4398 // {@link Configuration#diff} doesn't catch changes from unset values.
4399 // Check for changes we care about.
4400 if (oldTaskOverride.orientation != taskConfig.orientation) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004401 taskChanges |= CONFIG_ORIENTATION;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004402 }
4403 // We want to explicitly track situations where the size configuration goes from
4404 // undefined to defined. We don't care about crossing the threshold in that case,
4405 // because there is no threshold.
4406 final int oldHeight = oldTaskOverride.screenHeightDp;
4407 final int newHeight = taskConfig.screenHeightDp;
4408 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4409 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4410 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004411 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004412 }
4413 final int oldWidth = oldTaskOverride.screenWidthDp;
4414 final int newWidth = taskConfig.screenWidthDp;
4415 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4416 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4417 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004418 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004419 }
4420 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4421 final int newSmallest = taskConfig.smallestScreenWidthDp;
4422 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4423 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4424 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004425 taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4426 }
4427 final int oldLayout = oldTaskOverride.screenLayout;
4428 final int newLayout = taskConfig.screenLayout;
4429 if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4430 || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4431 taskChanges |= CONFIG_SCREEN_LAYOUT;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004432 }
4433 }
4434 return taskChanges;
4435 }
4436
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004437 private static boolean isResizeOnlyChange(int change) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004438 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4439 | CONFIG_SCREEN_LAYOUT)) == 0;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004440 }
4441
Wale Ogunwale83301a92015-09-24 15:54:08 -07004442 private void relaunchActivityLocked(
4443 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4444 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004445 r.configChangeFlags = 0;
Wale Ogunwale83301a92015-09-24 15:54:08 -07004446 return;
4447 }
4448
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004449 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004450 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004451 if (andResume) {
4452 results = r.results;
4453 newIntents = r.newIntents;
4454 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004455 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4456 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004457 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004458 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004459 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004460 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004461
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004462 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004463
Craig Mautner34b73df2014-01-12 21:11:08 -08004464 mStackSupervisor.removeChildActivityContainers(r);
4465
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004466 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004467 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08004468 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r
4469 + " callers=" + Debug.getCallers(6));
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004470 r.forceNewConfig = false;
Chong Zhangd78ddb42016-03-02 17:01:14 -08004471 mStackSupervisor.activityRelaunchingLocked(r);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004472 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4473 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004474 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004475 // Note: don't need to call pauseIfSleepingLocked() here, because
4476 // the caller will only pass in 'andResume' if this activity is
4477 // currently resumed, which implies we aren't sleeping.
4478 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004479 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004480 }
4481
4482 if (andResume) {
Chong Zhang2b79af12016-02-10 18:47:06 -08004483 if (DEBUG_STATES) {
4484 Slog.d(TAG_STATES, "Resumed after relaunch " + r);
4485 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004486 r.state = ActivityState.RESUMED;
Chong Zhang2b79af12016-02-10 18:47:06 -08004487 // Relaunch-resume could happen either when the app is already in the front,
4488 // or while it's being brought to front. In the latter case, it's marked RESUMED
4489 // but not yet visible (or stopped). We need to complete the resume here as the
4490 // code in resumeTopActivityInnerLocked to complete the resume might be skipped.
4491 if (!r.visible || r.stopped) {
4492 mWindowManager.setAppVisibility(r.appToken, true);
4493 completeResumeLocked(r);
4494 } else {
4495 r.results = null;
4496 r.newIntents = null;
4497 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004498 } else {
4499 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4500 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004501 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004502
4503 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004504 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004505
4506 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004507 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4508 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4509 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4510 final ActivityRecord r = activities.get(activityNdx);
4511 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004512 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004513 }
4514 if (r.fullscreen && !r.finishing) {
4515 return false;
4516 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004517 }
4518 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004519 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004520 if (r == null) {
4521 return false;
4522 }
4523 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4524 + " would have returned true for r=" + r);
4525 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004526 }
4527
4528 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004529 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4530 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4531 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4532 final ActivityRecord r = activities.get(activityNdx);
4533 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004534 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004535 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004536 }
4537 }
4538 }
4539
Wale Ogunwale540e1232015-05-01 15:35:39 -07004540 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4541 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004542 boolean didSomething = false;
4543 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004544 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004545 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4546 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4547 int numActivities = activities.size();
4548 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4549 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004550 final boolean sameComponent =
4551 (r.packageName.equals(packageName) && (filterByClasses == null
4552 || filterByClasses.contains(r.realActivity.getClassName())))
4553 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004554 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004555 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004556 && (r.app == null || evenPersistent || !r.app.persistent)) {
4557 if (!doit) {
4558 if (r.finishing) {
4559 // If this activity is just finishing, then it is not
4560 // interesting as far as something to stop.
4561 continue;
4562 }
4563 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004564 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004565 if (r.isHomeActivity()) {
4566 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4567 Slog.i(TAG, "Skip force-stop again " + r);
4568 continue;
4569 } else {
4570 homeActivity = r.realActivity;
4571 }
4572 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004573 didSomething = true;
4574 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004575 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004576 if (r.app != null) {
4577 r.app.removed = true;
4578 }
4579 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004580 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004581 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004582 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4583 true)) {
4584 // r has been deleted from mActivities, accommodate.
4585 --numActivities;
4586 --activityNdx;
4587 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004588 }
4589 }
4590 }
4591 return didSomething;
4592 }
4593
Dianne Hackborn09233282014-04-30 11:33:59 -07004594 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004595 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4596 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004597 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004598 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004599 if (task.getTopActivity() == null) {
4600 continue;
4601 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004602 ActivityRecord r = null;
4603 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004604 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004605 int numActivities = 0;
4606 int numRunning = 0;
4607 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004608 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004609 continue;
4610 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004611 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004612 tmp = activities.get(activityNdx);
4613 if (tmp.finishing) {
4614 continue;
4615 }
4616 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004617
Craig Mautneraab647e2013-02-28 16:31:36 -08004618 // Initialize state for next task if needed.
4619 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4620 top = r;
4621 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004622 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004623
4624 // Add 'r' into the current task.
4625 numActivities++;
4626 if (r.app != null && r.app.thread != null) {
4627 numRunning++;
4628 }
4629
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004630 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004631 TAG, r.intent.getComponent().flattenToShortString()
4632 + ": task=" + r.task);
4633 }
4634
4635 RunningTaskInfo ci = new RunningTaskInfo();
4636 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004637 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004638 ci.baseActivity = r.intent.getComponent();
4639 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004640 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004641 if (focusedStack && topTask) {
4642 // Give the latest time to ensure foreground task can be sorted
4643 // at the first, because lastActiveTime of creating task is 0.
4644 ci.lastActiveTime = System.currentTimeMillis();
4645 topTask = false;
4646 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004647
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004648 if (top.task != null) {
4649 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004650 }
4651 ci.numActivities = numActivities;
4652 ci.numRunning = numRunning;
Wale Ogunwale21b60582016-01-27 12:34:16 -08004653 ci.isDockable = task.canGoInDockedStack();
Craig Mautneraab647e2013-02-28 16:31:36 -08004654 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004655 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004656 }
4657
4658 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004659 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004660 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004661 if (top >= 0) {
4662 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4663 int activityTop = activities.size() - 1;
4664 if (activityTop > 0) {
4665 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4666 "unhandled-back", true);
4667 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004668 }
4669 }
4670
Craig Mautner6b74cb52013-09-27 17:02:21 -07004671 /**
4672 * Reset local parameters because an app's activity died.
4673 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004674 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004675 */
4676 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004677 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004678 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004679 "App died while pausing: " + mPausingActivity);
4680 mPausingActivity = null;
4681 }
4682 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4683 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004684 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004685 }
4686
Craig Mautner19091252013-10-05 00:03:53 -07004687 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004688 }
4689
Craig Mautnercae015f2013-02-08 14:31:27 -08004690 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004691 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4692 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4693 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4694 final ActivityRecord r = activities.get(activityNdx);
4695 if (r.app == app) {
4696 Slog.w(TAG, " Force finishing activity "
4697 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004698 // Force the destroy to skip right to removal.
4699 r.app = null;
4700 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004701 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004702 }
4703 }
4704 }
4705
Dianne Hackborn390517b2013-05-30 15:03:32 -07004706 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004707 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004708 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004709 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4710 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004711 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4712 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004713 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004714 " Task id #" + task.taskId + "\n" +
4715 " mFullscreen=" + task.mFullscreen + "\n" +
4716 " mBounds=" + task.mBounds + "\n" +
4717 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004718 if (printed) {
4719 header = null;
4720 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004721 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004722 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004723 }
4724
4725 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4726 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4727
4728 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004729 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4730 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004731 }
4732 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004733 final int top = mTaskHistory.size() - 1;
4734 if (top >= 0) {
4735 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4736 int listTop = list.size() - 1;
4737 if (listTop >= 0) {
4738 activities.add(list.get(listTop));
4739 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004740 }
4741 } else {
4742 ItemMatcher matcher = new ItemMatcher();
4743 matcher.build(name);
4744
Craig Mautneraab647e2013-02-28 16:31:36 -08004745 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4746 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4747 if (matcher.match(r1, r1.intent.getComponent())) {
4748 activities.add(r1);
4749 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004750 }
4751 }
4752 }
4753
4754 return activities;
4755 }
4756
4757 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004758 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004759
4760 // All activities that came from the package must be
4761 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004762 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4763 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4764 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4765 final ActivityRecord a = activities.get(activityNdx);
4766 if (a.info.packageName.equals(packageName)) {
4767 a.forceNewConfig = true;
4768 if (starting != null && a == starting && a.visible) {
4769 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004770 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08004771 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004772 }
4773 }
4774 }
4775
4776 return starting;
4777 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004778
Craig Mautner299f9602015-01-26 09:47:33 -08004779 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004780 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004781 }
4782
Wale Ogunwale000957c2015-04-03 08:19:12 -07004783 /**
4784 * Removes the input task from this stack.
4785 * @param task to remove.
4786 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004787 * @param moving task to another stack. In the case we are moving we don't want to perform
4788 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004789 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004790 void removeTask(TaskRecord task, String reason, boolean moving) {
4791 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004792 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004793 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004794 if (!StackId.persistTaskBounds(mStackId)) {
4795 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4796 // default configuration the next time it launches.
4797 task.updateOverrideConfiguration(null);
4798 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004799 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004800
Craig Mautner04a0ea62014-01-13 12:51:26 -08004801 final ActivityRecord r = mResumedActivity;
4802 if (r != null && r.task == task) {
4803 mResumedActivity = null;
4804 }
4805
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004806 final int taskNdx = mTaskHistory.indexOf(task);
4807 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004808 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4809 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4810 if (!nextTask.isOverHomeStack()) {
4811 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4812 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004813 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004814 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004815 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004816
Wale Ogunwale040b4702015-08-06 18:10:50 -07004817 if (!moving && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004818 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07004819 final boolean isVoiceSession = task.voiceSession != null;
4820 if (isVoiceSession) {
4821 try {
4822 task.voiceSession.taskFinished(task.intent, task.taskId);
4823 } catch (RemoteException e) {
4824 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004825 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004826 if (task.autoRemoveFromRecents() || isVoiceSession) {
4827 // Task creator asked to remove this when done, or this task was a voice
4828 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004829 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004830 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004831 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004832 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004833
4834 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004835 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004836 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004837 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004838 String myReason = reason + " leftTaskHistoryEmpty";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08004839 if (mFullscreen || !adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004840 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004841 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004842 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004843 if (mStacks != null) {
4844 mStacks.remove(this);
4845 mStacks.add(0, this);
4846 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004847 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004848 mActivityContainer.onTaskListEmptyLocked();
4849 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004850 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004851
4852 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004853 }
4854
Dianne Hackborn91097de2014-04-04 18:02:06 -07004855 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4856 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4857 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004858 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4859 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004860 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08004861 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08004862 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004863 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.isResizeable()
Chong Zhang75b37202015-12-04 14:16:36 -08004864 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004865 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004866 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004867 return task;
4868 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004869
Wale Ogunwale935e5022015-11-10 12:36:10 -08004870 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4871 if (mTaskPositioner == null) {
4872 return false;
4873 }
4874 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4875 return true;
4876 }
4877
Craig Mautnerc00204b2013-03-05 15:02:14 -08004878 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004879 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004880 }
4881
Wale Ogunwale5f986092015-12-04 15:35:38 -08004882 void addTask(final TaskRecord task, final boolean toTop, String reason) {
4883 final ActivityStack prevStack = preAddTask(task, reason);
4884
Craig Mautnerc00204b2013-03-05 15:02:14 -08004885 task.stack = this;
4886 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004887 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004888 } else {
4889 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004890 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004891 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08004892 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004893 }
4894
Wale Ogunwale5f986092015-12-04 15:35:38 -08004895 void positionTask(final TaskRecord task, int position) {
4896 final ActivityStack prevStack = preAddTask(task, "positionTask");
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004897 task.stack = this;
4898 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004899 postAddTask(task, prevStack);
4900 }
4901
4902 private ActivityStack preAddTask(TaskRecord task, String reason) {
4903 final ActivityStack prevStack = task.stack;
4904 if (prevStack != null && prevStack != this) {
4905 prevStack.removeTask(task, reason, MOVING);
4906 }
4907 return prevStack;
4908 }
4909
4910 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
4911 if (prevStack != null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004912 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004913 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004914 try {
4915 task.voiceSession.taskStarted(task.intent, task.taskId);
4916 } catch (RemoteException e) {
4917 }
4918 }
4919 }
4920
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004921 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004922 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004923 // TODO: VI deal with activity
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004924 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4925 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6cae7652015-12-26 07:36:26 -08004926 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
4927 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004928 task.mResizeMode, r.isAlwaysFocusable(), task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004929 r.taskConfigOverride = task.mOverrideConfig;
4930 }
4931
Wale Ogunwaled046a012015-12-24 13:05:59 -08004932 void moveToFrontAndResumeStateIfNeeded(
4933 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
4934 if (!moveToFront) {
4935 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07004936 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004937
4938 // If the activity owns the last resumed activity, transfer that together,
4939 // so that we don't resume the same activity again in the new stack.
4940 // Apps may depend on onResume()/onPause() being called in pairs.
4941 if (setResume) {
4942 mResumedActivity = r;
4943 }
4944 // Move the stack in which we are placing the activity to the front. The call will also
4945 // make sure the activity focus is set.
4946 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004947 }
4948
4949 /**
4950 * Moves the input activity from its current stack to this one.
4951 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4952 * created on this stack which the activity is added to.
4953 * */
4954 void moveActivityToStack(ActivityRecord r) {
4955 final ActivityStack prevStack = r.task.stack;
4956 if (prevStack.mStackId == mStackId) {
4957 // You are already in the right stack silly...
4958 return;
4959 }
4960
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004961 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004962 && (mStackSupervisor.topRunningActivityLocked() == r);
4963 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4964
4965 final TaskRecord task = createTaskRecord(
Suprabh Shukla09a88f52015-12-02 14:36:31 -08004966 mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
4967 r.info, r.intent, null, null, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004968 r.setTask(task, null);
4969 task.addActivityToTop(r);
4970 setAppTask(r, task);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004971 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004972 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4973 if (wasResumed) {
4974 prevStack.mResumedActivity = null;
4975 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07004976 }
4977
Wale Ogunwale706ed792015-08-02 10:29:44 -07004978 private void setAppTask(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004979 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004980 mWindowManager.setAppTask(r.appToken, task.taskId, mStackId, bounds, task.mOverrideConfig,
4981 task.mResizeMode, task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004982 r.taskConfigOverride = task.mOverrideConfig;
4983 }
4984
Craig Mautnerc00204b2013-03-05 15:02:14 -08004985 public int getStackId() {
4986 return mStackId;
4987 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004988
4989 @Override
4990 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004991 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4992 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004993 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004994
Craig Mautner15df08a2015-04-01 12:17:18 -07004995 void onLockTaskPackagesUpdatedLocked() {
4996 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4997 mTaskHistory.get(taskNdx).setLockTaskAuth();
4998 }
4999 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005000}