blob: 769bee4cff6e25e43760e3197c2d9688fb2e9441 [file] [log] [blame]
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
22import static android.app.ActivityManager.StackId.HOME_STACK_ID;
23import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080024import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
25import static android.content.pm.ActivityInfo.CONFIG_SCREEN_LAYOUT;
26import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
27import static android.content.pm.ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080028import static android.content.pm.ActivityInfo.FLAG_RESUME_WHILE_PAUSING;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070029import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070030
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080031import static android.content.res.Configuration.SCREENLAYOUT_UNDEFINED;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080032import static com.android.server.am.ActivityManagerDebugConfig.*;
Chong Zhang75b37202015-12-04 14:16:36 -080033import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
Craig Mautner84984fa2014-06-19 11:19:20 -070034import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
35import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070036
Wale Ogunwalef40c11b2016-02-26 08:16:02 -080037import static com.android.server.am.ActivityRecord.STARTING_WINDOW_REMOVED;
38import static com.android.server.am.ActivityRecord.STARTING_WINDOW_SHOWN;
Wale Ogunwale39381972015-12-17 17:15:29 -080039import static com.android.server.am.ActivityStackSupervisor.FindTaskResult;
Wale Ogunwale040b4702015-08-06 18:10:50 -070040import static com.android.server.am.ActivityStackSupervisor.MOVING;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070041import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwale040b4702015-08-06 18:10:50 -070042
Winsonc809cbb2015-11-02 12:06:15 -080043import android.graphics.Point;
Wale Ogunwale706ed792015-08-02 10:29:44 -070044import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070045import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070046
Dianne Hackborn91097de2014-04-04 18:02:06 -070047import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080048import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070049import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070050import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080051import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080052import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080053import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080054import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070055import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070056
57import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070058import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070059import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070060import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070061import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080062import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080064import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070067import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070068import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080069import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080070import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070071import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070072import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080073import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070074import android.os.Handler;
75import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090076import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070078import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070079import android.os.RemoteException;
80import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080081import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070082import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070083import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070084import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070085import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070086import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070087
Craig Mautnercae015f2013-02-08 14:31:27 -080088import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080089import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070090import java.lang.ref.WeakReference;
91import java.util.ArrayList;
92import java.util.Iterator;
93import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080094import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070095import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070096
97/**
98 * State and management of a single stack of activities.
99 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700100final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800101
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800102 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700103 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
104 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800105 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700106 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700107 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700108 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700109 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700110 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700111 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
112 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700113 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700114 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700115 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
116 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
117 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
118 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
119 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700120
121 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800122
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700123 // Ticks during which we check progress while waiting for an app to launch.
124 static final int LAUNCH_TICK = 500;
125
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700126 // How long we wait until giving up on the last activity to pause. This
127 // is short because it directly impacts the responsiveness of starting the
128 // next activity.
129 static final int PAUSE_TIMEOUT = 500;
130
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700131 // How long we wait for the activity to tell us it has stopped before
132 // giving up. This is a good amount of time because we really need this
133 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700134 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700135
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700136 // How long we wait until giving up on an activity telling us it has
137 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700138 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800139
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700140 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800141 // disabled.
142 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800143
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700144 // How long between activity launches that we consider safe to not warn
145 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700146 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800147
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700148 // Set to false to disable the preview that is shown while a new activity
149 // is being started.
150 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800151
Craig Mautner5eda9b32013-07-02 11:58:16 -0700152 // How long to wait for all background Activities to redraw following a call to
153 // convertToTranslucent().
154 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
155
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800156 // How many activities have to be scheduled to stop to force a stop pass.
157 private static final int MAX_STOPPING_TO_FORCE = 3;
158
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700159 enum ActivityState {
160 INITIALIZING,
161 RESUMED,
162 PAUSING,
163 PAUSED,
164 STOPPING,
165 STOPPED,
166 FINISHING,
167 DESTROYING,
168 DESTROYED
169 }
170
Filip Gruszczynski0e381e22016-01-14 16:31:33 -0800171 // Stack is not considered visible.
172 static final int STACK_INVISIBLE = 0;
173 // Stack is considered visible
174 static final int STACK_VISIBLE = 1;
175 // Stack is considered visible, but only becuase it has activity that is visible behind other
176 // activities and there is a specific combination of stacks.
177 static final int STACK_VISIBLE_ACTIVITY_BEHIND = 2;
178
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700179 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700180 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800181 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800182
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700183 /**
184 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800185 * running) activities. It contains #TaskRecord objects.
186 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800187 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800188
189 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800190 * Used for validating app tokens with window manager.
191 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800192 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800193
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700194 /**
195 * List of running activities, sorted by recent usage.
196 * The first entry in the list is the least recently used.
197 * It contains HistoryRecord objects.
198 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800199 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700200
201 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700202 * Animations that for the current transition have requested not to
203 * be considered for the transition animation.
204 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800205 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700206
207 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700208 * When we are in the process of pausing an activity, before starting the
209 * next one, this variable holds the activity that is currently being paused.
210 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800211 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700212
213 /**
214 * This is the last activity that we put into the paused state. This is
215 * used to determine if we need to do an activity transition while sleeping,
216 * when we normally hold the top activity paused.
217 */
218 ActivityRecord mLastPausedActivity = null;
219
220 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700221 * Activities that specify No History must be removed once the user navigates away from them.
222 * If the device goes to sleep with such an activity in the paused state then we save it here
223 * and finish it later if another activity replaces it on wakeup.
224 */
225 ActivityRecord mLastNoHistoryActivity = null;
226
227 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700228 * Current activity that is resumed, or null if there is none.
229 */
230 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800231
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700232 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700233 * This is the last activity that has been started. It is only used to
234 * identify when multiple activities are started at once so that the user
235 * can be warned they may not be in the activity they think they are.
236 */
237 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800238
Craig Mautner5eda9b32013-07-02 11:58:16 -0700239 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
240 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
241 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
242 // Activity in mTranslucentActivityWaiting is notified via
243 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
244 // background activity being drawn then the same call will be made with a true value.
245 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700246 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700247
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700248 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700249 * Set when we know we are going to be calling updateConfiguration()
250 * soon, so want to skip intermediate config checks.
251 */
252 boolean mConfigWillChange;
253
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700254 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800255 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700256 // Current bounds of the stack or null if fullscreen.
257 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800258
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700259 long mLaunchStartTime = 0;
260 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800261
Craig Mautner858d8a62013-04-23 17:08:34 -0700262 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700263
Craig Mautnerc00204b2013-03-05 15:02:14 -0800264 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800265 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800266 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
267 ArrayList<ActivityStack> mStacks;
268 /** The attached Display's unique identifier, or -1 if detached */
269 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800270
Craig Mautner27084302013-03-25 08:05:25 -0700271 /** Run all ActivityStacks through this */
272 final ActivityStackSupervisor mStackSupervisor;
273
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700274 private final LaunchingTaskPositioner mTaskPositioner;
275
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700276 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700277 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
278 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
279 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
280 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700281 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700282 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700283 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700284
285 static class ScheduleDestroyArgs {
286 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700287 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700288 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700289 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700290 mReason = reason;
291 }
292 }
293
Zoran Marcetaf958b322012-08-09 20:27:12 +0900294 final Handler mHandler;
295
296 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800297
Craig Mautnerc8143c62013-09-03 12:15:57 -0700298 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900299 super(looper);
300 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700301
Zoran Marcetaf958b322012-08-09 20:27:12 +0900302 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700303 public void handleMessage(Message msg) {
304 switch (msg.what) {
305 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800306 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700307 // We don't at this point know if the activity is fullscreen,
308 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800309 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700310 synchronized (mService) {
311 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700312 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700313 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700314 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800315 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700316 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700317 case LAUNCH_TICK_MSG: {
318 ActivityRecord r = (ActivityRecord)msg.obj;
319 synchronized (mService) {
320 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700321 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700322 }
323 }
324 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700325 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800326 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700327 // We don't at this point know if the activity is fullscreen,
328 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800329 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800330 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800331 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800332 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700333 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700334 case STOP_TIMEOUT_MSG: {
335 ActivityRecord r = (ActivityRecord)msg.obj;
336 // We don't at this point know if the activity is fullscreen,
337 // so we need to be conservative and assume it isn't.
338 Slog.w(TAG, "Activity stop timeout for " + r);
339 synchronized (mService) {
340 if (r.isInHistory()) {
341 activityStoppedLocked(r, null, null, null);
342 }
343 }
344 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700345 case DESTROY_ACTIVITIES_MSG: {
346 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
347 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700348 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700349 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700350 } break;
351 case TRANSLUCENT_TIMEOUT_MSG: {
352 synchronized (mService) {
353 notifyActivityDrawnLocked(null);
354 }
355 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700356 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700357 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700358 final ActivityRecord r = getVisibleBehindActivity();
359 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700360 if (r != null) {
361 mService.killAppAtUsersRequest(r.app, null);
362 }
363 }
364 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700365 }
366 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800367 }
368
Craig Mautner34b73df2014-01-12 21:11:08 -0800369 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800370 int count = 0;
371 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
372 count += mTaskHistory.get(taskNdx).mActivities.size();
373 }
374 return count;
375 }
376
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800377 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
378 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800379 mActivityContainer = activityContainer;
380 mStackSupervisor = activityContainer.getOuter();
381 mService = mStackSupervisor.mService;
382 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
383 mWindowManager = mService.mWindowManager;
384 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700385 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800386 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700387 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
388 ? new LaunchingTaskPositioner() : null;
389 }
390
391 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
392 mDisplayId = activityDisplay.mDisplayId;
393 mStacks = activityDisplay.mStacks;
394 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
395 mFullscreen = mBounds == null;
396 if (mTaskPositioner != null) {
397 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
398 mTaskPositioner.configure(mBounds);
399 }
Wale Ogunwale961f4852016-02-01 20:25:54 -0800400
401 if (mStackId == DOCKED_STACK_ID) {
402 // If we created a docked stack we want to resize it so it resizes all other stacks
403 // in the system.
404 mStackSupervisor.resizeDockedStackLocked(
405 mBounds, null, null, null, null, PRESERVE_WINDOWS);
406 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700407 }
408
409 void detachDisplay() {
410 mDisplayId = Display.INVALID_DISPLAY;
411 mStacks = null;
412 if (mTaskPositioner != null) {
413 mTaskPositioner.reset();
414 }
415 mWindowManager.detachStack(mStackId);
416 }
417
Winsonc809cbb2015-11-02 12:06:15 -0800418 public void getDisplaySize(Point out) {
419 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
420 }
421
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700422 void setBounds(Rect bounds) {
423 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700424 if (mTaskPositioner != null) {
425 mTaskPositioner.configure(bounds);
426 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700427 }
Craig Mautner5962b122012-10-05 14:45:52 -0700428
Amith Yamasani734983f2014-03-04 16:48:05 -0800429 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700430 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700431 }
432
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700433 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800434 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700435 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700436 if (r != null) {
437 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800438 }
439 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700440 return null;
441 }
442
443 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800444 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
445 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800446 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800447 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
448 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800449 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800450 return r;
451 }
452 }
453 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700454 return null;
455 }
456
457 /**
458 * This is a simplified version of topRunningActivityLocked that provides a number of
459 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800460 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700461 * @param token If non-null, any history records matching this token will be skipped.
462 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800463 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700464 * @return Returns the HistoryRecord of the next activity on the stack.
465 */
466 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800467 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
468 TaskRecord task = mTaskHistory.get(taskNdx);
469 if (task.taskId == taskId) {
470 continue;
471 }
472 ArrayList<ActivityRecord> activities = task.mActivities;
473 for (int i = activities.size() - 1; i >= 0; --i) {
474 final ActivityRecord r = activities.get(i);
475 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800476 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800477 return r;
478 }
479 }
480 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700481 return null;
482 }
483
Craig Mautner8849a5e2013-04-02 16:41:03 -0700484 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700485 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
486 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700487 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
488 final ActivityRecord r = activities.get(activityNdx);
489 if (!r.finishing) {
490 return r;
491 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700492 }
493 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700494 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700495 }
496
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700497 final TaskRecord topTask() {
498 final int size = mTaskHistory.size();
499 if (size > 0) {
500 return mTaskHistory.get(size - 1);
501 }
502 return null;
503 }
504
Craig Mautnerd2328952013-03-05 12:46:26 -0800505 TaskRecord taskForIdLocked(int id) {
506 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
507 final TaskRecord task = mTaskHistory.get(taskNdx);
508 if (task.taskId == id) {
509 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800510 }
511 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700512 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700513 }
514
Craig Mautnerd2328952013-03-05 12:46:26 -0800515 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700516 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800517 return isInStackLocked(r);
518 }
519
520 ActivityRecord isInStackLocked(ActivityRecord r) {
521 if (r == null) {
522 return null;
523 }
524 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700525 if (task != null && task.stack != null
526 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800527 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800528 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800529 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800530 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800531 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800532 }
533
Craig Mautner2420ead2013-04-01 17:13:20 -0700534 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700535 final boolean hadit = mLRUActivities.remove(r);
536 mLRUActivities.add(r);
537 return hadit;
538 }
539
Craig Mautnerde4ef022013-04-07 19:01:33 -0700540 final boolean isHomeStack() {
541 return mStackId == HOME_STACK_ID;
542 }
543
Winson Chung0583d3d2015-12-18 10:03:12 -0500544 final boolean isDockedStack() {
545 return mStackId == DOCKED_STACK_ID;
546 }
547
548 final boolean isPinnedStack() {
549 return mStackId == PINNED_STACK_ID;
550 }
551
Craig Mautnere0a38842013-12-16 16:14:02 -0800552 final boolean isOnHomeDisplay() {
553 return isAttached() &&
554 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
555 }
556
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700557 void moveToFront(String reason) {
558 moveToFront(reason, null);
559 }
560
561 /**
562 * @param reason The reason for moving the stack to the front.
563 * @param task If non-null, the task will be moved to the top of the stack.
564 * */
565 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700566 if (!isAttached()) {
567 return;
568 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700569
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700570 mStacks.remove(this);
571 int addIndex = mStacks.size();
572
573 if (addIndex > 0) {
574 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800575 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
576 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700577 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800578 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700579 }
580
581 mStacks.add(addIndex, this);
582
583 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
584 if (isOnHomeDisplay()) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800585 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700586 }
587 if (task != null) {
588 insertTaskAtTop(task, null);
589 } else {
590 task = topTask();
591 }
592 if (task != null) {
593 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800594 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800595 }
596
Wale Ogunwaled046a012015-12-24 13:05:59 -0800597 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800598 if (StackId.canReceiveKeys(mStackId)) {
599 return true;
600 }
601 // The stack isn't focusable. See if its top activity is focusable to force focus on the
602 // stack.
603 final ActivityRecord r = topRunningActivityLocked();
604 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800605 }
606
Craig Mautnere0a38842013-12-16 16:14:02 -0800607 final boolean isAttached() {
608 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800609 }
610
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700611 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800612 * Returns the top activity in any existing task matching the given Intent in the input result.
613 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700614 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800615 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700616 Intent intent = target.intent;
617 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700618 ComponentName cls = intent.getComponent();
619 if (info.targetActivity != null) {
620 cls = new ComponentName(info.packageName, info.targetActivity);
621 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700622 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700623 boolean isDocument = intent != null & intent.isDocument();
624 // If documentData is non-null then it must match the existing task data.
625 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800626
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700627 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800628 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
629 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700630 if (task.voiceSession != null) {
631 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700632 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700633 continue;
634 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700635 if (task.userId != userId) {
636 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700637 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700638 continue;
639 }
Craig Mautner000f0022013-02-26 15:04:29 -0800640 final ActivityRecord r = task.getTopActivity();
641 if (r == null || r.finishing || r.userId != userId ||
642 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700643 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800644 continue;
645 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700646 if (r.mActivityType != target.mActivityType) {
647 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
648 continue;
649 }
Craig Mautner000f0022013-02-26 15:04:29 -0800650
Craig Mautnerd00f4742014-03-12 14:17:26 -0700651 final Intent taskIntent = task.intent;
652 final Intent affinityIntent = task.affinityIntent;
653 final boolean taskIsDocument;
654 final Uri taskDocumentData;
655 if (taskIntent != null && taskIntent.isDocument()) {
656 taskIsDocument = true;
657 taskDocumentData = taskIntent.getData();
658 } else if (affinityIntent != null && affinityIntent.isDocument()) {
659 taskIsDocument = true;
660 taskDocumentData = affinityIntent.getData();
661 } else {
662 taskIsDocument = false;
663 taskDocumentData = null;
664 }
665
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700666 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700667 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700668 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700669 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Wale Ogunwale39381972015-12-17 17:15:29 -0800670 if (!isDocument && !taskIsDocument
671 && result.r == null && task.canMatchRootAffinity()) {
Dianne Hackborn79228822014-09-16 11:11:23 -0700672 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwale39381972015-12-17 17:15:29 -0800673 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
674 // It is possible for multiple tasks to have the same root affinity especially
675 // if they are in separate stacks. We save off this candidate, but keep looking
676 // to see if there is a better candidate.
677 result.r = r;
678 result.matchedByRootAffinity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700679 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700680 } else if (taskIntent != null && taskIntent.getComponent() != null &&
681 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700682 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700683 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800684 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700685 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
686 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800687 result.r = r;
688 result.matchedByRootAffinity = false;
689 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700690 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
691 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700692 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700693 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800694 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700695 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
696 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800697 result.r = r;
698 result.matchedByRootAffinity = false;
699 break;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700700 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700701 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700702 }
703
704 /**
705 * Returns the first activity (starting from the top of the stack) that
706 * is the same as the given activity. Returns null if no such activity
707 * is found.
708 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700709 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700710 ComponentName cls = intent.getComponent();
711 if (info.targetActivity != null) {
712 cls = new ComponentName(info.packageName, info.targetActivity);
713 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700714 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700715
Craig Mautner000f0022013-02-26 15:04:29 -0800716 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700717 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700718 final boolean notCurrentUserTask =
719 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700720 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700721
Craig Mautner000f0022013-02-26 15:04:29 -0800722 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
723 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700724 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700725 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700726 }
Craig Mautner000f0022013-02-26 15:04:29 -0800727 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700728 return r;
729 }
730 }
731 }
732
733 return null;
734 }
735
Amith Yamasani742a6712011-05-04 14:49:28 -0700736 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700737 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700738 */
Craig Mautner93529a42013-10-04 15:03:13 -0700739 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800740 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700741 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800742 }
743 mCurrentUser = userId;
744
745 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800746 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800747 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700748 final TaskRecord task = mTaskHistory.get(i);
749
750 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
751 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700752 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700753 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700754 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700755 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800756 mTaskHistory.remove(i);
757 mTaskHistory.add(task);
758 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800759 // Use same value for i.
760 } else {
761 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800762 }
763 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700764 if (VALIDATE_TOKENS) {
765 validateAppTokensLocked();
766 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700767 }
768
Craig Mautner2420ead2013-04-01 17:13:20 -0700769 void minimalResumeActivityLocked(ActivityRecord r) {
770 r.state = ActivityState.RESUMED;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800771 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + r + " (starting new instance)"
772 + " callers=" + Debug.getCallers(5));
Craig Mautner2420ead2013-04-01 17:13:20 -0700773 mResumedActivity = r;
774 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800775 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700776 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700777 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700778 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700779 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
780 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700781 }
782
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800783 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700784 if (r != null) {
785 mRecentTasks.addLocked(r.task);
786 r.task.touchActiveTime();
787 }
788 }
789
Narayan Kamath7829c812015-06-08 17:39:43 +0100790 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700791 if (mFullyDrawnStartTime != 0) {
792 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
793 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100794 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700795 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
796 }
797
798 private void stopFullyDrawnTraceIfNeeded() {
799 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
800 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
801 mFullyDrawnStartTime = 0;
802 }
803 }
804
Craig Mautnere79d42682013-04-01 19:01:53 -0700805 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700806 if (r.displayStartTime == 0) {
807 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
808 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100809 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700810 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700811 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700812 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100813 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700814 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700815 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700816 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800817
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700818 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700819 // Make sure that there is no activity waiting for this to launch.
820 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
821 r.displayStartTime = r.fullyDrawnStartTime = 0;
822 } else {
823 mStackSupervisor.removeTimeoutsForActivityLocked(r);
824 mStackSupervisor.scheduleIdleTimeoutLocked(r);
825 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700826 }
827
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800828 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800829 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800830 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
831 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
832 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
833 activities.get(activityNdx).setSleeping(false);
834 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800835 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800836 if (mPausingActivity != null) {
837 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
838 activityPausedLocked(mPausingActivity.appToken, true);
839 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800840 }
841
Craig Mautner0eea92c2013-05-16 13:35:39 -0700842 /**
843 * @return true if something must be done before going to sleep.
844 */
845 boolean checkReadyForSleepLocked() {
846 if (mResumedActivity != null) {
847 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700848 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
849 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
850 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700851 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700852 return true;
853 }
854 if (mPausingActivity != null) {
855 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700856 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700857 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800858 }
859
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700860 if (hasVisibleBehindActivity()) {
861 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800862 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700863 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800864 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700865 return true;
866 }
867
Craig Mautner0eea92c2013-05-16 13:35:39 -0700868 return false;
869 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800870
Craig Mautner0eea92c2013-05-16 13:35:39 -0700871 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700872 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800873
Craig Mautner0eea92c2013-05-16 13:35:39 -0700874 // Make sure any stopped but visible activities are now sleeping.
875 // This ensures that the activity's onStop() is called.
876 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
877 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
878 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
879 final ActivityRecord r = activities.get(activityNdx);
880 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
881 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800882 }
883 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800884 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700885 }
Craig Mautner59c00972012-07-30 12:10:24 -0700886
Winson8b1871d2015-11-20 09:56:20 -0800887 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
888 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800889 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700890 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800891 return null;
892 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800893
Winson Chung083baf92014-07-11 10:32:42 -0700894 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700895 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800896 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700897 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700898 return null;
899 }
900
Winson Chung48a10a52014-08-27 14:36:51 -0700901 int w = mService.mThumbnailWidth;
902 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800903 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800904 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700905 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800906
907 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800908 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
909 // use within SystemUI while keeping memory usage low.
Winson Chungead5c0f2015-12-14 11:18:57 -0500910 if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
Winson8b1871d2015-11-20 09:56:20 -0800911 w = h = -1;
Winson387aac62015-11-25 11:18:56 -0800912 scale = 0.5f;
Winson8b1871d2015-11-20 09:56:20 -0800913 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800914 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800915 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800916 }
Winson Chung376543b2014-05-21 17:43:28 -0700917 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800918 return null;
919 }
920
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700921 /**
922 * Start pausing the currently resumed activity. It is an error to call this if there
923 * is already an activity being paused or there is no resumed activity.
924 *
925 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
926 * @param uiSleeping True if this is happening with the user interface going to sleep (the
927 * screen turning off).
928 * @param resuming True if this is being called as part of resuming the top activity, so
929 * we shouldn't try to instigate a resume here.
930 * @param dontWait True if the caller does not want to wait for the pause to complete. If
931 * set to true, we will immediately complete the pause here before returning.
932 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
933 * it to tell us when it is done.
934 */
935 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
936 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800937 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800938 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
939 + " state=" + mPausingActivity.state);
940 if (!mService.isSleeping()) {
941 // Avoid recursion among check for sleep and complete pause during sleeping.
942 // Because activity will be paused immediately after resume, just let pause
943 // be completed by the order of activity paused from clients.
944 completePauseLocked(false);
945 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800946 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700947 ActivityRecord prev = mResumedActivity;
948 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700949 if (!resuming) {
950 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -0800951 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700952 }
953 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700954 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800955
956 if (mActivityContainer.mParentActivity == null) {
957 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700958 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800959 }
960
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700961 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700962 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700963 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800964 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700965 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700966 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
967 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700968 prev.state = ActivityState.PAUSING;
969 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700970 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700971 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700972 if (mService.mHasRecents
973 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800974 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700975 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700976 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700977
978 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800979
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700980 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700981 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700982 try {
983 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700984 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700985 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700986 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800987 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700988 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700989 } catch (Exception e) {
990 // Ignore exception, if process died other code will cleanup.
991 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800992 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700993 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700994 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700995 }
996 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800997 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700998 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700999 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001000 }
1001
1002 // If we are not going to sleep, we want to ensure the device is
1003 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001004 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001005 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001006 }
1007
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001008 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001009 // Have the window manager pause its key dispatching until the new
1010 // activity has started. If we're pausing the activity just because
1011 // the screen is being turned off and the UI is sleeping, don't interrupt
1012 // key dispatch; the same activity will pick it up again on wakeup.
1013 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001014 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001015 } else if (DEBUG_PAUSE) {
1016 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001017 }
1018
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001019 if (dontWait) {
1020 // If the caller said they don't want to wait for the pause, then complete
1021 // the pause now.
1022 completePauseLocked(false);
1023 return false;
1024
1025 } else {
1026 // Schedule a pause timeout in case the app doesn't respond.
1027 // We don't give it much time because this directly impacts the
1028 // responsiveness seen by the user.
1029 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1030 msg.obj = prev;
1031 prev.pauseTime = SystemClock.uptimeMillis();
1032 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001033 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001034 return true;
1035 }
1036
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001037 } else {
1038 // This activity failed to schedule the
1039 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001040 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001041 if (!resuming) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001042 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001043 }
1044 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001045 }
1046 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001047
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001048 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001049 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1050 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001051
Craig Mautnerd2328952013-03-05 12:46:26 -08001052 final ActivityRecord r = isInStackLocked(token);
1053 if (r != null) {
1054 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1055 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001056 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001057 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001058 completePauseLocked(true);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001059 return;
Craig Mautnerd2328952013-03-05 12:46:26 -08001060 } else {
1061 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1062 r.userId, System.identityHashCode(r), r.shortComponentName,
1063 mPausingActivity != null
1064 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001065 if (r.state == ActivityState.PAUSING) {
1066 r.state = ActivityState.PAUSED;
1067 if (r.finishing) {
1068 if (DEBUG_PAUSE) Slog.v(TAG,
1069 "Executing finish of failed to pause activity: " + r);
1070 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1071 }
louis_chang047dfd42015-04-08 16:35:55 +08001072 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001073 }
1074 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001075 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001076 }
1077
Craig Mautnera0026042014-04-23 11:45:37 -07001078 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1079 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001080 if (r.state != ActivityState.STOPPING) {
1081 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1082 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1083 return;
1084 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001085 if (persistentState != null) {
1086 r.persistentState = persistentState;
1087 mService.notifyTaskPersisterLocked(r.task, false);
1088 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001089 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001090 if (icicle != null) {
1091 // If icicle is null, this is happening due to a timeout, so we
1092 // haven't really saved the state.
1093 r.icicle = icicle;
1094 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001095 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001096 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001097 }
1098 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001099 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001100 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1101 r.stopped = true;
1102 r.state = ActivityState.STOPPED;
Robert Carre12aece2016-02-02 22:43:27 -08001103
1104 mWindowManager.notifyAppStopped(r.appToken);
1105
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001106 if (getVisibleBehindActivity() == r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001107 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001108 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001109 if (r.finishing) {
1110 r.clearOptionsLocked();
1111 } else {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001112 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001113 destroyActivityLocked(r, true, "stop-config");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001114 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001115 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001116 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001117 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001118 }
1119 }
1120 }
1121
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001122 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001123 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001124 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001125
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001126 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001127 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001128 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001129 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001130 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001131 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001132 } else if (prev.app != null) {
Wale Ogunwaled8026642016-02-09 20:40:18 -08001133 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueue pending stop if needed: " + prev
1134 + " wasStopping=" + wasStopping + " visible=" + prev.visible);
Craig Mautner8c14c152015-01-15 17:32:07 -08001135 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001136 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1137 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001138 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001139 if (prev.deferRelaunchUntilPaused) {
1140 // Complete the deferred relaunch that was waiting for pause to complete.
1141 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
1142 relaunchActivityLocked(prev, prev.configChangeFlags, false,
1143 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001144 } else if (wasStopping) {
1145 // We are also stopping, the stop request must have gone soon after the pause.
1146 // We can't clobber it, because the stop confirmation will not be handled.
1147 // We don't need to schedule another stop, we only need to let it happen.
1148 prev.state = ActivityState.STOPPING;
Wale Ogunwaled8026642016-02-09 20:40:18 -08001149 } else if ((!prev.visible && !hasVisibleBehindActivity())
1150 || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001151 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001152 // stopping.
Chong Zhang46b1ac62016-02-18 17:53:57 -08001153 addToStopping(prev, true /* immediate */);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001154 }
1155 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001156 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001157 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001158 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001159 // It is possible the activity was freezing the screen before it was paused.
1160 // In that case go ahead and remove the freeze this activity has on the screen
1161 // since it is no longer visible.
1162 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001163 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001164 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001165
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001166 if (resumeNext) {
1167 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1168 if (!mService.isSleepingOrShuttingDown()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001169 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001170 } else {
1171 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001172 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001173 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001174 // If there are no more activities available to run, do resume anyway to start
1175 // something. Also if the top activity on the stack is not the just paused
1176 // activity, we need to go ahead and resume it to ensure we complete an
1177 // in-flight app switch.
1178 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001179 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001180 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001181 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001182
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001183 if (prev != null) {
1184 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001185
Craig Mautner525f3d92013-05-07 14:01:50 -07001186 if (prev.app != null && prev.cpuTimeAtResume > 0
1187 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001188 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1189 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001190 if (diff > 0) {
1191 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1192 synchronized (bsi) {
1193 BatteryStatsImpl.Uid.Proc ps =
1194 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001195 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001196 if (ps != null) {
1197 ps.addForegroundTimeLocked(diff);
1198 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001199 }
1200 }
1201 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001202 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001203 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001204
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001205 // Notify when the task stack has changed, but only if visibilities changed (not just focus)
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001206 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause) {
1207 mService.notifyTaskStackChangedLocked();
1208 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1209 }
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08001210
1211 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001212 }
1213
Chong Zhang46b1ac62016-02-18 17:53:57 -08001214 private void addToStopping(ActivityRecord r, boolean immediate) {
1215 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1216 mStackSupervisor.mStoppingActivities.add(r);
1217 }
1218
1219 // If we already have a few activities waiting to stop, then give up
1220 // on things going idle and start clearing them out. Or if r is the
1221 // last of activity of the last task the stack will be empty and must
1222 // be cleared immediately.
1223 boolean forceIdle = mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE
1224 || (r.frontOfTask && mTaskHistory.size() <= 1);
1225
1226 if (immediate || forceIdle) {
1227 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Scheduling idle now: forceIdle="
1228 + forceIdle + "immediate=" + immediate);
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001229 mStackSupervisor.scheduleIdleLocked();
1230 } else {
1231 mStackSupervisor.checkReadyForSleepLocked();
1232 }
1233 }
1234
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001235 /**
1236 * Once we know that we have asked an application to put an activity in
1237 * the resumed state (either by launching it or explicitly telling it),
1238 * this function updates the rest of our state to match that fact.
1239 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001240 private void completeResumeLocked(ActivityRecord next) {
Chong Zhang2b79af12016-02-10 18:47:06 -08001241 next.visible = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001242 next.idle = false;
1243 next.results = null;
1244 next.newIntents = null;
Chong Zhang2b79af12016-02-10 18:47:06 -08001245 next.stopped = false;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001246
Chong Zhang85ee6542015-10-02 13:36:38 -07001247 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001248 ProcessRecord app = next.task.mActivities.get(0).app;
1249 if (app != null && app != mService.mHomeProcess) {
1250 mService.mHomeProcess = app;
1251 }
1252 }
1253
Craig Mautner07566322013-09-26 16:42:55 -07001254 if (next.nowVisible) {
1255 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001256 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001257 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001258
1259 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001260 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001261
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001262 mStackSupervisor.reportResumedActivityLocked(next);
1263
1264 next.resumeKeyDispatchingLocked();
1265 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001266
1267 // Mark the point when the activity is resuming
1268 // TODO: To be more accurate, the mark should be before the onCreate,
1269 // not after the onResume. But for subsequent starts, onResume is fine.
1270 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001271 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001272 } else {
1273 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1274 }
Winson Chung376543b2014-05-21 17:43:28 -07001275
George Mount6ba042b2014-07-28 11:12:28 -07001276 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001277
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001278 if (getVisibleBehindActivity() == next) {
Craig Mautner64ccb702014-10-01 09:38:40 -07001279 // When resuming an activity, require it to call requestVisibleBehind() again.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001280 setVisibleBehindActivity(null);
Craig Mautner64ccb702014-10-01 09:38:40 -07001281 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001282 }
1283
Craig Mautner2568c3a2015-03-26 14:22:34 -07001284 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001285 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001286 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1287 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1288 r.mUpdateTaskThumbnailWhenHidden = false;
1289 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001290 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001291 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001292 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001293 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001294 container.setVisible(visible);
1295 }
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001296 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001297 }
1298
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001299 // Find the first visible activity above the passed activity and if it is translucent return it
1300 // otherwise return null;
1301 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1302 TaskRecord task = r.task;
1303 if (task == null) {
1304 return null;
1305 }
1306
1307 ActivityStack stack = task.stack;
1308 if (stack == null) {
1309 return null;
1310 }
1311
1312 int stackNdx = mStacks.indexOf(stack);
1313
1314 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1315 int taskNdx = tasks.indexOf(task);
1316
1317 ArrayList<ActivityRecord> activities = task.mActivities;
1318 int activityNdx = activities.indexOf(r) + 1;
1319
1320 final int numStacks = mStacks.size();
1321 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001322 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001323 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001324 final int numTasks = tasks.size();
1325 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001326 final TaskRecord currentTask = tasks.get(taskNdx);
1327 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001328 final int numActivities = activities.size();
1329 while (activityNdx < numActivities) {
1330 final ActivityRecord activity = activities.get(activityNdx);
1331 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001332 return historyStack.mFullscreen
1333 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001334 }
1335 ++activityNdx;
1336 }
1337 activityNdx = 0;
1338 ++taskNdx;
1339 }
1340 taskNdx = 0;
1341 ++stackNdx;
1342 }
1343
1344 return null;
1345 }
1346
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001347 ActivityStack getNextFocusableStackLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001348 ArrayList<ActivityStack> stacks = mStacks;
1349 final ActivityRecord parent = mActivityContainer.mParentActivity;
1350 if (parent != null) {
1351 stacks = parent.task.stack.mStacks;
1352 }
1353 if (stacks != null) {
1354 for (int i = stacks.size() - 1; i >= 0; --i) {
1355 ActivityStack stack = stacks.get(i);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001356 if (stack != this && stack.isFocusable()
1357 && stack.getStackVisibilityLocked() != STACK_INVISIBLE) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001358 return stack;
1359 }
1360 }
1361 }
1362 return null;
1363 }
1364
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001365 /** Returns true if the stack contains a fullscreen task. */
1366 private boolean hasFullscreenTask() {
1367 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1368 final TaskRecord task = mTaskHistory.get(i);
1369 if (task.mFullscreen) {
1370 return true;
1371 }
1372 }
1373 return false;
1374 }
1375
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001376 private boolean hasTranslucentActivity(ActivityStack stack) {
1377 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1378 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1379 final TaskRecord task = tasks.get(taskNdx);
1380 final ArrayList<ActivityRecord> activities = task.mActivities;
1381 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1382 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001383
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001384 // Conditions for an activity to obscure the stack we're
1385 // examining:
1386 // 1. Not Finishing AND Visible AND:
1387 // 2. Either:
1388 // - Full Screen Activity OR
1389 // - On top of Home and our stack is NOT home
1390 if (!r.finishing && r.visible && (r.fullscreen ||
1391 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1392 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001393 }
1394 }
1395 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001396 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001397 }
1398
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001399 /**
1400 * Returns stack's visibility: {@link #STACK_INVISIBLE}, {@link #STACK_VISIBLE} or
1401 * {@link #STACK_VISIBLE_ACTIVITY_BEHIND}.
1402 */
1403 int getStackVisibilityLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001404 if (!isAttached()) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001405 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001406 }
1407
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001408 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001409 return STACK_VISIBLE;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001410 }
1411
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001412 final int stackIndex = mStacks.indexOf(this);
1413
1414 if (stackIndex == mStacks.size() - 1) {
1415 Slog.wtf(TAG,
1416 "Stack=" + this + " isn't front stack but is at the top of the stack list");
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001417 return STACK_INVISIBLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001418 }
1419
Chong Zhang75b37202015-12-04 14:16:36 -08001420 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1421 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001422 return STACK_INVISIBLE;
Chong Zhang75b37202015-12-04 14:16:36 -08001423 }
1424
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001425 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1426 final int focusedStackId = focusedStack.mStackId;
1427
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001428 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001429 && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID
1430 && !focusedStack.topActivity().fullscreen) {
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001431 // The fullscreen stack should be visible if it has a visible behind activity behind
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001432 // the home stack that is translucent.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001433 return STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001434 }
1435
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001436 if (mStackId == DOCKED_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001437 // Docked stack is always visible, except in the case where the top running activity
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001438 // task in the focus stack doesn't support any form of resizing but we show it for the
1439 // home task even though it's not resizable.
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001440 final ActivityRecord r = focusedStack.topRunningActivityLocked();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001441 final TaskRecord task = r != null ? r.task : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001442 return task == null || task.canGoInDockedStack() || task.isHomeTask() ? STACK_VISIBLE
1443 : STACK_INVISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001444 }
1445
Chong Zhangb16cf342015-11-12 17:22:40 -08001446 // Find the first stack below focused stack that actually got something visible.
1447 int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1448 while (belowFocusedIndex >= 0 &&
1449 mStacks.get(belowFocusedIndex).topRunningActivityLocked() == null) {
1450 belowFocusedIndex--;
1451 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001452 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1453 && stackIndex == belowFocusedIndex) {
1454 // Stacks directly behind the docked or pinned stack are always visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001455 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001456 }
1457
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001458 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1459 && hasTranslucentActivity(focusedStack)) {
1460 // Stacks behind the fullscreen stack with a translucent activity are always
1461 // visible so they can act as a backdrop to the translucent activity.
1462 // For example, dialog activities
1463 if (stackIndex == belowFocusedIndex) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001464 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001465 }
1466 if (belowFocusedIndex >= 0) {
1467 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001468 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1469 && stackIndex == (belowFocusedIndex - 1)) {
1470 // The stack behind the docked or pinned stack is also visible so we can have a
1471 // complete backdrop to the translucent activity when the docked stack is up.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001472 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001473 }
1474 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001475 }
1476
Wale Ogunwale3797c222015-10-27 14:21:58 -07001477 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001478 // Visibility of any static stack should have been determined by the conditions above.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001479 return STACK_INVISIBLE;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001480 }
1481
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001482 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001483 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001484
1485 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1486 continue;
1487 }
1488
Wale Ogunwale3797c222015-10-27 14:21:58 -07001489 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001490 // These stacks can't have any dynamic stacks visible behind them.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001491 return STACK_INVISIBLE;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001492 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001493
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001494 if (!hasTranslucentActivity(stack)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001495 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001496 }
1497 }
1498
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001499 return STACK_VISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001500 }
1501
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001502 final int rankTaskLayers(int baseLayer) {
1503 int layer = 0;
1504 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1505 final TaskRecord task = mTaskHistory.get(taskNdx);
1506 ActivityRecord r = task.topRunningActivityLocked();
1507 if (r == null || r.finishing || !r.visible) {
1508 task.mLayerRank = -1;
1509 } else {
1510 task.mLayerRank = baseLayer + layer++;
1511 }
1512 }
1513 return layer;
1514 }
1515
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001516 /**
1517 * Make sure that all activities that need to be visible (that is, they
1518 * currently can be seen by the user) actually are.
1519 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001520 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1521 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001522 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001523 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001524 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001525 if (top != null) {
1526 checkTranslucentActivityWaiting(top);
1527 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001528
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001529 // If the top activity is not fullscreen, then we need to
1530 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001531 boolean aboveTop = top != null;
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001532 final int stackVisibility = getStackVisibilityLocked();
1533 final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
1534 final boolean stackVisibleBehind = stackVisibility == STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001535 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaled046a012015-12-24 13:05:59 -08001536 boolean resumeNextActivity = isFocusable() && (isInStackLocked(starting) == null);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001537 boolean behindTranslucentActivity = false;
1538 final ActivityRecord visibleBehind = getVisibleBehindActivity();
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001539 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001540 final TaskRecord task = mTaskHistory.get(taskNdx);
1541 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001542 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1543 final ActivityRecord r = activities.get(activityNdx);
1544 if (r.finishing) {
1545 continue;
1546 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001547 final boolean isTop = r == top;
1548 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001549 continue;
1550 }
1551 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001552 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1553 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001554 final boolean activityVisibleBehind =
1555 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == r;
1556 final boolean isVisible = (!behindFullscreenActivity || r.mLaunchTaskBehind
1557 || activityVisibleBehind) && okToShowLocked(r);
1558 if (isVisible) {
1559 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1560 + " finishing=" + r.finishing + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001561 // First: if this is not the current activity being started, make
1562 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001563 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001564 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001565 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001566
1567 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001568 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001569 resumeNextActivity, r)) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001570 if (activityNdx >= activities.size()) {
1571 // Record may be removed if its process needs to restart.
1572 activityNdx = activities.size() - 1;
1573 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001574 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001575 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001576 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001577 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001578 // If this activity is already visible, then there is nothing to do here.
1579 if (handleAlreadyVisible(r)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001580 resumeNextActivity = false;
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001581 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001582 } else {
Jorim Jaggie66edb12016-02-05 12:41:17 -08001583 makeVisibleIfNeeded(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001584 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001585 // Aggregate current change flags.
1586 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001587 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1588 behindFullscreenActivity, task, r);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001589 if (behindFullscreenActivity && !r.fullscreen) {
1590 behindTranslucentActivity = true;
1591 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001592 } else {
Wale Ogunwale834c2362016-01-23 18:14:58 -08001593 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001594 + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1595 + stackInvisible + " behindFullscreenActivity="
1596 + behindFullscreenActivity + " mLaunchTaskBehind="
1597 + r.mLaunchTaskBehind);
1598 makeInvisible(r, visibleBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001599 }
1600 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001601 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1602 // The visibility of tasks and the activities they contain in freeform stack are
1603 // determined individually unlike other stacks where the visibility or fullscreen
1604 // status of an activity in a previous task affects other.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001605 behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001606 } else if (mStackId == HOME_STACK_ID) {
1607 if (task.isHomeTask()) {
1608 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + task
1609 + " stackInvisible=" + stackInvisible
1610 + " behindFullscreenActivity=" + behindFullscreenActivity);
1611 // No other task in the home stack should be visible behind the home activity.
1612 // Home activities is usually a translucent activity with the wallpaper behind
1613 // them. However, when they don't have the wallpaper behind them, we want to
1614 // show activities in the next application stack behind them vs. another
1615 // task in the home stack like recents.
1616 behindFullscreenActivity = true;
1617 } else if (task.isRecentsTask()
1618 && task.getTaskToReturnTo() == APPLICATION_ACTIVITY_TYPE) {
1619 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1620 "Recents task returning to app: at " + task
1621 + " stackInvisible=" + stackInvisible
1622 + " behindFullscreenActivity=" + behindFullscreenActivity);
1623 // We don't want any other tasks in the home stack visible if the recents
1624 // activity is going to be returning to an application activity type.
1625 // We do this to preserve the visible order the user used to get into the
1626 // recents activity. The recents activity is normally translucent and if it
1627 // doesn't have the wallpaper behind it the next activity in the home stack
1628 // shouldn't be visible when the home stack is brought to the front to display
1629 // the recents activity from an app.
1630 behindFullscreenActivity = true;
1631 }
1632
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001633 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001634 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001635
1636 if (mTranslucentActivityWaiting != null &&
1637 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1638 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1639 notifyActivityDrawnLocked(null);
1640 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001641 }
Craig Mautner58547802013-03-05 08:23:53 -08001642
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001643 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1644 if (mTranslucentActivityWaiting != top) {
1645 mUndrawnActivitiesBelowTopTranslucent.clear();
1646 if (mTranslucentActivityWaiting != null) {
1647 // Call the callback with a timeout indication.
1648 notifyActivityDrawnLocked(null);
1649 mTranslucentActivityWaiting = null;
1650 }
1651 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1652 }
1653 }
1654
1655 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001656 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001657 // We need to make sure the app is running if it's the top, or it is just made visible from
1658 // invisible. If the app is already visible, it must have died while it was visible. In this
1659 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1660 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001661 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001662 // This activity needs to be visible, but isn't even running...
1663 // get it started and resume if no other stack in this stack is resumed.
1664 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1665 if (r != starting) {
1666 r.startFreezingScreenLocked(r.app, configChanges);
1667 }
1668 if (!r.visible || r.mLaunchTaskBehind) {
1669 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1670 setVisible(r, true);
1671 }
1672 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001673 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001674 return true;
1675 }
1676 }
1677 return false;
1678 }
1679
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001680 private void makeInvisible(ActivityRecord r, ActivityRecord visibleBehind) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001681 if (!r.visible) {
1682 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1683 return;
1684 }
1685 // Now for any activities that aren't visible to the user, make sure they no longer are
1686 // keeping the screen frozen.
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001687 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001688 try {
1689 setVisible(r, false);
1690 switch (r.state) {
1691 case STOPPING:
1692 case STOPPED:
1693 if (r.app != null && r.app.thread != null) {
1694 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1695 "Scheduling invisibility: " + r);
1696 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1697 }
1698 break;
1699
1700 case INITIALIZING:
1701 case RESUMED:
1702 case PAUSING:
1703 case PAUSED:
1704 // This case created for transitioning activities from
1705 // translucent to opaque {@link Activity#convertToOpaque}.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001706 if (visibleBehind == r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001707 releaseBackgroundResources(r);
1708 } else {
Chong Zhang46b1ac62016-02-18 17:53:57 -08001709 addToStopping(r, true /* immediate */);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001710 }
1711 break;
1712
1713 default:
1714 break;
1715 }
1716 } catch (Exception e) {
1717 // Just skip on any failure; we'll make it visible when it next restarts.
1718 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1719 }
1720 }
1721
1722 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1723 TaskRecord task, ActivityRecord r) {
1724 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001725 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08001726 + " stackInvisible=" + stackInvisible
1727 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001728 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001729 behindFullscreenActivity = true;
Wale Ogunwale74e26592016-02-05 11:48:37 -08001730 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001731 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1732 + " stackInvisible=" + stackInvisible
1733 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001734 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001735 }
1736 return behindFullscreenActivity;
1737 }
1738
Jorim Jaggie66edb12016-02-05 12:41:17 -08001739 private void makeVisibleIfNeeded(ActivityRecord starting, ActivityRecord r) {
1740
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001741 // This activity is not currently visible, but is running. Tell it to become visible.
Jorim Jaggie66edb12016-02-05 12:41:17 -08001742 if (r.state == ActivityState.RESUMED || r == starting) {
Chong Zhange05db742016-02-16 16:58:37 -08001743 if (DEBUG_VISIBILITY) Slog.d(TAG_VISIBILITY,
1744 "Not making visible, r=" + r + " state=" + r.state + " starting=" + starting);
Jorim Jaggie66edb12016-02-05 12:41:17 -08001745 return;
1746 }
1747
1748 // If this activity is paused, tell it to now show its window.
1749 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1750 "Making visible and scheduling visibility: " + r);
1751 try {
1752 if (mTranslucentActivityWaiting != null) {
1753 r.updateOptionsLocked(r.returningOptions);
1754 mUndrawnActivitiesBelowTopTranslucent.add(r);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001755 }
Jorim Jaggie66edb12016-02-05 12:41:17 -08001756 setVisible(r, true);
1757 r.sleeping = false;
1758 r.app.pendingUiClean = true;
1759 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1760 r.stopFreezingScreenLocked(false);
1761 } catch (Exception e) {
1762 // Just skip on any failure; we'll make it
1763 // visible when it next restarts.
1764 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001765 }
1766 }
1767
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001768 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001769 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1770 r.stopFreezingScreenLocked(false);
1771 try {
1772 if (r.returningOptions != null) {
1773 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1774 }
1775 } catch(RemoteException e) {
1776 }
1777 return r.state == ActivityState.RESUMED;
1778 }
1779
Todd Kennedyaab56db2015-01-30 09:39:53 -08001780 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001781 mTranslucentActivityWaiting = r;
1782 mUndrawnActivitiesBelowTopTranslucent.clear();
1783 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1784 }
1785
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001786 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1787 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1788 final TaskRecord task = mTaskHistory.get(taskNdx);
1789 final ArrayList<ActivityRecord> activities = task.mActivities;
1790 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1791 final ActivityRecord r = activities.get(activityNdx);
1792 if ( r.appTimeTracker != except) {
1793 r.appTimeTracker = null;
1794 }
1795 }
1796 }
1797 }
1798
Craig Mautner5eda9b32013-07-02 11:58:16 -07001799 /**
1800 * Called as activities below the top translucent activity are redrawn. When the last one is
1801 * redrawn notify the top activity by calling
1802 * {@link Activity#onTranslucentConversionComplete}.
1803 *
1804 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1805 * occurred and the activity will be notified immediately.
1806 */
1807 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001808 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001809 if ((r == null)
1810 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1811 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1812 // The last undrawn activity below the top has just been drawn. If there is an
1813 // opaque activity at the top, notify it that it can become translucent safely now.
1814 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1815 mTranslucentActivityWaiting = null;
1816 mUndrawnActivitiesBelowTopTranslucent.clear();
1817 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1818
Craig Mautner71dd1b62014-02-18 15:48:52 -08001819 if (waitingActivity != null) {
1820 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1821 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1822 try {
1823 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1824 waitingActivity.appToken, r != null);
1825 } catch (RemoteException e) {
1826 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001827 }
1828 }
1829 }
1830 }
1831
Craig Mautnera61bc652013-10-28 15:43:18 -07001832 /** If any activities below the top running one are in the INITIALIZING state and they have a
1833 * starting window displayed then remove that starting window. It is possible that the activity
1834 * in this state will never resumed in which case that starting window will be orphaned. */
1835 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001836 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001837 boolean aboveTop = true;
1838 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1839 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1840 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1841 final ActivityRecord r = activities.get(activityNdx);
1842 if (aboveTop) {
1843 if (r == topActivity) {
1844 aboveTop = false;
1845 }
1846 continue;
1847 }
1848
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08001849 if (r.state == ActivityState.INITIALIZING
1850 && r.mStartingWindowState == STARTING_WINDOW_SHOWN) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001851 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1852 "Found orphaned starting window " + r);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08001853 r.mStartingWindowState = STARTING_WINDOW_REMOVED;
Craig Mautnera61bc652013-10-28 15:43:18 -07001854 mWindowManager.removeAppStartingWindow(r.appToken);
1855 }
1856 }
1857 }
1858 }
1859
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001860 /**
1861 * Ensure that the top activity in the stack is resumed.
1862 *
1863 * @param prev The previously resumed activity, for when in the process
1864 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001865 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001866 *
1867 * @return Returns true if something is being resumed, or false if
1868 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001869 *
1870 * NOTE: It is not safe to call this method directly as it can cause an activity in a
1871 * non-focused stack to be resumed.
1872 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
1873 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001874 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08001875 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001876 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001877 // Don't even start recursing.
1878 return false;
1879 }
1880
1881 boolean result = false;
1882 try {
1883 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001884 mStackSupervisor.inResumeTopActivity = true;
1885 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1886 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001887 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001888 }
Craig Mautner544efa72014-09-04 16:41:20 -07001889 result = resumeTopActivityInnerLocked(prev, options);
1890 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001891 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001892 }
1893 return result;
1894 }
1895
Chong Zhang280d3322015-11-03 17:27:26 -08001896 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001897 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001898
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001899 if (!mService.mBooting && !mService.mBooted) {
1900 // Not ready yet!
1901 return false;
1902 }
1903
Craig Mautnerdf88d732014-01-27 09:21:32 -08001904 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001905 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001906 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001907 // Do not resume this stack if its parent is not resumed.
1908 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1909 return false;
1910 }
1911
Wale Ogunwale2be760d2016-02-17 11:16:10 -08001912 mStackSupervisor.cancelInitializingActivities();
Craig Mautnera61bc652013-10-28 15:43:18 -07001913
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001914 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001915 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001916
1917 // Remember how we'll process this pause/resume situation, and ensure
1918 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001919 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1920 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001921
Craig Mautner84984fa2014-06-19 11:19:20 -07001922 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001923 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001924 // There are no more activities!
1925 final String reason = "noMoreActivities";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001926 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(reason)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001927 // Try to move focus to the next visible stack with a running activity if this
1928 // stack is not covering the entire screen.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001929 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
1930 mStackSupervisor.getFocusedStack(), prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001931 }
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001932
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001933 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001934 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001935 if (DEBUG_STATES) Slog.d(TAG_STATES,
1936 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001937 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001938 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001939 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1940 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1941 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001942 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001943 }
1944
1945 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001946
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001947 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001948 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1949 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001950 // Make sure we have executed any pending transitions, since there
1951 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001952 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001953 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001954 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001955 if (DEBUG_STATES) Slog.d(TAG_STATES,
1956 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001957 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001958 return false;
1959 }
1960
Craig Mautner525f3d92013-05-07 14:01:50 -07001961 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001962 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001963 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001964 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001965 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001966 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001967 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001968 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001969 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001970 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001971 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001972 } else if (!isOnHomeDisplay()) {
1973 return false;
1974 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001975 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001976 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001977 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1978 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1979 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001980 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001981 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001982 }
1983
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001984 // If we are sleeping, and there is no resumed activity, and the top
1985 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001986 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001987 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001988 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001989 // Make sure we have executed any pending transitions, since there
1990 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001991 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001992 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001993 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001994 if (DEBUG_STATES) Slog.d(TAG_STATES,
1995 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001996 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001997 return false;
1998 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001999
2000 // Make sure that the user who owns this activity is started. If not,
2001 // we will just leave it as is because someone should be bringing
2002 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07002003 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002004 Slog.w(TAG, "Skipping resume of top activity " + next
2005 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07002006 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002007 return false;
2008 }
2009
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002010 // The activity may be waiting for stop, but that is no longer
2011 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07002012 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002013 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002014 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07002015 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002016
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002017 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002018
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002019 // If we are currently pausing an activity, then don't do anything until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002020 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002021 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002022 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002023 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002024 return false;
2025 }
2026
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002027 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2028
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08002029 // We need to start pausing the current activity so the top one can be resumed...
2030 final boolean dontWaitForPause = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002031 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002032 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002033 if (DEBUG_STATES) Slog.d(TAG_STATES,
2034 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08002035 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002036 }
2037 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002038 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002039 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002040 // At this point we want to put the upcoming activity's process
2041 // at the top of the LRU list, since we know we will be needing it
2042 // very soon and it would be a waste to let it get killed if it
2043 // happens to be sitting towards the end.
2044 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002045 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002046 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002047 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002048 return true;
2049 }
2050
Christopher Tated3f175c2012-06-14 14:16:54 -07002051 // If the most recent activity was noHistory but was only stopped rather
2052 // than stopped+finished because the device went to sleep, we need to make
2053 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07002054 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002055 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002056 if (DEBUG_STATES) Slog.d(TAG_STATES,
2057 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002058 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002059 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002060 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002061 }
2062
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002063 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002064 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2065 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002066 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002067 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2068 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002069 } else {
2070 // The next activity is already visible, so hide the previous
2071 // activity's windows right now so we can show the new one ASAP.
2072 // We only do this if the previous is finishing, which should mean
2073 // it is on top of the one being resumed so hiding it quickly
2074 // is good. Otherwise, we want to do the normal route of allowing
2075 // the resumed activity to be shown so we can decide if the
2076 // previous should actually be hidden depending on whether the
2077 // new one is found to be full-screen or not.
2078 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002079 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002080 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2081 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002082 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002083 + ", nowVisible=" + next.nowVisible);
2084 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002085 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002086 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002087 + ", waitingVisible="
2088 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2089 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002090 }
2091 }
2092 }
2093
Dianne Hackborne7f97212011-02-24 14:40:20 -08002094 // Launching this app's activity, make sure the app is no longer
2095 // considered stopped.
2096 try {
2097 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002098 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002099 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002100 } catch (IllegalArgumentException e) {
2101 Slog.w(TAG, "Failed trying to unstop package "
2102 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002103 }
2104
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002105 // We are starting up the next activity, so tell the window manager
2106 // that the previous one will be hidden soon. This way it can know
2107 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002108 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002109 if (prev != null) {
2110 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002111 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002112 "Prepare close transition: prev=" + prev);
2113 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002114 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002115 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002116 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002117 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002118 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2119 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002120 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002121 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002122 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002123 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2124 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002125 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002126 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002127 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002128 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002129 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002130 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002131 : next.mLaunchTaskBehind
2132 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2133 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002134 }
2135 }
Craig Mautner967212c2013-04-13 21:10:58 -07002136 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002137 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002138 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002139 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002140 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002141 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002142 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002143 }
2144 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002145
2146 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002147 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002148 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2149 if (opts != null) {
2150 resumeAnimOptions = opts.toBundle();
2151 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002152 next.applyOptionsLocked();
2153 } else {
2154 next.clearOptionsLocked();
2155 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002156
Craig Mautnercf910b02013-04-23 11:23:27 -07002157 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002158 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002159 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002160
2161 // This activity is now becoming visible.
Jorim Jaggi1b025a62016-02-03 19:27:49 -08002162 if (!next.visible) {
2163 mWindowManager.setAppVisibility(next.appToken, true);
2164 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002165
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002166 // schedule launch ticks to collect information about slow apps.
2167 next.startLaunchTickingLocked();
2168
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002169 ActivityRecord lastResumedActivity =
2170 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002171 ActivityState lastState = next.state;
2172
2173 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002174
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002175 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002176 next.state = ActivityState.RESUMED;
2177 mResumedActivity = next;
2178 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002179 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002180 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002181 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002182 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002183
2184 // Have the window manager re-evaluate the orientation of
2185 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002186 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002187 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002188 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002189 mService.mConfiguration,
2190 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2191 if (config != null) {
2192 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002193 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002194 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002195 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002196
Craig Mautner525f3d92013-05-07 14:01:50 -07002197 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002198 // The configuration update wasn't able to keep the existing
2199 // instance of the activity, and instead started a new one.
2200 // We should be all done, but let's just make sure our activity
2201 // is still at the top and schedule another run if something
2202 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002203 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002204 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002205 "Activity config changed during resume: " + next
2206 + ", new next: " + nextNext);
2207 if (nextNext != next) {
2208 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002209 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002210 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002211 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002212 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002213 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002214 return true;
2215 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002216 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002217 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002218 }
Craig Mautner58547802013-03-05 08:23:53 -08002219
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002220 try {
2221 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002222 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002223 if (a != null) {
2224 final int N = a.size();
2225 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002226 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2227 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002228 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002229 }
2230 }
2231
2232 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002233 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002234 }
2235
Craig Mautner299f9602015-01-26 09:47:33 -08002236 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2237 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002238
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002239 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002240 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002241 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002242 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002243 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002244 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002245 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002246
Craig Mautner0eea92c2013-05-16 13:35:39 -07002247 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002248
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002249 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002250 } catch (Exception e) {
2251 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002252 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002253 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002254 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002255 if (lastStack != null) {
2256 lastStack.mResumedActivity = lastResumedActivity;
2257 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002258 Slog.i(TAG, "Restarting because process died: " + next);
2259 if (!next.hasBeenLaunched) {
2260 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002261 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2262 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002263 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002264 next.appToken, next.packageName, next.theme,
2265 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002266 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2267 next.windowFlags, null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002268 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002269 }
George Mount2c92c972014-03-20 09:38:23 -07002270 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002271 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002272 return true;
2273 }
2274
2275 // From this point on, if something goes wrong there is no way
2276 // to recover the activity.
2277 try {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002278 completeResumeLocked(next);
2279 } catch (Exception e) {
2280 // If any exception gets thrown, toss away this
2281 // activity and try the next one.
2282 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002283 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002284 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002285 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002286 return true;
2287 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002288 } else {
2289 // Whoops, need to restart this activity!
2290 if (!next.hasBeenLaunched) {
2291 next.hasBeenLaunched = true;
2292 } else {
2293 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002294 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002295 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002296 mService.compatibilityInfoForPackageLocked(
2297 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002298 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002299 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002300 null, true);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002301 next.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002302 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002303 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002304 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002305 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002306 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002307 }
2308
Craig Mautnercf910b02013-04-23 11:23:27 -07002309 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002310 return true;
2311 }
2312
riddle_hsuc215a4f2014-12-27 12:10:45 +08002313 private TaskRecord getNextTask(TaskRecord targetTask) {
2314 final int index = mTaskHistory.indexOf(targetTask);
2315 if (index >= 0) {
2316 final int numTasks = mTaskHistory.size();
2317 for (int i = index + 1; i < numTasks; ++i) {
2318 TaskRecord task = mTaskHistory.get(i);
2319 if (task.userId == targetTask.userId) {
2320 return task;
2321 }
2322 }
2323 }
2324 return null;
2325 }
2326
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002327 private void insertTaskAtPosition(TaskRecord task, int position) {
2328 if (position >= mTaskHistory.size()) {
2329 insertTaskAtTop(task, null);
2330 return;
2331 }
2332 // Calculate maximum possible position for this task.
2333 int maxPosition = mTaskHistory.size();
2334 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002335 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002336 // Put non-current user tasks below current user tasks.
2337 while (maxPosition > 0) {
2338 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2339 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002340 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002341 break;
2342 }
2343 maxPosition--;
2344 }
2345 }
2346 position = Math.min(position, maxPosition);
2347 mTaskHistory.remove(task);
2348 mTaskHistory.add(position, task);
2349 updateTaskMovement(task, true);
2350 }
2351
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002352 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002353 // If the moving task is over home stack, transfer its return type to next task
2354 if (task.isOverHomeStack()) {
2355 final TaskRecord nextTask = getNextTask(task);
2356 if (nextTask != null) {
2357 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2358 }
2359 }
2360
Craig Mautner9c85c202013-10-04 20:11:26 -07002361 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002362 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002363 if (isOnHomeDisplay()) {
2364 ActivityStack lastStack = mStackSupervisor.getLastStack();
2365 final boolean fromHome = lastStack.isHomeStack();
2366 if (!isHomeStack() && (fromHome || topTask() != task)) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002367 int returnToType = APPLICATION_ACTIVITY_TYPE;
2368 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2369 returnToType = lastStack.topTask() == null
2370 ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2371 }
2372 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002373 }
2374 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002375 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002376 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002377
Craig Mautnerac6f8432013-07-17 13:24:59 -07002378 mTaskHistory.remove(task);
2379 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002380 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002381 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002382 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002383 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002384 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002385 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002386 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002387 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002388 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002389 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002390 break;
2391 }
2392 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002393 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002394 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002395 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002396 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002397 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002398
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002399 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2400 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002401 TaskRecord rTask = r.task;
2402 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002403 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2404 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002405 // Last activity in task had been removed or ActivityManagerService is reusing task.
2406 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002407 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002408 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002409 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002410 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002411 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002412 if (!newTask) {
2413 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002414 boolean startIt = true;
2415 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2416 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002417 if (task.getTopActivity() == null) {
2418 // All activities in task are finishing.
2419 continue;
2420 }
Craig Mautner70a86932013-02-28 22:37:44 -08002421 if (task == r.task) {
2422 // Here it is! Now, if this is not yet visible to the
2423 // user, then just add it without starting; it will
2424 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002425 if (!startIt) {
2426 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2427 + task, new RuntimeException("here").fillInStackTrace());
2428 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002429 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002430 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002431 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002432 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002433 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002434 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002435 return;
2436 }
2437 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002438 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002439 startIt = false;
2440 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002441 }
2442 }
2443
2444 // Place a new activity at top of stack, so it is next to interact
2445 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002446
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002447 // If we are not placing the new activity frontmost, we do not want
2448 // to deliver the onUserLeaving callback to the actual frontmost
2449 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002450 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002451 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002452 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002453 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002454 }
Craig Mautner70a86932013-02-28 22:37:44 -08002455
2456 task = r.task;
2457
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002458 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002459 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002460 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002461 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002462 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002463
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002464 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002465 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002466 // We want to show the starting preview window if we are
2467 // switching to a new task, or the next activity's process is
2468 // not currently running.
2469 boolean showStartingIcon = newTask;
2470 ProcessRecord proc = r.app;
2471 if (proc == null) {
2472 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2473 }
2474 if (proc == null || proc.thread == null) {
2475 showStartingIcon = true;
2476 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002477 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002478 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002479 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002480 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002481 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002482 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002483 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002484 ? r.mLaunchTaskBehind
2485 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2486 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002487 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002488 mNoAnimActivities.remove(r);
2489 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002490 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002491 boolean doShow = true;
2492 if (newTask) {
2493 // Even though this activity is starting fresh, we still need
2494 // to reset it to make sure we apply affinities to move any
2495 // existing activities from other tasks in to it.
2496 // If the caller has requested that the target task be
2497 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002498 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002499 resetTaskIfNeededLocked(r, r);
2500 doShow = topRunningNonDelayedActivityLocked(null) == r;
2501 }
Chong Zhang280d3322015-11-03 17:27:26 -08002502 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002503 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2504 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002505 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002506 if (r.mLaunchTaskBehind) {
2507 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2508 // tell WindowManager that r is visible even though it is at the back of the stack.
2509 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002510 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002511 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002512 // Figure out if we are transitioning from another activity that is
2513 // "has the same starting icon" as the next one. This allows the
2514 // window manager to keep the previous window it had previously
2515 // created, if it still had one.
2516 ActivityRecord prev = mResumedActivity;
2517 if (prev != null) {
2518 // We don't want to reuse the previous starting preview if:
2519 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002520 if (prev.task != r.task) {
2521 prev = null;
2522 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002523 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002524 else if (prev.nowVisible) {
2525 prev = null;
2526 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002527 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002528 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002529 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002530 mService.compatibilityInfoForPackageLocked(
2531 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002532 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002533 prev != null ? prev.appToken : null, showStartingIcon);
Wale Ogunwalef40c11b2016-02-26 08:16:02 -08002534 r.mStartingWindowState = STARTING_WINDOW_SHOWN;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002535 }
2536 } else {
2537 // If this is the first activity, don't do any fancy animations,
2538 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002539 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002540 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002541 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002542 }
2543 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002544 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002545 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002546 }
2547
Dianne Hackbornbe707852011-11-11 14:32:10 -08002548 final void validateAppTokensLocked() {
2549 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002550 mValidateAppTokens.ensureCapacity(numActivities());
2551 final int numTasks = mTaskHistory.size();
2552 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2553 TaskRecord task = mTaskHistory.get(taskNdx);
2554 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002555 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002556 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002557 }
Craig Mautner000f0022013-02-26 15:04:29 -08002558 TaskGroup group = new TaskGroup();
2559 group.taskId = task.taskId;
2560 mValidateAppTokens.add(group);
2561 final int numActivities = activities.size();
2562 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2563 final ActivityRecord r = activities.get(activityNdx);
2564 group.tokens.add(r.appToken);
2565 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002566 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002567 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002568 }
2569
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002570 /**
2571 * Perform a reset of the given task, if needed as part of launching it.
2572 * Returns the new HistoryRecord at the top of the task.
2573 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002574 /**
2575 * Helper method for #resetTaskIfNeededLocked.
2576 * We are inside of the task being reset... we'll either finish this activity, push it out
2577 * for another task, or leave it as-is.
2578 * @param task The task containing the Activity (taskTop) that might be reset.
2579 * @param forceReset
2580 * @return An ActivityOptions that needs to be processed.
2581 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002582 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002583 ActivityOptions topOptions = null;
2584
2585 int replyChainEnd = -1;
2586 boolean canMoveOptions = true;
2587
2588 // We only do this for activities that are not the root of the task (since if we finish
2589 // the root, we may no longer have the task!).
2590 final ArrayList<ActivityRecord> activities = task.mActivities;
2591 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002592 final int rootActivityNdx = task.findEffectiveRootIndex();
2593 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002594 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002595 if (target.frontOfTask)
2596 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002597
2598 final int flags = target.info.flags;
2599 final boolean finishOnTaskLaunch =
2600 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2601 final boolean allowTaskReparenting =
2602 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2603 final boolean clearWhenTaskReset =
2604 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2605
2606 if (!finishOnTaskLaunch
2607 && !clearWhenTaskReset
2608 && target.resultTo != null) {
2609 // If this activity is sending a reply to a previous
2610 // activity, we can't do anything with it now until
2611 // we reach the start of the reply chain.
2612 // XXX note that we are assuming the result is always
2613 // to the previous activity, which is almost always
2614 // the case but we really shouldn't count on.
2615 if (replyChainEnd < 0) {
2616 replyChainEnd = i;
2617 }
2618 } else if (!finishOnTaskLaunch
2619 && !clearWhenTaskReset
2620 && allowTaskReparenting
2621 && target.taskAffinity != null
2622 && !target.taskAffinity.equals(task.affinity)) {
2623 // If this activity has an affinity for another
2624 // task, then we need to move it out of here. We will
2625 // move it as far out of the way as possible, to the
2626 // bottom of the activity stack. This also keeps it
2627 // correctly ordered with any activities we previously
2628 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002629 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002630 final ActivityRecord bottom =
2631 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002632 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002633 if (bottom != null && target.taskAffinity != null
2634 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002635 // If the activity currently at the bottom has the
2636 // same task affinity as the one we are moving,
2637 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002638 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002639 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002640 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002641 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002642 targetTask = createTaskRecord(
2643 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
2644 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002645 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002646 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002647 + " out to new task " + target.task);
2648 }
2649
Wale Ogunwale706ed792015-08-02 10:29:44 -07002650 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002651
Craig Mautner525f3d92013-05-07 14:01:50 -07002652 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002653 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2654 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002655 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002656 if (p.finishing) {
2657 continue;
2658 }
2659
Craig Mautnere3a74d52013-02-22 14:14:58 -08002660 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002661 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002662 topOptions = p.takeOptionsLocked();
2663 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002664 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002665 }
2666 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002667 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2668 "Removing activity " + p + " from task=" + task + " adding to task="
2669 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002670 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2671 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002672 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002673 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002674
Wale Ogunwale706ed792015-08-02 10:29:44 -07002675 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002676 }
2677
Wale Ogunwale706ed792015-08-02 10:29:44 -07002678 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002679 if (VALIDATE_TOKENS) {
2680 validateAppTokensLocked();
2681 }
2682
2683 replyChainEnd = -1;
2684 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2685 // If the activity should just be removed -- either
2686 // because it asks for it, or the task should be
2687 // cleared -- then finish it and anything that is
2688 // part of its reply chain.
2689 int end;
2690 if (clearWhenTaskReset) {
2691 // In this case, we want to finish this activity
2692 // and everything above it, so be sneaky and pretend
2693 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002694 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002695 } else if (replyChainEnd < 0) {
2696 end = i;
2697 } else {
2698 end = replyChainEnd;
2699 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002700 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002701 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002702 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002703 if (p.finishing) {
2704 continue;
2705 }
2706 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002707 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002708 topOptions = p.takeOptionsLocked();
2709 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002710 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002711 }
2712 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002713 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002714 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002715 if (finishActivityLocked(
2716 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002717 end--;
2718 srcPos--;
2719 }
2720 }
2721 replyChainEnd = -1;
2722 } else {
2723 // If we were in the middle of a chain, well the
2724 // activity that started it all doesn't want anything
2725 // special, so leave it all as-is.
2726 replyChainEnd = -1;
2727 }
2728 }
2729
2730 return topOptions;
2731 }
2732
2733 /**
2734 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2735 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2736 * @param affinityTask The task we are looking for an affinity to.
2737 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2738 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2739 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2740 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002741 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002742 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002743 int replyChainEnd = -1;
2744 final int taskId = task.taskId;
2745 final String taskAffinity = task.affinity;
2746
2747 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2748 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002749 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2750
2751 // Do not operate on or below the effective root Activity.
2752 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002753 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002754 if (target.frontOfTask)
2755 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002756
2757 final int flags = target.info.flags;
2758 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2759 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2760
2761 if (target.resultTo != null) {
2762 // If this activity is sending a reply to a previous
2763 // activity, we can't do anything with it now until
2764 // we reach the start of the reply chain.
2765 // XXX note that we are assuming the result is always
2766 // to the previous activity, which is almost always
2767 // the case but we really shouldn't count on.
2768 if (replyChainEnd < 0) {
2769 replyChainEnd = i;
2770 }
2771 } else if (topTaskIsHigher
2772 && allowTaskReparenting
2773 && taskAffinity != null
2774 && taskAffinity.equals(target.taskAffinity)) {
2775 // This activity has an affinity for our task. Either remove it if we are
2776 // clearing or move it over to our task. Note that
2777 // we currently punt on the case where we are resetting a
2778 // task that is not at the top but who has activities above
2779 // with an affinity to it... this is really not a normal
2780 // case, and we will need to later pull that task to the front
2781 // and usually at that point we will do the reset and pick
2782 // up those remaining activities. (This only happens if
2783 // someone starts an activity in a new task from an activity
2784 // in a task that is not currently on top.)
2785 if (forceReset || finishOnTaskLaunch) {
2786 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002787 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2788 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002789 for (int srcPos = start; srcPos >= i; --srcPos) {
2790 final ActivityRecord p = activities.get(srcPos);
2791 if (p.finishing) {
2792 continue;
2793 }
Todd Kennedy539db512014-12-15 09:57:55 -08002794 finishActivityLocked(
2795 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002796 }
2797 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002798 if (taskInsertionPoint < 0) {
2799 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002800
Craig Mautner77878772013-03-04 19:46:24 -08002801 }
Craig Mautner77878772013-03-04 19:46:24 -08002802
2803 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002804 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2805 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2806 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002807 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002808 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002809 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002810 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002811
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002812 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2813 "Removing and adding activity " + p + " to stack at " + task
2814 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002815 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2816 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002817 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002818 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002819 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002820 if (VALIDATE_TOKENS) {
2821 validateAppTokensLocked();
2822 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002823
2824 // Now we've moved it in to place... but what if this is
2825 // a singleTop activity and we have put it on top of another
2826 // instance of the same activity? Then we drop the instance
2827 // below so it remains singleTop.
2828 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2829 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002830 int targetNdx = taskActivities.indexOf(target);
2831 if (targetNdx > 0) {
2832 ActivityRecord p = taskActivities.get(targetNdx - 1);
2833 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002834 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2835 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002836 }
2837 }
2838 }
2839 }
2840
2841 replyChainEnd = -1;
2842 }
2843 }
Craig Mautner77878772013-03-04 19:46:24 -08002844 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002845 }
2846
Craig Mautner8849a5e2013-04-02 16:41:03 -07002847 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002848 ActivityRecord newActivity) {
2849 boolean forceReset =
2850 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2851 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2852 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2853 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2854 forceReset = true;
2855 }
2856 }
2857
2858 final TaskRecord task = taskTop.task;
2859
2860 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2861 * for remaining tasks. Used for later tasks to reparent to task. */
2862 boolean taskFound = false;
2863
2864 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2865 ActivityOptions topOptions = null;
2866
Craig Mautner77878772013-03-04 19:46:24 -08002867 // Preserve the location for reparenting in the new task.
2868 int reparentInsertionPoint = -1;
2869
Craig Mautnere3a74d52013-02-22 14:14:58 -08002870 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2871 final TaskRecord targetTask = mTaskHistory.get(i);
2872
2873 if (targetTask == task) {
2874 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2875 taskFound = true;
2876 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002877 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2878 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002879 }
2880 }
2881
Craig Mautner70a86932013-02-28 22:37:44 -08002882 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002883 if (taskNdx >= 0) {
2884 do {
2885 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2886 } while (taskTop == null && taskNdx >= 0);
2887 }
Craig Mautner70a86932013-02-28 22:37:44 -08002888
Craig Mautnere3a74d52013-02-22 14:14:58 -08002889 if (topOptions != null) {
2890 // If we got some ActivityOptions from an activity on top that
2891 // was removed from the task, propagate them to the new real top.
2892 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002893 taskTop.updateOptionsLocked(topOptions);
2894 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002895 topOptions.abort();
2896 }
2897 }
2898
2899 return taskTop;
2900 }
2901
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002902 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2903 String resultWho, int requestCode, int resultCode, Intent data) {
2904
2905 if (callingUid > 0) {
2906 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002907 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002908 }
2909
2910 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2911 + " : who=" + resultWho + " req=" + requestCode
2912 + " res=" + resultCode + " data=" + data);
2913 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2914 try {
2915 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2916 list.add(new ResultInfo(resultWho, requestCode,
2917 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002918 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002919 return;
2920 } catch (Exception e) {
2921 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2922 }
2923 }
2924
2925 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2926 }
2927
Craig Mautner299f9602015-01-26 09:47:33 -08002928 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002929 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2930 return;
2931 }
2932
2933 final ActivityRecord next = topRunningActivityLocked();
2934 final String myReason = reason + " adjustFocus";
2935 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002936 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002937 // For freeform, docked, and pinned stacks we always keep the focus within the
2938 // stack as long as there is a running activity in the stack that we can adjust
2939 // focus to.
2940 mService.setFocusedActivityLocked(next, myReason);
2941 return;
2942 } else {
2943 final TaskRecord task = r.task;
2944 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2945 // For non-fullscreen stack, we want to move the focus to the next visible
2946 // stack to prevent the home screen from moving to the top and obscuring
2947 // other visible stacks.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002948 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002949 return;
2950 }
2951 // Move the home stack to the top if this stack is fullscreen or there is no
2952 // other visible stack.
2953 if (mStackSupervisor.moveHomeStackTaskToTop(
2954 task.getTaskToReturnTo(), myReason)) {
2955 // Activity focus was already adjusted. Nothing else to do...
2956 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002957 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002958 }
2959 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08002960 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002961
Wale Ogunwaled046a012015-12-24 13:05:59 -08002962 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002963 }
2964
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002965 private boolean adjustFocusToNextFocusableStackLocked(String reason) {
2966 final ActivityStack stack = getNextFocusableStackLocked();
2967 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002968 if (stack == null) {
2969 return false;
2970 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002971 return mService.setFocusedActivityLocked(stack.topRunningActivityLocked(), myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002972 }
2973
Craig Mautnerf3333272013-04-22 10:55:53 -07002974 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002975 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002976 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2977 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2978 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002979 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002980 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002981 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2982 "stop-no-history", false)) {
2983 // Activity was finished, no need to continue trying to schedule stop.
2984 adjustFocusedActivityLocked(r, "stopActivityFinished");
2985 r.resumeKeyDispatchingLocked();
2986 return;
2987 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002988 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002989 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002990 + " on stop because we're just sleeping");
2991 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002992 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002993 }
2994
2995 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002996 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002997 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002998 try {
2999 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003000 if (DEBUG_STATES) Slog.v(TAG_STATES,
3001 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003002 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003003 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3004 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003005 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003006 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003007 }
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003008 EventLogTags.writeAmStopActivity(
3009 r.userId, System.identityHashCode(r), r.shortComponentName);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003010 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07003011 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003012 r.setSleeping(true);
3013 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003014 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003015 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003016 } catch (Exception e) {
3017 // Maybe just ignore exceptions here... if the process
3018 // has crashed, our death notification will clean things
3019 // up.
3020 Slog.w(TAG, "Exception thrown during pause", e);
3021 // Just in case, assume it to be stopped.
3022 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003023 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003024 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003025 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003026 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003027 }
3028 }
3029 }
3030 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003031
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003032 /**
3033 * @return Returns true if the activity is being finished, false if for
3034 * some reason it is being left as-is.
3035 */
3036 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003037 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003038 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003039 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3040 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003041 + ", result=" + resultCode + ", data=" + resultData
3042 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003043 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003044 return false;
3045 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003046
Craig Mautnerd44711d2013-02-23 11:24:36 -08003047 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003048 return true;
3049 }
3050
Craig Mautnerd2328952013-03-05 12:46:26 -08003051 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003052 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3053 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3054 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3055 ActivityRecord r = activities.get(activityNdx);
3056 if (r.resultTo == self && r.requestCode == requestCode) {
3057 if ((r.resultWho == null && resultWho == null) ||
3058 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3059 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3060 false);
3061 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003062 }
3063 }
3064 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003065 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003066 }
3067
Adrian Roos20d7df32016-01-12 18:59:43 +01003068 final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003069 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003070 TaskRecord finishedTask = null;
3071 if (r == null || r.app != app) {
3072 return null;
3073 }
3074 Slog.w(TAG, " Force finishing activity "
3075 + r.intent.getComponent().flattenToShortString());
3076 int taskNdx = mTaskHistory.indexOf(r.task);
3077 int activityNdx = r.task.mActivities.indexOf(r);
3078 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3079 finishedTask = r.task;
3080 // Also terminate any activities below it that aren't yet
3081 // stopped, to avoid a situation where one will get
3082 // re-start our crashing activity once it gets resumed again.
3083 --activityNdx;
3084 if (activityNdx < 0) {
3085 do {
3086 --taskNdx;
3087 if (taskNdx < 0) {
3088 break;
3089 }
3090 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3091 } while (activityNdx < 0);
3092 }
3093 if (activityNdx >= 0) {
3094 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3095 if (r.state == ActivityState.RESUMED
3096 || r.state == ActivityState.PAUSING
3097 || r.state == ActivityState.PAUSED) {
3098 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3099 Slog.w(TAG, " Force finishing activity "
3100 + r.intent.getComponent().flattenToShortString());
3101 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003102 }
3103 }
3104 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003105 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003106 }
3107
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003108 final void finishVoiceTask(IVoiceInteractionSession session) {
3109 IBinder sessionBinder = session.asBinder();
3110 boolean didOne = false;
3111 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3112 TaskRecord tr = mTaskHistory.get(taskNdx);
3113 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3114 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3115 ActivityRecord r = tr.mActivities.get(activityNdx);
3116 if (!r.finishing) {
3117 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3118 false);
3119 didOne = true;
3120 }
3121 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003122 } else {
3123 // Check if any of the activities are using voice
3124 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3125 ActivityRecord r = tr.mActivities.get(activityNdx);
3126 if (r.voiceSession != null
3127 && r.voiceSession.asBinder() == sessionBinder) {
3128 // Inform of cancellation
3129 r.clearVoiceSessionLocked();
3130 try {
3131 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3132 null);
3133 } catch (RemoteException re) {
3134 // Ok
3135 }
3136 // TODO: VI This is redundant in some cases
3137 mService.finishRunningVoiceLocked();
3138 break;
3139 }
3140 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003141 }
3142 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003143 Slog.d(TAG, "ActivityStack.finishVoiceTask()");
3144
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003145 if (didOne) {
3146 mService.updateOomAdjLocked();
3147 }
3148 }
3149
Craig Mautnerd2328952013-03-05 12:46:26 -08003150 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003151 ArrayList<ActivityRecord> activities = r.task.mActivities;
3152 for (int index = activities.indexOf(r); index >= 0; --index) {
3153 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003154 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003155 break;
3156 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003157 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003158 }
3159 return true;
3160 }
3161
Dianne Hackborn5c607432012-02-28 14:44:19 -08003162 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3163 // send the result
3164 ActivityRecord resultTo = r.resultTo;
3165 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003166 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003167 + " who=" + r.resultWho + " req=" + r.requestCode
3168 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003169 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003170 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003171 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003172 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003173 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003174 if (r.info.applicationInfo.uid > 0) {
3175 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3176 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003177 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003178 }
3179 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3180 resultData);
3181 r.resultTo = null;
3182 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003183 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003184
3185 // Make sure this HistoryRecord is not holding on to other resources,
3186 // because clients have remote IPC references to this object so we
3187 // can't assume that will go away and want to avoid circular IPC refs.
3188 r.results = null;
3189 r.pendingResults = null;
3190 r.newIntents = null;
3191 r.icicle = null;
3192 }
3193
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003194 /**
3195 * @return Returns true if this activity has been removed from the history
3196 * list, or false if it is still in the list and will be removed later.
3197 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003198 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3199 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003200 if (r.finishing) {
3201 Slog.w(TAG, "Duplicate finish request for " + r);
3202 return false;
3203 }
3204
Wale Ogunwale7d701172015-03-11 15:36:30 -07003205 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003206 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003207 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003208 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003209 task.taskId, r.shortComponentName, reason);
3210 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003211 final int index = activities.indexOf(r);
3212 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003213 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003214 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003215 // If the caller asked that this activity (and all above it)
3216 // be cleared when the task is reset, don't lose that information,
3217 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003218 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003219 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003220 }
3221 }
3222
3223 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003224
Craig Mautner299f9602015-01-26 09:47:33 -08003225 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003226
Dianne Hackborn5c607432012-02-28 14:44:19 -08003227 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003228
Craig Mautnerde4ef022013-04-07 19:01:33 -07003229 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003230 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003231 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003232 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003233 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003234 ? AppTransition.TRANSIT_TASK_CLOSE
3235 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003236
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003237 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003238 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003239
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003240 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003241 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3242 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3243 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003244 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003245 }
3246
Craig Mautneraea74a52014-03-08 14:23:10 -08003247 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003248 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003249 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003250 } else if (r.state != ActivityState.PAUSING) {
3251 // If the activity is PAUSING, we will complete the finish once
3252 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003253 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003254 if (r.visible) {
3255 mWindowManager.setAppVisibility(r.appToken, false);
3256 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003257 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003258 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003259 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003260 }
3261
3262 return false;
3263 }
3264
Craig Mautnerf3333272013-04-22 10:55:53 -07003265 static final int FINISH_IMMEDIATELY = 0;
3266 static final int FINISH_AFTER_PAUSE = 1;
3267 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003268
Craig Mautnerf3333272013-04-22 10:55:53 -07003269 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003270 // First things first: if this activity is currently visible,
3271 // and the resumed activity is not yet visible, then hold off on
3272 // finishing until the resumed one becomes visible.
3273 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003274 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Chong Zhang46b1ac62016-02-18 17:53:57 -08003275 addToStopping(r, false /* immediate */);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003276 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003277 if (DEBUG_STATES) Slog.v(TAG_STATES,
3278 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003279 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003280 if (oomAdj) {
3281 mService.updateOomAdjLocked();
3282 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003283 return r;
3284 }
3285
3286 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003287 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003288 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003289 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003290 if (mResumedActivity == r) {
3291 mResumedActivity = null;
3292 }
3293 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003294 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003295 r.state = ActivityState.FINISHING;
3296
3297 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003298 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003299 || prevState == ActivityState.STOPPED
3300 || prevState == ActivityState.INITIALIZING) {
3301 // If this activity is already stopped, we can just finish
3302 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003303 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003304 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003305 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003306 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003307 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003308 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003309 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3310 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003311 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003312 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003313
3314 // Need to go through the full pause cycle to get this
3315 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003316 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003317 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003318 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003319 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003320 return r;
3321 }
3322
Craig Mautneree36c772014-07-16 14:56:05 -07003323 void finishAllActivitiesLocked(boolean immediately) {
3324 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003325 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3326 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3327 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3328 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003329 noActivitiesInStack = false;
3330 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003331 continue;
3332 }
Craig Mautneree36c772014-07-16 14:56:05 -07003333 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003334 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3335 }
3336 }
Craig Mautneree36c772014-07-16 14:56:05 -07003337 if (noActivitiesInStack) {
3338 mActivityContainer.onTaskListEmptyLocked();
3339 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003340 }
3341
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003342 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3343 // Basic case: for simple app-centric recents, we need to recreate
3344 // the task if the affinity has changed.
3345 if (srec == null || srec.task.affinity == null ||
3346 !srec.task.affinity.equals(destAffinity)) {
3347 return true;
3348 }
3349 // Document-centric case: an app may be split in to multiple documents;
3350 // they need to re-create their task if this current activity is the root
3351 // of a document, unless simply finishing it will return them to the the
3352 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003353 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3354 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003355 // Okay, this activity is at the root of its task. What to do, what to do...
3356 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3357 // Finishing won't return to an application, so we need to recreate.
3358 return true;
3359 }
3360 // We now need to get the task below it to determine what to do.
3361 int taskIdx = mTaskHistory.indexOf(srec.task);
3362 if (taskIdx <= 0) {
3363 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3364 return false;
3365 }
3366 if (taskIdx == 0) {
3367 // At the bottom of the stack, nothing to go back to.
3368 return true;
3369 }
3370 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3371 if (!srec.task.affinity.equals(prevTask.affinity)) {
3372 // These are different apps, so need to recreate.
3373 return true;
3374 }
3375 }
3376 return false;
3377 }
3378
Wale Ogunwale7d701172015-03-11 15:36:30 -07003379 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003380 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003381 final TaskRecord task = srec.task;
3382 final ArrayList<ActivityRecord> activities = task.mActivities;
3383 final int start = activities.indexOf(srec);
3384 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003385 return false;
3386 }
3387 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003388 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003389 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003390 final ComponentName dest = destIntent.getComponent();
3391 if (start > 0 && dest != null) {
3392 for (int i = finishTo; i >= 0; i--) {
3393 ActivityRecord r = activities.get(i);
3394 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003395 r.info.name.equals(dest.getClassName())) {
3396 finishTo = i;
3397 parent = r;
3398 foundParentInTask = true;
3399 break;
3400 }
3401 }
3402 }
3403
3404 IActivityController controller = mService.mController;
3405 if (controller != null) {
3406 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3407 if (next != null) {
3408 // ask watcher if this is allowed
3409 boolean resumeOK = true;
3410 try {
3411 resumeOK = controller.activityResuming(next.packageName);
3412 } catch (RemoteException e) {
3413 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003414 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003415 }
3416
3417 if (!resumeOK) {
3418 return false;
3419 }
3420 }
3421 }
3422 final long origId = Binder.clearCallingIdentity();
3423 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003424 ActivityRecord r = activities.get(i);
3425 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003426 // Only return the supplied result for the first activity finished
3427 resultCode = Activity.RESULT_CANCELED;
3428 resultData = null;
3429 }
3430
3431 if (parent != null && foundParentInTask) {
3432 final int parentLaunchMode = parent.info.launchMode;
3433 final int destIntentFlags = destIntent.getFlags();
3434 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3435 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3436 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3437 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003438 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3439 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003440 } else {
3441 try {
3442 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3443 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003444 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3445 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3446 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003447 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3448 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003449 foundParentInTask = res == ActivityManager.START_SUCCESS;
3450 } catch (RemoteException e) {
3451 foundParentInTask = false;
3452 }
3453 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003454 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003455 }
3456 }
3457 Binder.restoreCallingIdentity(origId);
3458 return foundParentInTask;
3459 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003460 /**
3461 * Perform the common clean-up of an activity record. This is called both
3462 * as part of destroyActivityLocked() (when destroying the client-side
3463 * representation) and cleaning things up as a result of its hosting
3464 * processing going away, in which case there is no remaining client-side
3465 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003466 *
3467 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003468 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003469 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3470 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003471 if (mResumedActivity == r) {
3472 mResumedActivity = null;
3473 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003474 if (mPausingActivity == r) {
3475 mPausingActivity = null;
3476 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003477 mService.resetFocusedActivityIfNeededLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003478
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003479 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003480 r.frozenBeforeDestroy = false;
3481
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003482 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003483 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003484 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003485 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003486 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003487 }
3488
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003489 // Make sure this record is no longer in the pending finishes list.
3490 // This could happen, for example, if we are trimming activities
3491 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003492 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003493 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003494
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003495 // Remove any pending results.
3496 if (r.finishing && r.pendingResults != null) {
3497 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3498 PendingIntentRecord rec = apr.get();
3499 if (rec != null) {
3500 mService.cancelIntentSenderLocked(rec, false);
3501 }
3502 }
3503 r.pendingResults = null;
3504 }
3505
3506 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003507 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003508 }
3509
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003510 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003511 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003512 if (getVisibleBehindActivity() == r) {
3513 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003514 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003515 }
3516
3517 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003518 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003519 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003520 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003521 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003522 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003523 }
3524
Craig Mautner299f9602015-01-26 09:47:33 -08003525 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003526 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003527 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003528 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003529 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3530 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3531
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003532 r.takeFromHistory();
3533 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003534 if (DEBUG_STATES) Slog.v(TAG_STATES,
3535 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003536 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003537 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003538 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003539 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003540 if (VALIDATE_TOKENS) {
3541 validateAppTokensLocked();
3542 }
Craig Mautner312ba862014-02-10 17:55:01 -08003543 final TaskRecord task = r.task;
3544 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003545 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003546 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003547 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003548 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003549 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003550 }
Craig Mautner299f9602015-01-26 09:47:33 -08003551 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003552 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003553 cleanUpActivityServicesLocked(r);
3554 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003555 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003556
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003557 /**
3558 * Perform clean-up of service connections in an activity record.
3559 */
3560 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3561 // Throw away any services that have been bound by this activity.
3562 if (r.connections != null) {
3563 Iterator<ConnectionRecord> it = r.connections.iterator();
3564 while (it.hasNext()) {
3565 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003566 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003567 }
3568 r.connections = null;
3569 }
3570 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003571
Craig Mautneree2e45a2014-06-27 12:10:03 -07003572 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003573 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003574 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003575 mHandler.sendMessage(msg);
3576 }
3577
Craig Mautneree2e45a2014-06-27 12:10:03 -07003578 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003579 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003580 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003581 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3582 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3583 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3584 final ActivityRecord r = activities.get(activityNdx);
3585 if (r.finishing) {
3586 continue;
3587 }
3588 if (r.fullscreen) {
3589 lastIsOpaque = true;
3590 }
3591 if (owner != null && r.app != owner) {
3592 continue;
3593 }
3594 if (!lastIsOpaque) {
3595 continue;
3596 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003597 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003598 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003599 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003600 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003601 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003602 activityRemoved = true;
3603 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003604 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003605 }
3606 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003607 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003608 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003609 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003610 }
3611
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003612 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3613 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003614 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3615 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003616 + " pausing=" + mPausingActivity + " for reason " + reason);
3617 return destroyActivityLocked(r, true, reason);
3618 }
3619 return false;
3620 }
3621
3622 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3623 String reason) {
3624 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003625 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003626 int maxTasks = tasks.size() / 4;
3627 if (maxTasks < 1) {
3628 maxTasks = 1;
3629 }
3630 int numReleased = 0;
3631 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3632 final TaskRecord task = mTaskHistory.get(taskNdx);
3633 if (!tasks.contains(task)) {
3634 continue;
3635 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003636 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003637 int curNum = 0;
3638 final ArrayList<ActivityRecord> activities = task.mActivities;
3639 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3640 final ActivityRecord activity = activities.get(actNdx);
3641 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003642 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003643 + " in state " + activity.state + " resumed=" + mResumedActivity
3644 + " pausing=" + mPausingActivity + " for reason " + reason);
3645 destroyActivityLocked(activity, true, reason);
3646 if (activities.get(actNdx) != activity) {
3647 // Was removed from list, back up so we don't miss the next one.
3648 actNdx--;
3649 }
3650 curNum++;
3651 }
3652 }
3653 if (curNum > 0) {
3654 numReleased += curNum;
3655 maxTasks--;
3656 if (mTaskHistory.get(taskNdx) != task) {
3657 // The entire task got removed, back up so we don't miss the next one.
3658 taskNdx--;
3659 }
3660 }
3661 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003662 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3663 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003664 return numReleased;
3665 }
3666
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003667 /**
3668 * Destroy the current CLIENT SIDE instance of an activity. This may be
3669 * called both when actually finishing an activity, or when performing
3670 * a configuration switch where we destroy the current client-side object
3671 * but then create a new client-side object for this same HistoryRecord.
3672 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003673 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003674 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3675 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003676 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003677 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003678 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003679 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003680
3681 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003682
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003683 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003684
3685 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003686
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003687 if (hadApp) {
3688 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003689 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003690 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3691 mService.mHeavyWeightProcess = null;
3692 mService.mHandler.sendEmptyMessage(
3693 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3694 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003695 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003696 // Update any services we are bound to that might care about whether
3697 // their client may have activities.
3698 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003699 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003700 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003701 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003702 }
3703 }
3704
3705 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003706
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003707 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003708 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003709 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003710 r.configChangeFlags);
3711 } catch (Exception e) {
3712 // We can just ignore exceptions here... if the process
3713 // has crashed, our death notification will clean things
3714 // up.
3715 //Slog.w(TAG, "Exception thrown during finish", e);
3716 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003717 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003718 removedFromHistory = true;
3719 skipDestroy = true;
3720 }
3721 }
3722
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003723 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003724
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003725 // If the activity is finishing, we need to wait on removing it
3726 // from the list to give it a chance to do its cleanup. During
3727 // that time it may make calls back with its token so we need to
3728 // be able to find it on the list and so we don't want to remove
3729 // it from the list yet. Otherwise, we can just immediately put
3730 // it in the destroyed state since we are not removing it from the
3731 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003732 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003733 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003734 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003735 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003736 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003737 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3738 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003739 if (DEBUG_STATES) Slog.v(TAG_STATES,
3740 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003741 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003742 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003743 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003744 }
3745 } else {
3746 // remove this record from the history.
3747 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003748 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003749 removedFromHistory = true;
3750 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003751 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003752 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003753 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003754 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003755 }
3756 }
3757
3758 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003759
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003760 if (!mLRUActivities.remove(r) && hadApp) {
3761 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3762 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003763
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003764 return removedFromHistory;
3765 }
3766
Craig Mautner299f9602015-01-26 09:47:33 -08003767 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003768 final long origId = Binder.clearCallingIdentity();
3769 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003770 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003771 if (r != null) {
3772 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003773 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003774 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003775
Wale Ogunwale60454db2015-01-23 16:05:07 -08003776 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003777 if (r.state == ActivityState.DESTROYING) {
3778 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003779 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003780 }
3781 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003782 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003783 } finally {
3784 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003785 }
3786 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003787
Todd Kennedyaab56db2015-01-30 09:39:53 -08003788 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003789 if (hasVisibleBehindActivity() &&
3790 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08003791 if (r == topRunningActivityLocked() && getStackVisibilityLocked() == STACK_VISIBLE) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003792 // Don't release the top activity if it has requested to run behind the next
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08003793 // activity and the stack is currently visible.
Craig Mautner64ccb702014-10-01 09:38:40 -07003794 return;
3795 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003796 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003797 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003798 " thread=" + r.app.thread);
3799 if (r != null && r.app != null && r.app.thread != null) {
3800 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003801 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003802 } catch (RemoteException e) {
3803 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003804 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003805 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003806 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003807 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003808 }
3809 }
3810 }
3811
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003812 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003813 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3814 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003815 if (r != null) {
3816 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003817 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003818 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003819 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003820 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003821 }
3822
Jose Lima4b6c6692014-08-12 17:41:12 -07003823 boolean hasVisibleBehindActivity() {
3824 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003825 }
3826
Jose Lima4b6c6692014-08-12 17:41:12 -07003827 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003828 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003829 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003830 }
3831 }
3832
Jose Lima4b6c6692014-08-12 17:41:12 -07003833 ActivityRecord getVisibleBehindActivity() {
3834 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003835 }
3836
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003837 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3838 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003839 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003840 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3841 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003842 while (i > 0) {
3843 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003844 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003845 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003846 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003847 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003848 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003849 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003850 }
3851 }
3852 }
3853
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003854 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3855 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003856 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3857 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003858 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3859 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003860 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003861 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003862 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3863 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003864
3865 boolean hasVisibleActivities = false;
3866
3867 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003868 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003869 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3870 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003871 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3872 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3873 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3874 final ActivityRecord r = activities.get(activityNdx);
3875 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003876 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3877 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003878 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003879 if (r.visible) {
3880 hasVisibleActivities = true;
3881 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003882 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003883 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3884 // Don't currently have state for the activity, or
3885 // it is finishing -- always remove it.
3886 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003887 } else if (!r.visible && r.launchCount > 2 &&
3888 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003889 // We have launched this activity too many times since it was
3890 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003891 // (Note if the activity is visible, we don't remove the record.
3892 // We leave the dead window on the screen but the process will
3893 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003894 remove = true;
3895 } else {
3896 // The process may be gone, but the activity lives on!
3897 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003898 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003899 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003900 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3901 "Removing activity " + r + " from stack at " + i
3902 + ": haveState=" + r.haveState
3903 + " stateNotNeeded=" + r.stateNotNeeded
3904 + " finishing=" + r.finishing
3905 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003906 if (!r.finishing) {
3907 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3908 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3909 r.userId, System.identityHashCode(r),
3910 r.task.taskId, r.shortComponentName,
3911 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003912 if (r.state == ActivityState.RESUMED) {
3913 mService.updateUsageStats(r, false);
3914 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003915 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003916 } else {
3917 // We have the current state for this activity, so
3918 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003919 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3920 if (DEBUG_APP) Slog.v(TAG_APP,
3921 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003922 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003923 // Set nowVisible to previous visible state. If the app was visible while
3924 // it died, we leave the dead window on screen so it's basically visible.
3925 // This is needed when user later tap on the dead window, we need to stop
3926 // other apps when user transfers focus to the restarted activity.
3927 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003928 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003929 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003930 "App died, clearing saved state of " + r);
3931 r.icicle = null;
3932 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003933 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003934 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003935 if (remove) {
3936 removeActivityFromHistoryLocked(r, "appDied");
3937 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003938 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003939 }
3940 }
3941
3942 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003943 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003944
Chong Zhang280d3322015-11-03 17:27:26 -08003945 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003946 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003947 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003948 if (r != null && r.state != ActivityState.RESUMED) {
3949 r.updateOptionsLocked(options);
3950 } else {
3951 ActivityOptions.abort(options);
3952 }
3953 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003954 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003955 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003956
Craig Mautner21d24a22014-04-23 11:45:37 -07003957 void updateTaskMovement(TaskRecord task, boolean toFront) {
3958 if (task.isPersistable) {
3959 task.mLastTimeMoved = System.currentTimeMillis();
3960 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3961 // recently will be most negative, tasks sent to the bottom before that will be less
3962 // negative. Similarly for recent tasks moved to the top which will be most positive.
3963 if (!toFront) {
3964 task.mLastTimeMoved *= -1;
3965 }
3966 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003967 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003968 }
3969
Craig Mautner84984fa2014-06-19 11:19:20 -07003970 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003971 final int top = mTaskHistory.size() - 1;
3972 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3973 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003974 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003975 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3976 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003977 mTaskHistory.remove(taskNdx);
3978 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003979 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003980 return;
3981 }
3982 }
3983 }
3984
Chong Zhang280d3322015-11-03 17:27:26 -08003985 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003986 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003987 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003988
Craig Mautner11bf9a52013-02-19 14:08:51 -08003989 final int numTasks = mTaskHistory.size();
3990 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003991 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003992 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003993 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003994 ActivityOptions.abort(options);
3995 } else {
3996 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3997 }
3998 return;
3999 }
4000
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004001 if (timeTracker != null) {
4002 // The caller wants a time tracker associated with this task.
4003 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4004 tr.mActivities.get(i).appTimeTracker = timeTracker;
4005 }
4006 }
4007
Craig Mautner11bf9a52013-02-19 14:08:51 -08004008 // Shift all activities with this task up to the top
4009 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004010 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004011
Chong Zhang45c25ce2015-08-10 22:18:26 -07004012 // Don't refocus if invisible to current user
4013 ActivityRecord top = tr.getTopActivity();
4014 if (!okToShowLocked(top)) {
4015 addRecentActivityLocked(top);
4016 ActivityOptions.abort(options);
4017 return;
4018 }
4019
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004020 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004021 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004022 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004023
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004024 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004025 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004026 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004027 if (r != null) {
4028 mNoAnimActivities.add(r);
4029 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004030 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004031 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08004032 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004033 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004034
Wale Ogunwaled046a012015-12-24 13:05:59 -08004035 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004036 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004037
4038 if (VALIDATE_TOKENS) {
4039 validateAppTokensLocked();
4040 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004041 }
4042
4043 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004044 * Worker method for rearranging history stack. Implements the function of moving all
4045 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004046 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004047 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004048 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4049 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004050 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004051 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004052 * @return Returns true if the move completed, false if not.
4053 */
Craig Mautner299f9602015-01-26 09:47:33 -08004054 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004055 final TaskRecord tr = taskForIdLocked(taskId);
4056 if (tr == null) {
4057 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4058 return false;
4059 }
4060
4061 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07004062 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004063
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004064 // If we have a watcher, preflight the move before committing to it. First check
4065 // for *other* available tasks, but if none are available, then try again allowing the
4066 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004067 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004068 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004069 if (next == null) {
4070 next = topRunningActivityLocked(null, 0);
4071 }
4072 if (next != null) {
4073 // ask watcher if this is allowed
4074 boolean moveOK = true;
4075 try {
4076 moveOK = mService.mController.activityResuming(next.packageName);
4077 } catch (RemoteException e) {
4078 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004079 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004080 }
4081 if (!moveOK) {
4082 return false;
4083 }
4084 }
4085 }
4086
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004087 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004088
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004089 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4090 // For the case where we are moving the home task back and there is an activity visible
4091 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4092 // activity to maintain order with what the user is seeing.
4093 final ActivityStack fullscreenStack =
4094 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4095 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4096 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4097 mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004098 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004099 return true;
4100 }
4101 }
4102
riddle_hsuc215a4f2014-12-27 12:10:45 +08004103 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004104
4105 // If true, we should resume the home activity next if the task we are moving to the
4106 // back is over the home stack. We force to false if the task we are moving to back
4107 // is the home task and we don't want it resumed after moving to the back.
4108 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4109 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004110 final TaskRecord nextTask = getNextTask(tr);
4111 if (nextTask != null) {
4112 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4113 } else {
4114 prevIsHome = true;
4115 }
4116 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004117 mTaskHistory.remove(tr);
4118 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004119 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004120
Craig Mautnerc8143c62013-09-03 12:15:57 -07004121 // There is an assumption that moving a task to the back moves it behind the home activity.
4122 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004123 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004124 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4125 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004126 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004127 break;
4128 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004129 if (taskNdx == 1) {
4130 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004131 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004132 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004133 }
4134
Craig Mautner299f9602015-01-26 09:47:33 -08004135 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004136 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004137
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004138 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004139 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004140 }
4141
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004142 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004143 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004144 if (!mService.mBooting && !mService.mBooted) {
4145 // Not ready yet!
4146 return false;
4147 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004148 final int taskToReturnTo = tr.getTaskToReturnTo();
4149 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004150 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004151 }
4152
Wale Ogunwaled046a012015-12-24 13:05:59 -08004153 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004154 return true;
4155 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004156
Craig Mautner8849a5e2013-04-02 16:41:03 -07004157 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004158 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004159 final Uri data = r.intent.getData();
4160 final String strData = data != null ? data.toSafeString() : null;
4161
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004162 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004163 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004164 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004165 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004166 }
4167
4168 /**
4169 * Make sure the given activity matches the current configuration. Returns
4170 * false if the activity had to be destroyed. Returns true if the
4171 * configuration is the same, or the activity will remain running as-is
4172 * for whatever reason. Ensures the HistoryRecord is updated with the
4173 * correct configuration and all other bookkeeping is handled.
4174 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004175 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4176 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004177 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004178 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004179 "Skipping config check (will change): " + r);
4180 return true;
4181 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004182
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004183 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004184 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004185
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004186 // Short circuit: if the two configurations are the exact same
4187 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004188 final Configuration newConfig = mService.mConfiguration;
4189 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004190 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004191 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004192 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004193 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004194 "Configuration unchanged in " + r);
4195 return true;
4196 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004197
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004198 // We don't worry about activities that are finishing.
4199 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004200 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004201 "Configuration doesn't matter in finishing " + r);
4202 r.stopFreezingScreenLocked(false);
4203 return true;
4204 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004205
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004206 // Okay we now are going to make this activity have the new config.
4207 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004208 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004209 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004210 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004211 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004212
Filip Gruszczynski23493322015-07-29 17:02:59 -07004213 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004214 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004215 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004216 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004217 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004218 // There are no significant differences, so we won't relaunch but should still deliver
4219 // the new configuration to the client process.
4220 r.scheduleConfigurationChanged(taskConfig, false);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004221 return true;
4222 }
4223
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004224 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4225 "Configuration changes for " + r + " ; taskChanges="
4226 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4227 + Configuration.configurationDiffToString(changes));
4228
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004229 // If the activity isn't currently running, just leave the new
4230 // configuration and it will pick that up next time it starts.
4231 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004232 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004233 "Configuration doesn't matter not running " + r);
4234 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004235 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004236 return true;
4237 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004238
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004239 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004240 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4241 "Checking to restart " + r.info.name + ": changed=0x"
4242 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004243 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4244 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004245
Dianne Hackborne6676352011-06-01 16:51:20 -07004246 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004247 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4248 r.configChangeFlags |= changes;
4249 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004250 r.forceNewConfig = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004251 preserveWindow &= isResizeOnlyChange(changes);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004252 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004253 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004254 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004255 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004256 } else if (r.state == ActivityState.PAUSING) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004257 // A little annoying: we are waiting for this activity to finish pausing. Let's not
4258 // do anything now, but just flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004259 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004260 "Config is skipping already pausing " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004261 r.deferRelaunchUntilPaused = true;
4262 r.preserveWindowOnDeferredRelaunch = preserveWindow;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004263 return true;
4264 } else if (r.state == ActivityState.RESUMED) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004265 // Try to optimize this case: the configuration is changing and we need to restart
4266 // the top, resumed activity. Instead of doing the normal handshaking, just say
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004267 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004268 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004269 "Config is relaunching resumed " + r);
Chong Zhang2b79af12016-02-10 18:47:06 -08004270
4271 if (DEBUG_STATES && !r.visible) {
4272 Slog.v(TAG_STATES, "Config is relaunching resumed invisible activity " + r
4273 + " called by " + Debug.getCallers(4));
4274 }
4275
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004276 relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004277 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004278 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004279 "Config is relaunching non-resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004280 relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004281 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004282
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004283 // All done... tell the caller we weren't able to keep this activity around.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004284 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004285 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004286
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004287 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004288 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004289 // changes is always sent to all processes when they happen so it can just use whatever
4290 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004291 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004292 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004293
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004294 return true;
4295 }
4296
Filip Gruszczynski23493322015-07-29 17:02:59 -07004297 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4298 Configuration oldTaskOverride) {
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08004299
4300 // If we went from full-screen to non-full-screen, make sure to use the correct
4301 // configuration task diff, so the diff stays as small as possible.
4302 if (Configuration.EMPTY.equals(oldTaskOverride)
4303 && !Configuration.EMPTY.equals(taskConfig)) {
4304 oldTaskOverride = record.task.extractOverrideConfig(record.configuration);
4305 }
4306
Jorim Jaggi1b025a62016-02-03 19:27:49 -08004307 // Conversely, do the same when going the other direction.
4308 if (Configuration.EMPTY.equals(taskConfig)
4309 && !Configuration.EMPTY.equals(oldTaskOverride)) {
4310 taskConfig = record.task.extractOverrideConfig(record.configuration);
4311 }
4312
Filip Gruszczynski23493322015-07-29 17:02:59 -07004313 // Determine what has changed. May be nothing, if this is a config
4314 // that has come back from the app after going idle. In that case
4315 // we just want to leave the official config object now in the
4316 // activity and do nothing else.
4317 int taskChanges = oldTaskOverride.diff(taskConfig);
4318 // We don't want to use size changes if they don't cross boundaries that are important to
4319 // the app.
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004320 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004321 final boolean crosses = record.crossesHorizontalSizeThreshold(
4322 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4323 || record.crossesVerticalSizeThreshold(
4324 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4325 if (!crosses) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004326 taskChanges &= ~CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004327 }
4328 }
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004329 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004330 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4331 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004332 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004333 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004334 }
4335 }
4336 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4337 }
4338
4339 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4340 Configuration oldTaskOverride, int taskChanges) {
4341 if (taskChanges == 0) {
4342 // {@link Configuration#diff} doesn't catch changes from unset values.
4343 // Check for changes we care about.
4344 if (oldTaskOverride.orientation != taskConfig.orientation) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004345 taskChanges |= CONFIG_ORIENTATION;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004346 }
4347 // We want to explicitly track situations where the size configuration goes from
4348 // undefined to defined. We don't care about crossing the threshold in that case,
4349 // because there is no threshold.
4350 final int oldHeight = oldTaskOverride.screenHeightDp;
4351 final int newHeight = taskConfig.screenHeightDp;
4352 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4353 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4354 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004355 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004356 }
4357 final int oldWidth = oldTaskOverride.screenWidthDp;
4358 final int newWidth = taskConfig.screenWidthDp;
4359 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4360 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4361 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004362 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004363 }
4364 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4365 final int newSmallest = taskConfig.smallestScreenWidthDp;
4366 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4367 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4368 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004369 taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4370 }
4371 final int oldLayout = oldTaskOverride.screenLayout;
4372 final int newLayout = taskConfig.screenLayout;
4373 if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4374 || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4375 taskChanges |= CONFIG_SCREEN_LAYOUT;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004376 }
4377 }
4378 return taskChanges;
4379 }
4380
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004381 private static boolean isResizeOnlyChange(int change) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004382 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4383 | CONFIG_SCREEN_LAYOUT)) == 0;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004384 }
4385
Wale Ogunwale83301a92015-09-24 15:54:08 -07004386 private void relaunchActivityLocked(
4387 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4388 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004389 r.configChangeFlags = 0;
Wale Ogunwale83301a92015-09-24 15:54:08 -07004390 return;
4391 }
4392
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004393 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004394 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004395 if (andResume) {
4396 results = r.results;
4397 newIntents = r.newIntents;
4398 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004399 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4400 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004401 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004402 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004403 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004404 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004405
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004406 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004407
Craig Mautner34b73df2014-01-12 21:11:08 -08004408 mStackSupervisor.removeChildActivityContainers(r);
4409
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004410 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004411 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
Wale Ogunwale5658e4b2016-02-12 12:22:19 -08004412 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r
4413 + " callers=" + Debug.getCallers(6));
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004414 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004415 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4416 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004417 new Configuration(r.task.mOverrideConfig), preserveWindow);
Jorim Jaggife89d122015-12-22 16:28:44 +01004418 mStackSupervisor.activityRelaunchingLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004419 // Note: don't need to call pauseIfSleepingLocked() here, because
4420 // the caller will only pass in 'andResume' if this activity is
4421 // currently resumed, which implies we aren't sleeping.
4422 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004423 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004424 }
4425
4426 if (andResume) {
Chong Zhang2b79af12016-02-10 18:47:06 -08004427 if (DEBUG_STATES) {
4428 Slog.d(TAG_STATES, "Resumed after relaunch " + r);
4429 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004430 r.state = ActivityState.RESUMED;
Chong Zhang2b79af12016-02-10 18:47:06 -08004431 // Relaunch-resume could happen either when the app is already in the front,
4432 // or while it's being brought to front. In the latter case, it's marked RESUMED
4433 // but not yet visible (or stopped). We need to complete the resume here as the
4434 // code in resumeTopActivityInnerLocked to complete the resume might be skipped.
4435 if (!r.visible || r.stopped) {
4436 mWindowManager.setAppVisibility(r.appToken, true);
4437 completeResumeLocked(r);
4438 } else {
4439 r.results = null;
4440 r.newIntents = null;
4441 }
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004442 } else {
4443 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4444 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004445 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004446
4447 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004448 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004449
4450 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004451 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4452 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4453 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4454 final ActivityRecord r = activities.get(activityNdx);
4455 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004456 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004457 }
4458 if (r.fullscreen && !r.finishing) {
4459 return false;
4460 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004461 }
4462 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004463 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004464 if (r == null) {
4465 return false;
4466 }
4467 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4468 + " would have returned true for r=" + r);
4469 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004470 }
4471
4472 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004473 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4474 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4475 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4476 final ActivityRecord r = activities.get(activityNdx);
4477 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004478 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004479 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004480 }
4481 }
4482 }
4483
Wale Ogunwale540e1232015-05-01 15:35:39 -07004484 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4485 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004486 boolean didSomething = false;
4487 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004488 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004489 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4490 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4491 int numActivities = activities.size();
4492 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4493 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004494 final boolean sameComponent =
4495 (r.packageName.equals(packageName) && (filterByClasses == null
4496 || filterByClasses.contains(r.realActivity.getClassName())))
4497 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004498 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004499 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004500 && (r.app == null || evenPersistent || !r.app.persistent)) {
4501 if (!doit) {
4502 if (r.finishing) {
4503 // If this activity is just finishing, then it is not
4504 // interesting as far as something to stop.
4505 continue;
4506 }
4507 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004508 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004509 if (r.isHomeActivity()) {
4510 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4511 Slog.i(TAG, "Skip force-stop again " + r);
4512 continue;
4513 } else {
4514 homeActivity = r.realActivity;
4515 }
4516 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004517 didSomething = true;
4518 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004519 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004520 if (r.app != null) {
4521 r.app.removed = true;
4522 }
4523 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004524 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004525 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004526 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4527 true)) {
4528 // r has been deleted from mActivities, accommodate.
4529 --numActivities;
4530 --activityNdx;
4531 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004532 }
4533 }
4534 }
4535 return didSomething;
4536 }
4537
Dianne Hackborn09233282014-04-30 11:33:59 -07004538 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004539 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4540 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004541 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004542 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004543 if (task.getTopActivity() == null) {
4544 continue;
4545 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004546 ActivityRecord r = null;
4547 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004548 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004549 int numActivities = 0;
4550 int numRunning = 0;
4551 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004552 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004553 continue;
4554 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004555 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004556 tmp = activities.get(activityNdx);
4557 if (tmp.finishing) {
4558 continue;
4559 }
4560 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004561
Craig Mautneraab647e2013-02-28 16:31:36 -08004562 // Initialize state for next task if needed.
4563 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4564 top = r;
4565 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004566 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004567
4568 // Add 'r' into the current task.
4569 numActivities++;
4570 if (r.app != null && r.app.thread != null) {
4571 numRunning++;
4572 }
4573
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004574 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004575 TAG, r.intent.getComponent().flattenToShortString()
4576 + ": task=" + r.task);
4577 }
4578
4579 RunningTaskInfo ci = new RunningTaskInfo();
4580 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004581 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004582 ci.baseActivity = r.intent.getComponent();
4583 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004584 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004585 if (focusedStack && topTask) {
4586 // Give the latest time to ensure foreground task can be sorted
4587 // at the first, because lastActiveTime of creating task is 0.
4588 ci.lastActiveTime = System.currentTimeMillis();
4589 topTask = false;
4590 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004591
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004592 if (top.task != null) {
4593 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004594 }
4595 ci.numActivities = numActivities;
4596 ci.numRunning = numRunning;
Wale Ogunwale21b60582016-01-27 12:34:16 -08004597 ci.isDockable = task.canGoInDockedStack();
Craig Mautneraab647e2013-02-28 16:31:36 -08004598 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004599 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004600 }
4601
4602 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004603 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004604 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004605 if (top >= 0) {
4606 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4607 int activityTop = activities.size() - 1;
4608 if (activityTop > 0) {
4609 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4610 "unhandled-back", true);
4611 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004612 }
4613 }
4614
Craig Mautner6b74cb52013-09-27 17:02:21 -07004615 /**
4616 * Reset local parameters because an app's activity died.
4617 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004618 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004619 */
4620 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004621 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004622 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004623 "App died while pausing: " + mPausingActivity);
4624 mPausingActivity = null;
4625 }
4626 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4627 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004628 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004629 }
4630
Craig Mautner19091252013-10-05 00:03:53 -07004631 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004632 }
4633
Craig Mautnercae015f2013-02-08 14:31:27 -08004634 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004635 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4636 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4637 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4638 final ActivityRecord r = activities.get(activityNdx);
4639 if (r.app == app) {
4640 Slog.w(TAG, " Force finishing activity "
4641 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004642 // Force the destroy to skip right to removal.
4643 r.app = null;
4644 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004645 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004646 }
4647 }
4648 }
4649
Dianne Hackborn390517b2013-05-30 15:03:32 -07004650 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004651 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004652 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004653 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4654 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004655 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4656 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004657 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004658 " Task id #" + task.taskId + "\n" +
4659 " mFullscreen=" + task.mFullscreen + "\n" +
4660 " mBounds=" + task.mBounds + "\n" +
4661 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004662 if (printed) {
4663 header = null;
4664 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004665 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004666 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004667 }
4668
4669 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4670 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4671
4672 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004673 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4674 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004675 }
4676 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004677 final int top = mTaskHistory.size() - 1;
4678 if (top >= 0) {
4679 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4680 int listTop = list.size() - 1;
4681 if (listTop >= 0) {
4682 activities.add(list.get(listTop));
4683 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004684 }
4685 } else {
4686 ItemMatcher matcher = new ItemMatcher();
4687 matcher.build(name);
4688
Craig Mautneraab647e2013-02-28 16:31:36 -08004689 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4690 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4691 if (matcher.match(r1, r1.intent.getComponent())) {
4692 activities.add(r1);
4693 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004694 }
4695 }
4696 }
4697
4698 return activities;
4699 }
4700
4701 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004702 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004703
4704 // All activities that came from the package must be
4705 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004706 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4707 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4708 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4709 final ActivityRecord a = activities.get(activityNdx);
4710 if (a.info.packageName.equals(packageName)) {
4711 a.forceNewConfig = true;
4712 if (starting != null && a == starting && a.visible) {
4713 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004714 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08004715 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004716 }
4717 }
4718 }
4719
4720 return starting;
4721 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004722
Craig Mautner299f9602015-01-26 09:47:33 -08004723 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004724 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004725 }
4726
Wale Ogunwale000957c2015-04-03 08:19:12 -07004727 /**
4728 * Removes the input task from this stack.
4729 * @param task to remove.
4730 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004731 * @param moving task to another stack. In the case we are moving we don't want to perform
4732 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004733 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004734 void removeTask(TaskRecord task, String reason, boolean moving) {
4735 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004736 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004737 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004738 if (!StackId.persistTaskBounds(mStackId)) {
4739 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4740 // default configuration the next time it launches.
4741 task.updateOverrideConfiguration(null);
4742 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004743 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004744
Craig Mautner04a0ea62014-01-13 12:51:26 -08004745 final ActivityRecord r = mResumedActivity;
4746 if (r != null && r.task == task) {
4747 mResumedActivity = null;
4748 }
4749
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004750 final int taskNdx = mTaskHistory.indexOf(task);
4751 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004752 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4753 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4754 if (!nextTask.isOverHomeStack()) {
4755 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4756 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004757 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004758 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004759 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004760
Wale Ogunwale040b4702015-08-06 18:10:50 -07004761 if (!moving && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004762 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07004763 final boolean isVoiceSession = task.voiceSession != null;
4764 if (isVoiceSession) {
4765 try {
4766 task.voiceSession.taskFinished(task.intent, task.taskId);
4767 } catch (RemoteException e) {
4768 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004769 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004770 if (task.autoRemoveFromRecents() || isVoiceSession) {
4771 // Task creator asked to remove this when done, or this task was a voice
4772 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004773 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004774 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004775 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004776 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004777
4778 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004779 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004780 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004781 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004782 String myReason = reason + " leftTaskHistoryEmpty";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08004783 if (mFullscreen || !adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004784 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004785 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004786 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004787 if (mStacks != null) {
4788 mStacks.remove(this);
4789 mStacks.add(0, this);
4790 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004791 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004792 mActivityContainer.onTaskListEmptyLocked();
4793 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004794 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004795
4796 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004797 }
4798
Dianne Hackborn91097de2014-04-04 18:02:06 -07004799 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4800 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4801 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004802 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4803 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004804 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08004805 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08004806 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004807 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.isResizeable()
Chong Zhang75b37202015-12-04 14:16:36 -08004808 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004809 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004810 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004811 return task;
4812 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004813
Wale Ogunwale935e5022015-11-10 12:36:10 -08004814 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4815 if (mTaskPositioner == null) {
4816 return false;
4817 }
4818 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4819 return true;
4820 }
4821
Craig Mautnerc00204b2013-03-05 15:02:14 -08004822 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004823 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004824 }
4825
Wale Ogunwale5f986092015-12-04 15:35:38 -08004826 void addTask(final TaskRecord task, final boolean toTop, String reason) {
4827 final ActivityStack prevStack = preAddTask(task, reason);
4828
Craig Mautnerc00204b2013-03-05 15:02:14 -08004829 task.stack = this;
4830 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004831 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004832 } else {
4833 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004834 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004835 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08004836 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004837 }
4838
Wale Ogunwale5f986092015-12-04 15:35:38 -08004839 void positionTask(final TaskRecord task, int position) {
4840 final ActivityStack prevStack = preAddTask(task, "positionTask");
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004841 task.stack = this;
4842 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004843 postAddTask(task, prevStack);
4844 }
4845
4846 private ActivityStack preAddTask(TaskRecord task, String reason) {
4847 final ActivityStack prevStack = task.stack;
4848 if (prevStack != null && prevStack != this) {
4849 prevStack.removeTask(task, reason, MOVING);
4850 }
4851 return prevStack;
4852 }
4853
4854 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
4855 if (prevStack != null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004856 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004857 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004858 try {
4859 task.voiceSession.taskStarted(task.intent, task.taskId);
4860 } catch (RemoteException e) {
4861 }
4862 }
4863 }
4864
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004865 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004866 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004867 // TODO: VI deal with activity
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004868 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4869 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6cae7652015-12-26 07:36:26 -08004870 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
4871 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004872 task.mResizeMode, r.isAlwaysFocusable(), task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004873 r.taskConfigOverride = task.mOverrideConfig;
4874 }
4875
Wale Ogunwaled046a012015-12-24 13:05:59 -08004876 void moveToFrontAndResumeStateIfNeeded(
4877 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
4878 if (!moveToFront) {
4879 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07004880 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004881
4882 // If the activity owns the last resumed activity, transfer that together,
4883 // so that we don't resume the same activity again in the new stack.
4884 // Apps may depend on onResume()/onPause() being called in pairs.
4885 if (setResume) {
4886 mResumedActivity = r;
4887 }
4888 // Move the stack in which we are placing the activity to the front. The call will also
4889 // make sure the activity focus is set.
4890 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004891 }
4892
4893 /**
4894 * Moves the input activity from its current stack to this one.
4895 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4896 * created on this stack which the activity is added to.
4897 * */
4898 void moveActivityToStack(ActivityRecord r) {
4899 final ActivityStack prevStack = r.task.stack;
4900 if (prevStack.mStackId == mStackId) {
4901 // You are already in the right stack silly...
4902 return;
4903 }
4904
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004905 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004906 && (mStackSupervisor.topRunningActivityLocked() == r);
4907 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4908
4909 final TaskRecord task = createTaskRecord(
Suprabh Shukla09a88f52015-12-02 14:36:31 -08004910 mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
4911 r.info, r.intent, null, null, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004912 r.setTask(task, null);
4913 task.addActivityToTop(r);
4914 setAppTask(r, task);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004915 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004916 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4917 if (wasResumed) {
4918 prevStack.mResumedActivity = null;
4919 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07004920 }
4921
Wale Ogunwale706ed792015-08-02 10:29:44 -07004922 private void setAppTask(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004923 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004924 mWindowManager.setAppTask(r.appToken, task.taskId, mStackId, bounds, task.mOverrideConfig,
4925 task.mResizeMode, task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004926 r.taskConfigOverride = task.mOverrideConfig;
4927 }
4928
Craig Mautnerc00204b2013-03-05 15:02:14 -08004929 public int getStackId() {
4930 return mStackId;
4931 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004932
4933 @Override
4934 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004935 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4936 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004937 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004938
Craig Mautner15df08a2015-04-01 12:17:18 -07004939 void onLockTaskPackagesUpdatedLocked() {
4940 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4941 mTaskHistory.get(taskNdx).setLockTaskAuth();
4942 }
4943 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004944}