blob: 4bac2d6fd69a64d0801e8431fb0ca27117d8f8dc [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 }
398 }
399
400 void detachDisplay() {
401 mDisplayId = Display.INVALID_DISPLAY;
402 mStacks = null;
403 if (mTaskPositioner != null) {
404 mTaskPositioner.reset();
405 }
406 mWindowManager.detachStack(mStackId);
407 }
408
Winsonc809cbb2015-11-02 12:06:15 -0800409 public void getDisplaySize(Point out) {
410 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
411 }
412
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700413 void setBounds(Rect bounds) {
414 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700415 if (mTaskPositioner != null) {
416 mTaskPositioner.configure(bounds);
417 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700418 }
Craig Mautner5962b122012-10-05 14:45:52 -0700419
Amith Yamasani734983f2014-03-04 16:48:05 -0800420 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700421 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700422 }
423
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700424 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800425 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700426 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700427 if (r != null) {
428 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800429 }
430 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700431 return null;
432 }
433
434 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800435 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
436 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800437 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800438 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
439 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800440 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800441 return r;
442 }
443 }
444 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700445 return null;
446 }
447
448 /**
449 * This is a simplified version of topRunningActivityLocked that provides a number of
450 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800451 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700452 * @param token If non-null, any history records matching this token will be skipped.
453 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800454 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700455 * @return Returns the HistoryRecord of the next activity on the stack.
456 */
457 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800458 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
459 TaskRecord task = mTaskHistory.get(taskNdx);
460 if (task.taskId == taskId) {
461 continue;
462 }
463 ArrayList<ActivityRecord> activities = task.mActivities;
464 for (int i = activities.size() - 1; i >= 0; --i) {
465 final ActivityRecord r = activities.get(i);
466 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800467 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800468 return r;
469 }
470 }
471 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700472 return null;
473 }
474
Craig Mautner8849a5e2013-04-02 16:41:03 -0700475 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700476 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
477 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700478 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
479 final ActivityRecord r = activities.get(activityNdx);
480 if (!r.finishing) {
481 return r;
482 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700483 }
484 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700485 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700486 }
487
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700488 final TaskRecord topTask() {
489 final int size = mTaskHistory.size();
490 if (size > 0) {
491 return mTaskHistory.get(size - 1);
492 }
493 return null;
494 }
495
Craig Mautnerd2328952013-03-05 12:46:26 -0800496 TaskRecord taskForIdLocked(int id) {
497 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
498 final TaskRecord task = mTaskHistory.get(taskNdx);
499 if (task.taskId == id) {
500 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800501 }
502 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700503 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700504 }
505
Craig Mautnerd2328952013-03-05 12:46:26 -0800506 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700507 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800508 return isInStackLocked(r);
509 }
510
511 ActivityRecord isInStackLocked(ActivityRecord r) {
512 if (r == null) {
513 return null;
514 }
515 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700516 if (task != null && task.stack != null
517 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800518 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800519 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800520 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800521 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800522 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800523 }
524
Craig Mautner2420ead2013-04-01 17:13:20 -0700525 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700526 final boolean hadit = mLRUActivities.remove(r);
527 mLRUActivities.add(r);
528 return hadit;
529 }
530
Craig Mautnerde4ef022013-04-07 19:01:33 -0700531 final boolean isHomeStack() {
532 return mStackId == HOME_STACK_ID;
533 }
534
Winson Chung0583d3d2015-12-18 10:03:12 -0500535 final boolean isDockedStack() {
536 return mStackId == DOCKED_STACK_ID;
537 }
538
539 final boolean isPinnedStack() {
540 return mStackId == PINNED_STACK_ID;
541 }
542
Craig Mautnere0a38842013-12-16 16:14:02 -0800543 final boolean isOnHomeDisplay() {
544 return isAttached() &&
545 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
546 }
547
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700548 void moveToFront(String reason) {
549 moveToFront(reason, null);
550 }
551
552 /**
553 * @param reason The reason for moving the stack to the front.
554 * @param task If non-null, the task will be moved to the top of the stack.
555 * */
556 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700557 if (!isAttached()) {
558 return;
559 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700560
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700561 mStacks.remove(this);
562 int addIndex = mStacks.size();
563
564 if (addIndex > 0) {
565 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800566 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
567 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700568 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800569 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700570 }
571
572 mStacks.add(addIndex, this);
573
574 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
575 if (isOnHomeDisplay()) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800576 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700577 }
578 if (task != null) {
579 insertTaskAtTop(task, null);
580 } else {
581 task = topTask();
582 }
583 if (task != null) {
584 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800585 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800586 }
587
Wale Ogunwaled046a012015-12-24 13:05:59 -0800588 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800589 if (StackId.canReceiveKeys(mStackId)) {
590 return true;
591 }
592 // The stack isn't focusable. See if its top activity is focusable to force focus on the
593 // stack.
594 final ActivityRecord r = topRunningActivityLocked();
595 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800596 }
597
Craig Mautnere0a38842013-12-16 16:14:02 -0800598 final boolean isAttached() {
599 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800600 }
601
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700602 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800603 * Returns the top activity in any existing task matching the given Intent in the input result.
604 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700605 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800606 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700607 Intent intent = target.intent;
608 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700609 ComponentName cls = intent.getComponent();
610 if (info.targetActivity != null) {
611 cls = new ComponentName(info.packageName, info.targetActivity);
612 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700613 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700614 boolean isDocument = intent != null & intent.isDocument();
615 // If documentData is non-null then it must match the existing task data.
616 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800617
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700618 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800619 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
620 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700621 if (task.voiceSession != null) {
622 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700623 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700624 continue;
625 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700626 if (task.userId != userId) {
627 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700628 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700629 continue;
630 }
Craig Mautner000f0022013-02-26 15:04:29 -0800631 final ActivityRecord r = task.getTopActivity();
632 if (r == null || r.finishing || r.userId != userId ||
633 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700634 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800635 continue;
636 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700637 if (r.mActivityType != target.mActivityType) {
638 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
639 continue;
640 }
Craig Mautner000f0022013-02-26 15:04:29 -0800641
Craig Mautnerd00f4742014-03-12 14:17:26 -0700642 final Intent taskIntent = task.intent;
643 final Intent affinityIntent = task.affinityIntent;
644 final boolean taskIsDocument;
645 final Uri taskDocumentData;
646 if (taskIntent != null && taskIntent.isDocument()) {
647 taskIsDocument = true;
648 taskDocumentData = taskIntent.getData();
649 } else if (affinityIntent != null && affinityIntent.isDocument()) {
650 taskIsDocument = true;
651 taskDocumentData = affinityIntent.getData();
652 } else {
653 taskIsDocument = false;
654 taskDocumentData = null;
655 }
656
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700657 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700658 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700659 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700660 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Wale Ogunwale39381972015-12-17 17:15:29 -0800661 if (!isDocument && !taskIsDocument
662 && result.r == null && task.canMatchRootAffinity()) {
Dianne Hackborn79228822014-09-16 11:11:23 -0700663 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwale39381972015-12-17 17:15:29 -0800664 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
665 // It is possible for multiple tasks to have the same root affinity especially
666 // if they are in separate stacks. We save off this candidate, but keep looking
667 // to see if there is a better candidate.
668 result.r = r;
669 result.matchedByRootAffinity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700670 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700671 } else if (taskIntent != null && taskIntent.getComponent() != null &&
672 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700673 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700674 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800675 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700676 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
677 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800678 result.r = r;
679 result.matchedByRootAffinity = false;
680 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700681 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
682 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700683 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700684 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800685 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700686 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
687 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800688 result.r = r;
689 result.matchedByRootAffinity = false;
690 break;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700691 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700692 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700693 }
694
695 /**
696 * Returns the first activity (starting from the top of the stack) that
697 * is the same as the given activity. Returns null if no such activity
698 * is found.
699 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700700 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700701 ComponentName cls = intent.getComponent();
702 if (info.targetActivity != null) {
703 cls = new ComponentName(info.packageName, info.targetActivity);
704 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700705 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700706
Craig Mautner000f0022013-02-26 15:04:29 -0800707 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700708 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700709 final boolean notCurrentUserTask =
710 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700711 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700712
Craig Mautner000f0022013-02-26 15:04:29 -0800713 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
714 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700715 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700716 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700717 }
Craig Mautner000f0022013-02-26 15:04:29 -0800718 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700719 return r;
720 }
721 }
722 }
723
724 return null;
725 }
726
Amith Yamasani742a6712011-05-04 14:49:28 -0700727 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700728 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700729 */
Craig Mautner93529a42013-10-04 15:03:13 -0700730 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800731 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700732 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800733 }
734 mCurrentUser = userId;
735
736 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800737 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800738 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700739 final TaskRecord task = mTaskHistory.get(i);
740
741 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
742 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700743 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700744 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700745 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700746 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800747 mTaskHistory.remove(i);
748 mTaskHistory.add(task);
749 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800750 // Use same value for i.
751 } else {
752 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800753 }
754 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700755 if (VALIDATE_TOKENS) {
756 validateAppTokensLocked();
757 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700758 }
759
Craig Mautner2420ead2013-04-01 17:13:20 -0700760 void minimalResumeActivityLocked(ActivityRecord r) {
761 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700762 if (DEBUG_STATES) Slog.v(TAG_STATES,
763 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700764 r.stopped = false;
765 mResumedActivity = r;
766 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800767 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700768 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700769 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700770 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700771 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
772 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700773 }
774
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800775 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700776 if (r != null) {
777 mRecentTasks.addLocked(r.task);
778 r.task.touchActiveTime();
779 }
780 }
781
Narayan Kamath7829c812015-06-08 17:39:43 +0100782 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700783 if (mFullyDrawnStartTime != 0) {
784 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
785 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100786 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700787 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
788 }
789
790 private void stopFullyDrawnTraceIfNeeded() {
791 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
792 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
793 mFullyDrawnStartTime = 0;
794 }
795 }
796
Craig Mautnere79d42682013-04-01 19:01:53 -0700797 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700798 if (r.displayStartTime == 0) {
799 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
800 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100801 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700802 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700803 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700804 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100805 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700806 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700807 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700808 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800809
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700810 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700811 // Make sure that there is no activity waiting for this to launch.
812 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
813 r.displayStartTime = r.fullyDrawnStartTime = 0;
814 } else {
815 mStackSupervisor.removeTimeoutsForActivityLocked(r);
816 mStackSupervisor.scheduleIdleTimeoutLocked(r);
817 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700818 }
819
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800820 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800821 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800822 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
823 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
824 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
825 activities.get(activityNdx).setSleeping(false);
826 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800827 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800828 if (mPausingActivity != null) {
829 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
830 activityPausedLocked(mPausingActivity.appToken, true);
831 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800832 }
833
Craig Mautner0eea92c2013-05-16 13:35:39 -0700834 /**
835 * @return true if something must be done before going to sleep.
836 */
837 boolean checkReadyForSleepLocked() {
838 if (mResumedActivity != null) {
839 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700840 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
841 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
842 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700843 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700844 return true;
845 }
846 if (mPausingActivity != null) {
847 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700848 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700849 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800850 }
851
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700852 if (hasVisibleBehindActivity()) {
853 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800854 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700855 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800856 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700857 return true;
858 }
859
Craig Mautner0eea92c2013-05-16 13:35:39 -0700860 return false;
861 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800862
Craig Mautner0eea92c2013-05-16 13:35:39 -0700863 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700864 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800865
Craig Mautner0eea92c2013-05-16 13:35:39 -0700866 // Make sure any stopped but visible activities are now sleeping.
867 // This ensures that the activity's onStop() is called.
868 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
869 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
870 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
871 final ActivityRecord r = activities.get(activityNdx);
872 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
873 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800874 }
875 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800876 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700877 }
Craig Mautner59c00972012-07-30 12:10:24 -0700878
Winson8b1871d2015-11-20 09:56:20 -0800879 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
880 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800881 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700882 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800883 return null;
884 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800885
Winson Chung083baf92014-07-11 10:32:42 -0700886 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700887 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800888 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700889 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700890 return null;
891 }
892
Winson Chung48a10a52014-08-27 14:36:51 -0700893 int w = mService.mThumbnailWidth;
894 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800895 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800896 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700897 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800898
899 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800900 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
901 // use within SystemUI while keeping memory usage low.
Winson Chungead5c0f2015-12-14 11:18:57 -0500902 if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
Winson8b1871d2015-11-20 09:56:20 -0800903 w = h = -1;
Winson387aac62015-11-25 11:18:56 -0800904 scale = 0.5f;
Winson8b1871d2015-11-20 09:56:20 -0800905 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800906 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800907 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800908 }
Winson Chung376543b2014-05-21 17:43:28 -0700909 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800910 return null;
911 }
912
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700913 /**
914 * Start pausing the currently resumed activity. It is an error to call this if there
915 * is already an activity being paused or there is no resumed activity.
916 *
917 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
918 * @param uiSleeping True if this is happening with the user interface going to sleep (the
919 * screen turning off).
920 * @param resuming True if this is being called as part of resuming the top activity, so
921 * we shouldn't try to instigate a resume here.
922 * @param dontWait True if the caller does not want to wait for the pause to complete. If
923 * set to true, we will immediately complete the pause here before returning.
924 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
925 * it to tell us when it is done.
926 */
927 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
928 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800929 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800930 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
931 + " state=" + mPausingActivity.state);
932 if (!mService.isSleeping()) {
933 // Avoid recursion among check for sleep and complete pause during sleeping.
934 // Because activity will be paused immediately after resume, just let pause
935 // be completed by the order of activity paused from clients.
936 completePauseLocked(false);
937 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800938 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700939 ActivityRecord prev = mResumedActivity;
940 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700941 if (!resuming) {
942 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -0800943 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700944 }
945 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700946 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800947
948 if (mActivityContainer.mParentActivity == null) {
949 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700950 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800951 }
952
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700953 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700954 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700955 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800956 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700957 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700958 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
959 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700960 prev.state = ActivityState.PAUSING;
961 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700962 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700963 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700964 if (mService.mHasRecents
965 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800966 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700967 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700968 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700969
970 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800971
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700972 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700973 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700974 try {
975 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700976 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700977 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700978 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800979 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700980 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700981 } catch (Exception e) {
982 // Ignore exception, if process died other code will cleanup.
983 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800984 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700985 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700986 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700987 }
988 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800989 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700990 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700991 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700992 }
993
994 // If we are not going to sleep, we want to ensure the device is
995 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700996 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700997 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700998 }
999
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001000 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001001 // Have the window manager pause its key dispatching until the new
1002 // activity has started. If we're pausing the activity just because
1003 // the screen is being turned off and the UI is sleeping, don't interrupt
1004 // key dispatch; the same activity will pick it up again on wakeup.
1005 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001006 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001007 } else if (DEBUG_PAUSE) {
1008 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001009 }
1010
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001011 if (dontWait) {
1012 // If the caller said they don't want to wait for the pause, then complete
1013 // the pause now.
1014 completePauseLocked(false);
1015 return false;
1016
1017 } else {
1018 // Schedule a pause timeout in case the app doesn't respond.
1019 // We don't give it much time because this directly impacts the
1020 // responsiveness seen by the user.
1021 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1022 msg.obj = prev;
1023 prev.pauseTime = SystemClock.uptimeMillis();
1024 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001025 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001026 return true;
1027 }
1028
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001029 } else {
1030 // This activity failed to schedule the
1031 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001032 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001033 if (!resuming) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001034 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001035 }
1036 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001037 }
1038 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001039
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001040 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001041 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1042 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001043
Craig Mautnerd2328952013-03-05 12:46:26 -08001044 final ActivityRecord r = isInStackLocked(token);
1045 if (r != null) {
1046 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1047 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001048 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001049 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001050 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -08001051 } else {
1052 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1053 r.userId, System.identityHashCode(r), r.shortComponentName,
1054 mPausingActivity != null
1055 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001056 if (r.state == ActivityState.PAUSING) {
1057 r.state = ActivityState.PAUSED;
1058 if (r.finishing) {
1059 if (DEBUG_PAUSE) Slog.v(TAG,
1060 "Executing finish of failed to pause activity: " + r);
1061 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1062 }
louis_chang047dfd42015-04-08 16:35:55 +08001063 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001064 }
1065 }
1066 }
1067
Craig Mautnera0026042014-04-23 11:45:37 -07001068 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1069 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001070 if (r.state != ActivityState.STOPPING) {
1071 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1072 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1073 return;
1074 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001075 if (persistentState != null) {
1076 r.persistentState = persistentState;
1077 mService.notifyTaskPersisterLocked(r.task, false);
1078 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001079 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001080 if (icicle != null) {
1081 // If icicle is null, this is happening due to a timeout, so we
1082 // haven't really saved the state.
1083 r.icicle = icicle;
1084 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001085 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001086 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001087 }
1088 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001089 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001090 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1091 r.stopped = true;
1092 r.state = ActivityState.STOPPED;
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001093 if (getVisibleBehindActivity() == r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001094 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001095 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001096 if (r.finishing) {
1097 r.clearOptionsLocked();
1098 } else {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001099 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001100 destroyActivityLocked(r, true, "stop-config");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001101 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001102 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001103 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001104 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001105 }
1106 }
1107 }
1108
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001109 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001110 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001111 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001112
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001113 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001114 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001115 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001116 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001117 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001118 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001119 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001120 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001121 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001122 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1123 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001124 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001125 if (prev.deferRelaunchUntilPaused) {
1126 // Complete the deferred relaunch that was waiting for pause to complete.
1127 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
1128 relaunchActivityLocked(prev, prev.configChangeFlags, false,
1129 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001130 } else if (wasStopping) {
1131 // We are also stopping, the stop request must have gone soon after the pause.
1132 // We can't clobber it, because the stop confirmation will not be handled.
1133 // We don't need to schedule another stop, we only need to let it happen.
1134 prev.state = ActivityState.STOPPING;
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001135 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001136 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001137 // stopping.
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001138 addToStopping(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001139 }
1140 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001141 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001142 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001143 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001144 // It is possible the activity was freezing the screen before it was paused.
1145 // In that case go ahead and remove the freeze this activity has on the screen
1146 // since it is no longer visible.
1147 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001148 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001149 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001150
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001151 if (resumeNext) {
1152 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1153 if (!mService.isSleepingOrShuttingDown()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001154 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001155 } else {
1156 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001157 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001158 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001159 // If there are no more activities available to run, do resume anyway to start
1160 // something. Also if the top activity on the stack is not the just paused
1161 // activity, we need to go ahead and resume it to ensure we complete an
1162 // in-flight app switch.
1163 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001164 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001165 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001166 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001167
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001168 if (prev != null) {
1169 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001170
Craig Mautner525f3d92013-05-07 14:01:50 -07001171 if (prev.app != null && prev.cpuTimeAtResume > 0
1172 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001173 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1174 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001175 if (diff > 0) {
1176 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1177 synchronized (bsi) {
1178 BatteryStatsImpl.Uid.Proc ps =
1179 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001180 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001181 if (ps != null) {
1182 ps.addForegroundTimeLocked(diff);
1183 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001184 }
1185 }
1186 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001187 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001188 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001189
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001190 // Notify when the task stack has changed, but only if visibilities changed (not just
1191 // focus).
1192 if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause) {
1193 mService.notifyTaskStackChangedLocked();
1194 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
1195 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001196 }
1197
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001198 private void addToStopping(ActivityRecord r) {
1199 mStackSupervisor.mStoppingActivities.add(r);
1200 if (mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE ||
1201 r.frontOfTask && mTaskHistory.size() <= 1) {
1202 // If we already have a few activities waiting to stop,
1203 // then give up on things going idle and start clearing
1204 // them out. Or if r is the last of activity of the last task the stack
1205 // will be empty and must be cleared immediately.
1206 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
1207 mStackSupervisor.scheduleIdleLocked();
1208 } else {
1209 mStackSupervisor.checkReadyForSleepLocked();
1210 }
1211 }
1212
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001213 /**
1214 * Once we know that we have asked an application to put an activity in
1215 * the resumed state (either by launching it or explicitly telling it),
1216 * this function updates the rest of our state to match that fact.
1217 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001218 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001219 next.idle = false;
1220 next.results = null;
1221 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001222
Chong Zhang85ee6542015-10-02 13:36:38 -07001223 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001224 ProcessRecord app = next.task.mActivities.get(0).app;
1225 if (app != null && app != mService.mHomeProcess) {
1226 mService.mHomeProcess = app;
1227 }
1228 }
1229
Craig Mautner07566322013-09-26 16:42:55 -07001230 if (next.nowVisible) {
1231 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001232 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001233 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001234
1235 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001236 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001237
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001238 mStackSupervisor.reportResumedActivityLocked(next);
1239
1240 next.resumeKeyDispatchingLocked();
1241 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001242
1243 // Mark the point when the activity is resuming
1244 // TODO: To be more accurate, the mark should be before the onCreate,
1245 // not after the onResume. But for subsequent starts, onResume is fine.
1246 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001247 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001248 } else {
1249 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1250 }
Winson Chung376543b2014-05-21 17:43:28 -07001251
George Mount6ba042b2014-07-28 11:12:28 -07001252 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001253
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001254 if (getVisibleBehindActivity() == next) {
Craig Mautner64ccb702014-10-01 09:38:40 -07001255 // When resuming an activity, require it to call requestVisibleBehind() again.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001256 setVisibleBehindActivity(null);
Craig Mautner64ccb702014-10-01 09:38:40 -07001257 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001258 }
1259
Craig Mautner2568c3a2015-03-26 14:22:34 -07001260 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001261 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001262 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1263 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1264 r.mUpdateTaskThumbnailWhenHidden = false;
1265 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001266 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001267 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001268 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001269 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001270 container.setVisible(visible);
1271 }
Jorim Jaggia0fdeec2016-01-07 14:42:28 +01001272 mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = true;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001273 }
1274
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001275 // Find the first visible activity above the passed activity and if it is translucent return it
1276 // otherwise return null;
1277 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1278 TaskRecord task = r.task;
1279 if (task == null) {
1280 return null;
1281 }
1282
1283 ActivityStack stack = task.stack;
1284 if (stack == null) {
1285 return null;
1286 }
1287
1288 int stackNdx = mStacks.indexOf(stack);
1289
1290 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1291 int taskNdx = tasks.indexOf(task);
1292
1293 ArrayList<ActivityRecord> activities = task.mActivities;
1294 int activityNdx = activities.indexOf(r) + 1;
1295
1296 final int numStacks = mStacks.size();
1297 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001298 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001299 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001300 final int numTasks = tasks.size();
1301 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001302 final TaskRecord currentTask = tasks.get(taskNdx);
1303 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001304 final int numActivities = activities.size();
1305 while (activityNdx < numActivities) {
1306 final ActivityRecord activity = activities.get(activityNdx);
1307 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001308 return historyStack.mFullscreen
1309 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001310 }
1311 ++activityNdx;
1312 }
1313 activityNdx = 0;
1314 ++taskNdx;
1315 }
1316 taskNdx = 0;
1317 ++stackNdx;
1318 }
1319
1320 return null;
1321 }
1322
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001323 ActivityStack getNextFocusableStackLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001324 ArrayList<ActivityStack> stacks = mStacks;
1325 final ActivityRecord parent = mActivityContainer.mParentActivity;
1326 if (parent != null) {
1327 stacks = parent.task.stack.mStacks;
1328 }
1329 if (stacks != null) {
1330 for (int i = stacks.size() - 1; i >= 0; --i) {
1331 ActivityStack stack = stacks.get(i);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001332 if (stack != this && stack.isFocusable()
1333 && stack.getStackVisibilityLocked() != STACK_INVISIBLE) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001334 return stack;
1335 }
1336 }
1337 }
1338 return null;
1339 }
1340
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001341 /** Returns true if the stack contains a fullscreen task. */
1342 private boolean hasFullscreenTask() {
1343 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1344 final TaskRecord task = mTaskHistory.get(i);
1345 if (task.mFullscreen) {
1346 return true;
1347 }
1348 }
1349 return false;
1350 }
1351
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001352 private boolean hasTranslucentActivity(ActivityStack stack) {
1353 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1354 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1355 final TaskRecord task = tasks.get(taskNdx);
1356 final ArrayList<ActivityRecord> activities = task.mActivities;
1357 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1358 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001359
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001360 // Conditions for an activity to obscure the stack we're
1361 // examining:
1362 // 1. Not Finishing AND Visible AND:
1363 // 2. Either:
1364 // - Full Screen Activity OR
1365 // - On top of Home and our stack is NOT home
1366 if (!r.finishing && r.visible && (r.fullscreen ||
1367 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1368 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001369 }
1370 }
1371 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001372 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001373 }
1374
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001375 /**
1376 * Returns stack's visibility: {@link #STACK_INVISIBLE}, {@link #STACK_VISIBLE} or
1377 * {@link #STACK_VISIBLE_ACTIVITY_BEHIND}.
1378 */
1379 int getStackVisibilityLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001380 if (!isAttached()) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001381 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001382 }
1383
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001384 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001385 return STACK_VISIBLE;
Wale Ogunwale99db1862015-10-23 20:08:22 -07001386 }
1387
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001388 final int stackIndex = mStacks.indexOf(this);
1389
1390 if (stackIndex == mStacks.size() - 1) {
1391 Slog.wtf(TAG,
1392 "Stack=" + this + " isn't front stack but is at the top of the stack list");
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001393 return STACK_INVISIBLE;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001394 }
1395
Chong Zhang75b37202015-12-04 14:16:36 -08001396 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1397 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001398 return STACK_INVISIBLE;
Chong Zhang75b37202015-12-04 14:16:36 -08001399 }
1400
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001401 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1402 final int focusedStackId = focusedStack.mStackId;
1403
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001404 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001405 && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID
1406 && !focusedStack.topActivity().fullscreen) {
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001407 // The fullscreen stack should be visible if it has a visible behind activity behind
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001408 // the home stack that is translucent.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001409 return STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001410 }
1411
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001412 if (mStackId == DOCKED_STACK_ID) {
1413 // Docked stack is always visible, except in the case where the home activity
1414 // is the top running activity in the focused home stack.
1415 if (focusedStackId != HOME_STACK_ID) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001416 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001417 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001418 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked();
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001419 return topHomeActivity == null || !topHomeActivity.isHomeActivity() ?
1420 STACK_VISIBLE : STACK_INVISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001421 }
1422
Chong Zhangb16cf342015-11-12 17:22:40 -08001423 // Find the first stack below focused stack that actually got something visible.
1424 int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1425 while (belowFocusedIndex >= 0 &&
1426 mStacks.get(belowFocusedIndex).topRunningActivityLocked() == null) {
1427 belowFocusedIndex--;
1428 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001429 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1430 && stackIndex == belowFocusedIndex) {
1431 // Stacks directly behind the docked or pinned stack are always visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001432 return STACK_VISIBLE;
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001433 }
1434
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001435 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1436 && hasTranslucentActivity(focusedStack)) {
1437 // Stacks behind the fullscreen stack with a translucent activity are always
1438 // visible so they can act as a backdrop to the translucent activity.
1439 // For example, dialog activities
1440 if (stackIndex == belowFocusedIndex) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001441 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001442 }
1443 if (belowFocusedIndex >= 0) {
1444 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001445 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1446 && stackIndex == (belowFocusedIndex - 1)) {
1447 // The stack behind the docked or pinned stack is also visible so we can have a
1448 // complete backdrop to the translucent activity when the docked stack is up.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001449 return STACK_VISIBLE;
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001450 }
1451 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001452 }
1453
Wale Ogunwale3797c222015-10-27 14:21:58 -07001454 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001455 // Visibility of any static stack should have been determined by the conditions above.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001456 return STACK_INVISIBLE;
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001457 }
1458
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001459 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001460 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001461
1462 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1463 continue;
1464 }
1465
Wale Ogunwale3797c222015-10-27 14:21:58 -07001466 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001467 // These stacks can't have any dynamic stacks visible behind them.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001468 return STACK_INVISIBLE;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001469 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001470
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001471 if (!hasTranslucentActivity(stack)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001472 return STACK_INVISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001473 }
1474 }
1475
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001476 return STACK_VISIBLE;
Jose Lima7ba71252014-04-30 12:59:27 -07001477 }
1478
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001479 final int rankTaskLayers(int baseLayer) {
1480 int layer = 0;
1481 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1482 final TaskRecord task = mTaskHistory.get(taskNdx);
1483 ActivityRecord r = task.topRunningActivityLocked();
1484 if (r == null || r.finishing || !r.visible) {
1485 task.mLayerRank = -1;
1486 } else {
1487 task.mLayerRank = baseLayer + layer++;
1488 }
1489 }
1490 return layer;
1491 }
1492
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001493 /**
1494 * Make sure that all activities that need to be visible (that is, they
1495 * currently can be seen by the user) actually are.
1496 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001497 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1498 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001499 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001500 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001501 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001502 if (top != null) {
1503 checkTranslucentActivityWaiting(top);
1504 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001505
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001506 // If the top activity is not fullscreen, then we need to
1507 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001508 boolean aboveTop = top != null;
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001509 final int stackVisibility = getStackVisibilityLocked();
1510 final boolean stackInvisible = stackVisibility != STACK_VISIBLE;
1511 final boolean stackVisibleBehind = stackVisibility == STACK_VISIBLE_ACTIVITY_BEHIND;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001512 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaled046a012015-12-24 13:05:59 -08001513 boolean resumeNextActivity = isFocusable() && (isInStackLocked(starting) == null);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001514 boolean behindTranslucentActivity = false;
1515 final ActivityRecord visibleBehind = getVisibleBehindActivity();
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001516 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001517 final TaskRecord task = mTaskHistory.get(taskNdx);
1518 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08001519 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1520 final ActivityRecord r = activities.get(activityNdx);
1521 if (r.finishing) {
1522 continue;
1523 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001524 final boolean isTop = r == top;
1525 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001526 continue;
1527 }
1528 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001529 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1530 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001531 final boolean activityVisibleBehind =
1532 (behindTranslucentActivity || stackVisibleBehind) && visibleBehind == r;
1533 final boolean isVisible = (!behindFullscreenActivity || r.mLaunchTaskBehind
1534 || activityVisibleBehind) && okToShowLocked(r);
1535 if (isVisible) {
1536 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
1537 + " finishing=" + r.finishing + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001538 // First: if this is not the current activity being started, make
1539 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001540 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001541 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001542 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001543
1544 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001545 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001546 resumeNextActivity, r)) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001547 if (activityNdx >= activities.size()) {
1548 // Record may be removed if its process needs to restart.
1549 activityNdx = activities.size() - 1;
1550 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001551 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001552 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001553 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001554 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001555 // If this activity is already visible, then there is nothing to do here.
1556 if (handleAlreadyVisible(r)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001557 resumeNextActivity = false;
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001558 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001559 } else {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001560 makeVisible(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001561 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001562 // Aggregate current change flags.
1563 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001564 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1565 behindFullscreenActivity, task, r);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001566 if (behindFullscreenActivity && !r.fullscreen) {
1567 behindTranslucentActivity = true;
1568 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001569 } else {
Wale Ogunwale834c2362016-01-23 18:14:58 -08001570 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001571 + " finishing=" + r.finishing + " state=" + r.state + " stackInvisible="
1572 + stackInvisible + " behindFullscreenActivity="
1573 + behindFullscreenActivity + " mLaunchTaskBehind="
1574 + r.mLaunchTaskBehind);
1575 makeInvisible(r, visibleBehind);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001576 }
1577 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001578 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1579 // The visibility of tasks and the activities they contain in freeform stack are
1580 // determined individually unlike other stacks where the visibility or fullscreen
1581 // status of an activity in a previous task affects other.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001582 behindFullscreenActivity = stackVisibility == STACK_INVISIBLE;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001583 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001584 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001585
1586 if (mTranslucentActivityWaiting != null &&
1587 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1588 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1589 notifyActivityDrawnLocked(null);
1590 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001591 }
Craig Mautner58547802013-03-05 08:23:53 -08001592
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001593 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1594 if (mTranslucentActivityWaiting != top) {
1595 mUndrawnActivitiesBelowTopTranslucent.clear();
1596 if (mTranslucentActivityWaiting != null) {
1597 // Call the callback with a timeout indication.
1598 notifyActivityDrawnLocked(null);
1599 mTranslucentActivityWaiting = null;
1600 }
1601 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1602 }
1603 }
1604
1605 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001606 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001607 // We need to make sure the app is running if it's the top, or it is just made visible from
1608 // invisible. If the app is already visible, it must have died while it was visible. In this
1609 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1610 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001611 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001612 // This activity needs to be visible, but isn't even running...
1613 // get it started and resume if no other stack in this stack is resumed.
1614 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1615 if (r != starting) {
1616 r.startFreezingScreenLocked(r.app, configChanges);
1617 }
1618 if (!r.visible || r.mLaunchTaskBehind) {
1619 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1620 setVisible(r, true);
1621 }
1622 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001623 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001624 return true;
1625 }
1626 }
1627 return false;
1628 }
1629
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001630 private void makeInvisible(ActivityRecord r, ActivityRecord visibleBehind) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001631 if (!r.visible) {
1632 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1633 return;
1634 }
1635 // Now for any activities that aren't visible to the user, make sure they no longer are
1636 // keeping the screen frozen.
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08001637 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r + " " + r.state);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001638 try {
1639 setVisible(r, false);
1640 switch (r.state) {
1641 case STOPPING:
1642 case STOPPED:
1643 if (r.app != null && r.app.thread != null) {
1644 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1645 "Scheduling invisibility: " + r);
1646 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1647 }
1648 break;
1649
1650 case INITIALIZING:
1651 case RESUMED:
1652 case PAUSING:
1653 case PAUSED:
1654 // This case created for transitioning activities from
1655 // translucent to opaque {@link Activity#convertToOpaque}.
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001656 if (visibleBehind == r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001657 releaseBackgroundResources(r);
1658 } else {
1659 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1660 mStackSupervisor.mStoppingActivities.add(r);
1661 }
1662 mStackSupervisor.scheduleIdleLocked();
1663 }
1664 break;
1665
1666 default:
1667 break;
1668 }
1669 } catch (Exception e) {
1670 // Just skip on any failure; we'll make it visible when it next restarts.
1671 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1672 }
1673 }
1674
1675 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1676 TaskRecord task, ActivityRecord r) {
1677 if (r.fullscreen) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001678 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
1679 + " stackInvisible=" + stackInvisible
1680 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001681 // At this point, nothing else needs to be shown in this task.
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001682 behindFullscreenActivity = true;
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001683 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001684 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1685 + " stackInvisible=" + stackInvisible
1686 + " behindFullscreenActivity=" + behindFullscreenActivity);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08001687 behindFullscreenActivity = true;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001688 }
1689 return behindFullscreenActivity;
1690 }
1691
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001692 private void makeVisible(ActivityRecord starting, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001693 // This activity is not currently visible, but is running. Tell it to become visible.
1694 r.visible = true;
1695 if (r.state != ActivityState.RESUMED && r != starting) {
1696 // If this activity is paused, tell it to now show its window.
1697 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1698 "Making visible and scheduling visibility: " + r);
1699 try {
1700 if (mTranslucentActivityWaiting != null) {
1701 r.updateOptionsLocked(r.returningOptions);
1702 mUndrawnActivitiesBelowTopTranslucent.add(r);
1703 }
1704 setVisible(r, true);
1705 r.sleeping = false;
1706 r.app.pendingUiClean = true;
1707 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1708 r.stopFreezingScreenLocked(false);
1709 } catch (Exception e) {
1710 // Just skip on any failure; we'll make it
1711 // visible when it next restarts.
1712 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
1713 }
1714 }
1715 }
1716
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001717 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001718 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1719 r.stopFreezingScreenLocked(false);
1720 try {
1721 if (r.returningOptions != null) {
1722 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1723 }
1724 } catch(RemoteException e) {
1725 }
1726 return r.state == ActivityState.RESUMED;
1727 }
1728
Todd Kennedyaab56db2015-01-30 09:39:53 -08001729 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001730 mTranslucentActivityWaiting = r;
1731 mUndrawnActivitiesBelowTopTranslucent.clear();
1732 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1733 }
1734
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001735 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1736 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1737 final TaskRecord task = mTaskHistory.get(taskNdx);
1738 final ArrayList<ActivityRecord> activities = task.mActivities;
1739 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1740 final ActivityRecord r = activities.get(activityNdx);
1741 if ( r.appTimeTracker != except) {
1742 r.appTimeTracker = null;
1743 }
1744 }
1745 }
1746 }
1747
Craig Mautner5eda9b32013-07-02 11:58:16 -07001748 /**
1749 * Called as activities below the top translucent activity are redrawn. When the last one is
1750 * redrawn notify the top activity by calling
1751 * {@link Activity#onTranslucentConversionComplete}.
1752 *
1753 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1754 * occurred and the activity will be notified immediately.
1755 */
1756 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001757 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001758 if ((r == null)
1759 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1760 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1761 // The last undrawn activity below the top has just been drawn. If there is an
1762 // opaque activity at the top, notify it that it can become translucent safely now.
1763 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1764 mTranslucentActivityWaiting = null;
1765 mUndrawnActivitiesBelowTopTranslucent.clear();
1766 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1767
Craig Mautner71dd1b62014-02-18 15:48:52 -08001768 if (waitingActivity != null) {
1769 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1770 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1771 try {
1772 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1773 waitingActivity.appToken, r != null);
1774 } catch (RemoteException e) {
1775 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001776 }
1777 }
1778 }
1779 }
1780
Craig Mautnera61bc652013-10-28 15:43:18 -07001781 /** If any activities below the top running one are in the INITIALIZING state and they have a
1782 * starting window displayed then remove that starting window. It is possible that the activity
1783 * in this state will never resumed in which case that starting window will be orphaned. */
1784 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001785 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001786 boolean aboveTop = true;
1787 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1788 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1789 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1790 final ActivityRecord r = activities.get(activityNdx);
1791 if (aboveTop) {
1792 if (r == topActivity) {
1793 aboveTop = false;
1794 }
1795 continue;
1796 }
1797
1798 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001799 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1800 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001801 r.mStartingWindowShown = false;
1802 mWindowManager.removeAppStartingWindow(r.appToken);
1803 }
1804 }
1805 }
1806 }
1807
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001808 /**
1809 * Ensure that the top activity in the stack is resumed.
1810 *
1811 * @param prev The previously resumed activity, for when in the process
1812 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001813 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001814 *
1815 * @return Returns true if something is being resumed, or false if
1816 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001817 *
1818 * NOTE: It is not safe to call this method directly as it can cause an activity in a
1819 * non-focused stack to be resumed.
1820 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
1821 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001822 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08001823 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001824 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001825 // Don't even start recursing.
1826 return false;
1827 }
1828
1829 boolean result = false;
1830 try {
1831 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001832 mStackSupervisor.inResumeTopActivity = true;
1833 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1834 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001835 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001836 }
Craig Mautner544efa72014-09-04 16:41:20 -07001837 result = resumeTopActivityInnerLocked(prev, options);
1838 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001839 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001840 }
1841 return result;
1842 }
1843
Chong Zhang280d3322015-11-03 17:27:26 -08001844 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001845 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001846
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001847 if (!mService.mBooting && !mService.mBooted) {
1848 // Not ready yet!
1849 return false;
1850 }
1851
Craig Mautnerdf88d732014-01-27 09:21:32 -08001852 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001853 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001854 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001855 // Do not resume this stack if its parent is not resumed.
1856 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1857 return false;
1858 }
1859
Craig Mautnera61bc652013-10-28 15:43:18 -07001860 cancelInitializingActivities();
1861
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001862 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001863 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001864
1865 // Remember how we'll process this pause/resume situation, and ensure
1866 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001867 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1868 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001869
Craig Mautner84984fa2014-06-19 11:19:20 -07001870 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001871 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001872 // There are no more activities!
1873 final String reason = "noMoreActivities";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001874 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(reason)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001875 // Try to move focus to the next visible stack with a running activity if this
1876 // stack is not covering the entire screen.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001877 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
1878 mStackSupervisor.getFocusedStack(), prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001879 }
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001880
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001881 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001882 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001883 if (DEBUG_STATES) Slog.d(TAG_STATES,
1884 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001885 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001886 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001887 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1888 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1889 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001890 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001891 }
1892
1893 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001894
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001895 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001896 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1897 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001898 // Make sure we have executed any pending transitions, since there
1899 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001900 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001901 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001902 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001903 if (DEBUG_STATES) Slog.d(TAG_STATES,
1904 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001905 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001906 return false;
1907 }
1908
Craig Mautner525f3d92013-05-07 14:01:50 -07001909 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001910 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001911 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001912 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001913 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001914 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001915 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001916 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001917 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001918 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001919 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001920 } else if (!isOnHomeDisplay()) {
1921 return false;
1922 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001923 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001924 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001925 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1926 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1927 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001928 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001929 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001930 }
1931
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001932 // If we are sleeping, and there is no resumed activity, and the top
1933 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001934 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001935 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001936 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001937 // Make sure we have executed any pending transitions, since there
1938 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001939 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001940 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001941 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001942 if (DEBUG_STATES) Slog.d(TAG_STATES,
1943 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001944 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001945 return false;
1946 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001947
1948 // Make sure that the user who owns this activity is started. If not,
1949 // we will just leave it as is because someone should be bringing
1950 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001951 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001952 Slog.w(TAG, "Skipping resume of top activity " + next
1953 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001954 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001955 return false;
1956 }
1957
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001958 // The activity may be waiting for stop, but that is no longer
1959 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001960 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001961 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001962 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001963 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001964
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001965 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001966
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001967 // If we are currently pausing an activity, then don't do anything
1968 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001969 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001970 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001971 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001972 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001973 return false;
1974 }
1975
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001976 // Okay we are now going to start a switch, to 'next'. We may first
1977 // have to pause the current activity, but this is an important point
1978 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001979 // XXX "App Redirected" dialog is getting too many false positives
1980 // at this point, so turn off for now.
1981 if (false) {
1982 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1983 long now = SystemClock.uptimeMillis();
1984 final boolean inTime = mLastStartedActivity.startTime != 0
1985 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1986 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1987 final int nextUid = next.info.applicationInfo.uid;
1988 if (inTime && lastUid != nextUid
1989 && lastUid != next.launchedFromUid
1990 && mService.checkPermission(
1991 android.Manifest.permission.STOP_APP_SWITCHES,
1992 -1, next.launchedFromUid)
1993 != PackageManager.PERMISSION_GRANTED) {
1994 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1995 } else {
1996 next.startTime = now;
1997 mLastStartedActivity = next;
1998 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001999 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07002000 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07002001 mLastStartedActivity = next;
2002 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07002003 }
Craig Mautner58547802013-03-05 08:23:53 -08002004
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002005 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
2006
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002007 // We need to start pausing the current activity so the top one
2008 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07002009 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
2010 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002011 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002012 if (DEBUG_STATES) Slog.d(TAG_STATES,
2013 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08002014 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07002015 }
2016 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002017 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07002018 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002019 // At this point we want to put the upcoming activity's process
2020 // at the top of the LRU list, since we know we will be needing it
2021 // very soon and it would be a waste to let it get killed if it
2022 // happens to be sitting towards the end.
2023 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07002024 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002025 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002026 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002027 return true;
2028 }
2029
Christopher Tated3f175c2012-06-14 14:16:54 -07002030 // If the most recent activity was noHistory but was only stopped rather
2031 // than stopped+finished because the device went to sleep, we need to make
2032 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07002033 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002034 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002035 if (DEBUG_STATES) Slog.d(TAG_STATES,
2036 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002037 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002038 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002039 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002040 }
2041
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002042 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002043 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2044 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002045 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002046 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2047 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002048 } else {
2049 // The next activity is already visible, so hide the previous
2050 // activity's windows right now so we can show the new one ASAP.
2051 // We only do this if the previous is finishing, which should mean
2052 // it is on top of the one being resumed so hiding it quickly
2053 // is good. Otherwise, we want to do the normal route of allowing
2054 // the resumed activity to be shown so we can decide if the
2055 // previous should actually be hidden depending on whether the
2056 // new one is found to be full-screen or not.
2057 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002058 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002059 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2060 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002061 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002062 + ", nowVisible=" + next.nowVisible);
2063 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002064 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002065 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002066 + ", waitingVisible="
2067 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2068 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002069 }
2070 }
2071 }
2072
Dianne Hackborne7f97212011-02-24 14:40:20 -08002073 // Launching this app's activity, make sure the app is no longer
2074 // considered stopped.
2075 try {
2076 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002077 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002078 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002079 } catch (IllegalArgumentException e) {
2080 Slog.w(TAG, "Failed trying to unstop package "
2081 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002082 }
2083
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002084 // We are starting up the next activity, so tell the window manager
2085 // that the previous one will be hidden soon. This way it can know
2086 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002087 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002088 if (prev != null) {
2089 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002090 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002091 "Prepare close transition: prev=" + prev);
2092 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002093 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002094 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002095 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002096 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002097 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2098 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002099 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002100 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002101 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002102 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2103 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002105 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002106 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002107 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002108 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002109 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002110 : next.mLaunchTaskBehind
2111 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2112 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002113 }
2114 }
Craig Mautner967212c2013-04-13 21:10:58 -07002115 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002116 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002117 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002118 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002119 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002120 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002121 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002122 }
2123 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002124
2125 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002126 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002127 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2128 if (opts != null) {
2129 resumeAnimOptions = opts.toBundle();
2130 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002131 next.applyOptionsLocked();
2132 } else {
2133 next.clearOptionsLocked();
2134 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002135
Craig Mautnercf910b02013-04-23 11:23:27 -07002136 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002137 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002138 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002139
2140 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002141 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002142
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002143 // schedule launch ticks to collect information about slow apps.
2144 next.startLaunchTickingLocked();
2145
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002146 ActivityRecord lastResumedActivity =
2147 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002148 ActivityState lastState = next.state;
2149
2150 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002151
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002152 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002153 next.state = ActivityState.RESUMED;
2154 mResumedActivity = next;
2155 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002156 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002157 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002158 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002159 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002160
2161 // Have the window manager re-evaluate the orientation of
2162 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002163 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002164 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002165 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002166 mService.mConfiguration,
2167 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2168 if (config != null) {
2169 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002170 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002171 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002172 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002173
Craig Mautner525f3d92013-05-07 14:01:50 -07002174 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002175 // The configuration update wasn't able to keep the existing
2176 // instance of the activity, and instead started a new one.
2177 // We should be all done, but let's just make sure our activity
2178 // is still at the top and schedule another run if something
2179 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002180 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002181 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002182 "Activity config changed during resume: " + next
2183 + ", new next: " + nextNext);
2184 if (nextNext != next) {
2185 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002186 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002187 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002188 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002189 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002190 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002191 return true;
2192 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002193 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002194 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002195 }
Craig Mautner58547802013-03-05 08:23:53 -08002196
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002197 try {
2198 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002199 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002200 if (a != null) {
2201 final int N = a.size();
2202 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002203 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2204 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002205 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002206 }
2207 }
2208
2209 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002210 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002211 }
2212
Craig Mautner299f9602015-01-26 09:47:33 -08002213 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2214 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002215
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002216 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002217 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002218 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002219 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002220 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002221 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002222 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002223
Craig Mautner0eea92c2013-05-16 13:35:39 -07002224 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002225
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002226 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002227 } catch (Exception e) {
2228 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002229 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002230 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002231 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002232 if (lastStack != null) {
2233 lastStack.mResumedActivity = lastResumedActivity;
2234 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002235 Slog.i(TAG, "Restarting because process died: " + next);
2236 if (!next.hasBeenLaunched) {
2237 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002238 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2239 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002240 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002241 next.appToken, next.packageName, next.theme,
2242 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002243 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2244 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002245 }
George Mount2c92c972014-03-20 09:38:23 -07002246 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002247 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002248 return true;
2249 }
2250
2251 // From this point on, if something goes wrong there is no way
2252 // to recover the activity.
2253 try {
2254 next.visible = true;
2255 completeResumeLocked(next);
2256 } catch (Exception e) {
2257 // If any exception gets thrown, toss away this
2258 // activity and try the next one.
2259 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002260 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002261 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002262 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002263 return true;
2264 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002265 next.stopped = false;
2266
2267 } else {
2268 // Whoops, need to restart this activity!
2269 if (!next.hasBeenLaunched) {
2270 next.hasBeenLaunched = true;
2271 } else {
2272 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002273 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002274 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002275 mService.compatibilityInfoForPackageLocked(
2276 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002277 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002278 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002279 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002280 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002281 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002282 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002283 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002284 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002285 }
2286
Craig Mautnercf910b02013-04-23 11:23:27 -07002287 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002288 return true;
2289 }
2290
riddle_hsuc215a4f2014-12-27 12:10:45 +08002291 private TaskRecord getNextTask(TaskRecord targetTask) {
2292 final int index = mTaskHistory.indexOf(targetTask);
2293 if (index >= 0) {
2294 final int numTasks = mTaskHistory.size();
2295 for (int i = index + 1; i < numTasks; ++i) {
2296 TaskRecord task = mTaskHistory.get(i);
2297 if (task.userId == targetTask.userId) {
2298 return task;
2299 }
2300 }
2301 }
2302 return null;
2303 }
2304
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002305 private void insertTaskAtPosition(TaskRecord task, int position) {
2306 if (position >= mTaskHistory.size()) {
2307 insertTaskAtTop(task, null);
2308 return;
2309 }
2310 // Calculate maximum possible position for this task.
2311 int maxPosition = mTaskHistory.size();
2312 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002313 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002314 // Put non-current user tasks below current user tasks.
2315 while (maxPosition > 0) {
2316 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2317 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002318 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002319 break;
2320 }
2321 maxPosition--;
2322 }
2323 }
2324 position = Math.min(position, maxPosition);
2325 mTaskHistory.remove(task);
2326 mTaskHistory.add(position, task);
2327 updateTaskMovement(task, true);
2328 }
2329
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002330 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002331 // If the moving task is over home stack, transfer its return type to next task
2332 if (task.isOverHomeStack()) {
2333 final TaskRecord nextTask = getNextTask(task);
2334 if (nextTask != null) {
2335 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2336 }
2337 }
2338
Craig Mautner9c85c202013-10-04 20:11:26 -07002339 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002340 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002341 if (isOnHomeDisplay()) {
2342 ActivityStack lastStack = mStackSupervisor.getLastStack();
2343 final boolean fromHome = lastStack.isHomeStack();
2344 if (!isHomeStack() && (fromHome || topTask() != task)) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002345 int returnToType = APPLICATION_ACTIVITY_TYPE;
2346 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2347 returnToType = lastStack.topTask() == null
2348 ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2349 }
2350 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002351 }
2352 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002353 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002354 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002355
Craig Mautnerac6f8432013-07-17 13:24:59 -07002356 mTaskHistory.remove(task);
2357 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002358 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002359 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002360 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002361 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002362 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002363 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002364 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002365 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002366 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002367 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002368 break;
2369 }
2370 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002371 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002372 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002373 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002374 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002375 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002376
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002377 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2378 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002379 TaskRecord rTask = r.task;
2380 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002381 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2382 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002383 // Last activity in task had been removed or ActivityManagerService is reusing task.
2384 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002385 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002386 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002387 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002388 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002389 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002390 if (!newTask) {
2391 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002392 boolean startIt = true;
2393 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2394 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002395 if (task.getTopActivity() == null) {
2396 // All activities in task are finishing.
2397 continue;
2398 }
Craig Mautner70a86932013-02-28 22:37:44 -08002399 if (task == r.task) {
2400 // Here it is! Now, if this is not yet visible to the
2401 // user, then just add it without starting; it will
2402 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002403 if (!startIt) {
2404 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2405 + task, new RuntimeException("here").fillInStackTrace());
2406 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002407 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002408 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002409 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002410 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002411 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002412 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002413 return;
2414 }
2415 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002416 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002417 startIt = false;
2418 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002419 }
2420 }
2421
2422 // Place a new activity at top of stack, so it is next to interact
2423 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002424
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002425 // If we are not placing the new activity frontmost, we do not want
2426 // to deliver the onUserLeaving callback to the actual frontmost
2427 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002428 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002429 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002430 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002431 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002432 }
Craig Mautner70a86932013-02-28 22:37:44 -08002433
2434 task = r.task;
2435
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002436 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002437 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002438 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002439 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002440 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002441
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002442 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002443 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002444 // We want to show the starting preview window if we are
2445 // switching to a new task, or the next activity's process is
2446 // not currently running.
2447 boolean showStartingIcon = newTask;
2448 ProcessRecord proc = r.app;
2449 if (proc == null) {
2450 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2451 }
2452 if (proc == null || proc.thread == null) {
2453 showStartingIcon = true;
2454 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002455 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002456 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002457 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002458 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002459 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002460 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002461 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002462 ? r.mLaunchTaskBehind
2463 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2464 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002465 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002466 mNoAnimActivities.remove(r);
2467 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002468 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002469 boolean doShow = true;
2470 if (newTask) {
2471 // Even though this activity is starting fresh, we still need
2472 // to reset it to make sure we apply affinities to move any
2473 // existing activities from other tasks in to it.
2474 // If the caller has requested that the target task be
2475 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002476 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002477 resetTaskIfNeededLocked(r, r);
2478 doShow = topRunningNonDelayedActivityLocked(null) == r;
2479 }
Chong Zhang280d3322015-11-03 17:27:26 -08002480 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002481 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2482 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002483 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002484 if (r.mLaunchTaskBehind) {
2485 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2486 // tell WindowManager that r is visible even though it is at the back of the stack.
2487 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002488 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002489 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002490 // Figure out if we are transitioning from another activity that is
2491 // "has the same starting icon" as the next one. This allows the
2492 // window manager to keep the previous window it had previously
2493 // created, if it still had one.
2494 ActivityRecord prev = mResumedActivity;
2495 if (prev != null) {
2496 // We don't want to reuse the previous starting preview if:
2497 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002498 if (prev.task != r.task) {
2499 prev = null;
2500 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002501 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002502 else if (prev.nowVisible) {
2503 prev = null;
2504 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002505 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002506 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002507 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002508 mService.compatibilityInfoForPackageLocked(
2509 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002510 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002511 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002512 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002513 }
2514 } else {
2515 // If this is the first activity, don't do any fancy animations,
2516 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002517 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002518 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002519 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002520 }
2521 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002522 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002523 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002524 }
2525
Dianne Hackbornbe707852011-11-11 14:32:10 -08002526 final void validateAppTokensLocked() {
2527 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002528 mValidateAppTokens.ensureCapacity(numActivities());
2529 final int numTasks = mTaskHistory.size();
2530 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2531 TaskRecord task = mTaskHistory.get(taskNdx);
2532 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002533 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002534 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002535 }
Craig Mautner000f0022013-02-26 15:04:29 -08002536 TaskGroup group = new TaskGroup();
2537 group.taskId = task.taskId;
2538 mValidateAppTokens.add(group);
2539 final int numActivities = activities.size();
2540 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2541 final ActivityRecord r = activities.get(activityNdx);
2542 group.tokens.add(r.appToken);
2543 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002544 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002545 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002546 }
2547
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002548 /**
2549 * Perform a reset of the given task, if needed as part of launching it.
2550 * Returns the new HistoryRecord at the top of the task.
2551 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002552 /**
2553 * Helper method for #resetTaskIfNeededLocked.
2554 * We are inside of the task being reset... we'll either finish this activity, push it out
2555 * for another task, or leave it as-is.
2556 * @param task The task containing the Activity (taskTop) that might be reset.
2557 * @param forceReset
2558 * @return An ActivityOptions that needs to be processed.
2559 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002560 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002561 ActivityOptions topOptions = null;
2562
2563 int replyChainEnd = -1;
2564 boolean canMoveOptions = true;
2565
2566 // We only do this for activities that are not the root of the task (since if we finish
2567 // the root, we may no longer have the task!).
2568 final ArrayList<ActivityRecord> activities = task.mActivities;
2569 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002570 final int rootActivityNdx = task.findEffectiveRootIndex();
2571 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002572 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002573 if (target.frontOfTask)
2574 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002575
2576 final int flags = target.info.flags;
2577 final boolean finishOnTaskLaunch =
2578 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2579 final boolean allowTaskReparenting =
2580 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2581 final boolean clearWhenTaskReset =
2582 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2583
2584 if (!finishOnTaskLaunch
2585 && !clearWhenTaskReset
2586 && target.resultTo != null) {
2587 // If this activity is sending a reply to a previous
2588 // activity, we can't do anything with it now until
2589 // we reach the start of the reply chain.
2590 // XXX note that we are assuming the result is always
2591 // to the previous activity, which is almost always
2592 // the case but we really shouldn't count on.
2593 if (replyChainEnd < 0) {
2594 replyChainEnd = i;
2595 }
2596 } else if (!finishOnTaskLaunch
2597 && !clearWhenTaskReset
2598 && allowTaskReparenting
2599 && target.taskAffinity != null
2600 && !target.taskAffinity.equals(task.affinity)) {
2601 // If this activity has an affinity for another
2602 // task, then we need to move it out of here. We will
2603 // move it as far out of the way as possible, to the
2604 // bottom of the activity stack. This also keeps it
2605 // correctly ordered with any activities we previously
2606 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002607 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002608 final ActivityRecord bottom =
2609 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002610 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002611 if (bottom != null && target.taskAffinity != null
2612 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002613 // If the activity currently at the bottom has the
2614 // same task affinity as the one we are moving,
2615 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002616 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002617 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002618 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002619 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -08002620 targetTask = createTaskRecord(
2621 mStackSupervisor.getNextTaskIdForUserLocked(target.userId),
2622 target.info, null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002623 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002624 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002625 + " out to new task " + target.task);
2626 }
2627
Wale Ogunwale706ed792015-08-02 10:29:44 -07002628 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002629
Craig Mautner525f3d92013-05-07 14:01:50 -07002630 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002631 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2632 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002633 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002634 if (p.finishing) {
2635 continue;
2636 }
2637
Craig Mautnere3a74d52013-02-22 14:14:58 -08002638 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002639 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002640 topOptions = p.takeOptionsLocked();
2641 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002642 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002643 }
2644 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002645 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2646 "Removing activity " + p + " from task=" + task + " adding to task="
2647 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002648 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2649 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002650 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002651 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002652
Wale Ogunwale706ed792015-08-02 10:29:44 -07002653 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002654 }
2655
Wale Ogunwale706ed792015-08-02 10:29:44 -07002656 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002657 if (VALIDATE_TOKENS) {
2658 validateAppTokensLocked();
2659 }
2660
2661 replyChainEnd = -1;
2662 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2663 // If the activity should just be removed -- either
2664 // because it asks for it, or the task should be
2665 // cleared -- then finish it and anything that is
2666 // part of its reply chain.
2667 int end;
2668 if (clearWhenTaskReset) {
2669 // In this case, we want to finish this activity
2670 // and everything above it, so be sneaky and pretend
2671 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002672 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002673 } else if (replyChainEnd < 0) {
2674 end = i;
2675 } else {
2676 end = replyChainEnd;
2677 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002678 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002679 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002680 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002681 if (p.finishing) {
2682 continue;
2683 }
2684 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002685 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002686 topOptions = p.takeOptionsLocked();
2687 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002688 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002689 }
2690 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002691 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002692 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002693 if (finishActivityLocked(
2694 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002695 end--;
2696 srcPos--;
2697 }
2698 }
2699 replyChainEnd = -1;
2700 } else {
2701 // If we were in the middle of a chain, well the
2702 // activity that started it all doesn't want anything
2703 // special, so leave it all as-is.
2704 replyChainEnd = -1;
2705 }
2706 }
2707
2708 return topOptions;
2709 }
2710
2711 /**
2712 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2713 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2714 * @param affinityTask The task we are looking for an affinity to.
2715 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2716 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2717 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2718 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002719 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002720 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002721 int replyChainEnd = -1;
2722 final int taskId = task.taskId;
2723 final String taskAffinity = task.affinity;
2724
2725 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2726 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002727 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2728
2729 // Do not operate on or below the effective root Activity.
2730 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002731 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002732 if (target.frontOfTask)
2733 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002734
2735 final int flags = target.info.flags;
2736 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2737 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2738
2739 if (target.resultTo != null) {
2740 // If this activity is sending a reply to a previous
2741 // activity, we can't do anything with it now until
2742 // we reach the start of the reply chain.
2743 // XXX note that we are assuming the result is always
2744 // to the previous activity, which is almost always
2745 // the case but we really shouldn't count on.
2746 if (replyChainEnd < 0) {
2747 replyChainEnd = i;
2748 }
2749 } else if (topTaskIsHigher
2750 && allowTaskReparenting
2751 && taskAffinity != null
2752 && taskAffinity.equals(target.taskAffinity)) {
2753 // This activity has an affinity for our task. Either remove it if we are
2754 // clearing or move it over to our task. Note that
2755 // we currently punt on the case where we are resetting a
2756 // task that is not at the top but who has activities above
2757 // with an affinity to it... this is really not a normal
2758 // case, and we will need to later pull that task to the front
2759 // and usually at that point we will do the reset and pick
2760 // up those remaining activities. (This only happens if
2761 // someone starts an activity in a new task from an activity
2762 // in a task that is not currently on top.)
2763 if (forceReset || finishOnTaskLaunch) {
2764 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002765 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2766 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002767 for (int srcPos = start; srcPos >= i; --srcPos) {
2768 final ActivityRecord p = activities.get(srcPos);
2769 if (p.finishing) {
2770 continue;
2771 }
Todd Kennedy539db512014-12-15 09:57:55 -08002772 finishActivityLocked(
2773 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002774 }
2775 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002776 if (taskInsertionPoint < 0) {
2777 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002778
Craig Mautner77878772013-03-04 19:46:24 -08002779 }
Craig Mautner77878772013-03-04 19:46:24 -08002780
2781 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002782 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2783 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2784 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002785 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002786 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002787 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002788 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002789
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002790 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2791 "Removing and adding activity " + p + " to stack at " + task
2792 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002793 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2794 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002795 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002796 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002797 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002798 if (VALIDATE_TOKENS) {
2799 validateAppTokensLocked();
2800 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002801
2802 // Now we've moved it in to place... but what if this is
2803 // a singleTop activity and we have put it on top of another
2804 // instance of the same activity? Then we drop the instance
2805 // below so it remains singleTop.
2806 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2807 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002808 int targetNdx = taskActivities.indexOf(target);
2809 if (targetNdx > 0) {
2810 ActivityRecord p = taskActivities.get(targetNdx - 1);
2811 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002812 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2813 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002814 }
2815 }
2816 }
2817 }
2818
2819 replyChainEnd = -1;
2820 }
2821 }
Craig Mautner77878772013-03-04 19:46:24 -08002822 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002823 }
2824
Craig Mautner8849a5e2013-04-02 16:41:03 -07002825 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002826 ActivityRecord newActivity) {
2827 boolean forceReset =
2828 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2829 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2830 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2831 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2832 forceReset = true;
2833 }
2834 }
2835
2836 final TaskRecord task = taskTop.task;
2837
2838 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2839 * for remaining tasks. Used for later tasks to reparent to task. */
2840 boolean taskFound = false;
2841
2842 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2843 ActivityOptions topOptions = null;
2844
Craig Mautner77878772013-03-04 19:46:24 -08002845 // Preserve the location for reparenting in the new task.
2846 int reparentInsertionPoint = -1;
2847
Craig Mautnere3a74d52013-02-22 14:14:58 -08002848 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2849 final TaskRecord targetTask = mTaskHistory.get(i);
2850
2851 if (targetTask == task) {
2852 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2853 taskFound = true;
2854 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002855 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2856 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002857 }
2858 }
2859
Craig Mautner70a86932013-02-28 22:37:44 -08002860 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002861 if (taskNdx >= 0) {
2862 do {
2863 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2864 } while (taskTop == null && taskNdx >= 0);
2865 }
Craig Mautner70a86932013-02-28 22:37:44 -08002866
Craig Mautnere3a74d52013-02-22 14:14:58 -08002867 if (topOptions != null) {
2868 // If we got some ActivityOptions from an activity on top that
2869 // was removed from the task, propagate them to the new real top.
2870 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002871 taskTop.updateOptionsLocked(topOptions);
2872 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002873 topOptions.abort();
2874 }
2875 }
2876
2877 return taskTop;
2878 }
2879
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002880 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2881 String resultWho, int requestCode, int resultCode, Intent data) {
2882
2883 if (callingUid > 0) {
2884 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002885 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002886 }
2887
2888 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2889 + " : who=" + resultWho + " req=" + requestCode
2890 + " res=" + resultCode + " data=" + data);
2891 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2892 try {
2893 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2894 list.add(new ResultInfo(resultWho, requestCode,
2895 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002896 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002897 return;
2898 } catch (Exception e) {
2899 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2900 }
2901 }
2902
2903 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2904 }
2905
Craig Mautner299f9602015-01-26 09:47:33 -08002906 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002907 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2908 return;
2909 }
2910
2911 final ActivityRecord next = topRunningActivityLocked();
2912 final String myReason = reason + " adjustFocus";
2913 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002914 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002915 // For freeform, docked, and pinned stacks we always keep the focus within the
2916 // stack as long as there is a running activity in the stack that we can adjust
2917 // focus to.
2918 mService.setFocusedActivityLocked(next, myReason);
2919 return;
2920 } else {
2921 final TaskRecord task = r.task;
2922 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2923 // For non-fullscreen stack, we want to move the focus to the next visible
2924 // stack to prevent the home screen from moving to the top and obscuring
2925 // other visible stacks.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002926 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002927 return;
2928 }
2929 // Move the home stack to the top if this stack is fullscreen or there is no
2930 // other visible stack.
2931 if (mStackSupervisor.moveHomeStackTaskToTop(
2932 task.getTaskToReturnTo(), myReason)) {
2933 // Activity focus was already adjusted. Nothing else to do...
2934 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002935 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002936 }
2937 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08002938 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002939
Wale Ogunwaled046a012015-12-24 13:05:59 -08002940 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002941 }
2942
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002943 private boolean adjustFocusToNextFocusableStackLocked(String reason) {
2944 final ActivityStack stack = getNextFocusableStackLocked();
2945 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002946 if (stack == null) {
2947 return false;
2948 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002949 return mService.setFocusedActivityLocked(stack.topRunningActivityLocked(), myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002950 }
2951
Craig Mautnerf3333272013-04-22 10:55:53 -07002952 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002953 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002954 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2955 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2956 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002957 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002958 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002959 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2960 "stop-no-history", false)) {
2961 // Activity was finished, no need to continue trying to schedule stop.
2962 adjustFocusedActivityLocked(r, "stopActivityFinished");
2963 r.resumeKeyDispatchingLocked();
2964 return;
2965 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002966 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002967 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002968 + " on stop because we're just sleeping");
2969 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002970 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002971 }
2972
2973 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002974 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002975 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002976 try {
2977 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002978 if (DEBUG_STATES) Slog.v(TAG_STATES,
2979 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002980 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002981 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2982 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002983 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002984 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002985 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002986 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002987 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002988 r.setSleeping(true);
2989 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002990 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002991 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002992 } catch (Exception e) {
2993 // Maybe just ignore exceptions here... if the process
2994 // has crashed, our death notification will clean things
2995 // up.
2996 Slog.w(TAG, "Exception thrown during pause", e);
2997 // Just in case, assume it to be stopped.
2998 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002999 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003000 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003001 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003002 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003003 }
3004 }
3005 }
3006 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07003007
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003008 /**
3009 * @return Returns true if the activity is being finished, false if for
3010 * some reason it is being left as-is.
3011 */
3012 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003013 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003014 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003015 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
3016 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07003017 + ", result=" + resultCode + ", data=" + resultData
3018 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003019 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003020 return false;
3021 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003022
Craig Mautnerd44711d2013-02-23 11:24:36 -08003023 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003024 return true;
3025 }
3026
Craig Mautnerd2328952013-03-05 12:46:26 -08003027 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003028 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3029 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3030 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3031 ActivityRecord r = activities.get(activityNdx);
3032 if (r.resultTo == self && r.requestCode == requestCode) {
3033 if ((r.resultWho == null && resultWho == null) ||
3034 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3035 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3036 false);
3037 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003038 }
3039 }
3040 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003041 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003042 }
3043
Todd Kennedy539db512014-12-15 09:57:55 -08003044 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003045 ActivityRecord r = topRunningActivityLocked();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003046 if (r != null && r.app == app) {
3047 // If the top running activity is from this crashing
3048 // process, then terminate it to avoid getting in a loop.
3049 Slog.w(TAG, " Force finishing activity "
3050 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08003051 int taskNdx = mTaskHistory.indexOf(r.task);
3052 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08003053 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003054 // Also terminate any activities below it that aren't yet
3055 // stopped, to avoid a situation where one will get
3056 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08003057 --activityNdx;
3058 if (activityNdx < 0) {
3059 do {
3060 --taskNdx;
3061 if (taskNdx < 0) {
3062 break;
3063 }
3064 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3065 } while (activityNdx < 0);
3066 }
3067 if (activityNdx >= 0) {
3068 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003069 if (r.state == ActivityState.RESUMED
3070 || r.state == ActivityState.PAUSING
3071 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07003072 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003073 Slog.w(TAG, " Force finishing activity "
3074 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08003075 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003076 }
3077 }
3078 }
3079 }
3080 }
3081
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003082 final void finishVoiceTask(IVoiceInteractionSession session) {
3083 IBinder sessionBinder = session.asBinder();
3084 boolean didOne = false;
3085 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3086 TaskRecord tr = mTaskHistory.get(taskNdx);
3087 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3088 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3089 ActivityRecord r = tr.mActivities.get(activityNdx);
3090 if (!r.finishing) {
3091 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3092 false);
3093 didOne = true;
3094 }
3095 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003096 } else {
3097 // Check if any of the activities are using voice
3098 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3099 ActivityRecord r = tr.mActivities.get(activityNdx);
3100 if (r.voiceSession != null
3101 && r.voiceSession.asBinder() == sessionBinder) {
3102 // Inform of cancellation
3103 r.clearVoiceSessionLocked();
3104 try {
3105 r.app.thread.scheduleLocalVoiceInteractionStarted((IBinder) r.appToken,
3106 null);
3107 } catch (RemoteException re) {
3108 // Ok
3109 }
3110 // TODO: VI This is redundant in some cases
3111 mService.finishRunningVoiceLocked();
3112 break;
3113 }
3114 }
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003115 }
3116 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003117 Slog.d(TAG, "ActivityStack.finishVoiceTask()");
3118
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003119 if (didOne) {
3120 mService.updateOomAdjLocked();
3121 }
3122 }
3123
Craig Mautnerd2328952013-03-05 12:46:26 -08003124 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003125 ArrayList<ActivityRecord> activities = r.task.mActivities;
3126 for (int index = activities.indexOf(r); index >= 0; --index) {
3127 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003128 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003129 break;
3130 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003131 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003132 }
3133 return true;
3134 }
3135
Dianne Hackborn5c607432012-02-28 14:44:19 -08003136 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3137 // send the result
3138 ActivityRecord resultTo = r.resultTo;
3139 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003140 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003141 + " who=" + r.resultWho + " req=" + r.requestCode
3142 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003143 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003144 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003145 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003146 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003147 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003148 if (r.info.applicationInfo.uid > 0) {
3149 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3150 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003151 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003152 }
3153 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3154 resultData);
3155 r.resultTo = null;
3156 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003157 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003158
3159 // Make sure this HistoryRecord is not holding on to other resources,
3160 // because clients have remote IPC references to this object so we
3161 // can't assume that will go away and want to avoid circular IPC refs.
3162 r.results = null;
3163 r.pendingResults = null;
3164 r.newIntents = null;
3165 r.icicle = null;
3166 }
3167
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003168 /**
3169 * @return Returns true if this activity has been removed from the history
3170 * list, or false if it is still in the list and will be removed later.
3171 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003172 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3173 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003174 if (r.finishing) {
3175 Slog.w(TAG, "Duplicate finish request for " + r);
3176 return false;
3177 }
3178
Wale Ogunwale7d701172015-03-11 15:36:30 -07003179 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003180 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003181 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003182 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003183 task.taskId, r.shortComponentName, reason);
3184 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003185 final int index = activities.indexOf(r);
3186 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003187 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003188 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003189 // If the caller asked that this activity (and all above it)
3190 // be cleared when the task is reset, don't lose that information,
3191 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003192 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003193 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003194 }
3195 }
3196
3197 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003198
Craig Mautner299f9602015-01-26 09:47:33 -08003199 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003200
Dianne Hackborn5c607432012-02-28 14:44:19 -08003201 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003202
Craig Mautnerde4ef022013-04-07 19:01:33 -07003203 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003204 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003205 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003206 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003207 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003208 ? AppTransition.TRANSIT_TASK_CLOSE
3209 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003210
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003211 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003212 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003213
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003214 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003215 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3216 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3217 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003218 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003219 }
3220
Craig Mautneraea74a52014-03-08 14:23:10 -08003221 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003222 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003223 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003224 } else if (r.state != ActivityState.PAUSING) {
3225 // If the activity is PAUSING, we will complete the finish once
3226 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003227 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003228 if (r.visible) {
3229 mWindowManager.setAppVisibility(r.appToken, false);
3230 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003231 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003232 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003233 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003234 }
3235
3236 return false;
3237 }
3238
Craig Mautnerf3333272013-04-22 10:55:53 -07003239 static final int FINISH_IMMEDIATELY = 0;
3240 static final int FINISH_AFTER_PAUSE = 1;
3241 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003242
Craig Mautnerf3333272013-04-22 10:55:53 -07003243 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003244 // First things first: if this activity is currently visible,
3245 // and the resumed activity is not yet visible, then hold off on
3246 // finishing until the resumed one becomes visible.
3247 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003248 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08003249 addToStopping(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003250 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003251 if (DEBUG_STATES) Slog.v(TAG_STATES,
3252 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003253 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003254 if (oomAdj) {
3255 mService.updateOomAdjLocked();
3256 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003257 return r;
3258 }
3259
3260 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003261 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003262 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003263 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003264 if (mResumedActivity == r) {
3265 mResumedActivity = null;
3266 }
3267 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003268 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003269 r.state = ActivityState.FINISHING;
3270
3271 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003272 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003273 || prevState == ActivityState.STOPPED
3274 || prevState == ActivityState.INITIALIZING) {
3275 // If this activity is already stopped, we can just finish
3276 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003277 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003278 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003279 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003280 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003281 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003282 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003283 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3284 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003285 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003286 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003287
3288 // Need to go through the full pause cycle to get this
3289 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003290 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003291 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003292 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003293 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003294 return r;
3295 }
3296
Craig Mautneree36c772014-07-16 14:56:05 -07003297 void finishAllActivitiesLocked(boolean immediately) {
3298 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003299 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3300 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3301 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3302 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003303 noActivitiesInStack = false;
3304 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003305 continue;
3306 }
Craig Mautneree36c772014-07-16 14:56:05 -07003307 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003308 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3309 }
3310 }
Craig Mautneree36c772014-07-16 14:56:05 -07003311 if (noActivitiesInStack) {
3312 mActivityContainer.onTaskListEmptyLocked();
3313 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003314 }
3315
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003316 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3317 // Basic case: for simple app-centric recents, we need to recreate
3318 // the task if the affinity has changed.
3319 if (srec == null || srec.task.affinity == null ||
3320 !srec.task.affinity.equals(destAffinity)) {
3321 return true;
3322 }
3323 // Document-centric case: an app may be split in to multiple documents;
3324 // they need to re-create their task if this current activity is the root
3325 // of a document, unless simply finishing it will return them to the the
3326 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003327 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3328 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003329 // Okay, this activity is at the root of its task. What to do, what to do...
3330 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3331 // Finishing won't return to an application, so we need to recreate.
3332 return true;
3333 }
3334 // We now need to get the task below it to determine what to do.
3335 int taskIdx = mTaskHistory.indexOf(srec.task);
3336 if (taskIdx <= 0) {
3337 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3338 return false;
3339 }
3340 if (taskIdx == 0) {
3341 // At the bottom of the stack, nothing to go back to.
3342 return true;
3343 }
3344 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3345 if (!srec.task.affinity.equals(prevTask.affinity)) {
3346 // These are different apps, so need to recreate.
3347 return true;
3348 }
3349 }
3350 return false;
3351 }
3352
Wale Ogunwale7d701172015-03-11 15:36:30 -07003353 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003354 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003355 final TaskRecord task = srec.task;
3356 final ArrayList<ActivityRecord> activities = task.mActivities;
3357 final int start = activities.indexOf(srec);
3358 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003359 return false;
3360 }
3361 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003362 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003363 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003364 final ComponentName dest = destIntent.getComponent();
3365 if (start > 0 && dest != null) {
3366 for (int i = finishTo; i >= 0; i--) {
3367 ActivityRecord r = activities.get(i);
3368 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003369 r.info.name.equals(dest.getClassName())) {
3370 finishTo = i;
3371 parent = r;
3372 foundParentInTask = true;
3373 break;
3374 }
3375 }
3376 }
3377
3378 IActivityController controller = mService.mController;
3379 if (controller != null) {
3380 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3381 if (next != null) {
3382 // ask watcher if this is allowed
3383 boolean resumeOK = true;
3384 try {
3385 resumeOK = controller.activityResuming(next.packageName);
3386 } catch (RemoteException e) {
3387 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003388 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003389 }
3390
3391 if (!resumeOK) {
3392 return false;
3393 }
3394 }
3395 }
3396 final long origId = Binder.clearCallingIdentity();
3397 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003398 ActivityRecord r = activities.get(i);
3399 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003400 // Only return the supplied result for the first activity finished
3401 resultCode = Activity.RESULT_CANCELED;
3402 resultData = null;
3403 }
3404
3405 if (parent != null && foundParentInTask) {
3406 final int parentLaunchMode = parent.info.launchMode;
3407 final int destIntentFlags = destIntent.getFlags();
3408 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3409 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3410 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3411 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003412 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3413 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003414 } else {
3415 try {
3416 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3417 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003418 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3419 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3420 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003421 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3422 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003423 foundParentInTask = res == ActivityManager.START_SUCCESS;
3424 } catch (RemoteException e) {
3425 foundParentInTask = false;
3426 }
3427 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003428 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003429 }
3430 }
3431 Binder.restoreCallingIdentity(origId);
3432 return foundParentInTask;
3433 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003434 /**
3435 * Perform the common clean-up of an activity record. This is called both
3436 * as part of destroyActivityLocked() (when destroying the client-side
3437 * representation) and cleaning things up as a result of its hosting
3438 * processing going away, in which case there is no remaining client-side
3439 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003440 *
3441 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003442 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003443 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3444 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003445 if (mResumedActivity == r) {
3446 mResumedActivity = null;
3447 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003448 if (mPausingActivity == r) {
3449 mPausingActivity = null;
3450 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003451 mService.resetFocusedActivityIfNeededLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003452
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003453 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003454 r.frozenBeforeDestroy = false;
3455
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003456 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003457 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003458 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003459 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003460 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003461 }
3462
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003463 // Make sure this record is no longer in the pending finishes list.
3464 // This could happen, for example, if we are trimming activities
3465 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003466 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003467 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003468
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003469 // Remove any pending results.
3470 if (r.finishing && r.pendingResults != null) {
3471 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3472 PendingIntentRecord rec = apr.get();
3473 if (rec != null) {
3474 mService.cancelIntentSenderLocked(rec, false);
3475 }
3476 }
3477 r.pendingResults = null;
3478 }
3479
3480 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003481 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003482 }
3483
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003484 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003485 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003486 if (getVisibleBehindActivity() == r) {
3487 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003488 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003489 }
3490
3491 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003492 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003493 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003494 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003495 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003496 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003497 }
3498
Craig Mautner299f9602015-01-26 09:47:33 -08003499 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003500 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003501 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003502 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003503 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3504 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3505
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003506 r.takeFromHistory();
3507 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003508 if (DEBUG_STATES) Slog.v(TAG_STATES,
3509 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003510 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003511 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003512 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003513 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003514 if (VALIDATE_TOKENS) {
3515 validateAppTokensLocked();
3516 }
Craig Mautner312ba862014-02-10 17:55:01 -08003517 final TaskRecord task = r.task;
3518 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003519 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003520 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003521 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003522 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003523 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003524 }
Craig Mautner299f9602015-01-26 09:47:33 -08003525 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003526 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003527 cleanUpActivityServicesLocked(r);
3528 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003529 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003530
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003531 /**
3532 * Perform clean-up of service connections in an activity record.
3533 */
3534 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3535 // Throw away any services that have been bound by this activity.
3536 if (r.connections != null) {
3537 Iterator<ConnectionRecord> it = r.connections.iterator();
3538 while (it.hasNext()) {
3539 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003540 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003541 }
3542 r.connections = null;
3543 }
3544 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003545
Craig Mautneree2e45a2014-06-27 12:10:03 -07003546 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003547 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003548 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003549 mHandler.sendMessage(msg);
3550 }
3551
Craig Mautneree2e45a2014-06-27 12:10:03 -07003552 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003553 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003554 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003555 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3556 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3557 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3558 final ActivityRecord r = activities.get(activityNdx);
3559 if (r.finishing) {
3560 continue;
3561 }
3562 if (r.fullscreen) {
3563 lastIsOpaque = true;
3564 }
3565 if (owner != null && r.app != owner) {
3566 continue;
3567 }
3568 if (!lastIsOpaque) {
3569 continue;
3570 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003571 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003572 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003573 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003574 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003575 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003576 activityRemoved = true;
3577 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003578 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003579 }
3580 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003581 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003582 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003583 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003584 }
3585
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003586 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3587 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003588 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3589 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003590 + " pausing=" + mPausingActivity + " for reason " + reason);
3591 return destroyActivityLocked(r, true, reason);
3592 }
3593 return false;
3594 }
3595
3596 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3597 String reason) {
3598 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003599 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003600 int maxTasks = tasks.size() / 4;
3601 if (maxTasks < 1) {
3602 maxTasks = 1;
3603 }
3604 int numReleased = 0;
3605 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3606 final TaskRecord task = mTaskHistory.get(taskNdx);
3607 if (!tasks.contains(task)) {
3608 continue;
3609 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003610 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003611 int curNum = 0;
3612 final ArrayList<ActivityRecord> activities = task.mActivities;
3613 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3614 final ActivityRecord activity = activities.get(actNdx);
3615 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003616 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003617 + " in state " + activity.state + " resumed=" + mResumedActivity
3618 + " pausing=" + mPausingActivity + " for reason " + reason);
3619 destroyActivityLocked(activity, true, reason);
3620 if (activities.get(actNdx) != activity) {
3621 // Was removed from list, back up so we don't miss the next one.
3622 actNdx--;
3623 }
3624 curNum++;
3625 }
3626 }
3627 if (curNum > 0) {
3628 numReleased += curNum;
3629 maxTasks--;
3630 if (mTaskHistory.get(taskNdx) != task) {
3631 // The entire task got removed, back up so we don't miss the next one.
3632 taskNdx--;
3633 }
3634 }
3635 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003636 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3637 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003638 return numReleased;
3639 }
3640
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003641 /**
3642 * Destroy the current CLIENT SIDE instance of an activity. This may be
3643 * called both when actually finishing an activity, or when performing
3644 * a configuration switch where we destroy the current client-side object
3645 * but then create a new client-side object for this same HistoryRecord.
3646 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003647 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003648 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3649 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003650 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003651 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003652 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003653 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003654
3655 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003656
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003657 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003658
3659 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003660
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003661 if (hadApp) {
3662 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003663 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003664 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3665 mService.mHeavyWeightProcess = null;
3666 mService.mHandler.sendEmptyMessage(
3667 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3668 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003669 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003670 // Update any services we are bound to that might care about whether
3671 // their client may have activities.
3672 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003673 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003674 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003675 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003676 }
3677 }
3678
3679 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003680
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003681 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003682 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003683 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003684 r.configChangeFlags);
3685 } catch (Exception e) {
3686 // We can just ignore exceptions here... if the process
3687 // has crashed, our death notification will clean things
3688 // up.
3689 //Slog.w(TAG, "Exception thrown during finish", e);
3690 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003691 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003692 removedFromHistory = true;
3693 skipDestroy = true;
3694 }
3695 }
3696
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003697 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003698
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003699 // If the activity is finishing, we need to wait on removing it
3700 // from the list to give it a chance to do its cleanup. During
3701 // that time it may make calls back with its token so we need to
3702 // be able to find it on the list and so we don't want to remove
3703 // it from the list yet. Otherwise, we can just immediately put
3704 // it in the destroyed state since we are not removing it from the
3705 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003706 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003707 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003708 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003709 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003710 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003711 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3712 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003713 if (DEBUG_STATES) Slog.v(TAG_STATES,
3714 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003715 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003716 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003717 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003718 }
3719 } else {
3720 // remove this record from the history.
3721 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003722 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003723 removedFromHistory = true;
3724 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003725 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003726 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003727 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003728 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003729 }
3730 }
3731
3732 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003733
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003734 if (!mLRUActivities.remove(r) && hadApp) {
3735 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3736 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003737
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003738 return removedFromHistory;
3739 }
3740
Craig Mautner299f9602015-01-26 09:47:33 -08003741 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003742 final long origId = Binder.clearCallingIdentity();
3743 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003744 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003745 if (r != null) {
3746 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003747 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003748 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003749
Wale Ogunwale60454db2015-01-23 16:05:07 -08003750 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003751 if (r.state == ActivityState.DESTROYING) {
3752 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003753 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003754 }
3755 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003756 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003757 } finally {
3758 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003759 }
3760 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003761
Todd Kennedyaab56db2015-01-30 09:39:53 -08003762 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003763 if (hasVisibleBehindActivity() &&
3764 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08003765 if (r == topRunningActivityLocked() && getStackVisibilityLocked() == STACK_VISIBLE) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003766 // Don't release the top activity if it has requested to run behind the next
Filip Gruszczynski4e7fe712016-01-13 13:22:44 -08003767 // activity and the stack is currently visible.
Craig Mautner64ccb702014-10-01 09:38:40 -07003768 return;
3769 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003770 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003771 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003772 " thread=" + r.app.thread);
3773 if (r != null && r.app != null && r.app.thread != null) {
3774 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003775 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003776 } catch (RemoteException e) {
3777 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003778 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003779 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003780 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003781 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003782 }
3783 }
3784 }
3785
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003786 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003787 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3788 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003789 if (r != null) {
3790 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003791 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003792 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003793 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003794 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003795 }
3796
Jose Lima4b6c6692014-08-12 17:41:12 -07003797 boolean hasVisibleBehindActivity() {
3798 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003799 }
3800
Jose Lima4b6c6692014-08-12 17:41:12 -07003801 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003802 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003803 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003804 }
3805 }
3806
Jose Lima4b6c6692014-08-12 17:41:12 -07003807 ActivityRecord getVisibleBehindActivity() {
3808 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003809 }
3810
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003811 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3812 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003813 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003814 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3815 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003816 while (i > 0) {
3817 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003818 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003819 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003820 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003821 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003822 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003823 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003824 }
3825 }
3826 }
3827
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003828 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3829 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003830 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3831 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003832 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3833 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003834 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003835 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003836 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3837 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003838
3839 boolean hasVisibleActivities = false;
3840
3841 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003842 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003843 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3844 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003845 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3846 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3847 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3848 final ActivityRecord r = activities.get(activityNdx);
3849 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003850 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3851 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003852 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003853 if (r.visible) {
3854 hasVisibleActivities = true;
3855 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003856 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003857 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3858 // Don't currently have state for the activity, or
3859 // it is finishing -- always remove it.
3860 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003861 } else if (!r.visible && r.launchCount > 2 &&
3862 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003863 // We have launched this activity too many times since it was
3864 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003865 // (Note if the activity is visible, we don't remove the record.
3866 // We leave the dead window on the screen but the process will
3867 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003868 remove = true;
3869 } else {
3870 // The process may be gone, but the activity lives on!
3871 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003872 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003873 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003874 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3875 "Removing activity " + r + " from stack at " + i
3876 + ": haveState=" + r.haveState
3877 + " stateNotNeeded=" + r.stateNotNeeded
3878 + " finishing=" + r.finishing
3879 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003880 if (!r.finishing) {
3881 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3882 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3883 r.userId, System.identityHashCode(r),
3884 r.task.taskId, r.shortComponentName,
3885 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003886 if (r.state == ActivityState.RESUMED) {
3887 mService.updateUsageStats(r, false);
3888 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003889 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003890 } else {
3891 // We have the current state for this activity, so
3892 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003893 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3894 if (DEBUG_APP) Slog.v(TAG_APP,
3895 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003896 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003897 // Set nowVisible to previous visible state. If the app was visible while
3898 // it died, we leave the dead window on screen so it's basically visible.
3899 // This is needed when user later tap on the dead window, we need to stop
3900 // other apps when user transfers focus to the restarted activity.
3901 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003902 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003903 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003904 "App died, clearing saved state of " + r);
3905 r.icicle = null;
3906 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003907 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003908 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003909 if (remove) {
3910 removeActivityFromHistoryLocked(r, "appDied");
3911 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003912 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003913 }
3914 }
3915
3916 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003917 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003918
Chong Zhang280d3322015-11-03 17:27:26 -08003919 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003920 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003921 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003922 if (r != null && r.state != ActivityState.RESUMED) {
3923 r.updateOptionsLocked(options);
3924 } else {
3925 ActivityOptions.abort(options);
3926 }
3927 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003928 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003929 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003930
Craig Mautner21d24a22014-04-23 11:45:37 -07003931 void updateTaskMovement(TaskRecord task, boolean toFront) {
3932 if (task.isPersistable) {
3933 task.mLastTimeMoved = System.currentTimeMillis();
3934 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3935 // recently will be most negative, tasks sent to the bottom before that will be less
3936 // negative. Similarly for recent tasks moved to the top which will be most positive.
3937 if (!toFront) {
3938 task.mLastTimeMoved *= -1;
3939 }
3940 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003941 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003942 }
3943
Craig Mautner84984fa2014-06-19 11:19:20 -07003944 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003945 final int top = mTaskHistory.size() - 1;
3946 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3947 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003948 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003949 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3950 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003951 mTaskHistory.remove(taskNdx);
3952 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003953 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003954 return;
3955 }
3956 }
3957 }
3958
Chong Zhang280d3322015-11-03 17:27:26 -08003959 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003960 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003961 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003962
Craig Mautner11bf9a52013-02-19 14:08:51 -08003963 final int numTasks = mTaskHistory.size();
3964 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003965 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003966 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003967 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003968 ActivityOptions.abort(options);
3969 } else {
3970 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3971 }
3972 return;
3973 }
3974
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003975 if (timeTracker != null) {
3976 // The caller wants a time tracker associated with this task.
3977 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3978 tr.mActivities.get(i).appTimeTracker = timeTracker;
3979 }
3980 }
3981
Craig Mautner11bf9a52013-02-19 14:08:51 -08003982 // Shift all activities with this task up to the top
3983 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003984 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003985
Chong Zhang45c25ce2015-08-10 22:18:26 -07003986 // Don't refocus if invisible to current user
3987 ActivityRecord top = tr.getTopActivity();
3988 if (!okToShowLocked(top)) {
3989 addRecentActivityLocked(top);
3990 ActivityOptions.abort(options);
3991 return;
3992 }
3993
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003994 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003995 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003996 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003997
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003998 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003999 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004000 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004001 if (r != null) {
4002 mNoAnimActivities.add(r);
4003 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07004004 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004005 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08004006 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004007 }
Craig Mautner30e2d722013-02-12 11:30:16 -08004008
Wale Ogunwaled046a012015-12-24 13:05:59 -08004009 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08004010 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08004011
4012 if (VALIDATE_TOKENS) {
4013 validateAppTokensLocked();
4014 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004015 }
4016
4017 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004018 * Worker method for rearranging history stack. Implements the function of moving all
4019 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004020 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004021 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004022 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
4023 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004024 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004025 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004026 * @return Returns true if the move completed, false if not.
4027 */
Craig Mautner299f9602015-01-26 09:47:33 -08004028 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08004029 final TaskRecord tr = taskForIdLocked(taskId);
4030 if (tr == null) {
4031 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
4032 return false;
4033 }
4034
4035 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07004036 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004037
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004038 // If we have a watcher, preflight the move before committing to it. First check
4039 // for *other* available tasks, but if none are available, then try again allowing the
4040 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07004041 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004042 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004043 if (next == null) {
4044 next = topRunningActivityLocked(null, 0);
4045 }
4046 if (next != null) {
4047 // ask watcher if this is allowed
4048 boolean moveOK = true;
4049 try {
4050 moveOK = mService.mController.activityResuming(next.packageName);
4051 } catch (RemoteException e) {
4052 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004053 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004054 }
4055 if (!moveOK) {
4056 return false;
4057 }
4058 }
4059 }
4060
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004061 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004062
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004063 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4064 // For the case where we are moving the home task back and there is an activity visible
4065 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4066 // activity to maintain order with what the user is seeing.
4067 final ActivityStack fullscreenStack =
4068 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4069 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4070 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4071 mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004072 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004073 return true;
4074 }
4075 }
4076
riddle_hsuc215a4f2014-12-27 12:10:45 +08004077 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004078
4079 // If true, we should resume the home activity next if the task we are moving to the
4080 // back is over the home stack. We force to false if the task we are moving to back
4081 // is the home task and we don't want it resumed after moving to the back.
4082 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4083 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004084 final TaskRecord nextTask = getNextTask(tr);
4085 if (nextTask != null) {
4086 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4087 } else {
4088 prevIsHome = true;
4089 }
4090 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004091 mTaskHistory.remove(tr);
4092 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004093 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004094
Craig Mautnerc8143c62013-09-03 12:15:57 -07004095 // There is an assumption that moving a task to the back moves it behind the home activity.
4096 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004097 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004098 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4099 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004100 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004101 break;
4102 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004103 if (taskNdx == 1) {
4104 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004105 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004106 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004107 }
4108
Craig Mautner299f9602015-01-26 09:47:33 -08004109 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004110 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004111
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004112 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004113 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004114 }
4115
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004116 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004117 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004118 if (!mService.mBooting && !mService.mBooted) {
4119 // Not ready yet!
4120 return false;
4121 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004122 final int taskToReturnTo = tr.getTaskToReturnTo();
4123 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004124 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004125 }
4126
Wale Ogunwaled046a012015-12-24 13:05:59 -08004127 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004128 return true;
4129 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004130
Craig Mautner8849a5e2013-04-02 16:41:03 -07004131 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004132 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004133 final Uri data = r.intent.getData();
4134 final String strData = data != null ? data.toSafeString() : null;
4135
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004136 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004137 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004138 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004139 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004140 }
4141
4142 /**
4143 * Make sure the given activity matches the current configuration. Returns
4144 * false if the activity had to be destroyed. Returns true if the
4145 * configuration is the same, or the activity will remain running as-is
4146 * for whatever reason. Ensures the HistoryRecord is updated with the
4147 * correct configuration and all other bookkeeping is handled.
4148 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004149 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4150 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004151 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004152 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004153 "Skipping config check (will change): " + r);
4154 return true;
4155 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004156
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004157 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004158 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004159
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004160 // Short circuit: if the two configurations are the exact same
4161 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004162 final Configuration newConfig = mService.mConfiguration;
4163 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004164 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004165 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004166 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004167 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004168 "Configuration unchanged in " + r);
4169 return true;
4170 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004171
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004172 // We don't worry about activities that are finishing.
4173 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004174 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004175 "Configuration doesn't matter in finishing " + r);
4176 r.stopFreezingScreenLocked(false);
4177 return true;
4178 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004179
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004180 // Okay we now are going to make this activity have the new config.
4181 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004182 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004183 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004184 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004185 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004186
Filip Gruszczynski23493322015-07-29 17:02:59 -07004187 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004188 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004189 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004190 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004191 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004192 // There are no significant differences, so we won't relaunch but should still deliver
4193 // the new configuration to the client process.
4194 r.scheduleConfigurationChanged(taskConfig, false);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004195 return true;
4196 }
4197
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004198 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4199 "Configuration changes for " + r + " ; taskChanges="
4200 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4201 + Configuration.configurationDiffToString(changes));
4202
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004203 // If the activity isn't currently running, just leave the new
4204 // configuration and it will pick that up next time it starts.
4205 if (r.app == null || r.app.thread == null) {
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 doesn't matter not running " + r);
4208 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004209 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004210 return true;
4211 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004212
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004213 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004214 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4215 "Checking to restart " + r.info.name + ": changed=0x"
4216 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004217 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4218 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004219
Dianne Hackborne6676352011-06-01 16:51:20 -07004220 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004221 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4222 r.configChangeFlags |= changes;
4223 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004224 r.forceNewConfig = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004225 preserveWindow &= isResizeOnlyChange(changes);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004226 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004227 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004228 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004229 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004230 } else if (r.state == ActivityState.PAUSING) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004231 // A little annoying: we are waiting for this activity to finish pausing. Let's not
4232 // do anything now, but just flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004233 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004234 "Config is skipping already pausing " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004235 r.deferRelaunchUntilPaused = true;
4236 r.preserveWindowOnDeferredRelaunch = preserveWindow;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004237 return true;
4238 } else if (r.state == ActivityState.RESUMED) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004239 // Try to optimize this case: the configuration is changing and we need to restart
4240 // the top, resumed activity. Instead of doing the normal handshaking, just say
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004241 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004242 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004243 "Config is relaunching resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004244 relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004245 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004246 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004247 "Config is relaunching non-resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004248 relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004249 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004250
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004251 // All done... tell the caller we weren't able to keep this activity around.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004252 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004253 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004254
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004255 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004256 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004257 // changes is always sent to all processes when they happen so it can just use whatever
4258 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004259 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004260 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004261
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004262 return true;
4263 }
4264
Filip Gruszczynski23493322015-07-29 17:02:59 -07004265 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4266 Configuration oldTaskOverride) {
Jorim Jaggia95ca8d2016-01-15 22:54:46 -08004267
4268 // If we went from full-screen to non-full-screen, make sure to use the correct
4269 // configuration task diff, so the diff stays as small as possible.
4270 if (Configuration.EMPTY.equals(oldTaskOverride)
4271 && !Configuration.EMPTY.equals(taskConfig)) {
4272 oldTaskOverride = record.task.extractOverrideConfig(record.configuration);
4273 }
4274
Filip Gruszczynski23493322015-07-29 17:02:59 -07004275 // Determine what has changed. May be nothing, if this is a config
4276 // that has come back from the app after going idle. In that case
4277 // we just want to leave the official config object now in the
4278 // activity and do nothing else.
4279 int taskChanges = oldTaskOverride.diff(taskConfig);
4280 // We don't want to use size changes if they don't cross boundaries that are important to
4281 // the app.
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004282 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004283 final boolean crosses = record.crossesHorizontalSizeThreshold(
4284 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4285 || record.crossesVerticalSizeThreshold(
4286 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4287 if (!crosses) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004288 taskChanges &= ~CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004289 }
4290 }
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004291 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004292 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4293 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004294 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004295 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004296 }
4297 }
4298 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4299 }
4300
4301 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4302 Configuration oldTaskOverride, int taskChanges) {
4303 if (taskChanges == 0) {
4304 // {@link Configuration#diff} doesn't catch changes from unset values.
4305 // Check for changes we care about.
4306 if (oldTaskOverride.orientation != taskConfig.orientation) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004307 taskChanges |= CONFIG_ORIENTATION;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004308 }
4309 // We want to explicitly track situations where the size configuration goes from
4310 // undefined to defined. We don't care about crossing the threshold in that case,
4311 // because there is no threshold.
4312 final int oldHeight = oldTaskOverride.screenHeightDp;
4313 final int newHeight = taskConfig.screenHeightDp;
4314 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4315 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4316 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004317 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004318 }
4319 final int oldWidth = oldTaskOverride.screenWidthDp;
4320 final int newWidth = taskConfig.screenWidthDp;
4321 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4322 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4323 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004324 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004325 }
4326 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4327 final int newSmallest = taskConfig.smallestScreenWidthDp;
4328 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4329 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4330 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004331 taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4332 }
4333 final int oldLayout = oldTaskOverride.screenLayout;
4334 final int newLayout = taskConfig.screenLayout;
4335 if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4336 || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4337 taskChanges |= CONFIG_SCREEN_LAYOUT;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004338 }
4339 }
4340 return taskChanges;
4341 }
4342
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004343 private static boolean isResizeOnlyChange(int change) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004344 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4345 | CONFIG_SCREEN_LAYOUT)) == 0;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004346 }
4347
Wale Ogunwale83301a92015-09-24 15:54:08 -07004348 private void relaunchActivityLocked(
4349 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4350 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004351 r.configChangeFlags = 0;
Wale Ogunwale83301a92015-09-24 15:54:08 -07004352 return;
4353 }
4354
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004355 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004356 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004357 if (andResume) {
4358 results = r.results;
4359 newIntents = r.newIntents;
4360 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004361 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4362 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004363 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004364 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004365 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004366 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004367
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004368 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004369
Craig Mautner34b73df2014-01-12 21:11:08 -08004370 mStackSupervisor.removeChildActivityContainers(r);
4371
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004372 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004373 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4374 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004375 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004376 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4377 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004378 new Configuration(r.task.mOverrideConfig), preserveWindow);
Jorim Jaggife89d122015-12-22 16:28:44 +01004379 mStackSupervisor.activityRelaunchingLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004380 // Note: don't need to call pauseIfSleepingLocked() here, because
4381 // the caller will only pass in 'andResume' if this activity is
4382 // currently resumed, which implies we aren't sleeping.
4383 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004384 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004385 }
4386
4387 if (andResume) {
4388 r.results = null;
4389 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004390 r.state = ActivityState.RESUMED;
4391 } else {
4392 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4393 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004394 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004395
4396 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004397 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004398
4399 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004400 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4401 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4402 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4403 final ActivityRecord r = activities.get(activityNdx);
4404 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004405 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004406 }
4407 if (r.fullscreen && !r.finishing) {
4408 return false;
4409 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004410 }
4411 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004412 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004413 if (r == null) {
4414 return false;
4415 }
4416 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4417 + " would have returned true for r=" + r);
4418 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004419 }
4420
4421 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004422 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4423 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4424 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4425 final ActivityRecord r = activities.get(activityNdx);
4426 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004427 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004428 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004429 }
4430 }
4431 }
4432
Wale Ogunwale540e1232015-05-01 15:35:39 -07004433 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4434 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004435 boolean didSomething = false;
4436 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004437 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004438 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4439 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4440 int numActivities = activities.size();
4441 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4442 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004443 final boolean sameComponent =
4444 (r.packageName.equals(packageName) && (filterByClasses == null
4445 || filterByClasses.contains(r.realActivity.getClassName())))
4446 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004447 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004448 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004449 && (r.app == null || evenPersistent || !r.app.persistent)) {
4450 if (!doit) {
4451 if (r.finishing) {
4452 // If this activity is just finishing, then it is not
4453 // interesting as far as something to stop.
4454 continue;
4455 }
4456 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004457 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004458 if (r.isHomeActivity()) {
4459 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4460 Slog.i(TAG, "Skip force-stop again " + r);
4461 continue;
4462 } else {
4463 homeActivity = r.realActivity;
4464 }
4465 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004466 didSomething = true;
4467 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004468 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004469 if (r.app != null) {
4470 r.app.removed = true;
4471 }
4472 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004473 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004474 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004475 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4476 true)) {
4477 // r has been deleted from mActivities, accommodate.
4478 --numActivities;
4479 --activityNdx;
4480 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004481 }
4482 }
4483 }
4484 return didSomething;
4485 }
4486
Dianne Hackborn09233282014-04-30 11:33:59 -07004487 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004488 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4489 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004490 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004491 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004492 if (task.getTopActivity() == null) {
4493 continue;
4494 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004495 ActivityRecord r = null;
4496 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004497 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004498 int numActivities = 0;
4499 int numRunning = 0;
4500 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004501 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004502 continue;
4503 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004504 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004505 tmp = activities.get(activityNdx);
4506 if (tmp.finishing) {
4507 continue;
4508 }
4509 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004510
Craig Mautneraab647e2013-02-28 16:31:36 -08004511 // Initialize state for next task if needed.
4512 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4513 top = r;
4514 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004515 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004516
4517 // Add 'r' into the current task.
4518 numActivities++;
4519 if (r.app != null && r.app.thread != null) {
4520 numRunning++;
4521 }
4522
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004523 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004524 TAG, r.intent.getComponent().flattenToShortString()
4525 + ": task=" + r.task);
4526 }
4527
4528 RunningTaskInfo ci = new RunningTaskInfo();
4529 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004530 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004531 ci.baseActivity = r.intent.getComponent();
4532 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004533 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004534 if (focusedStack && topTask) {
4535 // Give the latest time to ensure foreground task can be sorted
4536 // at the first, because lastActiveTime of creating task is 0.
4537 ci.lastActiveTime = System.currentTimeMillis();
4538 topTask = false;
4539 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004540
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004541 if (top.task != null) {
4542 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004543 }
4544 ci.numActivities = numActivities;
4545 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004546 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004547 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004548 }
4549
4550 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004551 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004552 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004553 if (top >= 0) {
4554 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4555 int activityTop = activities.size() - 1;
4556 if (activityTop > 0) {
4557 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4558 "unhandled-back", true);
4559 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004560 }
4561 }
4562
Craig Mautner6b74cb52013-09-27 17:02:21 -07004563 /**
4564 * Reset local parameters because an app's activity died.
4565 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004566 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004567 */
4568 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004569 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004570 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004571 "App died while pausing: " + mPausingActivity);
4572 mPausingActivity = null;
4573 }
4574 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4575 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004576 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004577 }
4578
Craig Mautner19091252013-10-05 00:03:53 -07004579 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004580 }
4581
Craig Mautnercae015f2013-02-08 14:31:27 -08004582 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004583 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4584 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4585 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4586 final ActivityRecord r = activities.get(activityNdx);
4587 if (r.app == app) {
4588 Slog.w(TAG, " Force finishing activity "
4589 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004590 // Force the destroy to skip right to removal.
4591 r.app = null;
4592 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004593 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004594 }
4595 }
4596 }
4597
Dianne Hackborn390517b2013-05-30 15:03:32 -07004598 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004599 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004600 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004601 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4602 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004603 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4604 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004605 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004606 " Task id #" + task.taskId + "\n" +
4607 " mFullscreen=" + task.mFullscreen + "\n" +
4608 " mBounds=" + task.mBounds + "\n" +
4609 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004610 if (printed) {
4611 header = null;
4612 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004613 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004614 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004615 }
4616
4617 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4618 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4619
4620 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004621 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4622 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004623 }
4624 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004625 final int top = mTaskHistory.size() - 1;
4626 if (top >= 0) {
4627 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4628 int listTop = list.size() - 1;
4629 if (listTop >= 0) {
4630 activities.add(list.get(listTop));
4631 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004632 }
4633 } else {
4634 ItemMatcher matcher = new ItemMatcher();
4635 matcher.build(name);
4636
Craig Mautneraab647e2013-02-28 16:31:36 -08004637 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4638 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4639 if (matcher.match(r1, r1.intent.getComponent())) {
4640 activities.add(r1);
4641 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004642 }
4643 }
4644 }
4645
4646 return activities;
4647 }
4648
4649 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004650 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004651
4652 // All activities that came from the package must be
4653 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004654 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4655 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4656 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4657 final ActivityRecord a = activities.get(activityNdx);
4658 if (a.info.packageName.equals(packageName)) {
4659 a.forceNewConfig = true;
4660 if (starting != null && a == starting && a.visible) {
4661 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004662 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08004663 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004664 }
4665 }
4666 }
4667
4668 return starting;
4669 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004670
Craig Mautner299f9602015-01-26 09:47:33 -08004671 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004672 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004673 }
4674
Wale Ogunwale000957c2015-04-03 08:19:12 -07004675 /**
4676 * Removes the input task from this stack.
4677 * @param task to remove.
4678 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004679 * @param moving task to another stack. In the case we are moving we don't want to perform
4680 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004681 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004682 void removeTask(TaskRecord task, String reason, boolean moving) {
4683 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004684 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004685 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004686 if (!StackId.persistTaskBounds(mStackId)) {
4687 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4688 // default configuration the next time it launches.
4689 task.updateOverrideConfiguration(null);
4690 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004691 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004692
Craig Mautner04a0ea62014-01-13 12:51:26 -08004693 final ActivityRecord r = mResumedActivity;
4694 if (r != null && r.task == task) {
4695 mResumedActivity = null;
4696 }
4697
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004698 final int taskNdx = mTaskHistory.indexOf(task);
4699 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004700 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4701 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4702 if (!nextTask.isOverHomeStack()) {
4703 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4704 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004705 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004706 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004707 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004708
Wale Ogunwale040b4702015-08-06 18:10:50 -07004709 if (!moving && task.mActivities.isEmpty()) {
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004710 // TODO: VI what about activity?
Craig Mautner41db4a72014-05-07 17:20:56 -07004711 final boolean isVoiceSession = task.voiceSession != null;
4712 if (isVoiceSession) {
4713 try {
4714 task.voiceSession.taskFinished(task.intent, task.taskId);
4715 } catch (RemoteException e) {
4716 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004717 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004718 if (task.autoRemoveFromRecents() || isVoiceSession) {
4719 // Task creator asked to remove this when done, or this task was a voice
4720 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004721 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004722 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004723 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004724 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004725
4726 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004727 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004728 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004729 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004730 String myReason = reason + " leftTaskHistoryEmpty";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08004731 if (mFullscreen || !adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004732 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004733 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004734 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004735 if (mStacks != null) {
4736 mStacks.remove(this);
4737 mStacks.add(0, this);
4738 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004739 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004740 mActivityContainer.onTaskListEmptyLocked();
4741 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004742 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004743
4744 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004745 }
4746
Dianne Hackborn91097de2014-04-04 18:02:06 -07004747 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4748 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4749 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004750 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4751 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004752 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08004753 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08004754 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
4755 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.mResizeable
4756 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004757 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004758 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004759 return task;
4760 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004761
Wale Ogunwale935e5022015-11-10 12:36:10 -08004762 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4763 if (mTaskPositioner == null) {
4764 return false;
4765 }
4766 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4767 return true;
4768 }
4769
Craig Mautnerc00204b2013-03-05 15:02:14 -08004770 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004771 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004772 }
4773
Wale Ogunwale5f986092015-12-04 15:35:38 -08004774 void addTask(final TaskRecord task, final boolean toTop, String reason) {
4775 final ActivityStack prevStack = preAddTask(task, reason);
4776
Craig Mautnerc00204b2013-03-05 15:02:14 -08004777 task.stack = this;
4778 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004779 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004780 } else {
4781 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004782 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004783 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08004784 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004785 }
4786
Wale Ogunwale5f986092015-12-04 15:35:38 -08004787 void positionTask(final TaskRecord task, int position) {
4788 final ActivityStack prevStack = preAddTask(task, "positionTask");
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004789 task.stack = this;
4790 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004791 postAddTask(task, prevStack);
4792 }
4793
4794 private ActivityStack preAddTask(TaskRecord task, String reason) {
4795 final ActivityStack prevStack = task.stack;
4796 if (prevStack != null && prevStack != this) {
4797 prevStack.removeTask(task, reason, MOVING);
4798 }
4799 return prevStack;
4800 }
4801
4802 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
4803 if (prevStack != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004804 task.reportPictureInPictureModeChangeIfNeeded(prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004805 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004806 try {
4807 task.voiceSession.taskStarted(task.intent, task.taskId);
4808 } catch (RemoteException e) {
4809 }
4810 }
4811 }
4812
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004813 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004814 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Amith Yamasani0af6fa72016-01-17 15:36:19 -08004815 // TODO: VI deal with activity
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004816 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4817 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6cae7652015-12-26 07:36:26 -08004818 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
4819 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
4820 !r.isHomeActivity(), r.isAlwaysFocusable());
Chong Zhangb15758a2015-11-17 12:12:03 -08004821 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004822 r.taskConfigOverride = task.mOverrideConfig;
4823 }
4824
Wale Ogunwaled046a012015-12-24 13:05:59 -08004825 void moveToFrontAndResumeStateIfNeeded(
4826 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
4827 if (!moveToFront) {
4828 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07004829 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004830
4831 // If the activity owns the last resumed activity, transfer that together,
4832 // so that we don't resume the same activity again in the new stack.
4833 // Apps may depend on onResume()/onPause() being called in pairs.
4834 if (setResume) {
4835 mResumedActivity = r;
4836 }
4837 // Move the stack in which we are placing the activity to the front. The call will also
4838 // make sure the activity focus is set.
4839 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004840 }
4841
4842 /**
4843 * Moves the input activity from its current stack to this one.
4844 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4845 * created on this stack which the activity is added to.
4846 * */
4847 void moveActivityToStack(ActivityRecord r) {
4848 final ActivityStack prevStack = r.task.stack;
4849 if (prevStack.mStackId == mStackId) {
4850 // You are already in the right stack silly...
4851 return;
4852 }
4853
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004854 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004855 && (mStackSupervisor.topRunningActivityLocked() == r);
4856 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4857
4858 final TaskRecord task = createTaskRecord(
Suprabh Shukla09a88f52015-12-02 14:36:31 -08004859 mStackSupervisor.getNextTaskIdForUserLocked(r.userId),
4860 r.info, r.intent, null, null, true);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004861 r.setTask(task, null);
4862 task.addActivityToTop(r);
4863 setAppTask(r, task);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004864 task.reportPictureInPictureModeChangeIfNeeded(prevStack);
4865 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4866 if (wasResumed) {
4867 prevStack.mResumedActivity = null;
4868 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07004869 }
4870
Wale Ogunwale706ed792015-08-02 10:29:44 -07004871 private void setAppTask(ActivityRecord r, TaskRecord task) {
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004872 final Rect bounds = task.updateOverrideConfigurationFromLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004873 mWindowManager.setAppTask(
Filip Gruszczynskidce2d162016-01-12 15:40:13 -08004874 r.appToken, task.taskId, mStackId, bounds, task.mOverrideConfig);
Chong Zhangb15758a2015-11-17 12:12:03 -08004875 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004876 r.taskConfigOverride = task.mOverrideConfig;
4877 }
4878
Craig Mautnerc00204b2013-03-05 15:02:14 -08004879 public int getStackId() {
4880 return mStackId;
4881 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004882
4883 @Override
4884 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004885 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4886 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004887 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004888
Craig Mautner15df08a2015-04-01 12:17:18 -07004889 void onLockTaskPackagesUpdatedLocked() {
4890 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4891 mTaskHistory.get(taskNdx).setLockTaskAuth();
4892 }
4893 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004894}