blob: 0bccffa5b5053ba2ad84f9e4d2f80012a0e96731 [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 Ogunwale6dfdfd62015-04-15 12:01:38 -070028import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -070029
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -080030import static android.content.res.Configuration.SCREENLAYOUT_UNDEFINED;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080031import static com.android.server.am.ActivityManagerDebugConfig.*;
Chong Zhang75b37202015-12-04 14:16:36 -080032import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
Craig Mautner84984fa2014-06-19 11:19:20 -070033import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
34import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner84984fa2014-06-19 11:19:20 -070035
Wale Ogunwale39381972015-12-17 17:15:29 -080036import static com.android.server.am.ActivityStackSupervisor.FindTaskResult;
Wale Ogunwale040b4702015-08-06 18:10:50 -070037import static com.android.server.am.ActivityStackSupervisor.MOVING;
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -070038import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Wale Ogunwale040b4702015-08-06 18:10:50 -070039
Winsonc809cbb2015-11-02 12:06:15 -080040import android.graphics.Point;
Wale Ogunwale706ed792015-08-02 10:29:44 -070041import android.graphics.Rect;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070042import android.util.ArraySet;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -070043
Dianne Hackborn91097de2014-04-04 18:02:06 -070044import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -080045import com.android.internal.content.ReferrerIntent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070046import com.android.internal.os.BatteryStatsImpl;
Kenny Rootadd58212013-05-07 09:47:34 -070047import com.android.server.Watchdog;
Craig Mautnercae015f2013-02-08 14:31:27 -080048import com.android.server.am.ActivityManagerService.ItemMatcher;
Craig Mautner4a1cb222013-12-04 16:14:06 -080049import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
Craig Mautner4b71aa12012-12-27 17:20:01 -080050import com.android.server.wm.AppTransition;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080051import com.android.server.wm.TaskGroup;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070052import com.android.server.wm.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070053
54import android.app.Activity;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070055import android.app.ActivityManager;
Wale Ogunwale3797c222015-10-27 14:21:58 -070056import android.app.ActivityManager.StackId;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -070057import android.app.ActivityOptions;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070058import android.app.AppGlobals;
Craig Mautner05d6272ba2013-02-11 09:39:27 -080059import android.app.IActivityController;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070060import android.app.ResultInfo;
Craig Mautnercae015f2013-02-08 14:31:27 -080061import android.app.ActivityManager.RunningTaskInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070062import android.content.ComponentName;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070063import android.content.Intent;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070064import android.content.pm.ActivityInfo;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070065import android.content.pm.PackageManager;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070066import android.content.res.Configuration;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080067import android.graphics.Bitmap;
Santos Cordon73ff7d82013-03-06 17:24:11 -080068import android.net.Uri;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070069import android.os.Binder;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070070import android.os.Bundle;
Craig Mautner329f4122013-11-07 09:10:42 -080071import android.os.Debug;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070072import android.os.Handler;
73import android.os.IBinder;
Zoran Marcetaf958b322012-08-09 20:27:12 +090074import android.os.Looper;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070075import android.os.Message;
Craig Mautnera0026042014-04-23 11:45:37 -070076import android.os.PersistableBundle;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070077import android.os.RemoteException;
78import android.os.SystemClock;
Craig Mautner329f4122013-11-07 09:10:42 -080079import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070080import android.os.UserHandle;
Craig Mautner4c07d022014-06-11 17:12:59 -070081import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070082import android.util.EventLog;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070083import android.util.Slog;
Craig Mautner59c00972012-07-30 12:10:24 -070084import android.view.Display;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070085
Craig Mautnercae015f2013-02-08 14:31:27 -080086import java.io.FileDescriptor;
Craig Mautnercae015f2013-02-08 14:31:27 -080087import java.io.PrintWriter;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070088import java.lang.ref.WeakReference;
89import java.util.ArrayList;
90import java.util.Iterator;
91import java.util.List;
Kenny Roote6585b32013-12-13 12:00:26 -080092import java.util.Objects;
Wale Ogunwale540e1232015-05-01 15:35:39 -070093import java.util.Set;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070094
95/**
96 * State and management of a single stack of activities.
97 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070098final class ActivityStack {
Craig Mautner5d9c7be2013-02-15 14:02:56 -080099
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800100 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityStack" : TAG_AM;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700101 private static final String TAG_ADD_REMOVE = TAG + POSTFIX_ADD_REMOVE;
102 private static final String TAG_APP = TAG + POSTFIX_APP;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800103 private static final String TAG_CLEANUP = TAG + POSTFIX_CLEANUP;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700104 private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700105 private static final String TAG_CONTAINERS = TAG + POSTFIX_CONTAINERS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700106 private static final String TAG_PAUSE = TAG + POSTFIX_PAUSE;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700107 private static final String TAG_RELEASE = TAG + POSTFIX_RELEASE;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700108 private static final String TAG_RESULTS = TAG + POSTFIX_RESULTS;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700109 private static final String TAG_SAVED_STATE = TAG + POSTFIX_SAVED_STATE;
110 private static final String TAG_SCREENSHOTS = TAG + POSTFIX_SCREENSHOTS;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700111 private static final String TAG_STACK = TAG + POSTFIX_STACK;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700112 private static final String TAG_STATES = TAG + POSTFIX_STATES;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700113 private static final String TAG_SWITCH = TAG + POSTFIX_SWITCH;
114 private static final String TAG_TASKS = TAG + POSTFIX_TASKS;
115 private static final String TAG_TRANSITION = TAG + POSTFIX_TRANSITION;
116 private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
117 private static final String TAG_VISIBILITY = TAG + POSTFIX_VISIBILITY;
Wale Ogunwale3ab9a272015-03-16 09:55:45 -0700118
119 private static final boolean VALIDATE_TOKENS = false;
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800120
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700121 // Ticks during which we check progress while waiting for an app to launch.
122 static final int LAUNCH_TICK = 500;
123
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700124 // How long we wait until giving up on the last activity to pause. This
125 // is short because it directly impacts the responsiveness of starting the
126 // next activity.
127 static final int PAUSE_TIMEOUT = 500;
128
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700129 // How long we wait for the activity to tell us it has stopped before
130 // giving up. This is a good amount of time because we really need this
131 // from the application in order to get its saved state.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700132 static final int STOP_TIMEOUT = 10 * 1000;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700133
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700134 // How long we wait until giving up on an activity telling us it has
135 // finished destroying itself.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700136 static final int DESTROY_TIMEOUT = 10 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800137
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700138 // How long until we reset a task when the user returns to it. Currently
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800139 // disabled.
140 static final long ACTIVITY_INACTIVE_RESET_TIME = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800141
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700142 // How long between activity launches that we consider safe to not warn
143 // the user about an unexpected activity being launched on top.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700144 static final long START_WARN_TIME = 5 * 1000;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800145
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700146 // Set to false to disable the preview that is shown while a new activity
147 // is being started.
148 static final boolean SHOW_APP_STARTING_PREVIEW = true;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800149
Craig Mautner5eda9b32013-07-02 11:58:16 -0700150 // How long to wait for all background Activities to redraw following a call to
151 // convertToTranslucent().
152 static final long TRANSLUCENT_CONVERSION_TIMEOUT = 2000;
153
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -0800154 // How many activities have to be scheduled to stop to force a stop pass.
155 private static final int MAX_STOPPING_TO_FORCE = 3;
156
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700157 enum ActivityState {
158 INITIALIZING,
159 RESUMED,
160 PAUSING,
161 PAUSED,
162 STOPPING,
163 STOPPED,
164 FINISHING,
165 DESTROYING,
166 DESTROYED
167 }
168
Filip Gruszczynski0e381e22016-01-14 16:31:33 -0800169 // Stack is not considered visible.
170 static final int STACK_INVISIBLE = 0;
171 // Stack is considered visible
172 static final int STACK_VISIBLE = 1;
173 // Stack is considered visible, but only becuase it has activity that is visible behind other
174 // activities and there is a specific combination of stacks.
175 static final int STACK_VISIBLE_ACTIVITY_BEHIND = 2;
176
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700177 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700178 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800179 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800180
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700181 /**
182 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800183 * running) activities. It contains #TaskRecord objects.
184 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800185 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800186
187 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800188 * Used for validating app tokens with window manager.
189 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800190 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800191
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700192 /**
193 * List of running activities, sorted by recent usage.
194 * The first entry in the list is the least recently used.
195 * It contains HistoryRecord objects.
196 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800197 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700198
199 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700200 * Animations that for the current transition have requested not to
201 * be considered for the transition animation.
202 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800203 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700204
205 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700206 * When we are in the process of pausing an activity, before starting the
207 * next one, this variable holds the activity that is currently being paused.
208 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800209 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700210
211 /**
212 * This is the last activity that we put into the paused state. This is
213 * used to determine if we need to do an activity transition while sleeping,
214 * when we normally hold the top activity paused.
215 */
216 ActivityRecord mLastPausedActivity = null;
217
218 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700219 * Activities that specify No History must be removed once the user navigates away from them.
220 * If the device goes to sleep with such an activity in the paused state then we save it here
221 * and finish it later if another activity replaces it on wakeup.
222 */
223 ActivityRecord mLastNoHistoryActivity = null;
224
225 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700226 * Current activity that is resumed, or null if there is none.
227 */
228 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800229
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700230 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700231 * This is the last activity that has been started. It is only used to
232 * identify when multiple activities are started at once so that the user
233 * can be warned they may not be in the activity they think they are.
234 */
235 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800236
Craig Mautner5eda9b32013-07-02 11:58:16 -0700237 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
238 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
239 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
240 // Activity in mTranslucentActivityWaiting is notified via
241 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
242 // background activity being drawn then the same call will be made with a true value.
243 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700244 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700245
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700246 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700247 * Set when we know we are going to be calling updateConfiguration()
248 * soon, so want to skip intermediate config checks.
249 */
250 boolean mConfigWillChange;
251
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700252 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800253 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700254 // Current bounds of the stack or null if fullscreen.
255 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800256
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700257 long mLaunchStartTime = 0;
258 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800259
Craig Mautner858d8a62013-04-23 17:08:34 -0700260 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700261
Craig Mautnerc00204b2013-03-05 15:02:14 -0800262 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800263 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800264 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
265 ArrayList<ActivityStack> mStacks;
266 /** The attached Display's unique identifier, or -1 if detached */
267 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800268
Craig Mautner27084302013-03-25 08:05:25 -0700269 /** Run all ActivityStacks through this */
270 final ActivityStackSupervisor mStackSupervisor;
271
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700272 private final LaunchingTaskPositioner mTaskPositioner;
273
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700274 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700275 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
276 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
277 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
278 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700279 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700280 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700281 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700282
283 static class ScheduleDestroyArgs {
284 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700285 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700286 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700287 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700288 mReason = reason;
289 }
290 }
291
Zoran Marcetaf958b322012-08-09 20:27:12 +0900292 final Handler mHandler;
293
294 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800295
Craig Mautnerc8143c62013-09-03 12:15:57 -0700296 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900297 super(looper);
298 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700299
Zoran Marcetaf958b322012-08-09 20:27:12 +0900300 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700301 public void handleMessage(Message msg) {
302 switch (msg.what) {
303 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800304 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700305 // We don't at this point know if the activity is fullscreen,
306 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800307 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700308 synchronized (mService) {
309 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700310 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700311 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700312 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800313 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700314 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700315 case LAUNCH_TICK_MSG: {
316 ActivityRecord r = (ActivityRecord)msg.obj;
317 synchronized (mService) {
318 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700319 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700320 }
321 }
322 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700323 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800324 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700325 // We don't at this point know if the activity is fullscreen,
326 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800327 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800328 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800329 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800330 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700331 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700332 case STOP_TIMEOUT_MSG: {
333 ActivityRecord r = (ActivityRecord)msg.obj;
334 // We don't at this point know if the activity is fullscreen,
335 // so we need to be conservative and assume it isn't.
336 Slog.w(TAG, "Activity stop timeout for " + r);
337 synchronized (mService) {
338 if (r.isInHistory()) {
339 activityStoppedLocked(r, null, null, null);
340 }
341 }
342 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700343 case DESTROY_ACTIVITIES_MSG: {
344 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
345 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700346 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700347 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700348 } break;
349 case TRANSLUCENT_TIMEOUT_MSG: {
350 synchronized (mService) {
351 notifyActivityDrawnLocked(null);
352 }
353 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700354 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700355 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700356 final ActivityRecord r = getVisibleBehindActivity();
357 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700358 if (r != null) {
359 mService.killAppAtUsersRequest(r.app, null);
360 }
361 }
362 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700363 }
364 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800365 }
366
Craig Mautner34b73df2014-01-12 21:11:08 -0800367 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800368 int count = 0;
369 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
370 count += mTaskHistory.get(taskNdx).mActivities.size();
371 }
372 return count;
373 }
374
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800375 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
376 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800377 mActivityContainer = activityContainer;
378 mStackSupervisor = activityContainer.getOuter();
379 mService = mStackSupervisor.mService;
380 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
381 mWindowManager = mService.mWindowManager;
382 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700383 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800384 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700385 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
386 ? new LaunchingTaskPositioner() : null;
387 }
388
389 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
390 mDisplayId = activityDisplay.mDisplayId;
391 mStacks = activityDisplay.mStacks;
392 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
393 mFullscreen = mBounds == null;
394 if (mTaskPositioner != null) {
395 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
396 mTaskPositioner.configure(mBounds);
397 }
Wale Ogunwale961f4852016-02-01 20:25:54 -0800398
399 if (mStackId == DOCKED_STACK_ID) {
400 // If we created a docked stack we want to resize it so it resizes all other stacks
401 // in the system.
402 mStackSupervisor.resizeDockedStackLocked(
403 mBounds, null, null, null, null, PRESERVE_WINDOWS);
404 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700405 }
406
407 void detachDisplay() {
408 mDisplayId = Display.INVALID_DISPLAY;
409 mStacks = null;
410 if (mTaskPositioner != null) {
411 mTaskPositioner.reset();
412 }
413 mWindowManager.detachStack(mStackId);
414 }
415
Winsonc809cbb2015-11-02 12:06:15 -0800416 public void getDisplaySize(Point out) {
417 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
418 }
419
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700420 void setBounds(Rect bounds) {
421 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700422 if (mTaskPositioner != null) {
423 mTaskPositioner.configure(bounds);
424 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700425 }
Craig Mautner5962b122012-10-05 14:45:52 -0700426
Amith Yamasani734983f2014-03-04 16:48:05 -0800427 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700428 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700429 }
430
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700431 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800432 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700433 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700434 if (r != null) {
435 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800436 }
437 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700438 return null;
439 }
440
441 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800442 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
443 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800444 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800445 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
446 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800447 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800448 return r;
449 }
450 }
451 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700452 return null;
453 }
454
455 /**
456 * This is a simplified version of topRunningActivityLocked that provides a number of
457 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800458 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700459 * @param token If non-null, any history records matching this token will be skipped.
460 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800461 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700462 * @return Returns the HistoryRecord of the next activity on the stack.
463 */
464 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800465 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
466 TaskRecord task = mTaskHistory.get(taskNdx);
467 if (task.taskId == taskId) {
468 continue;
469 }
470 ArrayList<ActivityRecord> activities = task.mActivities;
471 for (int i = activities.size() - 1; i >= 0; --i) {
472 final ActivityRecord r = activities.get(i);
473 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800474 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800475 return r;
476 }
477 }
478 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700479 return null;
480 }
481
Craig Mautner8849a5e2013-04-02 16:41:03 -0700482 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700483 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
484 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700485 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
486 final ActivityRecord r = activities.get(activityNdx);
487 if (!r.finishing) {
488 return r;
489 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700490 }
491 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700492 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700493 }
494
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700495 final TaskRecord topTask() {
496 final int size = mTaskHistory.size();
497 if (size > 0) {
498 return mTaskHistory.get(size - 1);
499 }
500 return null;
501 }
502
Craig Mautnerd2328952013-03-05 12:46:26 -0800503 TaskRecord taskForIdLocked(int id) {
504 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
505 final TaskRecord task = mTaskHistory.get(taskNdx);
506 if (task.taskId == id) {
507 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800508 }
509 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700510 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700511 }
512
Craig Mautnerd2328952013-03-05 12:46:26 -0800513 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700514 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800515 return isInStackLocked(r);
516 }
517
518 ActivityRecord isInStackLocked(ActivityRecord r) {
519 if (r == null) {
520 return null;
521 }
522 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700523 if (task != null && task.stack != null
524 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800525 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800526 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800527 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800528 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800529 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800530 }
531
Craig Mautner2420ead2013-04-01 17:13:20 -0700532 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700533 final boolean hadit = mLRUActivities.remove(r);
534 mLRUActivities.add(r);
535 return hadit;
536 }
537
Craig Mautnerde4ef022013-04-07 19:01:33 -0700538 final boolean isHomeStack() {
539 return mStackId == HOME_STACK_ID;
540 }
541
Winson Chung0583d3d2015-12-18 10:03:12 -0500542 final boolean isDockedStack() {
543 return mStackId == DOCKED_STACK_ID;
544 }
545
546 final boolean isPinnedStack() {
547 return mStackId == PINNED_STACK_ID;
548 }
549
Craig Mautnere0a38842013-12-16 16:14:02 -0800550 final boolean isOnHomeDisplay() {
551 return isAttached() &&
552 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
553 }
554
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700555 void moveToFront(String reason) {
556 moveToFront(reason, null);
557 }
558
559 /**
560 * @param reason The reason for moving the stack to the front.
561 * @param task If non-null, the task will be moved to the top of the stack.
562 * */
563 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700564 if (!isAttached()) {
565 return;
566 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700567
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700568 mStacks.remove(this);
569 int addIndex = mStacks.size();
570
571 if (addIndex > 0) {
572 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800573 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
574 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700575 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800576 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700577 }
578
579 mStacks.add(addIndex, this);
580
581 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
582 if (isOnHomeDisplay()) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800583 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700584 }
585 if (task != null) {
586 insertTaskAtTop(task, null);
587 } else {
588 task = topTask();
589 }
590 if (task != null) {
591 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800592 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800593 }
594
Wale Ogunwaled046a012015-12-24 13:05:59 -0800595 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800596 if (StackId.canReceiveKeys(mStackId)) {
597 return true;
598 }
599 // The stack isn't focusable. See if its top activity is focusable to force focus on the
600 // stack.
601 final ActivityRecord r = topRunningActivityLocked();
602 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800603 }
604
Craig Mautnere0a38842013-12-16 16:14:02 -0800605 final boolean isAttached() {
606 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800607 }
608
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700609 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800610 * Returns the top activity in any existing task matching the given Intent in the input result.
611 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700612 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800613 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700614 Intent intent = target.intent;
615 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700616 ComponentName cls = intent.getComponent();
617 if (info.targetActivity != null) {
618 cls = new ComponentName(info.packageName, info.targetActivity);
619 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700620 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700621 boolean isDocument = intent != null & intent.isDocument();
622 // If documentData is non-null then it must match the existing task data.
623 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800624
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700625 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800626 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
627 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700628 if (task.voiceSession != null) {
629 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700630 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700631 continue;
632 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700633 if (task.userId != userId) {
634 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700635 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700636 continue;
637 }
Craig Mautner000f0022013-02-26 15:04:29 -0800638 final ActivityRecord r = task.getTopActivity();
639 if (r == null || r.finishing || r.userId != userId ||
640 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700641 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800642 continue;
643 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700644 if (r.mActivityType != target.mActivityType) {
645 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
646 continue;
647 }
Craig Mautner000f0022013-02-26 15:04:29 -0800648
Craig Mautnerd00f4742014-03-12 14:17:26 -0700649 final Intent taskIntent = task.intent;
650 final Intent affinityIntent = task.affinityIntent;
651 final boolean taskIsDocument;
652 final Uri taskDocumentData;
653 if (taskIntent != null && taskIntent.isDocument()) {
654 taskIsDocument = true;
655 taskDocumentData = taskIntent.getData();
656 } else if (affinityIntent != null && affinityIntent.isDocument()) {
657 taskIsDocument = true;
658 taskDocumentData = affinityIntent.getData();
659 } else {
660 taskIsDocument = false;
661 taskDocumentData = null;
662 }
663
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700664 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700665 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700666 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700667 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Wale Ogunwale39381972015-12-17 17:15:29 -0800668 if (!isDocument && !taskIsDocument
669 && result.r == null && task.canMatchRootAffinity()) {
Dianne Hackborn79228822014-09-16 11:11:23 -0700670 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwale39381972015-12-17 17:15:29 -0800671 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
672 // It is possible for multiple tasks to have the same root affinity especially
673 // if they are in separate stacks. We save off this candidate, but keep looking
674 // to see if there is a better candidate.
675 result.r = r;
676 result.matchedByRootAffinity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700677 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700678 } else if (taskIntent != null && taskIntent.getComponent() != null &&
679 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700680 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700681 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800682 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700683 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
684 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800685 result.r = r;
686 result.matchedByRootAffinity = false;
687 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700688 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
689 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700690 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700691 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800692 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700693 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
694 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800695 result.r = r;
696 result.matchedByRootAffinity = false;
697 break;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700698 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700699 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700700 }
701
702 /**
703 * Returns the first activity (starting from the top of the stack) that
704 * is the same as the given activity. Returns null if no such activity
705 * is found.
706 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700707 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700708 ComponentName cls = intent.getComponent();
709 if (info.targetActivity != null) {
710 cls = new ComponentName(info.packageName, info.targetActivity);
711 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700712 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700713
Craig Mautner000f0022013-02-26 15:04:29 -0800714 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700715 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700716 final boolean notCurrentUserTask =
717 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700718 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700719
Craig Mautner000f0022013-02-26 15:04:29 -0800720 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
721 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700722 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700723 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700724 }
Craig Mautner000f0022013-02-26 15:04:29 -0800725 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700726 return r;
727 }
728 }
729 }
730
731 return null;
732 }
733
Amith Yamasani742a6712011-05-04 14:49:28 -0700734 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700735 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700736 */
Craig Mautner93529a42013-10-04 15:03:13 -0700737 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800738 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700739 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800740 }
741 mCurrentUser = userId;
742
743 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800744 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800745 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700746 final TaskRecord task = mTaskHistory.get(i);
747
748 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
749 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700750 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700751 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700752 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700753 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800754 mTaskHistory.remove(i);
755 mTaskHistory.add(task);
756 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800757 // Use same value for i.
758 } else {
759 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800760 }
761 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700762 if (VALIDATE_TOKENS) {
763 validateAppTokensLocked();
764 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700765 }
766
Craig Mautner2420ead2013-04-01 17:13:20 -0700767 void minimalResumeActivityLocked(ActivityRecord r) {
768 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700769 if (DEBUG_STATES) Slog.v(TAG_STATES,
770 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700771 r.stopped = false;
772 mResumedActivity = r;
773 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800774 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700775 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700776 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700777 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700778 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
779 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700780 }
781
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800782 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700783 if (r != null) {
784 mRecentTasks.addLocked(r.task);
785 r.task.touchActiveTime();
786 }
787 }
788
Narayan Kamath7829c812015-06-08 17:39:43 +0100789 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700790 if (mFullyDrawnStartTime != 0) {
791 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
792 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100793 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700794 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
795 }
796
797 private void stopFullyDrawnTraceIfNeeded() {
798 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
799 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
800 mFullyDrawnStartTime = 0;
801 }
802 }
803
Craig Mautnere79d42682013-04-01 19:01:53 -0700804 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700805 if (r.displayStartTime == 0) {
806 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
807 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100808 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700809 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700810 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700811 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100812 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700813 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700814 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700815 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800816
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700817 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700818 // Make sure that there is no activity waiting for this to launch.
819 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
820 r.displayStartTime = r.fullyDrawnStartTime = 0;
821 } else {
822 mStackSupervisor.removeTimeoutsForActivityLocked(r);
823 mStackSupervisor.scheduleIdleTimeoutLocked(r);
824 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700825 }
826
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800827 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800828 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800829 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
830 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
831 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
832 activities.get(activityNdx).setSleeping(false);
833 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800834 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800835 if (mPausingActivity != null) {
836 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
837 activityPausedLocked(mPausingActivity.appToken, true);
838 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800839 }
840
Craig Mautner0eea92c2013-05-16 13:35:39 -0700841 /**
842 * @return true if something must be done before going to sleep.
843 */
844 boolean checkReadyForSleepLocked() {
845 if (mResumedActivity != null) {
846 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700847 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
848 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
849 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700850 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700851 return true;
852 }
853 if (mPausingActivity != null) {
854 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700855 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700856 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800857 }
858
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700859 if (hasVisibleBehindActivity()) {
860 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800861 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700862 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800863 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700864 return true;
865 }
866
Craig Mautner0eea92c2013-05-16 13:35:39 -0700867 return false;
868 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800869
Craig Mautner0eea92c2013-05-16 13:35:39 -0700870 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700871 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800872
Craig Mautner0eea92c2013-05-16 13:35:39 -0700873 // Make sure any stopped but visible activities are now sleeping.
874 // This ensures that the activity's onStop() is called.
875 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
876 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
877 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
878 final ActivityRecord r = activities.get(activityNdx);
879 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
880 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800881 }
882 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800883 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700884 }
Craig Mautner59c00972012-07-30 12:10:24 -0700885
Winson8b1871d2015-11-20 09:56:20 -0800886 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
887 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800888 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700889 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800890 return null;
891 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800892
Winson Chung083baf92014-07-11 10:32:42 -0700893 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700894 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800895 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700896 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700897 return null;
898 }
899
Winson Chung48a10a52014-08-27 14:36:51 -0700900 int w = mService.mThumbnailWidth;
901 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800902 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800903 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700904 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800905
906 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800907 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
908 // use within SystemUI while keeping memory usage low.
Winson Chungead5c0f2015-12-14 11:18:57 -0500909 if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
Winson8b1871d2015-11-20 09:56:20 -0800910 w = h = -1;
Winson387aac62015-11-25 11:18:56 -0800911 scale = 0.5f;
Winson8b1871d2015-11-20 09:56:20 -0800912 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800913 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800914 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800915 }
Winson Chung376543b2014-05-21 17:43:28 -0700916 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800917 return null;
918 }
919
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700920 /**
921 * Start pausing the currently resumed activity. It is an error to call this if there
922 * is already an activity being paused or there is no resumed activity.
923 *
924 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
925 * @param uiSleeping True if this is happening with the user interface going to sleep (the
926 * screen turning off).
927 * @param resuming True if this is being called as part of resuming the top activity, so
928 * we shouldn't try to instigate a resume here.
929 * @param dontWait True if the caller does not want to wait for the pause to complete. If
930 * set to true, we will immediately complete the pause here before returning.
931 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
932 * it to tell us when it is done.
933 */
934 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
935 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800936 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800937 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
938 + " state=" + mPausingActivity.state);
939 if (!mService.isSleeping()) {
940 // Avoid recursion among check for sleep and complete pause during sleeping.
941 // Because activity will be paused immediately after resume, just let pause
942 // be completed by the order of activity paused from clients.
943 completePauseLocked(false);
944 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800945 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700946 ActivityRecord prev = mResumedActivity;
947 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700948 if (!resuming) {
949 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -0800950 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700951 }
952 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700953 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800954
955 if (mActivityContainer.mParentActivity == null) {
956 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700957 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800958 }
959
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700960 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700961 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700962 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800963 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700964 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700965 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
966 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700967 prev.state = ActivityState.PAUSING;
968 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700969 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700970 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700971 if (mService.mHasRecents
972 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800973 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700974 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700975 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700976
977 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800978
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700979 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700980 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700981 try {
982 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700983 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700984 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700985 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800986 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700987 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700988 } catch (Exception e) {
989 // Ignore exception, if process died other code will cleanup.
990 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800991 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700992 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700993 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700994 }
995 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800996 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700997 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700998 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700999 }
1000
1001 // If we are not going to sleep, we want to ensure the device is
1002 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001003 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001004 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001005 }
1006
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001007 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001008 // Have the window manager pause its key dispatching until the new
1009 // activity has started. If we're pausing the activity just because
1010 // the screen is being turned off and the UI is sleeping, don't interrupt
1011 // key dispatch; the same activity will pick it up again on wakeup.
1012 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001013 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001014 } else if (DEBUG_PAUSE) {
1015 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001016 }
1017
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001018 if (dontWait) {
1019 // If the caller said they don't want to wait for the pause, then complete
1020 // the pause now.
1021 completePauseLocked(false);
1022 return false;
1023
1024 } else {
1025 // Schedule a pause timeout in case the app doesn't respond.
1026 // We don't give it much time because this directly impacts the
1027 // responsiveness seen by the user.
1028 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1029 msg.obj = prev;
1030 prev.pauseTime = SystemClock.uptimeMillis();
1031 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001032 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001033 return true;
1034 }
1035
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001036 } else {
1037 // This activity failed to schedule the
1038 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001039 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001040 if (!resuming) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001041 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001042 }
1043 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001044 }
1045 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001046
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001047 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001048 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1049 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001050
Craig Mautnerd2328952013-03-05 12:46:26 -08001051 final ActivityRecord r = isInStackLocked(token);
1052 if (r != null) {
1053 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1054 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001055 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001056 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001057 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -08001058 } else {
1059 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1060 r.userId, System.identityHashCode(r), r.shortComponentName,
1061 mPausingActivity != null
1062 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001063 if (r.state == ActivityState.PAUSING) {
1064 r.state = ActivityState.PAUSED;
1065 if (r.finishing) {
1066 if (DEBUG_PAUSE) Slog.v(TAG,
1067 "Executing finish of failed to pause activity: " + r);
1068 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1069 }
louis_chang047dfd42015-04-08 16:35:55 +08001070 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001071 }
1072 }
1073 }
1074
Craig Mautnera0026042014-04-23 11:45:37 -07001075 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1076 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001077 if (r.state != ActivityState.STOPPING) {
1078 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1079 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1080 return;
1081 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001082 if (persistentState != null) {
1083 r.persistentState = persistentState;
1084 mService.notifyTaskPersisterLocked(r.task, false);
1085 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001086 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001087 if (icicle != null) {
1088 // If icicle is null, this is happening due to a timeout, so we
1089 // haven't really saved the state.
1090 r.icicle = icicle;
1091 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001092 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001093 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001094 }
1095 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001096 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001097 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1098 r.stopped = true;
1099 r.state = ActivityState.STOPPED;
Robert Carre12aece2016-02-02 22:43:27 -08001100
1101 mWindowManager.notifyAppStopped(r.appToken);
1102
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001103 if (getVisibleBehindActivity() == r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001104 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001105 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001106 if (r.finishing) {
1107 r.clearOptionsLocked();
1108 } else {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001109 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001110 destroyActivityLocked(r, true, "stop-config");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001111 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001112 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001113 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001114 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001115 }
1116 }
1117 }
1118
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001119 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001120 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001121 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001122
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001123 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001124 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001125 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001126 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001127 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001128 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001129 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001130 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001131 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001132 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1133 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001134 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001135 if (prev.deferRelaunchUntilPaused) {
1136 // Complete the deferred relaunch that was waiting for pause to complete.
1137 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
1138 relaunchActivityLocked(prev, prev.configChangeFlags, false,
1139 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001140 } else if (wasStopping) {
1141 // We are also stopping, the stop request must have gone soon after the pause.
1142 // We can't clobber it, because the stop confirmation will not be handled.
1143 // We don't need to schedule another stop, we only need to let it happen.
1144 prev.state = ActivityState.STOPPING;
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001145 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001146 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001147 // stopping.
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001148 addToStopping(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001149 }
1150 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001151 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001152 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001153 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001154 // It is possible the activity was freezing the screen before it was paused.
1155 // In that case go ahead and remove the freeze this activity has on the screen
1156 // since it is no longer visible.
1157 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001158 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001159 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001160
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001161 if (resumeNext) {
1162 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1163 if (!mService.isSleepingOrShuttingDown()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001164 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001165 } else {
1166 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001167 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001168 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001169 // If there are no more activities available to run, do resume anyway to start
1170 // something. Also if the top activity on the stack is not the just paused
1171 // activity, we need to go ahead and resume it to ensure we complete an
1172 // in-flight app switch.
1173 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001174 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001175 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001176 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001177
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001178 if (prev != null) {
1179 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001180
Craig Mautner525f3d92013-05-07 14:01:50 -07001181 if (prev.app != null && prev.cpuTimeAtResume > 0
1182 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001183 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1184 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001185 if (diff > 0) {
1186 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1187 synchronized (bsi) {
1188 BatteryStatsImpl.Uid.Proc ps =
1189 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001190 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001191 if (ps != null) {
1192 ps.addForegroundTimeLocked(diff);
1193 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001194 }
1195 }
1196 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001197 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001198 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001199
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001200 // Notify when the task stack has changed, but only if visibilities changed (not just
1201 // focus).
1202 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause) {
1203 mService.notifyTaskStackChangedLocked();
1204 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1205 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001206 }
1207
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001208 private void addToStopping(ActivityRecord r) {
1209 mStackSupervisor.mStoppingActivities.add(r);
1210 if (mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE ||
1211 r.frontOfTask && mTaskHistory.size() <= 1) {
1212 // If we already have a few activities waiting to stop,
1213 // then give up on things going idle and start clearing
1214 // them out. Or if r is the last of activity of the last task the stack
1215 // will be empty and must be cleared immediately.
1216 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
1217 mStackSupervisor.scheduleIdleLocked();
1218 } else {
1219 mStackSupervisor.checkReadyForSleepLocked();
1220 }
1221 }
1222
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001223 /**
1224 * Once we know that we have asked an application to put an activity in
1225 * the resumed state (either by launching it or explicitly telling it),
1226 * this function updates the rest of our state to match that fact.
1227 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001228 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001229 next.idle = false;
1230 next.results = null;
1231 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001232
Chong Zhang85ee6542015-10-02 13:36:38 -07001233 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001234 ProcessRecord app = next.task.mActivities.get(0).app;
1235 if (app != null && app != mService.mHomeProcess) {
1236 mService.mHomeProcess = app;
1237 }
1238 }
1239
Craig Mautner07566322013-09-26 16:42:55 -07001240 if (next.nowVisible) {
1241 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001242 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001243 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001244
1245 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001246 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001247
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001248 mStackSupervisor.reportResumedActivityLocked(next);
1249
1250 next.resumeKeyDispatchingLocked();
1251 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001252
1253 // Mark the point when the activity is resuming
1254 // TODO: To be more accurate, the mark should be before the onCreate,
1255 // not after the onResume. But for subsequent starts, onResume is fine.
1256 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001257 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001258 } else {
1259 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1260 }
Winson Chung376543b2014-05-21 17:43:28 -07001261
George Mount6ba042b2014-07-28 11:12:28 -07001262 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001263
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001264 if (getVisibleBehindActivity() == next) {
Craig Mautner64ccb702014-10-01 09:38:40 -07001265 // When resuming an activity, require it to call requestVisibleBehind() again.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001266 setVisibleBehindActivity(null);
Craig Mautner64ccb702014-10-01 09:38:40 -07001267 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001268 }
1269
Craig Mautner2568c3a2015-03-26 14:22:34 -07001270 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001271 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001272 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1273 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1274 r.mUpdateTaskThumbnailWhenHidden = false;
1275 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001276 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001277 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001278 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001279 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001280 container.setVisible(visible);
1281 }
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001282 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001283 }
1284
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001285 // Find the first visible activity above the passed activity and if it is translucent return it
1286 // otherwise return null;
1287 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1288 TaskRecord task = r.task;
1289 if (task == null) {
1290 return null;
1291 }
1292
1293 ActivityStack stack = task.stack;
1294 if (stack == null) {
1295 return null;
1296 }
1297
1298 int stackNdx = mStacks.indexOf(stack);
1299
1300 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1301 int taskNdx = tasks.indexOf(task);
1302
1303 ArrayList<ActivityRecord> activities = task.mActivities;
1304 int activityNdx = activities.indexOf(r) + 1;
1305
1306 final int numStacks = mStacks.size();
1307 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001308 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001309 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001310 final int numTasks = tasks.size();
1311 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001312 final TaskRecord currentTask = tasks.get(taskNdx);
1313 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001314 final int numActivities = activities.size();
1315 while (activityNdx < numActivities) {
1316 final ActivityRecord activity = activities.get(activityNdx);
1317 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001318 return historyStack.mFullscreen
1319 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001320 }
1321 ++activityNdx;
1322 }
1323 activityNdx = 0;
1324 ++taskNdx;
1325 }
1326 taskNdx = 0;
1327 ++stackNdx;
1328 }
1329
1330 return null;
1331 }
1332
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001333 ActivityStack getNextFocusableStackLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001334 ArrayList<ActivityStack> stacks = mStacks;
1335 final ActivityRecord parent = mActivityContainer.mParentActivity;
1336 if (parent != null) {
1337 stacks = parent.task.stack.mStacks;
1338 }
1339 if (stacks != null) {
1340 for (int i = stacks.size() - 1; i >= 0; --i) {
1341 ActivityStack stack = stacks.get(i);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001342 if (stack != this && stack.isFocusable()
1343 && stack.getStackVisibilityLocked() != STACK_INVISIBLE) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001344 return stack;
1345 }
1346 }
1347 }
1348 return null;
1349 }
1350
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001351 /** Returns true if the stack contains a fullscreen task. */
1352 private boolean hasFullscreenTask() {
1353 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1354 final TaskRecord task = mTaskHistory.get(i);
1355 if (task.mFullscreen) {
1356 return true;
1357 }
1358 }
1359 return false;
1360 }
1361
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001362 private boolean hasTranslucentActivity(ActivityStack stack) {
1363 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1364 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1365 final TaskRecord task = tasks.get(taskNdx);
1366 final ArrayList<ActivityRecord> activities = task.mActivities;
1367 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1368 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001369
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001370 // Conditions for an activity to obscure the stack we're
1371 // examining:
1372 // 1. Not Finishing AND Visible AND:
1373 // 2. Either:
1374 // - Full Screen Activity OR
1375 // - On top of Home and our stack is NOT home
1376 if (!r.finishing && r.visible && (r.fullscreen ||
1377 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1378 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001379 }
1380 }
1381 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001382 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001383 }
1384
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001385 /**
1386 * Returns stack's visibility: {@link #STACK_INVISIBLE}, {@link #STACK_VISIBLE} or
1387 * {@link #STACK_VISIBLE_ACTIVITY_BEHIND}.
1388 */
1389 int getStackVisibilityLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001390 if (!isAttached()) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001391 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001392 }
1393
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001394 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001395 return STACK_VISIBLE;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001396 }
1397
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001398 final int stackIndex = mStacks.indexOf(this);
1399
1400 if (stackIndex == mStacks.size() - 1) {
1401 Slog.wtf(TAG,
1402 "Stack=" + this + " isn't front stack but is at the top of the stack list");
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001403 return STACK_INVISIBLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001404 }
1405
Chong Zhang75b37202015-12-04 14:16:36 -08001406 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1407 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001408 return STACK_INVISIBLE;
Chong Zhang75b37202015-12-04 14:16:36 -08001409 }
1410
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001411 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1412 final int focusedStackId = focusedStack.mStackId;
1413
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001414 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001415 && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID
1416 && !focusedStack.topActivity().fullscreen) {
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001417 // The fullscreen stack should be visible if it has a visible behind activity behind
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001418 // the home stack that is translucent.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001419 return STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001420 }
1421
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001422 if (mStackId == DOCKED_STACK_ID) {
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001423 // Docked stack is always visible, except in the case where the top running activity
1424 // task in the focus stack doesn't support any form of resizing.
Wale Ogunwaled26176f2016-01-25 20:04:04 -08001425 final ActivityRecord r = focusedStack.topRunningActivityLocked();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08001426 final TaskRecord task = r != null ? r.task : null;
Wale Ogunwale513346d2016-01-27 10:55:01 -08001427 return task == null || task.canGoInDockedStack() ? STACK_VISIBLE : STACK_INVISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001428 }
1429
Chong Zhangb16cf342015-11-12 17:22:40 -08001430 // Find the first stack below focused stack that actually got something visible.
1431 int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1432 while (belowFocusedIndex >= 0 &&
1433 mStacks.get(belowFocusedIndex).topRunningActivityLocked() == null) {
1434 belowFocusedIndex--;
1435 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001436 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1437 && stackIndex == belowFocusedIndex) {
1438 // Stacks directly behind the docked or pinned stack are always visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001439 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001440 }
1441
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001442 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1443 && hasTranslucentActivity(focusedStack)) {
1444 // Stacks behind the fullscreen stack with a translucent activity are always
1445 // visible so they can act as a backdrop to the translucent activity.
1446 // For example, dialog activities
1447 if (stackIndex == belowFocusedIndex) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001448 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001449 }
1450 if (belowFocusedIndex >= 0) {
1451 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001452 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1453 && stackIndex == (belowFocusedIndex - 1)) {
1454 // The stack behind the docked or pinned stack is also visible so we can have a
1455 // complete backdrop to the translucent activity when the docked stack is up.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001456 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001457 }
1458 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001459 }
1460
Wale Ogunwale3797c222015-10-27 14:21:58 -07001461 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001462 // Visibility of any static stack should have been determined by the conditions above.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001463 return STACK_INVISIBLE;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001464 }
1465
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001466 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001467 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001468
1469 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1470 continue;
1471 }
1472
Wale Ogunwale3797c222015-10-27 14:21:58 -07001473 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001474 // These stacks can't have any dynamic stacks visible behind them.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001475 return STACK_INVISIBLE;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001476 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001477
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001478 if (!hasTranslucentActivity(stack)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001479 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001480 }
1481 }
1482
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001483 return STACK_VISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001484 }
1485
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001486 final int rankTaskLayers(int baseLayer) {
1487 int layer = 0;
1488 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1489 final TaskRecord task = mTaskHistory.get(taskNdx);
1490 ActivityRecord r = task.topRunningActivityLocked();
1491 if (r == null || r.finishing || !r.visible) {
1492 task.mLayerRank = -1;
1493 } else {
1494 task.mLayerRank = baseLayer + layer++;
1495 }
1496 }
1497 return layer;
1498 }
1499
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001500 /**
1501 * Make sure that all activities that need to be visible (that is, they
1502 * currently can be seen by the user) actually are.
1503 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001504 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1505 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001506 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001507 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001508 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001509 if (top != null) {
1510 checkTranslucentActivityWaiting(top);
1511 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001512
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001513 // If the top activity is not fullscreen, then we need to
1514 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001515 boolean aboveTop = top != null;
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001516 final int stackVisibility = getStackVisibilityLocked();
1517 final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
1518 final boolean stackVisibleBehind = stackVisibility == STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001519 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaled046a012015-12-24 13:05:59 -08001520 boolean resumeNextActivity = isFocusable() && (isInStackLocked(starting) == null);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001521 boolean behindTranslucentActivity = false;
1522 final ActivityRecord visibleBehind = getVisibleBehindActivity();
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001523 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001524 final TaskRecord task = mTaskHistory.get(taskNdx);
1525 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001526 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1527 final ActivityRecord r = activities.get(activityNdx);
1528 if (r.finishing) {
1529 continue;
1530 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001531 final boolean isTop = r == top;
1532 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001533 continue;
1534 }
1535 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001536 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1537 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001538 final boolean activityVisibleBehind =
1539 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == r;
1540 final boolean isVisible = (!behindFullscreenActivity || r.mLaunchTaskBehind
1541 || activityVisibleBehind) && okToShowLocked(r);
1542 if (isVisible) {
1543 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1544 + " finishing=" + r.finishing + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001545 // First: if this is not the current activity being started, make
1546 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001547 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001548 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001549 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001550
1551 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001552 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001553 resumeNextActivity, r)) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001554 if (activityNdx >= activities.size()) {
1555 // Record may be removed if its process needs to restart.
1556 activityNdx = activities.size() - 1;
1557 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001558 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001559 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001560 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001561 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001562 // If this activity is already visible, then there is nothing to do here.
1563 if (handleAlreadyVisible(r)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001564 resumeNextActivity = false;
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001565 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001566 } else {
Jorim Jaggie66edb12016-02-05 12:41:17 -08001567 makeVisibleIfNeeded(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001568 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001569 // Aggregate current change flags.
1570 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001571 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1572 behindFullscreenActivity, task, r);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001573 if (behindFullscreenActivity && !r.fullscreen) {
1574 behindTranslucentActivity = true;
1575 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001576 } else {
Wale Ogunwale834c2362016-01-23 18:14:58 -08001577 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001578 + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1579 + stackInvisible + " behindFullscreenActivity="
1580 + behindFullscreenActivity + " mLaunchTaskBehind="
1581 + r.mLaunchTaskBehind);
1582 makeInvisible(r, visibleBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001583 }
1584 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001585 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1586 // The visibility of tasks and the activities they contain in freeform stack are
1587 // determined individually unlike other stacks where the visibility or fullscreen
1588 // status of an activity in a previous task affects other.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001589 behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001590 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001591 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001592
1593 if (mTranslucentActivityWaiting != null &&
1594 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1595 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1596 notifyActivityDrawnLocked(null);
1597 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001598 }
Craig Mautner58547802013-03-05 08:23:53 -08001599
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001600 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1601 if (mTranslucentActivityWaiting != top) {
1602 mUndrawnActivitiesBelowTopTranslucent.clear();
1603 if (mTranslucentActivityWaiting != null) {
1604 // Call the callback with a timeout indication.
1605 notifyActivityDrawnLocked(null);
1606 mTranslucentActivityWaiting = null;
1607 }
1608 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1609 }
1610 }
1611
1612 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001613 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001614 // We need to make sure the app is running if it's the top, or it is just made visible from
1615 // invisible. If the app is already visible, it must have died while it was visible. In this
1616 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1617 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001618 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001619 // This activity needs to be visible, but isn't even running...
1620 // get it started and resume if no other stack in this stack is resumed.
1621 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1622 if (r != starting) {
1623 r.startFreezingScreenLocked(r.app, configChanges);
1624 }
1625 if (!r.visible || r.mLaunchTaskBehind) {
1626 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1627 setVisible(r, true);
1628 }
1629 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001630 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001631 return true;
1632 }
1633 }
1634 return false;
1635 }
1636
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001637 private void makeInvisible(ActivityRecord r, ActivityRecord visibleBehind) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001638 if (!r.visible) {
1639 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1640 return;
1641 }
1642 // Now for any activities that aren't visible to the user, make sure they no longer are
1643 // keeping the screen frozen.
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001644 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001645 try {
1646 setVisible(r, false);
1647 switch (r.state) {
1648 case STOPPING:
1649 case STOPPED:
1650 if (r.app != null && r.app.thread != null) {
1651 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1652 "Scheduling invisibility: " + r);
1653 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1654 }
1655 break;
1656
1657 case INITIALIZING:
1658 case RESUMED:
1659 case PAUSING:
1660 case PAUSED:
1661 // This case created for transitioning activities from
1662 // translucent to opaque {@link Activity#convertToOpaque}.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001663 if (visibleBehind == r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001664 releaseBackgroundResources(r);
1665 } else {
1666 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1667 mStackSupervisor.mStoppingActivities.add(r);
1668 }
1669 mStackSupervisor.scheduleIdleLocked();
1670 }
1671 break;
1672
1673 default:
1674 break;
1675 }
1676 } catch (Exception e) {
1677 // Just skip on any failure; we'll make it visible when it next restarts.
1678 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1679 }
1680 }
1681
1682 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1683 TaskRecord task, ActivityRecord r) {
1684 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001685 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
Wale Ogunwale673cbd22016-01-30 18:30:55 -08001686 + " stackInvisible=" + stackInvisible
1687 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001688 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001689 behindFullscreenActivity = true;
Wale Ogunwale673cbd22016-01-30 18:30:55 -08001690 } else if (isHomeStack()) {
1691 if (r.isHomeActivity()) {
1692 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home activity: at " + r
1693 + " stackInvisible=" + stackInvisible
1694 + " behindFullscreenActivity=" + behindFullscreenActivity);
1695 // No other activity in the home stack should be visible behind the home activity.
1696 // Home activities is usually a translucent activity with the wallpaper behind them.
1697 // However, when they don't have the wallpaper behind them, we want to show
1698 // activities in the next application stack behind them vs. another activity in the
1699 // home stack like recents.
1700 behindFullscreenActivity = true;
1701 } else if (r.isRecentsActivity()
1702 && task.getTaskToReturnTo() == APPLICATION_ACTIVITY_TYPE) {
1703 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1704 "Recents activity returning to app: at " + r
1705 + " stackInvisible=" + stackInvisible
1706 + " behindFullscreenActivity=" + behindFullscreenActivity);
1707 // We don't want any other activities in the home stack visible if the recents
1708 // activity is going to be returning to an application activity type.
1709 // We do this to preserve the visible order the user used to get into the recents
1710 // activity. The recents activity is normally translucent and if it doesn't have
1711 // the wallpaper behind it the next activity in the home stack shouldn't be visible
1712 // when the home stack is brought to the front to display the recents activity from
1713 // an app.
1714 behindFullscreenActivity = true;
1715 }
1716 } else if (r.frontOfTask && task.isOverHomeStack()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001717 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1718 + " stackInvisible=" + stackInvisible
1719 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001720 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001721 }
1722 return behindFullscreenActivity;
1723 }
1724
Jorim Jaggie66edb12016-02-05 12:41:17 -08001725 private void makeVisibleIfNeeded(ActivityRecord starting, ActivityRecord r) {
1726
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001727 // This activity is not currently visible, but is running. Tell it to become visible.
Jorim Jaggie66edb12016-02-05 12:41:17 -08001728 if (r.state == ActivityState.RESUMED || r == starting) {
1729 return;
1730 }
1731
1732 // If this activity is paused, tell it to now show its window.
1733 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1734 "Making visible and scheduling visibility: " + r);
1735 try {
1736 if (mTranslucentActivityWaiting != null) {
1737 r.updateOptionsLocked(r.returningOptions);
1738 mUndrawnActivitiesBelowTopTranslucent.add(r);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001739 }
Jorim Jaggie66edb12016-02-05 12:41:17 -08001740 setVisible(r, true);
1741 r.sleeping = false;
1742 r.app.pendingUiClean = true;
1743 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1744 r.stopFreezingScreenLocked(false);
1745 } catch (Exception e) {
1746 // Just skip on any failure; we'll make it
1747 // visible when it next restarts.
1748 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001749 }
1750 }
1751
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001752 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001753 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1754 r.stopFreezingScreenLocked(false);
1755 try {
1756 if (r.returningOptions != null) {
1757 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1758 }
1759 } catch(RemoteException e) {
1760 }
1761 return r.state == ActivityState.RESUMED;
1762 }
1763
Todd Kennedyaab56db2015-01-30 09:39:53 -08001764 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001765 mTranslucentActivityWaiting = r;
1766 mUndrawnActivitiesBelowTopTranslucent.clear();
1767 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1768 }
1769
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001770 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1771 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1772 final TaskRecord task = mTaskHistory.get(taskNdx);
1773 final ArrayList<ActivityRecord> activities = task.mActivities;
1774 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1775 final ActivityRecord r = activities.get(activityNdx);
1776 if ( r.appTimeTracker != except) {
1777 r.appTimeTracker = null;
1778 }
1779 }
1780 }
1781 }
1782
Craig Mautner5eda9b32013-07-02 11:58:16 -07001783 /**
1784 * Called as activities below the top translucent activity are redrawn. When the last one is
1785 * redrawn notify the top activity by calling
1786 * {@link Activity#onTranslucentConversionComplete}.
1787 *
1788 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1789 * occurred and the activity will be notified immediately.
1790 */
1791 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001792 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001793 if ((r == null)
1794 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1795 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1796 // The last undrawn activity below the top has just been drawn. If there is an
1797 // opaque activity at the top, notify it that it can become translucent safely now.
1798 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1799 mTranslucentActivityWaiting = null;
1800 mUndrawnActivitiesBelowTopTranslucent.clear();
1801 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1802
Craig Mautner71dd1b62014-02-18 15:48:52 -08001803 if (waitingActivity != null) {
1804 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1805 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1806 try {
1807 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1808 waitingActivity.appToken, r != null);
1809 } catch (RemoteException e) {
1810 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001811 }
1812 }
1813 }
1814 }
1815
Craig Mautnera61bc652013-10-28 15:43:18 -07001816 /** If any activities below the top running one are in the INITIALIZING state and they have a
1817 * starting window displayed then remove that starting window. It is possible that the activity
1818 * in this state will never resumed in which case that starting window will be orphaned. */
1819 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001820 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001821 boolean aboveTop = true;
1822 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1823 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1824 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1825 final ActivityRecord r = activities.get(activityNdx);
1826 if (aboveTop) {
1827 if (r == topActivity) {
1828 aboveTop = false;
1829 }
1830 continue;
1831 }
1832
1833 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001834 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1835 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001836 r.mStartingWindowShown = false;
1837 mWindowManager.removeAppStartingWindow(r.appToken);
1838 }
1839 }
1840 }
1841 }
1842
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001843 /**
1844 * Ensure that the top activity in the stack is resumed.
1845 *
1846 * @param prev The previously resumed activity, for when in the process
1847 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001848 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001849 *
1850 * @return Returns true if something is being resumed, or false if
1851 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001852 *
1853 * NOTE: It is not safe to call this method directly as it can cause an activity in a
1854 * non-focused stack to be resumed.
1855 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
1856 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001857 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08001858 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001859 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001860 // Don't even start recursing.
1861 return false;
1862 }
1863
1864 boolean result = false;
1865 try {
1866 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001867 mStackSupervisor.inResumeTopActivity = true;
1868 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1869 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001870 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001871 }
Craig Mautner544efa72014-09-04 16:41:20 -07001872 result = resumeTopActivityInnerLocked(prev, options);
1873 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001874 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001875 }
1876 return result;
1877 }
1878
Chong Zhang280d3322015-11-03 17:27:26 -08001879 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001880 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001881
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001882 if (!mService.mBooting && !mService.mBooted) {
1883 // Not ready yet!
1884 return false;
1885 }
1886
Craig Mautnerdf88d732014-01-27 09:21:32 -08001887 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001888 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001889 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001890 // Do not resume this stack if its parent is not resumed.
1891 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1892 return false;
1893 }
1894
Craig Mautnera61bc652013-10-28 15:43:18 -07001895 cancelInitializingActivities();
1896
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001897 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001898 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001899
1900 // Remember how we'll process this pause/resume situation, and ensure
1901 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001902 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1903 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001904
Craig Mautner84984fa2014-06-19 11:19:20 -07001905 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001906 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001907 // There are no more activities!
1908 final String reason = "noMoreActivities";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001909 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(reason)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001910 // Try to move focus to the next visible stack with a running activity if this
1911 // stack is not covering the entire screen.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001912 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
1913 mStackSupervisor.getFocusedStack(), prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001914 }
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001915
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001916 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001917 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001918 if (DEBUG_STATES) Slog.d(TAG_STATES,
1919 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001920 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001921 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001922 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1923 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1924 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001925 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001926 }
1927
1928 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001929
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001930 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001931 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1932 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001933 // Make sure we have executed any pending transitions, since there
1934 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001935 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001936 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001937 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001938 if (DEBUG_STATES) Slog.d(TAG_STATES,
1939 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001940 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001941 return false;
1942 }
1943
Craig Mautner525f3d92013-05-07 14:01:50 -07001944 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001945 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001946 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001947 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001948 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001949 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001950 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001951 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001952 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001953 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001954 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001955 } else if (!isOnHomeDisplay()) {
1956 return false;
1957 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001958 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001959 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001960 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1961 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1962 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001963 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001964 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001965 }
1966
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001967 // If we are sleeping, and there is no resumed activity, and the top
1968 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001969 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001970 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001971 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001972 // Make sure we have executed any pending transitions, since there
1973 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001974 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001975 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001976 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001977 if (DEBUG_STATES) Slog.d(TAG_STATES,
1978 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001979 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001980 return false;
1981 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001982
1983 // Make sure that the user who owns this activity is started. If not,
1984 // we will just leave it as is because someone should be bringing
1985 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001986 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001987 Slog.w(TAG, "Skipping resume of top activity " + next
1988 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001989 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001990 return false;
1991 }
1992
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001993 // The activity may be waiting for stop, but that is no longer
1994 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001995 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001996 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001997 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001998 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001999
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002000 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002001
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002002 // If we are currently pausing an activity, then don't do anything
2003 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07002004 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002005 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002006 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07002007 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002008 return false;
2009 }
2010
Dianne Hackborn0dad3642010-09-09 21:25:35 -07002011 // Okay we are now going to start a switch, to 'next'. We may first
2012 // have to pause the current activity, but this is an important point
2013 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07002014 // XXX "App Redirected" dialog is getting too many false positives
2015 // at this point, so turn off for now.
2016 if (false) {
2017 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
2018 long now = SystemClock.uptimeMillis();
2019 final boolean inTime = mLastStartedActivity.startTime != 0
2020 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
2021 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
2022 final int nextUid = next.info.applicationInfo.uid;
2023 if (inTime && lastUid != nextUid
2024 && lastUid != next.launchedFromUid
2025 && mService.checkPermission(
2026 android.Manifest.permission.STOP_APP_SWITCHES,
2027 -1, next.launchedFromUid)
2028 != PackageManager.PERMISSION_GRANTED) {
2029 mService.showLaunchWarningLocked(mLastStartedActivity, next);
2030 } else {
2031 next.startTime = now;
2032 mLastStartedActivity = next;
2033 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07002034 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07002035 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07002036 mLastStartedActivity = next;
2037 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07002038 }
Craig Mautner58547802013-03-05 08:23:53 -08002039
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002040 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2041
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002042 // We need to start pausing the current activity so the top one
2043 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002044 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
2045 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002046 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002047 if (DEBUG_STATES) Slog.d(TAG_STATES,
2048 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08002049 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002050 }
2051 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002052 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002053 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002054 // At this point we want to put the upcoming activity's process
2055 // at the top of the LRU list, since we know we will be needing it
2056 // very soon and it would be a waste to let it get killed if it
2057 // happens to be sitting towards the end.
2058 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002059 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002060 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002061 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002062 return true;
2063 }
2064
Christopher Tated3f175c2012-06-14 14:16:54 -07002065 // If the most recent activity was noHistory but was only stopped rather
2066 // than stopped+finished because the device went to sleep, we need to make
2067 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07002068 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002069 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002070 if (DEBUG_STATES) Slog.d(TAG_STATES,
2071 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002072 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002073 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002074 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002075 }
2076
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002077 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002078 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2079 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002080 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002081 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2082 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002083 } else {
2084 // The next activity is already visible, so hide the previous
2085 // activity's windows right now so we can show the new one ASAP.
2086 // We only do this if the previous is finishing, which should mean
2087 // it is on top of the one being resumed so hiding it quickly
2088 // is good. Otherwise, we want to do the normal route of allowing
2089 // the resumed activity to be shown so we can decide if the
2090 // previous should actually be hidden depending on whether the
2091 // new one is found to be full-screen or not.
2092 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002093 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002094 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2095 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002096 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002097 + ", nowVisible=" + next.nowVisible);
2098 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002099 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002100 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002101 + ", waitingVisible="
2102 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2103 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 }
2105 }
2106 }
2107
Dianne Hackborne7f97212011-02-24 14:40:20 -08002108 // Launching this app's activity, make sure the app is no longer
2109 // considered stopped.
2110 try {
2111 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002112 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002113 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002114 } catch (IllegalArgumentException e) {
2115 Slog.w(TAG, "Failed trying to unstop package "
2116 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002117 }
2118
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002119 // We are starting up the next activity, so tell the window manager
2120 // that the previous one will be hidden soon. This way it can know
2121 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002122 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002123 if (prev != null) {
2124 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002125 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002126 "Prepare close transition: prev=" + prev);
2127 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002128 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002129 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002130 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002131 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002132 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2133 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002134 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002135 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002136 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002137 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2138 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002139 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002140 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002141 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002142 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002143 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002144 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002145 : next.mLaunchTaskBehind
2146 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2147 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002148 }
2149 }
Craig Mautner967212c2013-04-13 21:10:58 -07002150 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002151 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002152 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002153 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002154 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002155 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002156 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002157 }
2158 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002159
2160 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002161 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002162 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2163 if (opts != null) {
2164 resumeAnimOptions = opts.toBundle();
2165 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002166 next.applyOptionsLocked();
2167 } else {
2168 next.clearOptionsLocked();
2169 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002170
Craig Mautnercf910b02013-04-23 11:23:27 -07002171 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002172 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002173 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002174
2175 // This activity is now becoming visible.
Jorim Jaggi1b025a62016-02-03 19:27:49 -08002176 if (!next.visible) {
2177 mWindowManager.setAppVisibility(next.appToken, true);
2178 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002179
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002180 // schedule launch ticks to collect information about slow apps.
2181 next.startLaunchTickingLocked();
2182
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002183 ActivityRecord lastResumedActivity =
2184 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002185 ActivityState lastState = next.state;
2186
2187 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002188
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002189 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002190 next.state = ActivityState.RESUMED;
2191 mResumedActivity = next;
2192 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002193 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002194 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002195 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002196 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002197
2198 // Have the window manager re-evaluate the orientation of
2199 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002200 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002201 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002202 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002203 mService.mConfiguration,
2204 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2205 if (config != null) {
2206 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002207 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002208 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002209 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002210
Craig Mautner525f3d92013-05-07 14:01:50 -07002211 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002212 // The configuration update wasn't able to keep the existing
2213 // instance of the activity, and instead started a new one.
2214 // We should be all done, but let's just make sure our activity
2215 // is still at the top and schedule another run if something
2216 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002217 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002218 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002219 "Activity config changed during resume: " + next
2220 + ", new next: " + nextNext);
2221 if (nextNext != next) {
2222 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002223 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002224 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002225 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002226 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002227 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002228 return true;
2229 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002230 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002231 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002232 }
Craig Mautner58547802013-03-05 08:23:53 -08002233
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002234 try {
2235 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002236 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002237 if (a != null) {
2238 final int N = a.size();
2239 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002240 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2241 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002242 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002243 }
2244 }
2245
2246 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002247 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002248 }
2249
Craig Mautner299f9602015-01-26 09:47:33 -08002250 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2251 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002252
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002253 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002254 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002255 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002256 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002257 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002258 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002259 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002260
Craig Mautner0eea92c2013-05-16 13:35:39 -07002261 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002262
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002263 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002264 } catch (Exception e) {
2265 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002266 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002267 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002268 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002269 if (lastStack != null) {
2270 lastStack.mResumedActivity = lastResumedActivity;
2271 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002272 Slog.i(TAG, "Restarting because process died: " + next);
2273 if (!next.hasBeenLaunched) {
2274 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002275 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2276 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002277 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002278 next.appToken, next.packageName, next.theme,
2279 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002280 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2281 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002282 }
George Mount2c92c972014-03-20 09:38:23 -07002283 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002284 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002285 return true;
2286 }
2287
2288 // From this point on, if something goes wrong there is no way
2289 // to recover the activity.
2290 try {
2291 next.visible = true;
2292 completeResumeLocked(next);
2293 } catch (Exception e) {
2294 // If any exception gets thrown, toss away this
2295 // activity and try the next one.
2296 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002297 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002298 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002299 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002300 return true;
2301 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002302 next.stopped = false;
2303
2304 } else {
2305 // Whoops, need to restart this activity!
2306 if (!next.hasBeenLaunched) {
2307 next.hasBeenLaunched = true;
2308 } else {
2309 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002310 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002311 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002312 mService.compatibilityInfoForPackageLocked(
2313 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002314 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002315 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002316 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002317 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002318 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002319 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002320 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002321 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002322 }
2323
Craig Mautnercf910b02013-04-23 11:23:27 -07002324 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002325 return true;
2326 }
2327
riddle_hsuc215a4f2014-12-27 12:10:45 +08002328 private TaskRecord getNextTask(TaskRecord targetTask) {
2329 final int index = mTaskHistory.indexOf(targetTask);
2330 if (index >= 0) {
2331 final int numTasks = mTaskHistory.size();
2332 for (int i = index + 1; i < numTasks; ++i) {
2333 TaskRecord task = mTaskHistory.get(i);
2334 if (task.userId == targetTask.userId) {
2335 return task;
2336 }
2337 }
2338 }
2339 return null;
2340 }
2341
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002342 private void insertTaskAtPosition(TaskRecord task, int position) {
2343 if (position >= mTaskHistory.size()) {
2344 insertTaskAtTop(task, null);
2345 return;
2346 }
2347 // Calculate maximum possible position for this task.
2348 int maxPosition = mTaskHistory.size();
2349 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002350 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002351 // Put non-current user tasks below current user tasks.
2352 while (maxPosition > 0) {
2353 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2354 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002355 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002356 break;
2357 }
2358 maxPosition--;
2359 }
2360 }
2361 position = Math.min(position, maxPosition);
2362 mTaskHistory.remove(task);
2363 mTaskHistory.add(position, task);
2364 updateTaskMovement(task, true);
2365 }
2366
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002367 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002368 // If the moving task is over home stack, transfer its return type to next task
2369 if (task.isOverHomeStack()) {
2370 final TaskRecord nextTask = getNextTask(task);
2371 if (nextTask != null) {
2372 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2373 }
2374 }
2375
Craig Mautner9c85c202013-10-04 20:11:26 -07002376 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002377 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002378 if (isOnHomeDisplay()) {
2379 ActivityStack lastStack = mStackSupervisor.getLastStack();
2380 final boolean fromHome = lastStack.isHomeStack();
2381 if (!isHomeStack() && (fromHome || topTask() != task)) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002382 int returnToType = APPLICATION_ACTIVITY_TYPE;
2383 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2384 returnToType = lastStack.topTask() == null
2385 ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2386 }
2387 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002388 }
2389 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002390 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002391 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002392
Craig Mautnerac6f8432013-07-17 13:24:59 -07002393 mTaskHistory.remove(task);
2394 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002395 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002396 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002397 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002398 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002399 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002400 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002401 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002402 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002403 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002404 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002405 break;
2406 }
2407 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002408 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002409 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002410 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002411 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002412 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002413
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002414 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2415 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002416 TaskRecord rTask = r.task;
2417 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002418 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2419 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002420 // Last activity in task had been removed or ActivityManagerService is reusing task.
2421 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002422 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002423 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002424 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002425 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002426 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002427 if (!newTask) {
2428 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002429 boolean startIt = true;
2430 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2431 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002432 if (task.getTopActivity() == null) {
2433 // All activities in task are finishing.
2434 continue;
2435 }
Craig Mautner70a86932013-02-28 22:37:44 -08002436 if (task == r.task) {
2437 // Here it is! Now, if this is not yet visible to the
2438 // user, then just add it without starting; it will
2439 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002440 if (!startIt) {
2441 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2442 + task, new RuntimeException("here").fillInStackTrace());
2443 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002444 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002445 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002446 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002447 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002448 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002449 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002450 return;
2451 }
2452 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002453 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002454 startIt = false;
2455 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002456 }
2457 }
2458
2459 // Place a new activity at top of stack, so it is next to interact
2460 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002461
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002462 // If we are not placing the new activity frontmost, we do not want
2463 // to deliver the onUserLeaving callback to the actual frontmost
2464 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002465 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002466 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002467 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002468 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002469 }
Craig Mautner70a86932013-02-28 22:37:44 -08002470
2471 task = r.task;
2472
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002473 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002474 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002475 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002476 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002477 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002478
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002479 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002480 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002481 // We want to show the starting preview window if we are
2482 // switching to a new task, or the next activity's process is
2483 // not currently running.
2484 boolean showStartingIcon = newTask;
2485 ProcessRecord proc = r.app;
2486 if (proc == null) {
2487 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2488 }
2489 if (proc == null || proc.thread == null) {
2490 showStartingIcon = true;
2491 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002492 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002493 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002494 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002495 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002496 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002497 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002498 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002499 ? r.mLaunchTaskBehind
2500 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2501 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002502 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002503 mNoAnimActivities.remove(r);
2504 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002505 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002506 boolean doShow = true;
2507 if (newTask) {
2508 // Even though this activity is starting fresh, we still need
2509 // to reset it to make sure we apply affinities to move any
2510 // existing activities from other tasks in to it.
2511 // If the caller has requested that the target task be
2512 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002513 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002514 resetTaskIfNeededLocked(r, r);
2515 doShow = topRunningNonDelayedActivityLocked(null) == r;
2516 }
Chong Zhang280d3322015-11-03 17:27:26 -08002517 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002518 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2519 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002520 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002521 if (r.mLaunchTaskBehind) {
2522 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2523 // tell WindowManager that r is visible even though it is at the back of the stack.
2524 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002525 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002526 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002527 // Figure out if we are transitioning from another activity that is
2528 // "has the same starting icon" as the next one. This allows the
2529 // window manager to keep the previous window it had previously
2530 // created, if it still had one.
2531 ActivityRecord prev = mResumedActivity;
2532 if (prev != null) {
2533 // We don't want to reuse the previous starting preview if:
2534 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002535 if (prev.task != r.task) {
2536 prev = null;
2537 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002538 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002539 else if (prev.nowVisible) {
2540 prev = null;
2541 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002542 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002543 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002544 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002545 mService.compatibilityInfoForPackageLocked(
2546 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002547 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002548 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002549 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002550 }
2551 } else {
2552 // If this is the first activity, don't do any fancy animations,
2553 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002554 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002555 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002556 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002557 }
2558 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002559 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002560 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002561 }
2562
Dianne Hackbornbe707852011-11-11 14:32:10 -08002563 final void validateAppTokensLocked() {
2564 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002565 mValidateAppTokens.ensureCapacity(numActivities());
2566 final int numTasks = mTaskHistory.size();
2567 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2568 TaskRecord task = mTaskHistory.get(taskNdx);
2569 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002570 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002571 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002572 }
Craig Mautner000f0022013-02-26 15:04:29 -08002573 TaskGroup group = new TaskGroup();
2574 group.taskId = task.taskId;
2575 mValidateAppTokens.add(group);
2576 final int numActivities = activities.size();
2577 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2578 final ActivityRecord r = activities.get(activityNdx);
2579 group.tokens.add(r.appToken);
2580 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002581 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002582 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002583 }
2584
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002585 /**
2586 * Perform a reset of the given task, if needed as part of launching it.
2587 * Returns the new HistoryRecord at the top of the task.
2588 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002589 /**
2590 * Helper method for #resetTaskIfNeededLocked.
2591 * We are inside of the task being reset... we'll either finish this activity, push it out
2592 * for another task, or leave it as-is.
2593 * @param task The task containing the Activity (taskTop) that might be reset.
2594 * @param forceReset
2595 * @return An ActivityOptions that needs to be processed.
2596 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002597 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002598 ActivityOptions topOptions = null;
2599
2600 int replyChainEnd = -1;
2601 boolean canMoveOptions = true;
2602
2603 // We only do this for activities that are not the root of the task (since if we finish
2604 // the root, we may no longer have the task!).
2605 final ArrayList<ActivityRecord> activities = task.mActivities;
2606 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002607 final int rootActivityNdx = task.findEffectiveRootIndex();
2608 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002609 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002610 if (target.frontOfTask)
2611 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002612
2613 final int flags = target.info.flags;
2614 final boolean finishOnTaskLaunch =
2615 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2616 final boolean allowTaskReparenting =
2617 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2618 final boolean clearWhenTaskReset =
2619 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2620
2621 if (!finishOnTaskLaunch
2622 && !clearWhenTaskReset
2623 && target.resultTo != null) {
2624 // If this activity is sending a reply to a previous
2625 // activity, we can't do anything with it now until
2626 // we reach the start of the reply chain.
2627 // XXX note that we are assuming the result is always
2628 // to the previous activity, which is almost always
2629 // the case but we really shouldn't count on.
2630 if (replyChainEnd < 0) {
2631 replyChainEnd = i;
2632 }
2633 } else if (!finishOnTaskLaunch
2634 && !clearWhenTaskReset
2635 && allowTaskReparenting
2636 && target.taskAffinity != null
2637 && !target.taskAffinity.equals(task.affinity)) {
2638 // If this activity has an affinity for another
2639 // task, then we need to move it out of here. We will
2640 // move it as far out of the way as possible, to the
2641 // bottom of the activity stack. This also keeps it
2642 // correctly ordered with any activities we previously
2643 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002644 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002645 final ActivityRecord bottom =
2646 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002647 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002648 if (bottom != null && target.taskAffinity != null
2649 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002650 // If the activity currently at the bottom has the
2651 // same task affinity as the one we are moving,
2652 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002653 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002654 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002655 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002656 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002657 targetTask = createTaskRecord(
2658 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
2659 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002660 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002661 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002662 + " out to new task " + target.task);
2663 }
2664
Wale Ogunwale706ed792015-08-02 10:29:44 -07002665 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002666
Craig Mautner525f3d92013-05-07 14:01:50 -07002667 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002668 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2669 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002670 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002671 if (p.finishing) {
2672 continue;
2673 }
2674
Craig Mautnere3a74d52013-02-22 14:14:58 -08002675 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002676 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002677 topOptions = p.takeOptionsLocked();
2678 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002679 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002680 }
2681 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002682 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2683 "Removing activity " + p + " from task=" + task + " adding to task="
2684 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002685 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2686 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002687 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002688 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002689
Wale Ogunwale706ed792015-08-02 10:29:44 -07002690 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002691 }
2692
Wale Ogunwale706ed792015-08-02 10:29:44 -07002693 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002694 if (VALIDATE_TOKENS) {
2695 validateAppTokensLocked();
2696 }
2697
2698 replyChainEnd = -1;
2699 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2700 // If the activity should just be removed -- either
2701 // because it asks for it, or the task should be
2702 // cleared -- then finish it and anything that is
2703 // part of its reply chain.
2704 int end;
2705 if (clearWhenTaskReset) {
2706 // In this case, we want to finish this activity
2707 // and everything above it, so be sneaky and pretend
2708 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002709 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002710 } else if (replyChainEnd < 0) {
2711 end = i;
2712 } else {
2713 end = replyChainEnd;
2714 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002715 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002716 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002717 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002718 if (p.finishing) {
2719 continue;
2720 }
2721 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002722 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002723 topOptions = p.takeOptionsLocked();
2724 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002725 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002726 }
2727 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002728 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002729 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002730 if (finishActivityLocked(
2731 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002732 end--;
2733 srcPos--;
2734 }
2735 }
2736 replyChainEnd = -1;
2737 } else {
2738 // If we were in the middle of a chain, well the
2739 // activity that started it all doesn't want anything
2740 // special, so leave it all as-is.
2741 replyChainEnd = -1;
2742 }
2743 }
2744
2745 return topOptions;
2746 }
2747
2748 /**
2749 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2750 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2751 * @param affinityTask The task we are looking for an affinity to.
2752 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2753 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2754 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2755 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002756 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002757 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002758 int replyChainEnd = -1;
2759 final int taskId = task.taskId;
2760 final String taskAffinity = task.affinity;
2761
2762 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2763 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002764 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2765
2766 // Do not operate on or below the effective root Activity.
2767 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002768 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002769 if (target.frontOfTask)
2770 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002771
2772 final int flags = target.info.flags;
2773 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2774 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2775
2776 if (target.resultTo != null) {
2777 // If this activity is sending a reply to a previous
2778 // activity, we can't do anything with it now until
2779 // we reach the start of the reply chain.
2780 // XXX note that we are assuming the result is always
2781 // to the previous activity, which is almost always
2782 // the case but we really shouldn't count on.
2783 if (replyChainEnd < 0) {
2784 replyChainEnd = i;
2785 }
2786 } else if (topTaskIsHigher
2787 && allowTaskReparenting
2788 && taskAffinity != null
2789 && taskAffinity.equals(target.taskAffinity)) {
2790 // This activity has an affinity for our task. Either remove it if we are
2791 // clearing or move it over to our task. Note that
2792 // we currently punt on the case where we are resetting a
2793 // task that is not at the top but who has activities above
2794 // with an affinity to it... this is really not a normal
2795 // case, and we will need to later pull that task to the front
2796 // and usually at that point we will do the reset and pick
2797 // up those remaining activities. (This only happens if
2798 // someone starts an activity in a new task from an activity
2799 // in a task that is not currently on top.)
2800 if (forceReset || finishOnTaskLaunch) {
2801 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002802 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2803 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002804 for (int srcPos = start; srcPos >= i; --srcPos) {
2805 final ActivityRecord p = activities.get(srcPos);
2806 if (p.finishing) {
2807 continue;
2808 }
Todd Kennedy539db512014-12-15 09:57:55 -08002809 finishActivityLocked(
2810 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002811 }
2812 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002813 if (taskInsertionPoint < 0) {
2814 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002815
Craig Mautner77878772013-03-04 19:46:24 -08002816 }
Craig Mautner77878772013-03-04 19:46:24 -08002817
2818 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002819 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2820 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2821 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002822 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002823 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002824 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002825 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002826
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002827 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2828 "Removing and adding activity " + p + " to stack at " + task
2829 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002830 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2831 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002832 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002833 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002834 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002835 if (VALIDATE_TOKENS) {
2836 validateAppTokensLocked();
2837 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002838
2839 // Now we've moved it in to place... but what if this is
2840 // a singleTop activity and we have put it on top of another
2841 // instance of the same activity? Then we drop the instance
2842 // below so it remains singleTop.
2843 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2844 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002845 int targetNdx = taskActivities.indexOf(target);
2846 if (targetNdx > 0) {
2847 ActivityRecord p = taskActivities.get(targetNdx - 1);
2848 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002849 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2850 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002851 }
2852 }
2853 }
2854 }
2855
2856 replyChainEnd = -1;
2857 }
2858 }
Craig Mautner77878772013-03-04 19:46:24 -08002859 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002860 }
2861
Craig Mautner8849a5e2013-04-02 16:41:03 -07002862 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002863 ActivityRecord newActivity) {
2864 boolean forceReset =
2865 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2866 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2867 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2868 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2869 forceReset = true;
2870 }
2871 }
2872
2873 final TaskRecord task = taskTop.task;
2874
2875 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2876 * for remaining tasks. Used for later tasks to reparent to task. */
2877 boolean taskFound = false;
2878
2879 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2880 ActivityOptions topOptions = null;
2881
Craig Mautner77878772013-03-04 19:46:24 -08002882 // Preserve the location for reparenting in the new task.
2883 int reparentInsertionPoint = -1;
2884
Craig Mautnere3a74d52013-02-22 14:14:58 -08002885 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2886 final TaskRecord targetTask = mTaskHistory.get(i);
2887
2888 if (targetTask == task) {
2889 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2890 taskFound = true;
2891 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002892 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2893 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002894 }
2895 }
2896
Craig Mautner70a86932013-02-28 22:37:44 -08002897 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002898 if (taskNdx >= 0) {
2899 do {
2900 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2901 } while (taskTop == null && taskNdx >= 0);
2902 }
Craig Mautner70a86932013-02-28 22:37:44 -08002903
Craig Mautnere3a74d52013-02-22 14:14:58 -08002904 if (topOptions != null) {
2905 // If we got some ActivityOptions from an activity on top that
2906 // was removed from the task, propagate them to the new real top.
2907 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002908 taskTop.updateOptionsLocked(topOptions);
2909 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002910 topOptions.abort();
2911 }
2912 }
2913
2914 return taskTop;
2915 }
2916
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002917 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2918 String resultWho, int requestCode, int resultCode, Intent data) {
2919
2920 if (callingUid > 0) {
2921 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002922 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002923 }
2924
2925 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2926 + " : who=" + resultWho + " req=" + requestCode
2927 + " res=" + resultCode + " data=" + data);
2928 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2929 try {
2930 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2931 list.add(new ResultInfo(resultWho, requestCode,
2932 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002933 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002934 return;
2935 } catch (Exception e) {
2936 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2937 }
2938 }
2939
2940 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2941 }
2942
Craig Mautner299f9602015-01-26 09:47:33 -08002943 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002944 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2945 return;
2946 }
2947
2948 final ActivityRecord next = topRunningActivityLocked();
2949 final String myReason = reason + " adjustFocus";
2950 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002951 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002952 // For freeform, docked, and pinned stacks we always keep the focus within the
2953 // stack as long as there is a running activity in the stack that we can adjust
2954 // focus to.
2955 mService.setFocusedActivityLocked(next, myReason);
2956 return;
2957 } else {
2958 final TaskRecord task = r.task;
2959 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2960 // For non-fullscreen stack, we want to move the focus to the next visible
2961 // stack to prevent the home screen from moving to the top and obscuring
2962 // other visible stacks.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002963 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002964 return;
2965 }
2966 // Move the home stack to the top if this stack is fullscreen or there is no
2967 // other visible stack.
2968 if (mStackSupervisor.moveHomeStackTaskToTop(
2969 task.getTaskToReturnTo(), myReason)) {
2970 // Activity focus was already adjusted. Nothing else to do...
2971 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002972 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002973 }
2974 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08002975 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002976
Wale Ogunwaled046a012015-12-24 13:05:59 -08002977 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002978 }
2979
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002980 private boolean adjustFocusToNextFocusableStackLocked(String reason) {
2981 final ActivityStack stack = getNextFocusableStackLocked();
2982 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002983 if (stack == null) {
2984 return false;
2985 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002986 return mService.setFocusedActivityLocked(stack.topRunningActivityLocked(), myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002987 }
2988
Craig Mautnerf3333272013-04-22 10:55:53 -07002989 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002990 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002991 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2992 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2993 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002994 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002995 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002996 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2997 "stop-no-history", false)) {
2998 // Activity was finished, no need to continue trying to schedule stop.
2999 adjustFocusedActivityLocked(r, "stopActivityFinished");
3000 r.resumeKeyDispatchingLocked();
3001 return;
3002 }
Christopher Tated3f175c2012-06-14 14:16:54 -07003003 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003004 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07003005 + " on stop because we're just sleeping");
3006 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003007 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07003008 }
3009
3010 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08003011 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003012 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003013 try {
3014 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003015 if (DEBUG_STATES) Slog.v(TAG_STATES,
3016 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003017 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003018 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
3019 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003020 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003021 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003022 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08003023 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07003024 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003025 r.setSleeping(true);
3026 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003027 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003028 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003029 } catch (Exception e) {
3030 // Maybe just ignore exceptions here... if the process
3031 // has crashed, our death notification will clean things
3032 // up.
3033 Slog.w(TAG, "Exception thrown during pause", e);
3034 // Just in case, assume it to be stopped.
3035 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003036 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003037 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003038 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003039 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003040 }
3041 }
3042 }
3043 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003044
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003045 /**
3046 * @return Returns true if the activity is being finished, false if for
3047 * some reason it is being left as-is.
3048 */
3049 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003050 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003051 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003052 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3053 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003054 + ", result=" + resultCode + ", data=" + resultData
3055 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003056 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003057 return false;
3058 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003059
Craig Mautnerd44711d2013-02-23 11:24:36 -08003060 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003061 return true;
3062 }
3063
Craig Mautnerd2328952013-03-05 12:46:26 -08003064 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003065 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3066 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3067 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3068 ActivityRecord r = activities.get(activityNdx);
3069 if (r.resultTo == self && r.requestCode == requestCode) {
3070 if ((r.resultWho == null && resultWho == null) ||
3071 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3072 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3073 false);
3074 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003075 }
3076 }
3077 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003078 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003079 }
3080
Adrian Roos20d7df32016-01-12 18:59:43 +01003081 final TaskRecord finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003082 ActivityRecord r = topRunningActivityLocked();
Adrian Roos20d7df32016-01-12 18:59:43 +01003083 TaskRecord finishedTask = null;
3084 if (r == null || r.app != app) {
3085 return null;
3086 }
3087 Slog.w(TAG, " Force finishing activity "
3088 + r.intent.getComponent().flattenToShortString());
3089 int taskNdx = mTaskHistory.indexOf(r.task);
3090 int activityNdx = r.task.mActivities.indexOf(r);
3091 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
3092 finishedTask = r.task;
3093 // Also terminate any activities below it that aren't yet
3094 // stopped, to avoid a situation where one will get
3095 // re-start our crashing activity once it gets resumed again.
3096 --activityNdx;
3097 if (activityNdx < 0) {
3098 do {
3099 --taskNdx;
3100 if (taskNdx < 0) {
3101 break;
3102 }
3103 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3104 } while (activityNdx < 0);
3105 }
3106 if (activityNdx >= 0) {
3107 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
3108 if (r.state == ActivityState.RESUMED
3109 || r.state == ActivityState.PAUSING
3110 || r.state == ActivityState.PAUSED) {
3111 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
3112 Slog.w(TAG, " Force finishing activity "
3113 + r.intent.getComponent().flattenToShortString());
3114 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003115 }
3116 }
3117 }
Adrian Roos20d7df32016-01-12 18:59:43 +01003118 return finishedTask;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003119 }
3120
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003121 final void finishVoiceTask(IVoiceInteractionSession session) {
3122 IBinder sessionBinder = session.asBinder();
3123 boolean didOne = false;
3124 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3125 TaskRecord tr = mTaskHistory.get(taskNdx);
3126 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3127 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3128 ActivityRecord r = tr.mActivities.get(activityNdx);
3129 if (!r.finishing) {
3130 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3131 false);
3132 didOne = true;
3133 }
3134 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003135 } else {
3136 // Check if any of the activities are using voice
3137 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3138 ActivityRecord r = tr.mActivities.get(activityNdx);
3139 if (r.voiceSession != null
3140 && r.voiceSession.asBinder() == sessionBinder) {
3141 // Inform of cancellation
3142 r.clearVoiceSessionLocked();
3143 try {
3144 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3145 null);
3146 } catch (RemoteException re) {
3147 // Ok
3148 }
3149 // TODO: VI This is redundant in some cases
3150 mService.finishRunningVoiceLocked();
3151 break;
3152 }
3153 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003154 }
3155 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003156 Slog.d(TAG, "ActivityStack.finishVoiceTask()");
3157
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003158 if (didOne) {
3159 mService.updateOomAdjLocked();
3160 }
3161 }
3162
Craig Mautnerd2328952013-03-05 12:46:26 -08003163 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003164 ArrayList<ActivityRecord> activities = r.task.mActivities;
3165 for (int index = activities.indexOf(r); index >= 0; --index) {
3166 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003167 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003168 break;
3169 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003170 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003171 }
3172 return true;
3173 }
3174
Dianne Hackborn5c607432012-02-28 14:44:19 -08003175 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3176 // send the result
3177 ActivityRecord resultTo = r.resultTo;
3178 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003179 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003180 + " who=" + r.resultWho + " req=" + r.requestCode
3181 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003182 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003183 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003184 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003185 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003186 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003187 if (r.info.applicationInfo.uid > 0) {
3188 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3189 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003190 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003191 }
3192 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3193 resultData);
3194 r.resultTo = null;
3195 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003196 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003197
3198 // Make sure this HistoryRecord is not holding on to other resources,
3199 // because clients have remote IPC references to this object so we
3200 // can't assume that will go away and want to avoid circular IPC refs.
3201 r.results = null;
3202 r.pendingResults = null;
3203 r.newIntents = null;
3204 r.icicle = null;
3205 }
3206
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003207 /**
3208 * @return Returns true if this activity has been removed from the history
3209 * list, or false if it is still in the list and will be removed later.
3210 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003211 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3212 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003213 if (r.finishing) {
3214 Slog.w(TAG, "Duplicate finish request for " + r);
3215 return false;
3216 }
3217
Wale Ogunwale7d701172015-03-11 15:36:30 -07003218 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003219 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003220 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003221 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003222 task.taskId, r.shortComponentName, reason);
3223 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003224 final int index = activities.indexOf(r);
3225 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003226 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003227 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003228 // If the caller asked that this activity (and all above it)
3229 // be cleared when the task is reset, don't lose that information,
3230 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003231 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003232 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003233 }
3234 }
3235
3236 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003237
Craig Mautner299f9602015-01-26 09:47:33 -08003238 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003239
Dianne Hackborn5c607432012-02-28 14:44:19 -08003240 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003241
Craig Mautnerde4ef022013-04-07 19:01:33 -07003242 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003243 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003244 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003245 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003246 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003247 ? AppTransition.TRANSIT_TASK_CLOSE
3248 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003249
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003250 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003251 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003252
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003253 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003254 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3255 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3256 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003257 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003258 }
3259
Craig Mautneraea74a52014-03-08 14:23:10 -08003260 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003261 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003262 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003263 } else if (r.state != ActivityState.PAUSING) {
3264 // If the activity is PAUSING, we will complete the finish once
3265 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003266 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003267 if (r.visible) {
3268 mWindowManager.setAppVisibility(r.appToken, false);
3269 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003270 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003271 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003272 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003273 }
3274
3275 return false;
3276 }
3277
Craig Mautnerf3333272013-04-22 10:55:53 -07003278 static final int FINISH_IMMEDIATELY = 0;
3279 static final int FINISH_AFTER_PAUSE = 1;
3280 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003281
Craig Mautnerf3333272013-04-22 10:55:53 -07003282 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003283 // First things first: if this activity is currently visible,
3284 // and the resumed activity is not yet visible, then hold off on
3285 // finishing until the resumed one becomes visible.
3286 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003287 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08003288 addToStopping(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003289 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003290 if (DEBUG_STATES) Slog.v(TAG_STATES,
3291 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003292 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003293 if (oomAdj) {
3294 mService.updateOomAdjLocked();
3295 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003296 return r;
3297 }
3298
3299 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003300 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003301 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003302 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003303 if (mResumedActivity == r) {
3304 mResumedActivity = null;
3305 }
3306 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003307 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003308 r.state = ActivityState.FINISHING;
3309
3310 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003311 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003312 || prevState == ActivityState.STOPPED
3313 || prevState == ActivityState.INITIALIZING) {
3314 // If this activity is already stopped, we can just finish
3315 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003316 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003317 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003318 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003319 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003320 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003321 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003322 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3323 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003324 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003325 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003326
3327 // Need to go through the full pause cycle to get this
3328 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003329 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003330 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003331 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003332 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003333 return r;
3334 }
3335
Craig Mautneree36c772014-07-16 14:56:05 -07003336 void finishAllActivitiesLocked(boolean immediately) {
3337 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003338 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3339 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3340 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3341 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003342 noActivitiesInStack = false;
3343 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003344 continue;
3345 }
Craig Mautneree36c772014-07-16 14:56:05 -07003346 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003347 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3348 }
3349 }
Craig Mautneree36c772014-07-16 14:56:05 -07003350 if (noActivitiesInStack) {
3351 mActivityContainer.onTaskListEmptyLocked();
3352 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003353 }
3354
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003355 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3356 // Basic case: for simple app-centric recents, we need to recreate
3357 // the task if the affinity has changed.
3358 if (srec == null || srec.task.affinity == null ||
3359 !srec.task.affinity.equals(destAffinity)) {
3360 return true;
3361 }
3362 // Document-centric case: an app may be split in to multiple documents;
3363 // they need to re-create their task if this current activity is the root
3364 // of a document, unless simply finishing it will return them to the the
3365 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003366 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3367 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003368 // Okay, this activity is at the root of its task. What to do, what to do...
3369 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3370 // Finishing won't return to an application, so we need to recreate.
3371 return true;
3372 }
3373 // We now need to get the task below it to determine what to do.
3374 int taskIdx = mTaskHistory.indexOf(srec.task);
3375 if (taskIdx <= 0) {
3376 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3377 return false;
3378 }
3379 if (taskIdx == 0) {
3380 // At the bottom of the stack, nothing to go back to.
3381 return true;
3382 }
3383 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3384 if (!srec.task.affinity.equals(prevTask.affinity)) {
3385 // These are different apps, so need to recreate.
3386 return true;
3387 }
3388 }
3389 return false;
3390 }
3391
Wale Ogunwale7d701172015-03-11 15:36:30 -07003392 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003393 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003394 final TaskRecord task = srec.task;
3395 final ArrayList<ActivityRecord> activities = task.mActivities;
3396 final int start = activities.indexOf(srec);
3397 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003398 return false;
3399 }
3400 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003401 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003402 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003403 final ComponentName dest = destIntent.getComponent();
3404 if (start > 0 && dest != null) {
3405 for (int i = finishTo; i >= 0; i--) {
3406 ActivityRecord r = activities.get(i);
3407 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003408 r.info.name.equals(dest.getClassName())) {
3409 finishTo = i;
3410 parent = r;
3411 foundParentInTask = true;
3412 break;
3413 }
3414 }
3415 }
3416
3417 IActivityController controller = mService.mController;
3418 if (controller != null) {
3419 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3420 if (next != null) {
3421 // ask watcher if this is allowed
3422 boolean resumeOK = true;
3423 try {
3424 resumeOK = controller.activityResuming(next.packageName);
3425 } catch (RemoteException e) {
3426 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003427 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003428 }
3429
3430 if (!resumeOK) {
3431 return false;
3432 }
3433 }
3434 }
3435 final long origId = Binder.clearCallingIdentity();
3436 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003437 ActivityRecord r = activities.get(i);
3438 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003439 // Only return the supplied result for the first activity finished
3440 resultCode = Activity.RESULT_CANCELED;
3441 resultData = null;
3442 }
3443
3444 if (parent != null && foundParentInTask) {
3445 final int parentLaunchMode = parent.info.launchMode;
3446 final int destIntentFlags = destIntent.getFlags();
3447 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3448 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3449 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3450 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003451 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3452 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003453 } else {
3454 try {
3455 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3456 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003457 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3458 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3459 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003460 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3461 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003462 foundParentInTask = res == ActivityManager.START_SUCCESS;
3463 } catch (RemoteException e) {
3464 foundParentInTask = false;
3465 }
3466 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003467 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003468 }
3469 }
3470 Binder.restoreCallingIdentity(origId);
3471 return foundParentInTask;
3472 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003473 /**
3474 * Perform the common clean-up of an activity record. This is called both
3475 * as part of destroyActivityLocked() (when destroying the client-side
3476 * representation) and cleaning things up as a result of its hosting
3477 * processing going away, in which case there is no remaining client-side
3478 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003479 *
3480 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003481 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003482 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3483 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003484 if (mResumedActivity == r) {
3485 mResumedActivity = null;
3486 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003487 if (mPausingActivity == r) {
3488 mPausingActivity = null;
3489 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003490 mService.resetFocusedActivityIfNeededLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003491
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003492 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003493 r.frozenBeforeDestroy = false;
3494
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003495 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003496 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003497 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003498 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003499 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003500 }
3501
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003502 // Make sure this record is no longer in the pending finishes list.
3503 // This could happen, for example, if we are trimming activities
3504 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003505 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003506 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003507
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003508 // Remove any pending results.
3509 if (r.finishing && r.pendingResults != null) {
3510 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3511 PendingIntentRecord rec = apr.get();
3512 if (rec != null) {
3513 mService.cancelIntentSenderLocked(rec, false);
3514 }
3515 }
3516 r.pendingResults = null;
3517 }
3518
3519 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003520 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003521 }
3522
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003523 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003524 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003525 if (getVisibleBehindActivity() == r) {
3526 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003527 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003528 }
3529
3530 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003531 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003532 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003533 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003534 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003535 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003536 }
3537
Craig Mautner299f9602015-01-26 09:47:33 -08003538 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003539 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003540 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003541 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003542 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3543 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3544
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003545 r.takeFromHistory();
3546 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003547 if (DEBUG_STATES) Slog.v(TAG_STATES,
3548 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003549 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003550 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003551 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003552 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003553 if (VALIDATE_TOKENS) {
3554 validateAppTokensLocked();
3555 }
Craig Mautner312ba862014-02-10 17:55:01 -08003556 final TaskRecord task = r.task;
3557 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003558 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003559 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003560 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003561 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003562 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003563 }
Craig Mautner299f9602015-01-26 09:47:33 -08003564 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003565 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003566 cleanUpActivityServicesLocked(r);
3567 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003568 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003569
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003570 /**
3571 * Perform clean-up of service connections in an activity record.
3572 */
3573 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3574 // Throw away any services that have been bound by this activity.
3575 if (r.connections != null) {
3576 Iterator<ConnectionRecord> it = r.connections.iterator();
3577 while (it.hasNext()) {
3578 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003579 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003580 }
3581 r.connections = null;
3582 }
3583 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003584
Craig Mautneree2e45a2014-06-27 12:10:03 -07003585 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003586 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003587 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003588 mHandler.sendMessage(msg);
3589 }
3590
Craig Mautneree2e45a2014-06-27 12:10:03 -07003591 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003592 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003593 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003594 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3595 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3596 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3597 final ActivityRecord r = activities.get(activityNdx);
3598 if (r.finishing) {
3599 continue;
3600 }
3601 if (r.fullscreen) {
3602 lastIsOpaque = true;
3603 }
3604 if (owner != null && r.app != owner) {
3605 continue;
3606 }
3607 if (!lastIsOpaque) {
3608 continue;
3609 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003610 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003611 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003612 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003613 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003614 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003615 activityRemoved = true;
3616 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003617 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003618 }
3619 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003620 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003621 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003622 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003623 }
3624
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003625 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3626 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003627 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3628 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003629 + " pausing=" + mPausingActivity + " for reason " + reason);
3630 return destroyActivityLocked(r, true, reason);
3631 }
3632 return false;
3633 }
3634
3635 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3636 String reason) {
3637 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003638 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003639 int maxTasks = tasks.size() / 4;
3640 if (maxTasks < 1) {
3641 maxTasks = 1;
3642 }
3643 int numReleased = 0;
3644 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3645 final TaskRecord task = mTaskHistory.get(taskNdx);
3646 if (!tasks.contains(task)) {
3647 continue;
3648 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003649 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003650 int curNum = 0;
3651 final ArrayList<ActivityRecord> activities = task.mActivities;
3652 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3653 final ActivityRecord activity = activities.get(actNdx);
3654 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003655 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003656 + " in state " + activity.state + " resumed=" + mResumedActivity
3657 + " pausing=" + mPausingActivity + " for reason " + reason);
3658 destroyActivityLocked(activity, true, reason);
3659 if (activities.get(actNdx) != activity) {
3660 // Was removed from list, back up so we don't miss the next one.
3661 actNdx--;
3662 }
3663 curNum++;
3664 }
3665 }
3666 if (curNum > 0) {
3667 numReleased += curNum;
3668 maxTasks--;
3669 if (mTaskHistory.get(taskNdx) != task) {
3670 // The entire task got removed, back up so we don't miss the next one.
3671 taskNdx--;
3672 }
3673 }
3674 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003675 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3676 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003677 return numReleased;
3678 }
3679
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003680 /**
3681 * Destroy the current CLIENT SIDE instance of an activity. This may be
3682 * called both when actually finishing an activity, or when performing
3683 * a configuration switch where we destroy the current client-side object
3684 * but then create a new client-side object for this same HistoryRecord.
3685 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003686 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003687 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3688 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003689 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003690 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003691 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003692 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003693
3694 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003695
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003696 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003697
3698 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003699
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003700 if (hadApp) {
3701 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003702 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003703 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3704 mService.mHeavyWeightProcess = null;
3705 mService.mHandler.sendEmptyMessage(
3706 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3707 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003708 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003709 // Update any services we are bound to that might care about whether
3710 // their client may have activities.
3711 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003712 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003713 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003714 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003715 }
3716 }
3717
3718 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003719
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003720 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003721 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003722 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003723 r.configChangeFlags);
3724 } catch (Exception e) {
3725 // We can just ignore exceptions here... if the process
3726 // has crashed, our death notification will clean things
3727 // up.
3728 //Slog.w(TAG, "Exception thrown during finish", e);
3729 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003730 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003731 removedFromHistory = true;
3732 skipDestroy = true;
3733 }
3734 }
3735
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003736 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003737
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003738 // If the activity is finishing, we need to wait on removing it
3739 // from the list to give it a chance to do its cleanup. During
3740 // that time it may make calls back with its token so we need to
3741 // be able to find it on the list and so we don't want to remove
3742 // it from the list yet. Otherwise, we can just immediately put
3743 // it in the destroyed state since we are not removing it from the
3744 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003745 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003746 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003747 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003748 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003749 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003750 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3751 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003752 if (DEBUG_STATES) Slog.v(TAG_STATES,
3753 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003754 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003755 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003756 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003757 }
3758 } else {
3759 // remove this record from the history.
3760 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003761 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003762 removedFromHistory = true;
3763 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003764 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003765 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003766 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003767 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003768 }
3769 }
3770
3771 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003772
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003773 if (!mLRUActivities.remove(r) && hadApp) {
3774 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3775 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003776
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003777 return removedFromHistory;
3778 }
3779
Craig Mautner299f9602015-01-26 09:47:33 -08003780 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003781 final long origId = Binder.clearCallingIdentity();
3782 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003783 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003784 if (r != null) {
3785 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003786 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003787 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003788
Wale Ogunwale60454db2015-01-23 16:05:07 -08003789 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003790 if (r.state == ActivityState.DESTROYING) {
3791 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003792 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003793 }
3794 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003795 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003796 } finally {
3797 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003798 }
3799 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003800
Todd Kennedyaab56db2015-01-30 09:39:53 -08003801 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003802 if (hasVisibleBehindActivity() &&
3803 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08003804 if (r == topRunningActivityLocked() && getStackVisibilityLocked() == STACK_VISIBLE) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003805 // Don't release the top activity if it has requested to run behind the next
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08003806 // activity and the stack is currently visible.
Craig Mautner64ccb702014-10-01 09:38:40 -07003807 return;
3808 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003809 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003810 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003811 " thread=" + r.app.thread);
3812 if (r != null && r.app != null && r.app.thread != null) {
3813 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003814 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003815 } catch (RemoteException e) {
3816 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003817 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003818 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003819 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003820 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003821 }
3822 }
3823 }
3824
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003825 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003826 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3827 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003828 if (r != null) {
3829 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003830 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003831 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003832 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003833 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003834 }
3835
Jose Lima4b6c6692014-08-12 17:41:12 -07003836 boolean hasVisibleBehindActivity() {
3837 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003838 }
3839
Jose Lima4b6c6692014-08-12 17:41:12 -07003840 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003841 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003842 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003843 }
3844 }
3845
Jose Lima4b6c6692014-08-12 17:41:12 -07003846 ActivityRecord getVisibleBehindActivity() {
3847 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003848 }
3849
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003850 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3851 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003852 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003853 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3854 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003855 while (i > 0) {
3856 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003857 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003858 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003859 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003860 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003861 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003862 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003863 }
3864 }
3865 }
3866
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003867 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3868 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003869 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3870 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003871 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3872 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003873 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003874 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003875 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3876 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003877
3878 boolean hasVisibleActivities = false;
3879
3880 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003881 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003882 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3883 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003884 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3885 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3886 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3887 final ActivityRecord r = activities.get(activityNdx);
3888 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003889 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3890 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003891 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003892 if (r.visible) {
3893 hasVisibleActivities = true;
3894 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003895 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003896 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3897 // Don't currently have state for the activity, or
3898 // it is finishing -- always remove it.
3899 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003900 } else if (!r.visible && r.launchCount > 2 &&
3901 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003902 // We have launched this activity too many times since it was
3903 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003904 // (Note if the activity is visible, we don't remove the record.
3905 // We leave the dead window on the screen but the process will
3906 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003907 remove = true;
3908 } else {
3909 // The process may be gone, but the activity lives on!
3910 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003911 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003912 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003913 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3914 "Removing activity " + r + " from stack at " + i
3915 + ": haveState=" + r.haveState
3916 + " stateNotNeeded=" + r.stateNotNeeded
3917 + " finishing=" + r.finishing
3918 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003919 if (!r.finishing) {
3920 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3921 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3922 r.userId, System.identityHashCode(r),
3923 r.task.taskId, r.shortComponentName,
3924 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003925 if (r.state == ActivityState.RESUMED) {
3926 mService.updateUsageStats(r, false);
3927 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003928 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003929 } else {
3930 // We have the current state for this activity, so
3931 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003932 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3933 if (DEBUG_APP) Slog.v(TAG_APP,
3934 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003935 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003936 // Set nowVisible to previous visible state. If the app was visible while
3937 // it died, we leave the dead window on screen so it's basically visible.
3938 // This is needed when user later tap on the dead window, we need to stop
3939 // other apps when user transfers focus to the restarted activity.
3940 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003941 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003942 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003943 "App died, clearing saved state of " + r);
3944 r.icicle = null;
3945 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003946 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003947 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003948 if (remove) {
3949 removeActivityFromHistoryLocked(r, "appDied");
3950 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003951 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003952 }
3953 }
3954
3955 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003956 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003957
Chong Zhang280d3322015-11-03 17:27:26 -08003958 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003959 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003960 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003961 if (r != null && r.state != ActivityState.RESUMED) {
3962 r.updateOptionsLocked(options);
3963 } else {
3964 ActivityOptions.abort(options);
3965 }
3966 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003967 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003968 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003969
Craig Mautner21d24a22014-04-23 11:45:37 -07003970 void updateTaskMovement(TaskRecord task, boolean toFront) {
3971 if (task.isPersistable) {
3972 task.mLastTimeMoved = System.currentTimeMillis();
3973 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3974 // recently will be most negative, tasks sent to the bottom before that will be less
3975 // negative. Similarly for recent tasks moved to the top which will be most positive.
3976 if (!toFront) {
3977 task.mLastTimeMoved *= -1;
3978 }
3979 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003980 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003981 }
3982
Craig Mautner84984fa2014-06-19 11:19:20 -07003983 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003984 final int top = mTaskHistory.size() - 1;
3985 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3986 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003987 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003988 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3989 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003990 mTaskHistory.remove(taskNdx);
3991 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003992 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003993 return;
3994 }
3995 }
3996 }
3997
Chong Zhang280d3322015-11-03 17:27:26 -08003998 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003999 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004000 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004001
Craig Mautner11bf9a52013-02-19 14:08:51 -08004002 final int numTasks = mTaskHistory.size();
4003 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07004004 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004005 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004006 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08004007 ActivityOptions.abort(options);
4008 } else {
4009 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
4010 }
4011 return;
4012 }
4013
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07004014 if (timeTracker != null) {
4015 // The caller wants a time tracker associated with this task.
4016 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
4017 tr.mActivities.get(i).appTimeTracker = timeTracker;
4018 }
4019 }
4020
Craig Mautner11bf9a52013-02-19 14:08:51 -08004021 // Shift all activities with this task up to the top
4022 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004023 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004024
Chong Zhang45c25ce2015-08-10 22:18:26 -07004025 // Don't refocus if invisible to current user
4026 ActivityRecord top = tr.getTopActivity();
4027 if (!okToShowLocked(top)) {
4028 addRecentActivityLocked(top);
4029 ActivityOptions.abort(options);
4030 return;
4031 }
4032
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004033 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004034 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07004035 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004036
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004037 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08004038 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004039 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004040 if (r != null) {
4041 mNoAnimActivities.add(r);
4042 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004043 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004044 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08004045 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004046 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004047
Wale Ogunwaled046a012015-12-24 13:05:59 -08004048 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004049 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004050
4051 if (VALIDATE_TOKENS) {
4052 validateAppTokensLocked();
4053 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004054 }
4055
4056 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004057 * Worker method for rearranging history stack. Implements the function of moving all
4058 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004059 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004060 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004061 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4062 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004063 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004064 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004065 * @return Returns true if the move completed, false if not.
4066 */
Craig Mautner299f9602015-01-26 09:47:33 -08004067 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004068 final TaskRecord tr = taskForIdLocked(taskId);
4069 if (tr == null) {
4070 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4071 return false;
4072 }
4073
4074 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07004075 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004076
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004077 // If we have a watcher, preflight the move before committing to it. First check
4078 // for *other* available tasks, but if none are available, then try again allowing the
4079 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004080 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004081 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004082 if (next == null) {
4083 next = topRunningActivityLocked(null, 0);
4084 }
4085 if (next != null) {
4086 // ask watcher if this is allowed
4087 boolean moveOK = true;
4088 try {
4089 moveOK = mService.mController.activityResuming(next.packageName);
4090 } catch (RemoteException e) {
4091 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004092 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004093 }
4094 if (!moveOK) {
4095 return false;
4096 }
4097 }
4098 }
4099
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004100 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004101
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004102 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4103 // For the case where we are moving the home task back and there is an activity visible
4104 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4105 // activity to maintain order with what the user is seeing.
4106 final ActivityStack fullscreenStack =
4107 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4108 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4109 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4110 mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004111 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004112 return true;
4113 }
4114 }
4115
riddle_hsuc215a4f2014-12-27 12:10:45 +08004116 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004117
4118 // If true, we should resume the home activity next if the task we are moving to the
4119 // back is over the home stack. We force to false if the task we are moving to back
4120 // is the home task and we don't want it resumed after moving to the back.
4121 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4122 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004123 final TaskRecord nextTask = getNextTask(tr);
4124 if (nextTask != null) {
4125 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4126 } else {
4127 prevIsHome = true;
4128 }
4129 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004130 mTaskHistory.remove(tr);
4131 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004132 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004133
Craig Mautnerc8143c62013-09-03 12:15:57 -07004134 // There is an assumption that moving a task to the back moves it behind the home activity.
4135 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004136 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004137 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4138 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004139 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004140 break;
4141 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004142 if (taskNdx == 1) {
4143 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004144 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004145 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004146 }
4147
Craig Mautner299f9602015-01-26 09:47:33 -08004148 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004149 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004150
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004151 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004152 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004153 }
4154
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004155 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004156 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004157 if (!mService.mBooting && !mService.mBooted) {
4158 // Not ready yet!
4159 return false;
4160 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004161 final int taskToReturnTo = tr.getTaskToReturnTo();
4162 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004163 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004164 }
4165
Wale Ogunwaled046a012015-12-24 13:05:59 -08004166 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004167 return true;
4168 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004169
Craig Mautner8849a5e2013-04-02 16:41:03 -07004170 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004171 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004172 final Uri data = r.intent.getData();
4173 final String strData = data != null ? data.toSafeString() : null;
4174
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004175 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004176 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004177 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004178 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004179 }
4180
4181 /**
4182 * Make sure the given activity matches the current configuration. Returns
4183 * false if the activity had to be destroyed. Returns true if the
4184 * configuration is the same, or the activity will remain running as-is
4185 * for whatever reason. Ensures the HistoryRecord is updated with the
4186 * correct configuration and all other bookkeeping is handled.
4187 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004188 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4189 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004190 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004191 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004192 "Skipping config check (will change): " + r);
4193 return true;
4194 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004195
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004196 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004197 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004198
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004199 // Short circuit: if the two configurations are the exact same
4200 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004201 final Configuration newConfig = mService.mConfiguration;
4202 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004203 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004204 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004205 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004206 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004207 "Configuration unchanged in " + r);
4208 return true;
4209 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004210
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004211 // We don't worry about activities that are finishing.
4212 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004213 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004214 "Configuration doesn't matter in finishing " + r);
4215 r.stopFreezingScreenLocked(false);
4216 return true;
4217 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004218
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004219 // Okay we now are going to make this activity have the new config.
4220 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004221 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004222 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004223 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004224 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004225
Filip Gruszczynski23493322015-07-29 17:02:59 -07004226 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004227 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004228 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004229 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004230 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004231 // There are no significant differences, so we won't relaunch but should still deliver
4232 // the new configuration to the client process.
4233 r.scheduleConfigurationChanged(taskConfig, false);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004234 return true;
4235 }
4236
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004237 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4238 "Configuration changes for " + r + " ; taskChanges="
4239 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4240 + Configuration.configurationDiffToString(changes));
4241
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004242 // If the activity isn't currently running, just leave the new
4243 // configuration and it will pick that up next time it starts.
4244 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004245 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004246 "Configuration doesn't matter not running " + r);
4247 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004248 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004249 return true;
4250 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004251
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004252 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004253 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4254 "Checking to restart " + r.info.name + ": changed=0x"
4255 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004256 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4257 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004258
Dianne Hackborne6676352011-06-01 16:51:20 -07004259 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004260 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4261 r.configChangeFlags |= changes;
4262 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004263 r.forceNewConfig = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004264 preserveWindow &= isResizeOnlyChange(changes);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004265 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004266 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004267 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004268 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004269 } else if (r.state == ActivityState.PAUSING) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004270 // A little annoying: we are waiting for this activity to finish pausing. Let's not
4271 // do anything now, but just flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004272 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004273 "Config is skipping already pausing " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004274 r.deferRelaunchUntilPaused = true;
4275 r.preserveWindowOnDeferredRelaunch = preserveWindow;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004276 return true;
4277 } else if (r.state == ActivityState.RESUMED) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004278 // Try to optimize this case: the configuration is changing and we need to restart
4279 // the top, resumed activity. Instead of doing the normal handshaking, just say
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004280 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004281 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004282 "Config is relaunching resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004283 relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004284 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004285 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004286 "Config is relaunching non-resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004287 relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004288 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004289
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004290 // All done... tell the caller we weren't able to keep this activity around.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004291 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004292 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004293
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004294 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004295 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004296 // changes is always sent to all processes when they happen so it can just use whatever
4297 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004298 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004299 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004300
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004301 return true;
4302 }
4303
Filip Gruszczynski23493322015-07-29 17:02:59 -07004304 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4305 Configuration oldTaskOverride) {
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08004306
4307 // If we went from full-screen to non-full-screen, make sure to use the correct
4308 // configuration task diff, so the diff stays as small as possible.
4309 if (Configuration.EMPTY.equals(oldTaskOverride)
4310 && !Configuration.EMPTY.equals(taskConfig)) {
4311 oldTaskOverride = record.task.extractOverrideConfig(record.configuration);
4312 }
4313
Jorim Jaggi1b025a62016-02-03 19:27:49 -08004314 // Conversely, do the same when going the other direction.
4315 if (Configuration.EMPTY.equals(taskConfig)
4316 && !Configuration.EMPTY.equals(oldTaskOverride)) {
4317 taskConfig = record.task.extractOverrideConfig(record.configuration);
4318 }
4319
Filip Gruszczynski23493322015-07-29 17:02:59 -07004320 // Determine what has changed. May be nothing, if this is a config
4321 // that has come back from the app after going idle. In that case
4322 // we just want to leave the official config object now in the
4323 // activity and do nothing else.
4324 int taskChanges = oldTaskOverride.diff(taskConfig);
4325 // We don't want to use size changes if they don't cross boundaries that are important to
4326 // the app.
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004327 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004328 final boolean crosses = record.crossesHorizontalSizeThreshold(
4329 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4330 || record.crossesVerticalSizeThreshold(
4331 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4332 if (!crosses) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004333 taskChanges &= ~CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004334 }
4335 }
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004336 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004337 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4338 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004339 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004340 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004341 }
4342 }
4343 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4344 }
4345
4346 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4347 Configuration oldTaskOverride, int taskChanges) {
4348 if (taskChanges == 0) {
4349 // {@link Configuration#diff} doesn't catch changes from unset values.
4350 // Check for changes we care about.
4351 if (oldTaskOverride.orientation != taskConfig.orientation) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004352 taskChanges |= CONFIG_ORIENTATION;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004353 }
4354 // We want to explicitly track situations where the size configuration goes from
4355 // undefined to defined. We don't care about crossing the threshold in that case,
4356 // because there is no threshold.
4357 final int oldHeight = oldTaskOverride.screenHeightDp;
4358 final int newHeight = taskConfig.screenHeightDp;
4359 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4360 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4361 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004362 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004363 }
4364 final int oldWidth = oldTaskOverride.screenWidthDp;
4365 final int newWidth = taskConfig.screenWidthDp;
4366 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4367 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4368 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004369 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004370 }
4371 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4372 final int newSmallest = taskConfig.smallestScreenWidthDp;
4373 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4374 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4375 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004376 taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4377 }
4378 final int oldLayout = oldTaskOverride.screenLayout;
4379 final int newLayout = taskConfig.screenLayout;
4380 if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4381 || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4382 taskChanges |= CONFIG_SCREEN_LAYOUT;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004383 }
4384 }
4385 return taskChanges;
4386 }
4387
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004388 private static boolean isResizeOnlyChange(int change) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004389 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4390 | CONFIG_SCREEN_LAYOUT)) == 0;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004391 }
4392
Wale Ogunwale83301a92015-09-24 15:54:08 -07004393 private void relaunchActivityLocked(
4394 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4395 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004396 r.configChangeFlags = 0;
Wale Ogunwale83301a92015-09-24 15:54:08 -07004397 return;
4398 }
4399
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004400 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004401 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004402 if (andResume) {
4403 results = r.results;
4404 newIntents = r.newIntents;
4405 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004406 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4407 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004408 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004409 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004410 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004411 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004412
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004413 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004414
Craig Mautner34b73df2014-01-12 21:11:08 -08004415 mStackSupervisor.removeChildActivityContainers(r);
4416
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004417 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004418 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4419 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004420 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004421 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4422 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004423 new Configuration(r.task.mOverrideConfig), preserveWindow);
Jorim Jaggife89d122015-12-22 16:28:44 +01004424 mStackSupervisor.activityRelaunchingLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004425 // Note: don't need to call pauseIfSleepingLocked() here, because
4426 // the caller will only pass in 'andResume' if this activity is
4427 // currently resumed, which implies we aren't sleeping.
4428 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004429 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004430 }
4431
4432 if (andResume) {
4433 r.results = null;
4434 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004435 r.state = ActivityState.RESUMED;
4436 } else {
4437 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4438 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004439 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004440
4441 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004442 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004443
4444 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004445 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4446 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4447 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4448 final ActivityRecord r = activities.get(activityNdx);
4449 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004450 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004451 }
4452 if (r.fullscreen && !r.finishing) {
4453 return false;
4454 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004455 }
4456 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004457 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004458 if (r == null) {
4459 return false;
4460 }
4461 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4462 + " would have returned true for r=" + r);
4463 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004464 }
4465
4466 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004467 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4468 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4469 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4470 final ActivityRecord r = activities.get(activityNdx);
4471 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004472 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004473 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004474 }
4475 }
4476 }
4477
Wale Ogunwale540e1232015-05-01 15:35:39 -07004478 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4479 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004480 boolean didSomething = false;
4481 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004482 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004483 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4484 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4485 int numActivities = activities.size();
4486 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4487 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004488 final boolean sameComponent =
4489 (r.packageName.equals(packageName) && (filterByClasses == null
4490 || filterByClasses.contains(r.realActivity.getClassName())))
4491 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004492 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004493 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004494 && (r.app == null || evenPersistent || !r.app.persistent)) {
4495 if (!doit) {
4496 if (r.finishing) {
4497 // If this activity is just finishing, then it is not
4498 // interesting as far as something to stop.
4499 continue;
4500 }
4501 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004502 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004503 if (r.isHomeActivity()) {
4504 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4505 Slog.i(TAG, "Skip force-stop again " + r);
4506 continue;
4507 } else {
4508 homeActivity = r.realActivity;
4509 }
4510 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004511 didSomething = true;
4512 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004513 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004514 if (r.app != null) {
4515 r.app.removed = true;
4516 }
4517 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004518 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004519 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004520 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4521 true)) {
4522 // r has been deleted from mActivities, accommodate.
4523 --numActivities;
4524 --activityNdx;
4525 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004526 }
4527 }
4528 }
4529 return didSomething;
4530 }
4531
Dianne Hackborn09233282014-04-30 11:33:59 -07004532 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004533 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4534 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004535 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004536 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004537 if (task.getTopActivity() == null) {
4538 continue;
4539 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004540 ActivityRecord r = null;
4541 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004542 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004543 int numActivities = 0;
4544 int numRunning = 0;
4545 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004546 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004547 continue;
4548 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004549 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004550 tmp = activities.get(activityNdx);
4551 if (tmp.finishing) {
4552 continue;
4553 }
4554 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004555
Craig Mautneraab647e2013-02-28 16:31:36 -08004556 // Initialize state for next task if needed.
4557 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4558 top = r;
4559 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004560 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004561
4562 // Add 'r' into the current task.
4563 numActivities++;
4564 if (r.app != null && r.app.thread != null) {
4565 numRunning++;
4566 }
4567
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004568 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004569 TAG, r.intent.getComponent().flattenToShortString()
4570 + ": task=" + r.task);
4571 }
4572
4573 RunningTaskInfo ci = new RunningTaskInfo();
4574 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004575 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004576 ci.baseActivity = r.intent.getComponent();
4577 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004578 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004579 if (focusedStack && topTask) {
4580 // Give the latest time to ensure foreground task can be sorted
4581 // at the first, because lastActiveTime of creating task is 0.
4582 ci.lastActiveTime = System.currentTimeMillis();
4583 topTask = false;
4584 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004585
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004586 if (top.task != null) {
4587 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004588 }
4589 ci.numActivities = numActivities;
4590 ci.numRunning = numRunning;
Wale Ogunwale21b60582016-01-27 12:34:16 -08004591 ci.isDockable = task.canGoInDockedStack();
Craig Mautneraab647e2013-02-28 16:31:36 -08004592 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004593 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004594 }
4595
4596 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004597 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004598 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004599 if (top >= 0) {
4600 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4601 int activityTop = activities.size() - 1;
4602 if (activityTop > 0) {
4603 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4604 "unhandled-back", true);
4605 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004606 }
4607 }
4608
Craig Mautner6b74cb52013-09-27 17:02:21 -07004609 /**
4610 * Reset local parameters because an app's activity died.
4611 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004612 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004613 */
4614 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004615 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004616 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004617 "App died while pausing: " + mPausingActivity);
4618 mPausingActivity = null;
4619 }
4620 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4621 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004622 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004623 }
4624
Craig Mautner19091252013-10-05 00:03:53 -07004625 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004626 }
4627
Craig Mautnercae015f2013-02-08 14:31:27 -08004628 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004629 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4630 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4631 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4632 final ActivityRecord r = activities.get(activityNdx);
4633 if (r.app == app) {
4634 Slog.w(TAG, " Force finishing activity "
4635 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004636 // Force the destroy to skip right to removal.
4637 r.app = null;
4638 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004639 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004640 }
4641 }
4642 }
4643
Dianne Hackborn390517b2013-05-30 15:03:32 -07004644 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004645 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004646 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004647 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4648 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004649 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4650 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004651 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004652 " Task id #" + task.taskId + "\n" +
4653 " mFullscreen=" + task.mFullscreen + "\n" +
4654 " mBounds=" + task.mBounds + "\n" +
4655 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004656 if (printed) {
4657 header = null;
4658 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004659 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004660 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004661 }
4662
4663 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4664 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4665
4666 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004667 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4668 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004669 }
4670 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004671 final int top = mTaskHistory.size() - 1;
4672 if (top >= 0) {
4673 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4674 int listTop = list.size() - 1;
4675 if (listTop >= 0) {
4676 activities.add(list.get(listTop));
4677 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004678 }
4679 } else {
4680 ItemMatcher matcher = new ItemMatcher();
4681 matcher.build(name);
4682
Craig Mautneraab647e2013-02-28 16:31:36 -08004683 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4684 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4685 if (matcher.match(r1, r1.intent.getComponent())) {
4686 activities.add(r1);
4687 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004688 }
4689 }
4690 }
4691
4692 return activities;
4693 }
4694
4695 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004696 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004697
4698 // All activities that came from the package must be
4699 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004700 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4701 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4702 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4703 final ActivityRecord a = activities.get(activityNdx);
4704 if (a.info.packageName.equals(packageName)) {
4705 a.forceNewConfig = true;
4706 if (starting != null && a == starting && a.visible) {
4707 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004708 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08004709 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004710 }
4711 }
4712 }
4713
4714 return starting;
4715 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004716
Craig Mautner299f9602015-01-26 09:47:33 -08004717 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004718 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004719 }
4720
Wale Ogunwale000957c2015-04-03 08:19:12 -07004721 /**
4722 * Removes the input task from this stack.
4723 * @param task to remove.
4724 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004725 * @param moving task to another stack. In the case we are moving we don't want to perform
4726 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004727 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004728 void removeTask(TaskRecord task, String reason, boolean moving) {
4729 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004730 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004731 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004732 if (!StackId.persistTaskBounds(mStackId)) {
4733 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4734 // default configuration the next time it launches.
4735 task.updateOverrideConfiguration(null);
4736 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004737 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004738
Craig Mautner04a0ea62014-01-13 12:51:26 -08004739 final ActivityRecord r = mResumedActivity;
4740 if (r != null && r.task == task) {
4741 mResumedActivity = null;
4742 }
4743
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004744 final int taskNdx = mTaskHistory.indexOf(task);
4745 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004746 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4747 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4748 if (!nextTask.isOverHomeStack()) {
4749 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4750 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004751 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004752 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004753 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004754
Wale Ogunwale040b4702015-08-06 18:10:50 -07004755 if (!moving && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004756 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07004757 final boolean isVoiceSession = task.voiceSession != null;
4758 if (isVoiceSession) {
4759 try {
4760 task.voiceSession.taskFinished(task.intent, task.taskId);
4761 } catch (RemoteException e) {
4762 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004763 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004764 if (task.autoRemoveFromRecents() || isVoiceSession) {
4765 // Task creator asked to remove this when done, or this task was a voice
4766 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004767 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004768 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004769 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004770 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004771
4772 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004773 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004774 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004775 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004776 String myReason = reason + " leftTaskHistoryEmpty";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08004777 if (mFullscreen || !adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004778 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004779 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004780 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004781 if (mStacks != null) {
4782 mStacks.remove(this);
4783 mStacks.add(0, this);
4784 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004785 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004786 mActivityContainer.onTaskListEmptyLocked();
4787 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004788 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004789
4790 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004791 }
4792
Dianne Hackborn91097de2014-04-04 18:02:06 -07004793 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4794 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4795 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004796 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4797 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004798 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08004799 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08004800 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004801 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.isResizeable()
Chong Zhang75b37202015-12-04 14:16:36 -08004802 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004803 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004804 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004805 return task;
4806 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004807
Wale Ogunwale935e5022015-11-10 12:36:10 -08004808 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4809 if (mTaskPositioner == null) {
4810 return false;
4811 }
4812 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4813 return true;
4814 }
4815
Craig Mautnerc00204b2013-03-05 15:02:14 -08004816 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004817 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004818 }
4819
Wale Ogunwale5f986092015-12-04 15:35:38 -08004820 void addTask(final TaskRecord task, final boolean toTop, String reason) {
4821 final ActivityStack prevStack = preAddTask(task, reason);
4822
Craig Mautnerc00204b2013-03-05 15:02:14 -08004823 task.stack = this;
4824 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004825 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004826 } else {
4827 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004828 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004829 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08004830 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004831 }
4832
Wale Ogunwale5f986092015-12-04 15:35:38 -08004833 void positionTask(final TaskRecord task, int position) {
4834 final ActivityStack prevStack = preAddTask(task, "positionTask");
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004835 task.stack = this;
4836 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004837 postAddTask(task, prevStack);
4838 }
4839
4840 private ActivityStack preAddTask(TaskRecord task, String reason) {
4841 final ActivityStack prevStack = task.stack;
4842 if (prevStack != null && prevStack != this) {
4843 prevStack.removeTask(task, reason, MOVING);
4844 }
4845 return prevStack;
4846 }
4847
4848 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
4849 if (prevStack != null) {
Wale Ogunwale22e25262016-02-01 10:32:02 -08004850 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004851 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004852 try {
4853 task.voiceSession.taskStarted(task.intent, task.taskId);
4854 } catch (RemoteException e) {
4855 }
4856 }
4857 }
4858
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004859 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004860 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004861 // TODO: VI deal with activity
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004862 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4863 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6cae7652015-12-26 07:36:26 -08004864 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
4865 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004866 task.mResizeMode, r.isAlwaysFocusable(), task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004867 r.taskConfigOverride = task.mOverrideConfig;
4868 }
4869
Wale Ogunwaled046a012015-12-24 13:05:59 -08004870 void moveToFrontAndResumeStateIfNeeded(
4871 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
4872 if (!moveToFront) {
4873 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07004874 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004875
4876 // If the activity owns the last resumed activity, transfer that together,
4877 // so that we don't resume the same activity again in the new stack.
4878 // Apps may depend on onResume()/onPause() being called in pairs.
4879 if (setResume) {
4880 mResumedActivity = r;
4881 }
4882 // Move the stack in which we are placing the activity to the front. The call will also
4883 // make sure the activity focus is set.
4884 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004885 }
4886
4887 /**
4888 * Moves the input activity from its current stack to this one.
4889 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4890 * created on this stack which the activity is added to.
4891 * */
4892 void moveActivityToStack(ActivityRecord r) {
4893 final ActivityStack prevStack = r.task.stack;
4894 if (prevStack.mStackId == mStackId) {
4895 // You are already in the right stack silly...
4896 return;
4897 }
4898
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004899 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004900 && (mStackSupervisor.topRunningActivityLocked() == r);
4901 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4902
4903 final TaskRecord task = createTaskRecord(
Suprabh Shukla09a88f52015-12-02 14:36:31 -08004904 mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
4905 r.info, r.intent, null, null, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004906 r.setTask(task, null);
4907 task.addActivityToTop(r);
4908 setAppTask(r, task);
Wale Ogunwale22e25262016-02-01 10:32:02 -08004909 mStackSupervisor.scheduleReportPictureInPictureChangedIfNeeded(task, prevStack);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004910 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4911 if (wasResumed) {
4912 prevStack.mResumedActivity = null;
4913 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07004914 }
4915
Wale Ogunwale706ed792015-08-02 10:29:44 -07004916 private void setAppTask(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004917 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Wale Ogunwaleb1faf602016-01-27 09:12:31 -08004918 mWindowManager.setAppTask(r.appToken, task.taskId, mStackId, bounds, task.mOverrideConfig,
4919 task.mResizeMode, task.isHomeTask());
Wale Ogunwale706ed792015-08-02 10:29:44 -07004920 r.taskConfigOverride = task.mOverrideConfig;
4921 }
4922
Craig Mautnerc00204b2013-03-05 15:02:14 -08004923 public int getStackId() {
4924 return mStackId;
4925 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004926
4927 @Override
4928 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004929 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4930 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004931 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004932
Craig Mautner15df08a2015-04-01 12:17:18 -07004933 void onLockTaskPackagesUpdatedLocked() {
4934 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4935 mTaskHistory.get(taskNdx).setLockTaskAuth();
4936 }
4937 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004938}