blob: 659486f3710e50cfb32d95d678a6d1ef4ca84fef [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;
Winson21700932016-03-24 17:26:23 -0700932 scale = mService.mFullscreenThumbnailScale;
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);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001812
1813 // The activity may be waiting for stop, but that is no longer
1814 // appropriate for it.
1815 mStackSupervisor.mStoppingActivities.remove(r);
1816 mStackSupervisor.mGoingToSleepActivities.remove(r);
1817 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001818 } catch (Exception e) {
1819 // Just skip on any failure; we'll make it
1820 // visible when it next restarts.
1821 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001822 }
1823 }
1824
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001825 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001826 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1827 r.stopFreezingScreenLocked(false);
1828 try {
1829 if (r.returningOptions != null) {
1830 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1831 }
1832 } catch(RemoteException e) {
1833 }
1834 return r.state == ActivityState.RESUMED;
1835 }
1836
Todd Kennedyaab56db2015-01-30 09:39:53 -08001837 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001838 mTranslucentActivityWaiting = r;
1839 mUndrawnActivitiesBelowTopTranslucent.clear();
1840 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1841 }
1842
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001843 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1844 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1845 final TaskRecord task = mTaskHistory.get(taskNdx);
1846 final ArrayList<ActivityRecord> activities = task.mActivities;
1847 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1848 final ActivityRecord r = activities.get(activityNdx);
1849 if ( r.appTimeTracker != except) {
1850 r.appTimeTracker = null;
1851 }
1852 }
1853 }
1854 }
1855
Craig Mautner5eda9b32013-07-02 11:58:16 -07001856 /**
1857 * Called as activities below the top translucent activity are redrawn. When the last one is
1858 * redrawn notify the top activity by calling
1859 * {@link Activity#onTranslucentConversionComplete}.
1860 *
1861 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1862 * occurred and the activity will be notified immediately.
1863 */
1864 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001865 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001866 if ((r == null)
1867 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1868 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1869 // The last undrawn activity below the top has just been drawn. If there is an
1870 // opaque activity at the top, notify it that it can become translucent safely now.
1871 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1872 mTranslucentActivityWaiting = null;
1873 mUndrawnActivitiesBelowTopTranslucent.clear();
1874 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1875
Craig Mautner71dd1b62014-02-18 15:48:52 -08001876 if (waitingActivity != null) {
1877 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1878 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1879 try {
1880 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1881 waitingActivity.appToken, r != null);
1882 } catch (RemoteException e) {
1883 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001884 }
1885 }
1886 }
1887 }
1888
Craig Mautnera61bc652013-10-28 15:43:18 -07001889 /** If any activities below the top running one are in the INITIALIZING state and they have a
1890 * starting window displayed then remove that starting window. It is possible that the activity
1891 * in this state will never resumed in which case that starting window will be orphaned. */
1892 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001893 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001894 boolean aboveTop = true;
1895 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1896 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1897 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1898 final ActivityRecord r = activities.get(activityNdx);
1899 if (aboveTop) {
1900 if (r == topActivity) {
1901 aboveTop = false;
1902 }
1903 continue;
1904 }
1905
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08001906 if (r.state == ActivityState.INITIALIZING
1907 && r.mStartingWindowState == STARTING_WINDOW_SHOWN) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001908 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1909 "Found orphaned starting window " + r);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08001910 r.mStartingWindowState = STARTING_WINDOW_REMOVED;
Craig Mautnera61bc652013-10-28 15:43:18 -07001911 mWindowManager.removeAppStartingWindow(r.appToken);
1912 }
1913 }
1914 }
1915 }
1916
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001917 /**
1918 * Ensure that the top activity in the stack is resumed.
1919 *
1920 * @param prev The previously resumed activity, for when in the process
1921 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001922 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001923 *
1924 * @return Returns true if something is being resumed, or false if
1925 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001926 *
1927 * NOTE: It is not safe to call this method directly as it can cause an activity in a
1928 * non-focused stack to be resumed.
1929 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
1930 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001931 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08001932 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001933 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001934 // Don't even start recursing.
1935 return false;
1936 }
1937
1938 boolean result = false;
1939 try {
1940 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001941 mStackSupervisor.inResumeTopActivity = true;
1942 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1943 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001944 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001945 }
Craig Mautner544efa72014-09-04 16:41:20 -07001946 result = resumeTopActivityInnerLocked(prev, options);
1947 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001948 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001949 }
1950 return result;
1951 }
1952
Chong Zhang280d3322015-11-03 17:27:26 -08001953 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001954 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001955
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001956 if (!mService.mBooting && !mService.mBooted) {
1957 // Not ready yet!
1958 return false;
1959 }
1960
Craig Mautnerdf88d732014-01-27 09:21:32 -08001961 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001962 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001963 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001964 // Do not resume this stack if its parent is not resumed.
1965 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1966 return false;
1967 }
1968
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001969 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07001970
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001971 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001972 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001973
1974 // Remember how we'll process this pause/resume situation, and ensure
1975 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001976 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1977 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001978
Craig Mautner84984fa2014-06-19 11:19:20 -07001979 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001980 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001981 // There are no more activities!
1982 final String reason = "noMoreActivities";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001983 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(reason)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001984 // Try to move focus to the next visible stack with a running activity if this
1985 // stack is not covering the entire screen.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001986 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
1987 mStackSupervisor.getFocusedStack(), prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001988 }
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001989
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001990 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001991 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001992 if (DEBUG_STATES) Slog.d(TAG_STATES,
1993 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001994 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001995 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001996 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1997 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1998 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001999 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002000 }
2001
2002 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08002003
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002004 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002005 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
2006 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002007 // Make sure we have executed any pending transitions, since there
2008 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002009 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002010 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002011 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002012 if (DEBUG_STATES) Slog.d(TAG_STATES,
2013 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07002014 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002015 return false;
2016 }
2017
Craig Mautner525f3d92013-05-07 14:01:50 -07002018 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07002019 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07002020 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002021 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07002022 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002023 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07002024 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07002025 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07002026 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07002027 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07002028 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08002029 } else if (!isOnHomeDisplay()) {
2030 return false;
2031 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002032 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08002033 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07002034 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
2035 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
2036 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08002037 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07002038 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002039 }
2040
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002041 // If we are sleeping, and there is no resumed activity, and the top
2042 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07002043 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09002044 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07002045 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002046 // Make sure we have executed any pending transitions, since there
2047 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002048 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002049 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07002050 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002051 if (DEBUG_STATES) Slog.d(TAG_STATES,
2052 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07002053 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002054 return false;
2055 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002056
2057 // Make sure that the user who owns this activity is started. If not,
2058 // we will just leave it as is because someone should be bringing
2059 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002060 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002061 Slog.w(TAG, "Skipping resume of top activity " + next
2062 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002063 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002064 return false;
2065 }
2066
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002067 // The activity may be waiting for stop, but that is no longer
2068 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002069 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002070 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002071 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002072 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002073
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002074 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002075
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002076 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002077 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002078 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002079 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002080 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002081 return false;
2082 }
2083
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002084 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2085
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002086 // We need to start pausing the current activity so the top one can be resumed...
2087 final boolean dontWaitForPause = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002088 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002089 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002090 if (DEBUG_STATES) Slog.d(TAG_STATES,
2091 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08002092 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002093 }
2094 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002095 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002096 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002097 // At this point we want to put the upcoming activity's process
2098 // at the top of the LRU list, since we know we will be needing it
2099 // very soon and it would be a waste to let it get killed if it
2100 // happens to be sitting towards the end.
2101 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002102 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002103 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002104 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002105 return true;
2106 }
2107
Christopher Tated3f175c2012-06-14 14:16:54 -07002108 // If the most recent activity was noHistory but was only stopped rather
2109 // than stopped+finished because the device went to sleep, we need to make
2110 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07002111 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002112 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002113 if (DEBUG_STATES) Slog.d(TAG_STATES,
2114 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002115 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002116 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002117 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002118 }
2119
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002120 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002121 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2122 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002123 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002124 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2125 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002126 } else {
2127 // The next activity is already visible, so hide the previous
2128 // activity's windows right now so we can show the new one ASAP.
2129 // We only do this if the previous is finishing, which should mean
2130 // it is on top of the one being resumed so hiding it quickly
2131 // is good. Otherwise, we want to do the normal route of allowing
2132 // the resumed activity to be shown so we can decide if the
2133 // previous should actually be hidden depending on whether the
2134 // new one is found to be full-screen or not.
2135 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002136 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002137 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2138 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002139 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002140 + ", nowVisible=" + next.nowVisible);
2141 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002142 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002143 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002144 + ", waitingVisible="
2145 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2146 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002147 }
2148 }
2149 }
2150
Dianne Hackborne7f97212011-02-24 14:40:20 -08002151 // Launching this app's activity, make sure the app is no longer
2152 // considered stopped.
2153 try {
2154 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002155 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002156 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002157 } catch (IllegalArgumentException e) {
2158 Slog.w(TAG, "Failed trying to unstop package "
2159 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002160 }
2161
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002162 // We are starting up the next activity, so tell the window manager
2163 // that the previous one will be hidden soon. This way it can know
2164 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002165 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002166 if (prev != null) {
2167 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002168 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002169 "Prepare close transition: prev=" + prev);
2170 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002171 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002172 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002173 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002174 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002175 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2176 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002177 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002178 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002179 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002180 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2181 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002182 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002183 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002184 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002185 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002186 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002187 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002188 : next.mLaunchTaskBehind
2189 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2190 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002191 }
2192 }
Craig Mautner967212c2013-04-13 21:10:58 -07002193 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002194 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002195 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002196 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002197 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002198 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002199 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002200 }
2201 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002202
2203 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002204 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002205 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2206 if (opts != null) {
2207 resumeAnimOptions = opts.toBundle();
2208 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002209 next.applyOptionsLocked();
2210 } else {
2211 next.clearOptionsLocked();
2212 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002213
Craig Mautnercf910b02013-04-23 11:23:27 -07002214 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002215 if (next.app != null && next.app.thread != null) {
Chong Zhangdea4bd92016-03-15 12:50:03 -07002216 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next
2217 + " stopped=" + next.stopped + " visible=" + next.visible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002218
2219 // This activity is now becoming visible.
Chong Zhangdea4bd92016-03-15 12:50:03 -07002220 if (!next.visible || next.stopped) {
Jorim Jaggi1b025a62016-02-03 19:27:49 -08002221 mWindowManager.setAppVisibility(next.appToken, true);
2222 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002223
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002224 // schedule launch ticks to collect information about slow apps.
2225 next.startLaunchTickingLocked();
2226
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002227 ActivityRecord lastResumedActivity =
2228 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002229 ActivityState lastState = next.state;
2230
2231 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002232
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002233 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002234 next.state = ActivityState.RESUMED;
2235 mResumedActivity = next;
2236 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002237 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002238 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002239 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002240 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002241
2242 // Have the window manager re-evaluate the orientation of
2243 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002244 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002245 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002246 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002247 mService.mConfiguration,
2248 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2249 if (config != null) {
2250 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002251 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002252 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002253 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002254
Craig Mautner525f3d92013-05-07 14:01:50 -07002255 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002256 // The configuration update wasn't able to keep the existing
2257 // instance of the activity, and instead started a new one.
2258 // We should be all done, but let's just make sure our activity
2259 // is still at the top and schedule another run if something
2260 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002261 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002262 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002263 "Activity config changed during resume: " + next
2264 + ", new next: " + nextNext);
2265 if (nextNext != next) {
2266 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002267 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002268 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002269 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002270 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002271 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002272 return true;
2273 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002274 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002275 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002276 }
Craig Mautner58547802013-03-05 08:23:53 -08002277
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002278 try {
2279 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002280 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002281 if (a != null) {
2282 final int N = a.size();
2283 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002284 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2285 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002286 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002287 }
2288 }
2289
2290 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002291 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002292 }
2293
Wale Ogunwale8d5a5422016-03-03 18:28:21 -08002294 // Well the app will no longer be stopped.
2295 // Clear app token stopped state in window manager if needed.
2296 mWindowManager.notifyAppStopped(next.appToken, false);
2297
Craig Mautner299f9602015-01-26 09:47:33 -08002298 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2299 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002300
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002301 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002302 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002303 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002304 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002305 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002306 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002307 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002308
Craig Mautner0eea92c2013-05-16 13:35:39 -07002309 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002310
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002311 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002312 } catch (Exception e) {
2313 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002314 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002315 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002316 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002317 if (lastStack != null) {
2318 lastStack.mResumedActivity = lastResumedActivity;
2319 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002320 Slog.i(TAG, "Restarting because process died: " + next);
2321 if (!next.hasBeenLaunched) {
2322 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002323 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2324 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002325 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002326 next.appToken, next.packageName, next.theme,
2327 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002328 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2329 next.windowFlags, null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002330 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002331 }
George Mount2c92c972014-03-20 09:38:23 -07002332 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002333 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002334 return true;
2335 }
2336
2337 // From this point on, if something goes wrong there is no way
2338 // to recover the activity.
2339 try {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002340 completeResumeLocked(next);
2341 } catch (Exception e) {
2342 // If any exception gets thrown, toss away this
2343 // activity and try the next one.
2344 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002345 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002346 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002347 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002348 return true;
2349 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002350 } else {
2351 // Whoops, need to restart this activity!
2352 if (!next.hasBeenLaunched) {
2353 next.hasBeenLaunched = true;
2354 } else {
2355 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002356 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002357 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002358 mService.compatibilityInfoForPackageLocked(
2359 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002360 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002361 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002362 null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002363 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002364 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002365 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002366 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002367 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002368 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002369 }
2370
Craig Mautnercf910b02013-04-23 11:23:27 -07002371 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002372 return true;
2373 }
2374
riddle_hsuc215a4f2014-12-27 12:10:45 +08002375 private TaskRecord getNextTask(TaskRecord targetTask) {
2376 final int index = mTaskHistory.indexOf(targetTask);
2377 if (index >= 0) {
2378 final int numTasks = mTaskHistory.size();
2379 for (int i = index + 1; i < numTasks; ++i) {
2380 TaskRecord task = mTaskHistory.get(i);
2381 if (task.userId == targetTask.userId) {
2382 return task;
2383 }
2384 }
2385 }
2386 return null;
2387 }
2388
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002389 private void insertTaskAtPosition(TaskRecord task, int position) {
2390 if (position >= mTaskHistory.size()) {
2391 insertTaskAtTop(task, null);
2392 return;
2393 }
2394 // Calculate maximum possible position for this task.
2395 int maxPosition = mTaskHistory.size();
2396 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002397 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002398 // Put non-current user tasks below current user tasks.
2399 while (maxPosition > 0) {
2400 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2401 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002402 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002403 break;
2404 }
2405 maxPosition--;
2406 }
2407 }
2408 position = Math.min(position, maxPosition);
2409 mTaskHistory.remove(task);
2410 mTaskHistory.add(position, task);
2411 updateTaskMovement(task, true);
2412 }
2413
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002414 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002415 // If the moving task is over home stack, transfer its return type to next task
2416 if (task.isOverHomeStack()) {
2417 final TaskRecord nextTask = getNextTask(task);
2418 if (nextTask != null) {
2419 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2420 }
2421 }
2422
Craig Mautner9c85c202013-10-04 20:11:26 -07002423 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002424 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002425 if (isOnHomeDisplay()) {
2426 ActivityStack lastStack = mStackSupervisor.getLastStack();
2427 final boolean fromHome = lastStack.isHomeStack();
2428 if (!isHomeStack() && (fromHome || topTask() != task)) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002429 int returnToType = APPLICATION_ACTIVITY_TYPE;
2430 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2431 returnToType = lastStack.topTask() == null
2432 ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2433 }
2434 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002435 }
2436 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002437 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002438 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002439
Craig Mautnerac6f8432013-07-17 13:24:59 -07002440 mTaskHistory.remove(task);
2441 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002442 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002443 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002444 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002445 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002446 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002447 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002448 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002449 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002450 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002451 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002452 break;
2453 }
2454 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002455 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002456 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002457 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002458 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002459 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002460
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002461 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2462 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002463 TaskRecord rTask = r.task;
2464 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002465 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2466 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002467 // Last activity in task had been removed or ActivityManagerService is reusing task.
2468 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002469 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002470 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002471 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002472 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002473 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002474 if (!newTask) {
2475 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002476 boolean startIt = true;
2477 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2478 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002479 if (task.getTopActivity() == null) {
2480 // All activities in task are finishing.
2481 continue;
2482 }
Craig Mautner70a86932013-02-28 22:37:44 -08002483 if (task == r.task) {
2484 // Here it is! Now, if this is not yet visible to the
2485 // user, then just add it without starting; it will
2486 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002487 if (!startIt) {
2488 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2489 + task, new RuntimeException("here").fillInStackTrace());
2490 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002491 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002492 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002493 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002494 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002495 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002496 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002497 return;
2498 }
2499 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002500 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002501 startIt = false;
2502 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002503 }
2504 }
2505
2506 // Place a new activity at top of stack, so it is next to interact
2507 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002508
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002509 // If we are not placing the new activity frontmost, we do not want
2510 // to deliver the onUserLeaving callback to the actual frontmost
2511 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002512 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002513 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002514 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002515 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002516 }
Craig Mautner70a86932013-02-28 22:37:44 -08002517
2518 task = r.task;
2519
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002520 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002521 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002522 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002523 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002524 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002525
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002526 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002527 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002528 // We want to show the starting preview window if we are
2529 // switching to a new task, or the next activity's process is
2530 // not currently running.
2531 boolean showStartingIcon = newTask;
2532 ProcessRecord proc = r.app;
2533 if (proc == null) {
2534 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2535 }
2536 if (proc == null || proc.thread == null) {
2537 showStartingIcon = true;
2538 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002539 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002540 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002541 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002542 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002543 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002544 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002545 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002546 ? r.mLaunchTaskBehind
2547 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2548 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002549 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002550 mNoAnimActivities.remove(r);
2551 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002552 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002553 boolean doShow = true;
2554 if (newTask) {
2555 // Even though this activity is starting fresh, we still need
2556 // to reset it to make sure we apply affinities to move any
2557 // existing activities from other tasks in to it.
2558 // If the caller has requested that the target task be
2559 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002560 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002561 resetTaskIfNeededLocked(r, r);
2562 doShow = topRunningNonDelayedActivityLocked(null) == r;
2563 }
Chong Zhang280d3322015-11-03 17:27:26 -08002564 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002565 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2566 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002567 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002568 if (r.mLaunchTaskBehind) {
2569 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2570 // tell WindowManager that r is visible even though it is at the back of the stack.
2571 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002572 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002573 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002574 // Figure out if we are transitioning from another activity that is
2575 // "has the same starting icon" as the next one. This allows the
2576 // window manager to keep the previous window it had previously
2577 // created, if it still had one.
2578 ActivityRecord prev = mResumedActivity;
2579 if (prev != null) {
2580 // We don't want to reuse the previous starting preview if:
2581 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002582 if (prev.task != r.task) {
2583 prev = null;
2584 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002585 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002586 else if (prev.nowVisible) {
2587 prev = null;
2588 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002589 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002590 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002591 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002592 mService.compatibilityInfoForPackageLocked(
2593 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002594 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002595 prev != null ? prev.appToken : null, showStartingIcon);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002596 r.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002597 }
2598 } else {
2599 // If this is the first activity, don't do any fancy animations,
2600 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002601 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002602 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002603 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002604 }
2605 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002606 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002607 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002608 }
2609
Dianne Hackbornbe707852011-11-11 14:32:10 -08002610 final void validateAppTokensLocked() {
2611 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002612 mValidateAppTokens.ensureCapacity(numActivities());
2613 final int numTasks = mTaskHistory.size();
2614 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2615 TaskRecord task = mTaskHistory.get(taskNdx);
2616 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002617 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002618 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002619 }
Craig Mautner000f0022013-02-26 15:04:29 -08002620 TaskGroup group = new TaskGroup();
2621 group.taskId = task.taskId;
2622 mValidateAppTokens.add(group);
2623 final int numActivities = activities.size();
2624 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2625 final ActivityRecord r = activities.get(activityNdx);
2626 group.tokens.add(r.appToken);
2627 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002628 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002629 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002630 }
2631
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002632 /**
2633 * Perform a reset of the given task, if needed as part of launching it.
2634 * Returns the new HistoryRecord at the top of the task.
2635 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002636 /**
2637 * Helper method for #resetTaskIfNeededLocked.
2638 * We are inside of the task being reset... we'll either finish this activity, push it out
2639 * for another task, or leave it as-is.
2640 * @param task The task containing the Activity (taskTop) that might be reset.
2641 * @param forceReset
2642 * @return An ActivityOptions that needs to be processed.
2643 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002644 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002645 ActivityOptions topOptions = null;
2646
2647 int replyChainEnd = -1;
2648 boolean canMoveOptions = true;
2649
2650 // We only do this for activities that are not the root of the task (since if we finish
2651 // the root, we may no longer have the task!).
2652 final ArrayList<ActivityRecord> activities = task.mActivities;
2653 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002654 final int rootActivityNdx = task.findEffectiveRootIndex();
2655 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002656 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002657 if (target.frontOfTask)
2658 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002659
2660 final int flags = target.info.flags;
2661 final boolean finishOnTaskLaunch =
2662 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2663 final boolean allowTaskReparenting =
2664 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2665 final boolean clearWhenTaskReset =
2666 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2667
2668 if (!finishOnTaskLaunch
2669 && !clearWhenTaskReset
2670 && target.resultTo != null) {
2671 // If this activity is sending a reply to a previous
2672 // activity, we can't do anything with it now until
2673 // we reach the start of the reply chain.
2674 // XXX note that we are assuming the result is always
2675 // to the previous activity, which is almost always
2676 // the case but we really shouldn't count on.
2677 if (replyChainEnd < 0) {
2678 replyChainEnd = i;
2679 }
2680 } else if (!finishOnTaskLaunch
2681 && !clearWhenTaskReset
2682 && allowTaskReparenting
2683 && target.taskAffinity != null
2684 && !target.taskAffinity.equals(task.affinity)) {
2685 // If this activity has an affinity for another
2686 // task, then we need to move it out of here. We will
2687 // move it as far out of the way as possible, to the
2688 // bottom of the activity stack. This also keeps it
2689 // correctly ordered with any activities we previously
2690 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002691 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002692 final ActivityRecord bottom =
2693 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002694 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002695 if (bottom != null && target.taskAffinity != null
2696 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002697 // If the activity currently at the bottom has the
2698 // same task affinity as the one we are moving,
2699 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002700 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002701 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002702 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002703 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002704 targetTask = createTaskRecord(
2705 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
2706 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002707 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002708 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002709 + " out to new task " + target.task);
2710 }
2711
Wale Ogunwale706ed792015-08-02 10:29:44 -07002712 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002713
Craig Mautner525f3d92013-05-07 14:01:50 -07002714 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002715 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2716 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002717 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002718 if (p.finishing) {
2719 continue;
2720 }
2721
Craig Mautnere3a74d52013-02-22 14:14:58 -08002722 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002723 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002724 topOptions = p.takeOptionsLocked();
2725 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002726 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002727 }
2728 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002729 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2730 "Removing activity " + p + " from task=" + task + " adding to task="
2731 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002732 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2733 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002734 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002735 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002736
Wale Ogunwale706ed792015-08-02 10:29:44 -07002737 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002738 }
2739
Wale Ogunwale706ed792015-08-02 10:29:44 -07002740 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002741 if (VALIDATE_TOKENS) {
2742 validateAppTokensLocked();
2743 }
2744
2745 replyChainEnd = -1;
2746 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2747 // If the activity should just be removed -- either
2748 // because it asks for it, or the task should be
2749 // cleared -- then finish it and anything that is
2750 // part of its reply chain.
2751 int end;
2752 if (clearWhenTaskReset) {
2753 // In this case, we want to finish this activity
2754 // and everything above it, so be sneaky and pretend
2755 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002756 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002757 } else if (replyChainEnd < 0) {
2758 end = i;
2759 } else {
2760 end = replyChainEnd;
2761 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002762 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002763 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002764 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002765 if (p.finishing) {
2766 continue;
2767 }
2768 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002769 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002770 topOptions = p.takeOptionsLocked();
2771 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002772 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002773 }
2774 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002775 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002776 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002777 if (finishActivityLocked(
2778 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002779 end--;
2780 srcPos--;
2781 }
2782 }
2783 replyChainEnd = -1;
2784 } else {
2785 // If we were in the middle of a chain, well the
2786 // activity that started it all doesn't want anything
2787 // special, so leave it all as-is.
2788 replyChainEnd = -1;
2789 }
2790 }
2791
2792 return topOptions;
2793 }
2794
2795 /**
2796 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2797 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2798 * @param affinityTask The task we are looking for an affinity to.
2799 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2800 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2801 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2802 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002803 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002804 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002805 int replyChainEnd = -1;
2806 final int taskId = task.taskId;
2807 final String taskAffinity = task.affinity;
2808
2809 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2810 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002811 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2812
2813 // Do not operate on or below the effective root Activity.
2814 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002815 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002816 if (target.frontOfTask)
2817 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002818
2819 final int flags = target.info.flags;
2820 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2821 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2822
2823 if (target.resultTo != null) {
2824 // If this activity is sending a reply to a previous
2825 // activity, we can't do anything with it now until
2826 // we reach the start of the reply chain.
2827 // XXX note that we are assuming the result is always
2828 // to the previous activity, which is almost always
2829 // the case but we really shouldn't count on.
2830 if (replyChainEnd < 0) {
2831 replyChainEnd = i;
2832 }
2833 } else if (topTaskIsHigher
2834 && allowTaskReparenting
2835 && taskAffinity != null
2836 && taskAffinity.equals(target.taskAffinity)) {
2837 // This activity has an affinity for our task. Either remove it if we are
2838 // clearing or move it over to our task. Note that
2839 // we currently punt on the case where we are resetting a
2840 // task that is not at the top but who has activities above
2841 // with an affinity to it... this is really not a normal
2842 // case, and we will need to later pull that task to the front
2843 // and usually at that point we will do the reset and pick
2844 // up those remaining activities. (This only happens if
2845 // someone starts an activity in a new task from an activity
2846 // in a task that is not currently on top.)
2847 if (forceReset || finishOnTaskLaunch) {
2848 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002849 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2850 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002851 for (int srcPos = start; srcPos >= i; --srcPos) {
2852 final ActivityRecord p = activities.get(srcPos);
2853 if (p.finishing) {
2854 continue;
2855 }
Todd Kennedy539db512014-12-15 09:57:55 -08002856 finishActivityLocked(
2857 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002858 }
2859 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002860 if (taskInsertionPoint < 0) {
2861 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002862
Craig Mautner77878772013-03-04 19:46:24 -08002863 }
Craig Mautner77878772013-03-04 19:46:24 -08002864
2865 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002866 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2867 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2868 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002869 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002870 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002871 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002872 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002873
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002874 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2875 "Removing and adding activity " + p + " to stack at " + task
2876 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002877 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2878 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002879 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002880 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002881 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002882 if (VALIDATE_TOKENS) {
2883 validateAppTokensLocked();
2884 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002885
2886 // Now we've moved it in to place... but what if this is
2887 // a singleTop activity and we have put it on top of another
2888 // instance of the same activity? Then we drop the instance
2889 // below so it remains singleTop.
2890 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2891 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002892 int targetNdx = taskActivities.indexOf(target);
2893 if (targetNdx > 0) {
2894 ActivityRecord p = taskActivities.get(targetNdx - 1);
2895 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002896 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2897 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002898 }
2899 }
2900 }
2901 }
2902
2903 replyChainEnd = -1;
2904 }
2905 }
Craig Mautner77878772013-03-04 19:46:24 -08002906 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002907 }
2908
Craig Mautner8849a5e2013-04-02 16:41:03 -07002909 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002910 ActivityRecord newActivity) {
2911 boolean forceReset =
2912 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2913 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2914 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2915 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2916 forceReset = true;
2917 }
2918 }
2919
2920 final TaskRecord task = taskTop.task;
2921
2922 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2923 * for remaining tasks. Used for later tasks to reparent to task. */
2924 boolean taskFound = false;
2925
2926 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2927 ActivityOptions topOptions = null;
2928
Craig Mautner77878772013-03-04 19:46:24 -08002929 // Preserve the location for reparenting in the new task.
2930 int reparentInsertionPoint = -1;
2931
Craig Mautnere3a74d52013-02-22 14:14:58 -08002932 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2933 final TaskRecord targetTask = mTaskHistory.get(i);
2934
2935 if (targetTask == task) {
2936 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2937 taskFound = true;
2938 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002939 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2940 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002941 }
2942 }
2943
Craig Mautner70a86932013-02-28 22:37:44 -08002944 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002945 if (taskNdx >= 0) {
2946 do {
2947 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2948 } while (taskTop == null && taskNdx >= 0);
2949 }
Craig Mautner70a86932013-02-28 22:37:44 -08002950
Craig Mautnere3a74d52013-02-22 14:14:58 -08002951 if (topOptions != null) {
2952 // If we got some ActivityOptions from an activity on top that
2953 // was removed from the task, propagate them to the new real top.
2954 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002955 taskTop.updateOptionsLocked(topOptions);
2956 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002957 topOptions.abort();
2958 }
2959 }
2960
2961 return taskTop;
2962 }
2963
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002964 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2965 String resultWho, int requestCode, int resultCode, Intent data) {
2966
2967 if (callingUid > 0) {
2968 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002969 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002970 }
2971
2972 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2973 + " : who=" + resultWho + " req=" + requestCode
2974 + " res=" + resultCode + " data=" + data);
2975 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2976 try {
2977 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2978 list.add(new ResultInfo(resultWho, requestCode,
2979 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002980 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002981 return;
2982 } catch (Exception e) {
2983 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2984 }
2985 }
2986
2987 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2988 }
2989
Craig Mautner299f9602015-01-26 09:47:33 -08002990 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002991 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2992 return;
2993 }
2994
2995 final ActivityRecord next = topRunningActivityLocked();
2996 final String myReason = reason + " adjustFocus";
2997 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002998 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002999 // For freeform, docked, and pinned stacks we always keep the focus within the
3000 // stack as long as there is a running activity in the stack that we can adjust
3001 // focus to.
3002 mService.setFocusedActivityLocked(next, myReason);
3003 return;
3004 } else {
3005 final TaskRecord task = r.task;
3006 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
3007 // For non-fullscreen stack, we want to move the focus to the next visible
3008 // stack to prevent the home screen from moving to the top and obscuring
3009 // other visible stacks.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003010 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08003011 return;
3012 }
3013 // Move the home stack to the top if this stack is fullscreen or there is no
3014 // other visible stack.
3015 if (mStackSupervisor.moveHomeStackTaskToTop(
3016 task.getTaskToReturnTo(), myReason)) {
3017 // Activity focus was already adjusted. Nothing else to do...
3018 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003019 }
Craig Mautner04f0b702013-10-22 12:31:01 -07003020 }
3021 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08003022 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003023
Wale Ogunwaled046a012015-12-24 13:05:59 -08003024 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07003025 }
3026
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08003027 private boolean adjustFocusToNextFocusableStackLocked(String reason) {
3028 final ActivityStack stack = getNextFocusableStackLocked();
3029 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003030 if (stack == null) {
3031 return false;
3032 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003033 return mService.setFocusedActivityLocked(stack.topRunningActivityLocked(), myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08003034 }
3035
Craig Mautnerf3333272013-04-22 10:55:53 -07003036 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003037 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003038 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3039 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3040 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07003041 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003042 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003043 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
3044 "stop-no-history", false)) {
3045 // Activity was finished, no need to continue trying to schedule stop.
3046 adjustFocusedActivityLocked(r, "stopActivityFinished");
3047 r.resumeKeyDispatchingLocked();
3048 return;
3049 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003050 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003051 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003052 + " on stop because we're just sleeping");
3053 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003054 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003055 }
3056
3057 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08003058 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003059 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003060 try {
3061 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003062 if (DEBUG_STATES) Slog.v(TAG_STATES,
3063 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003064 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003065 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3066 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003067 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003068 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003069 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003070 EventLogTags.writeAmStopActivity(
3071 r.userId, System.identityHashCode(r), r.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003072 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07003073 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003074 r.setSleeping(true);
3075 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003076 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003077 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003078 } catch (Exception e) {
3079 // Maybe just ignore exceptions here... if the process
3080 // has crashed, our death notification will clean things
3081 // up.
3082 Slog.w(TAG, "Exception thrown during pause", e);
3083 // Just in case, assume it to be stopped.
3084 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003085 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003086 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003087 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003088 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003089 }
3090 }
3091 }
3092 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003093
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003094 /**
3095 * @return Returns true if the activity is being finished, false if for
3096 * some reason it is being left as-is.
3097 */
3098 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003099 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003100 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003101 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3102 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003103 + ", result=" + resultCode + ", data=" + resultData
3104 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003105 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003106 return false;
3107 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003108
Craig Mautnerd44711d2013-02-23 11:24:36 -08003109 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003110 return true;
3111 }
3112
Craig Mautnerd2328952013-03-05 12:46:26 -08003113 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003114 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3115 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3116 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3117 ActivityRecord r = activities.get(activityNdx);
3118 if (r.resultTo == self && r.requestCode == requestCode) {
3119 if ((r.resultWho == null && resultWho == null) ||
3120 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3121 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3122 false);
3123 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003124 }
3125 }
3126 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003127 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003128 }
3129
Adrian Roos20d7df32016-01-12 18:59:43 +01003130 final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003131 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003132 TaskRecord finishedTask = null;
3133 if (r == null || r.app != app) {
3134 return null;
3135 }
3136 Slog.w(TAG, " Force finishing activity "
3137 + r.intent.getComponent().flattenToShortString());
3138 int taskNdx = mTaskHistory.indexOf(r.task);
3139 int activityNdx = r.task.mActivities.indexOf(r);
3140 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3141 finishedTask = r.task;
3142 // Also terminate any activities below it that aren't yet
3143 // stopped, to avoid a situation where one will get
3144 // re-start our crashing activity once it gets resumed again.
3145 --activityNdx;
3146 if (activityNdx < 0) {
3147 do {
3148 --taskNdx;
3149 if (taskNdx < 0) {
3150 break;
3151 }
3152 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3153 } while (activityNdx < 0);
3154 }
3155 if (activityNdx >= 0) {
3156 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3157 if (r.state == ActivityState.RESUMED
3158 || r.state == ActivityState.PAUSING
3159 || r.state == ActivityState.PAUSED) {
3160 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3161 Slog.w(TAG, " Force finishing activity "
3162 + r.intent.getComponent().flattenToShortString());
3163 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003164 }
3165 }
3166 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003167 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003168 }
3169
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003170 final void finishVoiceTask(IVoiceInteractionSession session) {
3171 IBinder sessionBinder = session.asBinder();
3172 boolean didOne = false;
3173 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3174 TaskRecord tr = mTaskHistory.get(taskNdx);
3175 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3176 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3177 ActivityRecord r = tr.mActivities.get(activityNdx);
3178 if (!r.finishing) {
3179 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3180 false);
3181 didOne = true;
3182 }
3183 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003184 } else {
3185 // Check if any of the activities are using voice
3186 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3187 ActivityRecord r = tr.mActivities.get(activityNdx);
3188 if (r.voiceSession != null
3189 && r.voiceSession.asBinder() == sessionBinder) {
3190 // Inform of cancellation
3191 r.clearVoiceSessionLocked();
3192 try {
3193 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3194 null);
3195 } catch (RemoteException re) {
3196 // Ok
3197 }
3198 // TODO: VI This is redundant in some cases
3199 mService.finishRunningVoiceLocked();
3200 break;
3201 }
3202 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003203 }
3204 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003205 Slog.d(TAG, "ActivityStack.finishVoiceTask()");
3206
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003207 if (didOne) {
3208 mService.updateOomAdjLocked();
3209 }
3210 }
3211
Craig Mautnerd2328952013-03-05 12:46:26 -08003212 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003213 ArrayList<ActivityRecord> activities = r.task.mActivities;
3214 for (int index = activities.indexOf(r); index >= 0; --index) {
3215 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003216 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003217 break;
3218 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003219 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003220 }
3221 return true;
3222 }
3223
Dianne Hackborn5c607432012-02-28 14:44:19 -08003224 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3225 // send the result
3226 ActivityRecord resultTo = r.resultTo;
3227 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003228 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003229 + " who=" + r.resultWho + " req=" + r.requestCode
3230 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003231 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003232 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003233 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003234 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003235 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003236 if (r.info.applicationInfo.uid > 0) {
3237 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3238 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003239 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003240 }
3241 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3242 resultData);
3243 r.resultTo = null;
3244 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003245 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003246
3247 // Make sure this HistoryRecord is not holding on to other resources,
3248 // because clients have remote IPC references to this object so we
3249 // can't assume that will go away and want to avoid circular IPC refs.
3250 r.results = null;
3251 r.pendingResults = null;
3252 r.newIntents = null;
3253 r.icicle = null;
3254 }
3255
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003256 /**
3257 * @return Returns true if this activity has been removed from the history
3258 * list, or false if it is still in the list and will be removed later.
3259 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003260 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3261 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003262 if (r.finishing) {
3263 Slog.w(TAG, "Duplicate finish request for " + r);
3264 return false;
3265 }
3266
Wale Ogunwale7d701172015-03-11 15:36:30 -07003267 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003268 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003269 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003270 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003271 task.taskId, r.shortComponentName, reason);
3272 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003273 final int index = activities.indexOf(r);
3274 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003275 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003276 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003277 // If the caller asked that this activity (and all above it)
3278 // be cleared when the task is reset, don't lose that information,
3279 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003280 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003281 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003282 }
3283 }
3284
3285 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003286
Craig Mautner299f9602015-01-26 09:47:33 -08003287 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003288
Dianne Hackborn5c607432012-02-28 14:44:19 -08003289 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003290
Craig Mautnerde4ef022013-04-07 19:01:33 -07003291 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003292 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003293 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003294 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003295 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003296 ? AppTransition.TRANSIT_TASK_CLOSE
3297 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003298
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003299 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003300 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003301
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003302 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003303 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3304 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3305 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003306 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003307 }
3308
Craig Mautneraea74a52014-03-08 14:23:10 -08003309 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003310 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003311 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003312 } else if (r.state != ActivityState.PAUSING) {
3313 // If the activity is PAUSING, we will complete the finish once
3314 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003315 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003316 if (r.visible) {
3317 mWindowManager.setAppVisibility(r.appToken, false);
3318 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003319 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003320 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003321 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003322 }
3323
3324 return false;
3325 }
3326
Craig Mautnerf3333272013-04-22 10:55:53 -07003327 static final int FINISH_IMMEDIATELY = 0;
3328 static final int FINISH_AFTER_PAUSE = 1;
3329 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003330
Craig Mautnerf3333272013-04-22 10:55:53 -07003331 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003332 // First things first: if this activity is currently visible,
3333 // and the resumed activity is not yet visible, then hold off on
3334 // finishing until the resumed one becomes visible.
3335 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003336 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08003337 addToStopping(r, false /* immediate */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003338 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003339 if (DEBUG_STATES) Slog.v(TAG_STATES,
3340 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003341 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003342 if (oomAdj) {
3343 mService.updateOomAdjLocked();
3344 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003345 return r;
3346 }
3347
3348 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003349 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003350 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003351 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003352 if (mResumedActivity == r) {
3353 mResumedActivity = null;
3354 }
3355 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003356 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003357 r.state = ActivityState.FINISHING;
3358
3359 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003360 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003361 || prevState == ActivityState.STOPPED
3362 || prevState == ActivityState.INITIALIZING) {
3363 // If this activity is already stopped, we can just finish
3364 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003365 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003366 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003367 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003368 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003369 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003370 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003371 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3372 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003373 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003374 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003375
3376 // Need to go through the full pause cycle to get this
3377 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003378 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003379 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003380 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003381 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003382 return r;
3383 }
3384
Craig Mautneree36c772014-07-16 14:56:05 -07003385 void finishAllActivitiesLocked(boolean immediately) {
3386 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003387 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3388 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3389 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3390 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003391 noActivitiesInStack = false;
3392 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003393 continue;
3394 }
Craig Mautneree36c772014-07-16 14:56:05 -07003395 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003396 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3397 }
3398 }
Craig Mautneree36c772014-07-16 14:56:05 -07003399 if (noActivitiesInStack) {
3400 mActivityContainer.onTaskListEmptyLocked();
3401 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003402 }
3403
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003404 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3405 // Basic case: for simple app-centric recents, we need to recreate
3406 // the task if the affinity has changed.
3407 if (srec == null || srec.task.affinity == null ||
3408 !srec.task.affinity.equals(destAffinity)) {
3409 return true;
3410 }
3411 // Document-centric case: an app may be split in to multiple documents;
3412 // they need to re-create their task if this current activity is the root
3413 // of a document, unless simply finishing it will return them to the the
3414 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003415 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3416 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003417 // Okay, this activity is at the root of its task. What to do, what to do...
3418 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3419 // Finishing won't return to an application, so we need to recreate.
3420 return true;
3421 }
3422 // We now need to get the task below it to determine what to do.
3423 int taskIdx = mTaskHistory.indexOf(srec.task);
3424 if (taskIdx <= 0) {
3425 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3426 return false;
3427 }
3428 if (taskIdx == 0) {
3429 // At the bottom of the stack, nothing to go back to.
3430 return true;
3431 }
3432 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3433 if (!srec.task.affinity.equals(prevTask.affinity)) {
3434 // These are different apps, so need to recreate.
3435 return true;
3436 }
3437 }
3438 return false;
3439 }
3440
Wale Ogunwale7d701172015-03-11 15:36:30 -07003441 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003442 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003443 final TaskRecord task = srec.task;
3444 final ArrayList<ActivityRecord> activities = task.mActivities;
3445 final int start = activities.indexOf(srec);
3446 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003447 return false;
3448 }
3449 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003450 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003451 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003452 final ComponentName dest = destIntent.getComponent();
3453 if (start > 0 && dest != null) {
3454 for (int i = finishTo; i >= 0; i--) {
3455 ActivityRecord r = activities.get(i);
3456 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003457 r.info.name.equals(dest.getClassName())) {
3458 finishTo = i;
3459 parent = r;
3460 foundParentInTask = true;
3461 break;
3462 }
3463 }
3464 }
3465
3466 IActivityController controller = mService.mController;
3467 if (controller != null) {
3468 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3469 if (next != null) {
3470 // ask watcher if this is allowed
3471 boolean resumeOK = true;
3472 try {
3473 resumeOK = controller.activityResuming(next.packageName);
3474 } catch (RemoteException e) {
3475 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003476 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003477 }
3478
3479 if (!resumeOK) {
3480 return false;
3481 }
3482 }
3483 }
3484 final long origId = Binder.clearCallingIdentity();
3485 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003486 ActivityRecord r = activities.get(i);
3487 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003488 // Only return the supplied result for the first activity finished
3489 resultCode = Activity.RESULT_CANCELED;
3490 resultData = null;
3491 }
3492
3493 if (parent != null && foundParentInTask) {
3494 final int parentLaunchMode = parent.info.launchMode;
3495 final int destIntentFlags = destIntent.getFlags();
3496 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3497 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3498 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3499 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003500 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3501 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003502 } else {
3503 try {
3504 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3505 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003506 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3507 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3508 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003509 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3510 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003511 foundParentInTask = res == ActivityManager.START_SUCCESS;
3512 } catch (RemoteException e) {
3513 foundParentInTask = false;
3514 }
3515 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003516 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003517 }
3518 }
3519 Binder.restoreCallingIdentity(origId);
3520 return foundParentInTask;
3521 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003522 /**
3523 * Perform the common clean-up of an activity record. This is called both
3524 * as part of destroyActivityLocked() (when destroying the client-side
3525 * representation) and cleaning things up as a result of its hosting
3526 * processing going away, in which case there is no remaining client-side
3527 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003528 *
3529 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003530 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003531 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3532 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003533 if (mResumedActivity == r) {
3534 mResumedActivity = null;
3535 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003536 if (mPausingActivity == r) {
3537 mPausingActivity = null;
3538 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003539 mService.resetFocusedActivityIfNeededLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003540
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003541 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003542 r.frozenBeforeDestroy = false;
3543
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003544 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003545 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003546 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003547 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003548 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003549 }
3550
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003551 // Make sure this record is no longer in the pending finishes list.
3552 // This could happen, for example, if we are trimming activities
3553 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003554 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003555 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003556
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003557 // Remove any pending results.
3558 if (r.finishing && r.pendingResults != null) {
3559 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3560 PendingIntentRecord rec = apr.get();
3561 if (rec != null) {
3562 mService.cancelIntentSenderLocked(rec, false);
3563 }
3564 }
3565 r.pendingResults = null;
3566 }
3567
3568 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003569 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003570 }
3571
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003572 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003573 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003574 if (getVisibleBehindActivity() == r) {
3575 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003576 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003577 }
3578
3579 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003580 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003581 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003582 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003583 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003584 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003585 }
3586
Craig Mautner299f9602015-01-26 09:47:33 -08003587 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003588 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003589 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003590 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003591 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3592 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3593
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003594 r.takeFromHistory();
3595 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003596 if (DEBUG_STATES) Slog.v(TAG_STATES,
3597 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003598 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003599 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003600 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003601 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003602 if (VALIDATE_TOKENS) {
3603 validateAppTokensLocked();
3604 }
Craig Mautner312ba862014-02-10 17:55:01 -08003605 final TaskRecord task = r.task;
3606 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003607 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003608 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003609 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003610 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003611 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003612 }
Craig Mautner299f9602015-01-26 09:47:33 -08003613 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003614 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003615 cleanUpActivityServicesLocked(r);
3616 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003617 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003618
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003619 /**
3620 * Perform clean-up of service connections in an activity record.
3621 */
3622 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3623 // Throw away any services that have been bound by this activity.
3624 if (r.connections != null) {
3625 Iterator<ConnectionRecord> it = r.connections.iterator();
3626 while (it.hasNext()) {
3627 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003628 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003629 }
3630 r.connections = null;
3631 }
3632 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003633
Craig Mautneree2e45a2014-06-27 12:10:03 -07003634 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003635 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003636 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003637 mHandler.sendMessage(msg);
3638 }
3639
Craig Mautneree2e45a2014-06-27 12:10:03 -07003640 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003641 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003642 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003643 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3644 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3645 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3646 final ActivityRecord r = activities.get(activityNdx);
3647 if (r.finishing) {
3648 continue;
3649 }
3650 if (r.fullscreen) {
3651 lastIsOpaque = true;
3652 }
3653 if (owner != null && r.app != owner) {
3654 continue;
3655 }
3656 if (!lastIsOpaque) {
3657 continue;
3658 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003659 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003660 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003661 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003662 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003663 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003664 activityRemoved = true;
3665 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003666 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003667 }
3668 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003669 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003670 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003671 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003672 }
3673
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003674 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3675 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003676 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3677 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003678 + " pausing=" + mPausingActivity + " for reason " + reason);
3679 return destroyActivityLocked(r, true, reason);
3680 }
3681 return false;
3682 }
3683
3684 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3685 String reason) {
3686 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003687 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003688 int maxTasks = tasks.size() / 4;
3689 if (maxTasks < 1) {
3690 maxTasks = 1;
3691 }
3692 int numReleased = 0;
3693 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3694 final TaskRecord task = mTaskHistory.get(taskNdx);
3695 if (!tasks.contains(task)) {
3696 continue;
3697 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003698 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003699 int curNum = 0;
3700 final ArrayList<ActivityRecord> activities = task.mActivities;
3701 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3702 final ActivityRecord activity = activities.get(actNdx);
3703 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003704 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003705 + " in state " + activity.state + " resumed=" + mResumedActivity
3706 + " pausing=" + mPausingActivity + " for reason " + reason);
3707 destroyActivityLocked(activity, true, reason);
3708 if (activities.get(actNdx) != activity) {
3709 // Was removed from list, back up so we don't miss the next one.
3710 actNdx--;
3711 }
3712 curNum++;
3713 }
3714 }
3715 if (curNum > 0) {
3716 numReleased += curNum;
3717 maxTasks--;
3718 if (mTaskHistory.get(taskNdx) != task) {
3719 // The entire task got removed, back up so we don't miss the next one.
3720 taskNdx--;
3721 }
3722 }
3723 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003724 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3725 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003726 return numReleased;
3727 }
3728
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003729 /**
3730 * Destroy the current CLIENT SIDE instance of an activity. This may be
3731 * called both when actually finishing an activity, or when performing
3732 * a configuration switch where we destroy the current client-side object
3733 * but then create a new client-side object for this same HistoryRecord.
3734 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003735 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003736 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3737 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003738 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003739 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003740 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003741 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003742
3743 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003744
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003745 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003746
3747 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003748
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003749 if (hadApp) {
3750 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003751 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003752 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3753 mService.mHeavyWeightProcess = null;
3754 mService.mHandler.sendEmptyMessage(
3755 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3756 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003757 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003758 // Update any services we are bound to that might care about whether
3759 // their client may have activities.
3760 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003761 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003762 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003763 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003764 }
3765 }
3766
3767 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003768
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003769 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003770 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003771 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003772 r.configChangeFlags);
3773 } catch (Exception e) {
3774 // We can just ignore exceptions here... if the process
3775 // has crashed, our death notification will clean things
3776 // up.
3777 //Slog.w(TAG, "Exception thrown during finish", e);
3778 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003779 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003780 removedFromHistory = true;
3781 skipDestroy = true;
3782 }
3783 }
3784
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003785 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003786
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003787 // If the activity is finishing, we need to wait on removing it
3788 // from the list to give it a chance to do its cleanup. During
3789 // that time it may make calls back with its token so we need to
3790 // be able to find it on the list and so we don't want to remove
3791 // it from the list yet. Otherwise, we can just immediately put
3792 // it in the destroyed state since we are not removing it from the
3793 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003794 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003795 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003796 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003797 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003798 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003799 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3800 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003801 if (DEBUG_STATES) Slog.v(TAG_STATES,
3802 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003803 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003804 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003805 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003806 }
3807 } else {
3808 // remove this record from the history.
3809 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003810 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003811 removedFromHistory = true;
3812 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003813 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003814 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003815 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003816 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003817 }
3818 }
3819
3820 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003821
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003822 if (!mLRUActivities.remove(r) && hadApp) {
3823 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3824 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003825
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003826 return removedFromHistory;
3827 }
3828
Craig Mautner299f9602015-01-26 09:47:33 -08003829 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003830 final long origId = Binder.clearCallingIdentity();
3831 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003832 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003833 if (r != null) {
3834 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003835 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003836 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003837
Wale Ogunwale60454db2015-01-23 16:05:07 -08003838 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003839 if (r.state == ActivityState.DESTROYING) {
3840 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003841 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003842 }
3843 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003844 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003845 } finally {
3846 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003847 }
3848 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003849
Todd Kennedyaab56db2015-01-30 09:39:53 -08003850 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003851 if (hasVisibleBehindActivity() &&
3852 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Wale Ogunwale8051c5c2016-03-04 10:27:32 -08003853 if (r == topRunningActivityLocked()
3854 && getStackVisibilityLocked(null) == STACK_VISIBLE) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003855 // Don't release the top activity if it has requested to run behind the next
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08003856 // activity and the stack is currently visible.
Craig Mautner64ccb702014-10-01 09:38:40 -07003857 return;
3858 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003859 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003860 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003861 " thread=" + r.app.thread);
3862 if (r != null && r.app != null && r.app.thread != null) {
3863 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003864 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003865 } catch (RemoteException e) {
3866 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003867 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003868 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003869 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003870 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003871 }
3872 }
3873 }
3874
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003875 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003876 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3877 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003878 if (r != null) {
3879 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003880 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003881 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003882 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003883 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003884 }
3885
Jose Lima4b6c6692014-08-12 17:41:12 -07003886 boolean hasVisibleBehindActivity() {
3887 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003888 }
3889
Jose Lima4b6c6692014-08-12 17:41:12 -07003890 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003891 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003892 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003893 }
3894 }
3895
Jose Lima4b6c6692014-08-12 17:41:12 -07003896 ActivityRecord getVisibleBehindActivity() {
3897 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003898 }
3899
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003900 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3901 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003902 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003903 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3904 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003905 while (i > 0) {
3906 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003907 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003908 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003909 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003910 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003911 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003912 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003913 }
3914 }
3915 }
3916
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003917 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3918 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003919 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3920 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003921 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3922 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003923 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003924 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003925 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3926 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003927
3928 boolean hasVisibleActivities = false;
3929
3930 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003931 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003932 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3933 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003934 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3935 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3936 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3937 final ActivityRecord r = activities.get(activityNdx);
3938 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003939 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3940 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003941 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003942 if (r.visible) {
3943 hasVisibleActivities = true;
3944 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003945 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003946 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3947 // Don't currently have state for the activity, or
3948 // it is finishing -- always remove it.
3949 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003950 } else if (!r.visible && r.launchCount > 2 &&
3951 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003952 // We have launched this activity too many times since it was
3953 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003954 // (Note if the activity is visible, we don't remove the record.
3955 // We leave the dead window on the screen but the process will
3956 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003957 remove = true;
3958 } else {
3959 // The process may be gone, but the activity lives on!
3960 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003961 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003962 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003963 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3964 "Removing activity " + r + " from stack at " + i
3965 + ": haveState=" + r.haveState
3966 + " stateNotNeeded=" + r.stateNotNeeded
3967 + " finishing=" + r.finishing
3968 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003969 if (!r.finishing) {
3970 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3971 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3972 r.userId, System.identityHashCode(r),
3973 r.task.taskId, r.shortComponentName,
3974 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003975 if (r.state == ActivityState.RESUMED) {
3976 mService.updateUsageStats(r, false);
3977 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003978 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003979 } else {
3980 // We have the current state for this activity, so
3981 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003982 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3983 if (DEBUG_APP) Slog.v(TAG_APP,
3984 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003985 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003986 // Set nowVisible to previous visible state. If the app was visible while
3987 // it died, we leave the dead window on screen so it's basically visible.
3988 // This is needed when user later tap on the dead window, we need to stop
3989 // other apps when user transfers focus to the restarted activity.
3990 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003991 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003992 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003993 "App died, clearing saved state of " + r);
3994 r.icicle = null;
3995 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003996 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003997 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003998 if (remove) {
3999 removeActivityFromHistoryLocked(r, "appDied");
4000 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004001 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07004002 }
4003 }
4004
4005 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004006 }
Craig Mautner0247fc82013-02-28 14:32:06 -08004007
Chong Zhang280d3322015-11-03 17:27:26 -08004008 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004009 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004010 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004011 if (r != null && r.state != ActivityState.RESUMED) {
4012 r.updateOptionsLocked(options);
4013 } else {
4014 ActivityOptions.abort(options);
4015 }
4016 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004017 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07004018 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004019
Craig Mautner21d24a22014-04-23 11:45:37 -07004020 void updateTaskMovement(TaskRecord task, boolean toFront) {
4021 if (task.isPersistable) {
4022 task.mLastTimeMoved = System.currentTimeMillis();
4023 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
4024 // recently will be most negative, tasks sent to the bottom before that will be less
4025 // negative. Similarly for recent tasks moved to the top which will be most positive.
4026 if (!toFront) {
4027 task.mLastTimeMoved *= -1;
4028 }
4029 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07004030 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07004031 }
4032
Craig Mautner84984fa2014-06-19 11:19:20 -07004033 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07004034 final int top = mTaskHistory.size() - 1;
4035 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
4036 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004037 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004038 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
4039 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07004040 mTaskHistory.remove(taskNdx);
4041 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004042 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07004043 return;
4044 }
4045 }
4046 }
4047
Chong Zhang280d3322015-11-03 17:27:26 -08004048 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004049 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004050 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004051
Craig Mautner11bf9a52013-02-19 14:08:51 -08004052 final int numTasks = mTaskHistory.size();
4053 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004054 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004055 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004056 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004057 ActivityOptions.abort(options);
4058 } else {
4059 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
4060 }
4061 return;
4062 }
4063
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004064 if (timeTracker != null) {
4065 // The caller wants a time tracker associated with this task.
4066 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4067 tr.mActivities.get(i).appTimeTracker = timeTracker;
4068 }
4069 }
4070
Craig Mautner11bf9a52013-02-19 14:08:51 -08004071 // Shift all activities with this task up to the top
4072 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004073 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004074
Chong Zhang45c25ce2015-08-10 22:18:26 -07004075 // Don't refocus if invisible to current user
4076 ActivityRecord top = tr.getTopActivity();
4077 if (!okToShowLocked(top)) {
4078 addRecentActivityLocked(top);
4079 ActivityOptions.abort(options);
4080 return;
4081 }
4082
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004083 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004084 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004085 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004086
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004087 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004088 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004089 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004090 if (r != null) {
4091 mNoAnimActivities.add(r);
4092 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004093 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004094 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08004095 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004096 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004097
Wale Ogunwaled046a012015-12-24 13:05:59 -08004098 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004099 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004100
4101 if (VALIDATE_TOKENS) {
4102 validateAppTokensLocked();
4103 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004104 }
4105
4106 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004107 * Worker method for rearranging history stack. Implements the function of moving all
4108 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004109 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004110 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004111 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4112 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004113 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004114 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004115 * @return Returns true if the move completed, false if not.
4116 */
Craig Mautner299f9602015-01-26 09:47:33 -08004117 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004118 final TaskRecord tr = taskForIdLocked(taskId);
4119 if (tr == null) {
4120 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4121 return false;
4122 }
4123
4124 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07004125 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004126
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004127 // If we have a watcher, preflight the move before committing to it. First check
4128 // for *other* available tasks, but if none are available, then try again allowing the
4129 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004130 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004131 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004132 if (next == null) {
4133 next = topRunningActivityLocked(null, 0);
4134 }
4135 if (next != null) {
4136 // ask watcher if this is allowed
4137 boolean moveOK = true;
4138 try {
4139 moveOK = mService.mController.activityResuming(next.packageName);
4140 } catch (RemoteException e) {
4141 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004142 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004143 }
4144 if (!moveOK) {
4145 return false;
4146 }
4147 }
4148 }
4149
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004150 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004151
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004152 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4153 // For the case where we are moving the home task back and there is an activity visible
4154 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4155 // activity to maintain order with what the user is seeing.
4156 final ActivityStack fullscreenStack =
4157 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4158 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4159 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4160 mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004161 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004162 return true;
4163 }
4164 }
4165
riddle_hsuc215a4f2014-12-27 12:10:45 +08004166 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004167
4168 // If true, we should resume the home activity next if the task we are moving to the
4169 // back is over the home stack. We force to false if the task we are moving to back
4170 // is the home task and we don't want it resumed after moving to the back.
4171 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4172 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004173 final TaskRecord nextTask = getNextTask(tr);
4174 if (nextTask != null) {
4175 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4176 } else {
4177 prevIsHome = true;
4178 }
4179 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004180 mTaskHistory.remove(tr);
4181 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004182 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004183
Craig Mautnerc8143c62013-09-03 12:15:57 -07004184 // There is an assumption that moving a task to the back moves it behind the home activity.
4185 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004186 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004187 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4188 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004189 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004190 break;
4191 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004192 if (taskNdx == 1) {
4193 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004194 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004195 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004196 }
4197
Craig Mautner299f9602015-01-26 09:47:33 -08004198 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004199 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004200
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004201 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004202 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004203 }
4204
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004205 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004206 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004207 if (!mService.mBooting && !mService.mBooted) {
4208 // Not ready yet!
4209 return false;
4210 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004211 final int taskToReturnTo = tr.getTaskToReturnTo();
4212 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004213 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004214 }
4215
Wale Ogunwaled046a012015-12-24 13:05:59 -08004216 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004217 return true;
4218 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004219
Craig Mautner8849a5e2013-04-02 16:41:03 -07004220 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004221 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004222 final Uri data = r.intent.getData();
4223 final String strData = data != null ? data.toSafeString() : null;
4224
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004225 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004226 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004227 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004228 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004229 }
4230
4231 /**
4232 * Make sure the given activity matches the current configuration. Returns
4233 * false if the activity had to be destroyed. Returns true if the
4234 * configuration is the same, or the activity will remain running as-is
4235 * for whatever reason. Ensures the HistoryRecord is updated with the
4236 * correct configuration and all other bookkeeping is handled.
4237 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004238 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4239 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004240 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004241 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004242 "Skipping config check (will change): " + r);
4243 return true;
4244 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004245
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004246 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004247 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004248
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004249 // Short circuit: if the two configurations are the exact same
4250 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004251 final Configuration newConfig = mService.mConfiguration;
4252 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004253 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004254 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004255 && !r.forceNewConfig) {
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 unchanged in " + r);
4258 return true;
4259 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004260
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004261 // We don't worry about activities that are finishing.
4262 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004263 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004264 "Configuration doesn't matter in finishing " + r);
4265 r.stopFreezingScreenLocked(false);
4266 return true;
4267 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004268
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004269 // Okay we now are going to make this activity have the new config.
4270 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004271 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004272 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004273 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004274 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004275
Filip Gruszczynski23493322015-07-29 17:02:59 -07004276 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004277 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004278 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004279 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004280 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004281 // There are no significant differences, so we won't relaunch but should still deliver
4282 // the new configuration to the client process.
4283 r.scheduleConfigurationChanged(taskConfig, false);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004284 return true;
4285 }
4286
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004287 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4288 "Configuration changes for " + r + " ; taskChanges="
4289 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4290 + Configuration.configurationDiffToString(changes));
4291
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004292 // If the activity isn't currently running, just leave the new
4293 // configuration and it will pick that up next time it starts.
4294 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004295 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004296 "Configuration doesn't matter not running " + r);
4297 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004298 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004299 return true;
4300 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004301
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004302 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004303 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4304 "Checking to restart " + r.info.name + ": changed=0x"
4305 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004306 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4307 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004308
Dianne Hackborne6676352011-06-01 16:51:20 -07004309 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004310 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4311 r.configChangeFlags |= changes;
4312 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004313 r.forceNewConfig = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004314 preserveWindow &= isResizeOnlyChange(changes);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004315 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004316 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004317 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004318 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004319 } else if (r.state == ActivityState.PAUSING) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004320 // A little annoying: we are waiting for this activity to finish pausing. Let's not
4321 // do anything now, but just flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004322 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004323 "Config is skipping already pausing " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004324 r.deferRelaunchUntilPaused = true;
4325 r.preserveWindowOnDeferredRelaunch = preserveWindow;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004326 return true;
4327 } else if (r.state == ActivityState.RESUMED) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004328 // Try to optimize this case: the configuration is changing and we need to restart
4329 // the top, resumed activity. Instead of doing the normal handshaking, just say
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004330 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004331 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004332 "Config is relaunching resumed " + r);
Chong Zhang2b79af12016-02-10 18:47:06 -08004333
4334 if (DEBUG_STATES && !r.visible) {
4335 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + r
4336 + " called by " + Debug.getCallers(4));
4337 }
4338
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004339 relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004340 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004341 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004342 "Config is relaunching non-resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004343 relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004344 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004345
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004346 // All done... tell the caller we weren't able to keep this activity around.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004347 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004348 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004349
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004350 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004351 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004352 // changes is always sent to all processes when they happen so it can just use whatever
4353 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004354 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004355 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004356
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004357 return true;
4358 }
4359
Filip Gruszczynski23493322015-07-29 17:02:59 -07004360 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4361 Configuration oldTaskOverride) {
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08004362
4363 // If we went from full-screen to non-full-screen, make sure to use the correct
4364 // configuration task diff, so the diff stays as small as possible.
4365 if (Configuration.EMPTY.equals(oldTaskOverride)
4366 && !Configuration.EMPTY.equals(taskConfig)) {
4367 oldTaskOverride = record.task.extractOverrideConfig(record.configuration);
4368 }
4369
Jorim Jaggi1b025a62016-02-03 19:27:49 -08004370 // Conversely, do the same when going the other direction.
4371 if (Configuration.EMPTY.equals(taskConfig)
4372 && !Configuration.EMPTY.equals(oldTaskOverride)) {
4373 taskConfig = record.task.extractOverrideConfig(record.configuration);
4374 }
4375
Filip Gruszczynski23493322015-07-29 17:02:59 -07004376 // Determine what has changed. May be nothing, if this is a config
4377 // that has come back from the app after going idle. In that case
4378 // we just want to leave the official config object now in the
4379 // activity and do nothing else.
4380 int taskChanges = oldTaskOverride.diff(taskConfig);
4381 // We don't want to use size changes if they don't cross boundaries that are important to
4382 // the app.
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004383 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004384 final boolean crosses = record.crossesHorizontalSizeThreshold(
4385 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4386 || record.crossesVerticalSizeThreshold(
4387 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4388 if (!crosses) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004389 taskChanges &= ~CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004390 }
4391 }
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004392 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004393 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4394 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004395 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004396 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004397 }
4398 }
4399 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4400 }
4401
4402 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4403 Configuration oldTaskOverride, int taskChanges) {
4404 if (taskChanges == 0) {
4405 // {@link Configuration#diff} doesn't catch changes from unset values.
4406 // Check for changes we care about.
4407 if (oldTaskOverride.orientation != taskConfig.orientation) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004408 taskChanges |= CONFIG_ORIENTATION;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004409 }
4410 // We want to explicitly track situations where the size configuration goes from
4411 // undefined to defined. We don't care about crossing the threshold in that case,
4412 // because there is no threshold.
4413 final int oldHeight = oldTaskOverride.screenHeightDp;
4414 final int newHeight = taskConfig.screenHeightDp;
4415 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4416 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4417 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004418 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004419 }
4420 final int oldWidth = oldTaskOverride.screenWidthDp;
4421 final int newWidth = taskConfig.screenWidthDp;
4422 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4423 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4424 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004425 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004426 }
4427 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4428 final int newSmallest = taskConfig.smallestScreenWidthDp;
4429 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4430 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4431 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004432 taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4433 }
4434 final int oldLayout = oldTaskOverride.screenLayout;
4435 final int newLayout = taskConfig.screenLayout;
4436 if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4437 || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4438 taskChanges |= CONFIG_SCREEN_LAYOUT;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004439 }
4440 }
4441 return taskChanges;
4442 }
4443
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004444 private static boolean isResizeOnlyChange(int change) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004445 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4446 | CONFIG_SCREEN_LAYOUT)) == 0;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004447 }
4448
Wale Ogunwale83301a92015-09-24 15:54:08 -07004449 private void relaunchActivityLocked(
4450 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4451 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004452 r.configChangeFlags = 0;
Wale Ogunwale83301a92015-09-24 15:54:08 -07004453 return;
4454 }
4455
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004456 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004457 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004458 if (andResume) {
4459 results = r.results;
4460 newIntents = r.newIntents;
4461 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004462 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4463 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004464 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004465 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004466 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004467 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004468
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004469 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004470
Craig Mautner34b73df2014-01-12 21:11:08 -08004471 mStackSupervisor.removeChildActivityContainers(r);
4472
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004473 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004474 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08004475 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r
4476 + " callers=" + Debug.getCallers(6));
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004477 r.forceNewConfig = false;
Chong Zhangd78ddb42016-03-02 17:01:14 -08004478 mStackSupervisor.activityRelaunchingLocked(r);
Wale Ogunwale60454db2015-01-23 16:05:07 -08004479 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4480 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004481 new Configuration(r.task.mOverrideConfig), preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004482 // Note: don't need to call pauseIfSleepingLocked() here, because
4483 // the caller will only pass in 'andResume' if this activity is
4484 // currently resumed, which implies we aren't sleeping.
4485 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004486 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004487 }
4488
4489 if (andResume) {
Chong Zhang2b79af12016-02-10 18:47:06 -08004490 if (DEBUG_STATES) {
4491 Slog.d(TAG_STATES, "Resumed after relaunch " + r);
4492 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004493 r.state = ActivityState.RESUMED;
Chong Zhang2b79af12016-02-10 18:47:06 -08004494 // Relaunch-resume could happen either when the app is already in the front,
4495 // or while it's being brought to front. In the latter case, it's marked RESUMED
4496 // but not yet visible (or stopped). We need to complete the resume here as the
4497 // code in resumeTopActivityInnerLocked to complete the resume might be skipped.
4498 if (!r.visible || r.stopped) {
4499 mWindowManager.setAppVisibility(r.appToken, true);
4500 completeResumeLocked(r);
4501 } else {
4502 r.results = null;
4503 r.newIntents = null;
4504 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004505 } else {
4506 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4507 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004508 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004509
4510 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004511 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004512
4513 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004514 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4515 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4516 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4517 final ActivityRecord r = activities.get(activityNdx);
4518 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004519 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004520 }
4521 if (r.fullscreen && !r.finishing) {
4522 return false;
4523 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004524 }
4525 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004526 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004527 if (r == null) {
4528 return false;
4529 }
4530 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4531 + " would have returned true for r=" + r);
4532 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004533 }
4534
4535 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004536 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4537 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4538 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4539 final ActivityRecord r = activities.get(activityNdx);
4540 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004541 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004542 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004543 }
4544 }
4545 }
4546
Wale Ogunwale540e1232015-05-01 15:35:39 -07004547 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4548 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004549 boolean didSomething = false;
4550 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004551 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004552 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4553 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4554 int numActivities = activities.size();
4555 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4556 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004557 final boolean sameComponent =
4558 (r.packageName.equals(packageName) && (filterByClasses == null
4559 || filterByClasses.contains(r.realActivity.getClassName())))
4560 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004561 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004562 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004563 && (r.app == null || evenPersistent || !r.app.persistent)) {
4564 if (!doit) {
4565 if (r.finishing) {
4566 // If this activity is just finishing, then it is not
4567 // interesting as far as something to stop.
4568 continue;
4569 }
4570 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004571 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004572 if (r.isHomeActivity()) {
4573 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4574 Slog.i(TAG, "Skip force-stop again " + r);
4575 continue;
4576 } else {
4577 homeActivity = r.realActivity;
4578 }
4579 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004580 didSomething = true;
4581 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004582 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004583 if (r.app != null) {
4584 r.app.removed = true;
4585 }
4586 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004587 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004588 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004589 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4590 true)) {
4591 // r has been deleted from mActivities, accommodate.
4592 --numActivities;
4593 --activityNdx;
4594 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004595 }
4596 }
4597 }
4598 return didSomething;
4599 }
4600
Dianne Hackborn09233282014-04-30 11:33:59 -07004601 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004602 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4603 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004604 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004605 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004606 if (task.getTopActivity() == null) {
4607 continue;
4608 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004609 ActivityRecord r = null;
4610 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004611 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004612 int numActivities = 0;
4613 int numRunning = 0;
4614 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004615 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004616 continue;
4617 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004618 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004619 tmp = activities.get(activityNdx);
4620 if (tmp.finishing) {
4621 continue;
4622 }
4623 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004624
Craig Mautneraab647e2013-02-28 16:31:36 -08004625 // Initialize state for next task if needed.
4626 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4627 top = r;
4628 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004629 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004630
4631 // Add 'r' into the current task.
4632 numActivities++;
4633 if (r.app != null && r.app.thread != null) {
4634 numRunning++;
4635 }
4636
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004637 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004638 TAG, r.intent.getComponent().flattenToShortString()
4639 + ": task=" + r.task);
4640 }
4641
4642 RunningTaskInfo ci = new RunningTaskInfo();
4643 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004644 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004645 ci.baseActivity = r.intent.getComponent();
4646 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004647 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004648 if (focusedStack && topTask) {
4649 // Give the latest time to ensure foreground task can be sorted
4650 // at the first, because lastActiveTime of creating task is 0.
4651 ci.lastActiveTime = System.currentTimeMillis();
4652 topTask = false;
4653 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004654
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004655 if (top.task != null) {
4656 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004657 }
4658 ci.numActivities = numActivities;
4659 ci.numRunning = numRunning;
Wale Ogunwale21b60582016-01-27 12:34:16 -08004660 ci.isDockable = task.canGoInDockedStack();
Craig Mautneraab647e2013-02-28 16:31:36 -08004661 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004662 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004663 }
4664
4665 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004666 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004667 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004668 if (top >= 0) {
4669 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4670 int activityTop = activities.size() - 1;
4671 if (activityTop > 0) {
4672 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4673 "unhandled-back", true);
4674 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004675 }
4676 }
4677
Craig Mautner6b74cb52013-09-27 17:02:21 -07004678 /**
4679 * Reset local parameters because an app's activity died.
4680 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004681 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004682 */
4683 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004684 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004685 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004686 "App died while pausing: " + mPausingActivity);
4687 mPausingActivity = null;
4688 }
4689 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4690 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004691 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004692 }
4693
Craig Mautner19091252013-10-05 00:03:53 -07004694 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004695 }
4696
Craig Mautnercae015f2013-02-08 14:31:27 -08004697 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004698 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4699 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4700 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4701 final ActivityRecord r = activities.get(activityNdx);
4702 if (r.app == app) {
4703 Slog.w(TAG, " Force finishing activity "
4704 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004705 // Force the destroy to skip right to removal.
4706 r.app = null;
4707 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004708 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004709 }
4710 }
4711 }
4712
Dianne Hackborn390517b2013-05-30 15:03:32 -07004713 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004714 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004715 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004716 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4717 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004718 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4719 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004720 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004721 " Task id #" + task.taskId + "\n" +
4722 " mFullscreen=" + task.mFullscreen + "\n" +
4723 " mBounds=" + task.mBounds + "\n" +
4724 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004725 if (printed) {
4726 header = null;
4727 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004728 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004729 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004730 }
4731
4732 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4733 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4734
4735 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004736 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4737 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004738 }
4739 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004740 final int top = mTaskHistory.size() - 1;
4741 if (top >= 0) {
4742 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4743 int listTop = list.size() - 1;
4744 if (listTop >= 0) {
4745 activities.add(list.get(listTop));
4746 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004747 }
4748 } else {
4749 ItemMatcher matcher = new ItemMatcher();
4750 matcher.build(name);
4751
Craig Mautneraab647e2013-02-28 16:31:36 -08004752 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4753 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4754 if (matcher.match(r1, r1.intent.getComponent())) {
4755 activities.add(r1);
4756 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004757 }
4758 }
4759 }
4760
4761 return activities;
4762 }
4763
4764 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004765 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004766
4767 // All activities that came from the package must be
4768 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004769 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4770 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4771 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4772 final ActivityRecord a = activities.get(activityNdx);
4773 if (a.info.packageName.equals(packageName)) {
4774 a.forceNewConfig = true;
4775 if (starting != null && a == starting && a.visible) {
4776 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004777 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08004778 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004779 }
4780 }
4781 }
4782
4783 return starting;
4784 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004785
Craig Mautner299f9602015-01-26 09:47:33 -08004786 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004787 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004788 }
4789
Wale Ogunwale000957c2015-04-03 08:19:12 -07004790 /**
4791 * Removes the input task from this stack.
4792 * @param task to remove.
4793 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004794 * @param moving task to another stack. In the case we are moving we don't want to perform
4795 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004796 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004797 void removeTask(TaskRecord task, String reason, boolean moving) {
4798 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004799 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004800 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004801 if (!StackId.persistTaskBounds(mStackId)) {
4802 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4803 // default configuration the next time it launches.
4804 task.updateOverrideConfiguration(null);
4805 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004806 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004807
Craig Mautner04a0ea62014-01-13 12:51:26 -08004808 final ActivityRecord r = mResumedActivity;
4809 if (r != null && r.task == task) {
4810 mResumedActivity = null;
4811 }
4812
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004813 final int taskNdx = mTaskHistory.indexOf(task);
4814 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004815 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4816 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4817 if (!nextTask.isOverHomeStack()) {
4818 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4819 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004820 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004821 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004822 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004823
Wale Ogunwale040b4702015-08-06 18:10:50 -07004824 if (!moving && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004825 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07004826 final boolean isVoiceSession = task.voiceSession != null;
4827 if (isVoiceSession) {
4828 try {
4829 task.voiceSession.taskFinished(task.intent, task.taskId);
4830 } catch (RemoteException e) {
4831 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004832 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004833 if (task.autoRemoveFromRecents() || isVoiceSession) {
4834 // Task creator asked to remove this when done, or this task was a voice
4835 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004836 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004837 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004838 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004839 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004840
4841 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004842 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004843 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004844 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004845 String myReason = reason + " leftTaskHistoryEmpty";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08004846 if (mFullscreen || !adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004847 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004848 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004849 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004850 if (mStacks != null) {
4851 mStacks.remove(this);
4852 mStacks.add(0, this);
4853 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004854 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004855 mActivityContainer.onTaskListEmptyLocked();
4856 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004857 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004858
4859 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004860 }
4861
Dianne Hackborn91097de2014-04-04 18:02:06 -07004862 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4863 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4864 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004865 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4866 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004867 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08004868 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08004869 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004870 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.isResizeable()
Chong Zhang75b37202015-12-04 14:16:36 -08004871 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004872 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004873 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004874 return task;
4875 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004876
Wale Ogunwale935e5022015-11-10 12:36:10 -08004877 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4878 if (mTaskPositioner == null) {
4879 return false;
4880 }
4881 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4882 return true;
4883 }
4884
Craig Mautnerc00204b2013-03-05 15:02:14 -08004885 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004886 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004887 }
4888
Wale Ogunwale5f986092015-12-04 15:35:38 -08004889 void addTask(final TaskRecord task, final boolean toTop, String reason) {
4890 final ActivityStack prevStack = preAddTask(task, reason);
4891
Craig Mautnerc00204b2013-03-05 15:02:14 -08004892 task.stack = this;
4893 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004894 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004895 } else {
4896 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004897 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004898 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08004899 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004900 }
4901
Wale Ogunwale5f986092015-12-04 15:35:38 -08004902 void positionTask(final TaskRecord task, int position) {
4903 final ActivityStack prevStack = preAddTask(task, "positionTask");
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004904 task.stack = this;
4905 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004906 postAddTask(task, prevStack);
4907 }
4908
4909 private ActivityStack preAddTask(TaskRecord task, String reason) {
4910 final ActivityStack prevStack = task.stack;
4911 if (prevStack != null && prevStack != this) {
4912 prevStack.removeTask(task, reason, MOVING);
4913 }
4914 return prevStack;
4915 }
4916
4917 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
4918 if (prevStack != null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004919 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004920 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004921 try {
4922 task.voiceSession.taskStarted(task.intent, task.taskId);
4923 } catch (RemoteException e) {
4924 }
4925 }
4926 }
4927
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004928 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004929 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004930 // TODO: VI deal with activity
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004931 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4932 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6cae7652015-12-26 07:36:26 -08004933 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
4934 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004935 task.mResizeMode, r.isAlwaysFocusable(), task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004936 r.taskConfigOverride = task.mOverrideConfig;
4937 }
4938
Wale Ogunwaled046a012015-12-24 13:05:59 -08004939 void moveToFrontAndResumeStateIfNeeded(
4940 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
4941 if (!moveToFront) {
4942 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07004943 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004944
4945 // If the activity owns the last resumed activity, transfer that together,
4946 // so that we don't resume the same activity again in the new stack.
4947 // Apps may depend on onResume()/onPause() being called in pairs.
4948 if (setResume) {
4949 mResumedActivity = r;
4950 }
4951 // Move the stack in which we are placing the activity to the front. The call will also
4952 // make sure the activity focus is set.
4953 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004954 }
4955
4956 /**
4957 * Moves the input activity from its current stack to this one.
4958 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4959 * created on this stack which the activity is added to.
4960 * */
4961 void moveActivityToStack(ActivityRecord r) {
4962 final ActivityStack prevStack = r.task.stack;
4963 if (prevStack.mStackId == mStackId) {
4964 // You are already in the right stack silly...
4965 return;
4966 }
4967
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004968 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004969 && (mStackSupervisor.topRunningActivityLocked() == r);
4970 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4971
4972 final TaskRecord task = createTaskRecord(
Suprabh Shukla09a88f52015-12-02 14:36:31 -08004973 mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
4974 r.info, r.intent, null, null, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004975 r.setTask(task, null);
4976 task.addActivityToTop(r);
4977 setAppTask(r, task);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004978 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004979 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4980 if (wasResumed) {
4981 prevStack.mResumedActivity = null;
4982 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07004983 }
4984
Wale Ogunwale706ed792015-08-02 10:29:44 -07004985 private void setAppTask(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004986 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004987 mWindowManager.setAppTask(r.appToken, task.taskId, mStackId, bounds, task.mOverrideConfig,
4988 task.mResizeMode, task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004989 r.taskConfigOverride = task.mOverrideConfig;
4990 }
4991
Craig Mautnerc00204b2013-03-05 15:02:14 -08004992 public int getStackId() {
4993 return mStackId;
4994 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004995
4996 @Override
4997 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004998 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4999 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07005000 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08005001
Craig Mautner15df08a2015-04-01 12:17:18 -07005002 void onLockTaskPackagesUpdatedLocked() {
5003 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
5004 mTaskHistory.get(taskNdx).setLockTaskAuth();
5005 }
5006 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005007}