blob: 312e309e39507430209ce2020a9ef0b7bc64b3dd [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
169 final ActivityManagerService mService;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700170 final WindowManagerService mWindowManager;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800171 private final RecentTasks mRecentTasks;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800172
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700173 /**
174 * The back history of all previous (and possibly still
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800175 * running) activities. It contains #TaskRecord objects.
176 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800177 private ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800178
179 /**
Dianne Hackbornbe707852011-11-11 14:32:10 -0800180 * Used for validating app tokens with window manager.
181 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800182 final ArrayList<TaskGroup> mValidateAppTokens = new ArrayList<>();
Dianne Hackbornbe707852011-11-11 14:32:10 -0800183
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700184 /**
185 * List of running activities, sorted by recent usage.
186 * The first entry in the list is the least recently used.
187 * It contains HistoryRecord objects.
188 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800189 final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700190
191 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700192 * Animations that for the current transition have requested not to
193 * be considered for the transition animation.
194 */
Wale Ogunwale53a29a92015-02-23 15:42:52 -0800195 final ArrayList<ActivityRecord> mNoAnimActivities = new ArrayList<>();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700196
197 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700198 * When we are in the process of pausing an activity, before starting the
199 * next one, this variable holds the activity that is currently being paused.
200 */
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800201 ActivityRecord mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700202
203 /**
204 * This is the last activity that we put into the paused state. This is
205 * used to determine if we need to do an activity transition while sleeping,
206 * when we normally hold the top activity paused.
207 */
208 ActivityRecord mLastPausedActivity = null;
209
210 /**
Craig Mautner0f922742013-08-06 08:44:42 -0700211 * Activities that specify No History must be removed once the user navigates away from them.
212 * If the device goes to sleep with such an activity in the paused state then we save it here
213 * and finish it later if another activity replaces it on wakeup.
214 */
215 ActivityRecord mLastNoHistoryActivity = null;
216
217 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700218 * Current activity that is resumed, or null if there is none.
219 */
220 ActivityRecord mResumedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800221
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700222 /**
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700223 * This is the last activity that has been started. It is only used to
224 * identify when multiple activities are started at once so that the user
225 * can be warned they may not be in the activity they think they are.
226 */
227 ActivityRecord mLastStartedActivity = null;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800228
Craig Mautner5eda9b32013-07-02 11:58:16 -0700229 // The topmost Activity passed to convertToTranslucent(). When non-null it means we are
230 // waiting for all Activities in mUndrawnActivitiesBelowTopTranslucent to be removed as they
231 // are drawn. When the last member of mUndrawnActivitiesBelowTopTranslucent is removed the
232 // Activity in mTranslucentActivityWaiting is notified via
233 // Activity.onTranslucentConversionComplete(false). If a timeout occurs prior to the last
234 // background activity being drawn then the same call will be made with a true value.
235 ActivityRecord mTranslucentActivityWaiting = null;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700236 private ArrayList<ActivityRecord> mUndrawnActivitiesBelowTopTranslucent = new ArrayList<>();
Craig Mautner5eda9b32013-07-02 11:58:16 -0700237
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700238 /**
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700239 * Set when we know we are going to be calling updateConfiguration()
240 * soon, so want to skip intermediate config checks.
241 */
242 boolean mConfigWillChange;
243
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700244 // Whether or not this stack covers the entire screen; by default stacks are fullscreen
Todd Kennedyaab56db2015-01-30 09:39:53 -0800245 boolean mFullscreen = true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -0700246 // Current bounds of the stack or null if fullscreen.
247 Rect mBounds = null;
Todd Kennedyaab56db2015-01-30 09:39:53 -0800248
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700249 long mLaunchStartTime = 0;
250 long mFullyDrawnStartTime = 0;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800251
Craig Mautner858d8a62013-04-23 17:08:34 -0700252 int mCurrentUser;
Amith Yamasani742a6712011-05-04 14:49:28 -0700253
Craig Mautnerc00204b2013-03-05 15:02:14 -0800254 final int mStackId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800255 final ActivityContainer mActivityContainer;
Craig Mautnere0a38842013-12-16 16:14:02 -0800256 /** The other stacks, in order, on the attached display. Updated at attach/detach time. */
257 ArrayList<ActivityStack> mStacks;
258 /** The attached Display's unique identifier, or -1 if detached */
259 int mDisplayId;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800260
Craig Mautner27084302013-03-25 08:05:25 -0700261 /** Run all ActivityStacks through this */
262 final ActivityStackSupervisor mStackSupervisor;
263
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700264 private final LaunchingTaskPositioner mTaskPositioner;
265
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700266 static final int PAUSE_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 1;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700267 static final int DESTROY_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 2;
268 static final int LAUNCH_TICK_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 3;
269 static final int STOP_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 4;
270 static final int DESTROY_ACTIVITIES_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 5;
Craig Mautner5eda9b32013-07-02 11:58:16 -0700271 static final int TRANSLUCENT_TIMEOUT_MSG = ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 6;
Jose Lima4b6c6692014-08-12 17:41:12 -0700272 static final int RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG =
Craig Mautneree2e45a2014-06-27 12:10:03 -0700273 ActivityManagerService.FIRST_ACTIVITY_STACK_MSG + 7;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700274
275 static class ScheduleDestroyArgs {
276 final ProcessRecord mOwner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700277 final String mReason;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700278 ScheduleDestroyArgs(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700279 mOwner = owner;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700280 mReason = reason;
281 }
282 }
283
Zoran Marcetaf958b322012-08-09 20:27:12 +0900284 final Handler mHandler;
285
286 final class ActivityStackHandler extends Handler {
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800287
Craig Mautnerc8143c62013-09-03 12:15:57 -0700288 ActivityStackHandler(Looper looper) {
Zoran Marcetaf958b322012-08-09 20:27:12 +0900289 super(looper);
290 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700291
Zoran Marcetaf958b322012-08-09 20:27:12 +0900292 @Override
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700293 public void handleMessage(Message msg) {
294 switch (msg.what) {
295 case PAUSE_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800296 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700297 // We don't at this point know if the activity is fullscreen,
298 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800299 Slog.w(TAG, "Activity pause timeout for " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700300 synchronized (mService) {
301 if (r.app != null) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700302 mService.logAppTooSlow(r.app, r.pauseTime, "pausing " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700303 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700304 activityPausedLocked(r.appToken, true);
Craig Mautnerd2328952013-03-05 12:46:26 -0800305 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700306 } break;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700307 case LAUNCH_TICK_MSG: {
308 ActivityRecord r = (ActivityRecord)msg.obj;
309 synchronized (mService) {
310 if (r.continueLaunchTickingLocked()) {
Craig Mautnerf7bfefb2013-05-16 17:30:44 -0700311 mService.logAppTooSlow(r.app, r.launchTickTime, "launching " + r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700312 }
313 }
314 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700315 case DESTROY_TIMEOUT_MSG: {
Dianne Hackbornbe707852011-11-11 14:32:10 -0800316 ActivityRecord r = (ActivityRecord)msg.obj;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700317 // We don't at this point know if the activity is fullscreen,
318 // so we need to be conservative and assume it isn't.
Dianne Hackbornbe707852011-11-11 14:32:10 -0800319 Slog.w(TAG, "Activity destroy timeout for " + r);
Craig Mautnerd2328952013-03-05 12:46:26 -0800320 synchronized (mService) {
Craig Mautner299f9602015-01-26 09:47:33 -0800321 activityDestroyedLocked(r != null ? r.appToken : null, "destroyTimeout");
Craig Mautnerd2328952013-03-05 12:46:26 -0800322 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700323 } break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700324 case STOP_TIMEOUT_MSG: {
325 ActivityRecord r = (ActivityRecord)msg.obj;
326 // We don't at this point know if the activity is fullscreen,
327 // so we need to be conservative and assume it isn't.
328 Slog.w(TAG, "Activity stop timeout for " + r);
329 synchronized (mService) {
330 if (r.isInHistory()) {
331 activityStoppedLocked(r, null, null, null);
332 }
333 }
334 } break;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700335 case DESTROY_ACTIVITIES_MSG: {
336 ScheduleDestroyArgs args = (ScheduleDestroyArgs)msg.obj;
337 synchronized (mService) {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700338 destroyActivitiesLocked(args.mOwner, args.mReason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700339 }
Craig Mautner5eda9b32013-07-02 11:58:16 -0700340 } break;
341 case TRANSLUCENT_TIMEOUT_MSG: {
342 synchronized (mService) {
343 notifyActivityDrawnLocked(null);
344 }
345 } break;
Jose Lima4b6c6692014-08-12 17:41:12 -0700346 case RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG: {
Craig Mautneree2e45a2014-06-27 12:10:03 -0700347 synchronized (mService) {
Jose Lima4b6c6692014-08-12 17:41:12 -0700348 final ActivityRecord r = getVisibleBehindActivity();
349 Slog.e(TAG, "Timeout waiting for cancelVisibleBehind player=" + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -0700350 if (r != null) {
351 mService.killAppAtUsersRequest(r.app, null);
352 }
353 }
354 } break;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700355 }
356 }
Craig Mautner4b71aa12012-12-27 17:20:01 -0800357 }
358
Craig Mautner34b73df2014-01-12 21:11:08 -0800359 int numActivities() {
Craig Mautner000f0022013-02-26 15:04:29 -0800360 int count = 0;
361 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
362 count += mTaskHistory.get(taskNdx).mActivities.size();
363 }
364 return count;
365 }
366
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800367 ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer,
368 RecentTasks recentTasks) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800369 mActivityContainer = activityContainer;
370 mStackSupervisor = activityContainer.getOuter();
371 mService = mStackSupervisor.mService;
372 mHandler = new ActivityStackHandler(mService.mHandler.getLooper());
373 mWindowManager = mService.mWindowManager;
374 mStackId = activityContainer.mStackId;
Fyodor Kupolovf63b89c2015-10-27 18:08:56 -0700375 mCurrentUser = mService.mUserController.getCurrentUserIdLocked();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800376 mRecentTasks = recentTasks;
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700377 mTaskPositioner = mStackId == FREEFORM_WORKSPACE_STACK_ID
378 ? new LaunchingTaskPositioner() : null;
379 }
380
381 void attachDisplay(ActivityStackSupervisor.ActivityDisplay activityDisplay, boolean onTop) {
382 mDisplayId = activityDisplay.mDisplayId;
383 mStacks = activityDisplay.mStacks;
384 mBounds = mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId, onTop);
385 mFullscreen = mBounds == null;
386 if (mTaskPositioner != null) {
387 mTaskPositioner.setDisplay(activityDisplay.mDisplay);
388 mTaskPositioner.configure(mBounds);
389 }
390 }
391
392 void detachDisplay() {
393 mDisplayId = Display.INVALID_DISPLAY;
394 mStacks = null;
395 if (mTaskPositioner != null) {
396 mTaskPositioner.reset();
397 }
398 mWindowManager.detachStack(mStackId);
399 }
400
Winsonc809cbb2015-11-02 12:06:15 -0800401 public void getDisplaySize(Point out) {
402 mActivityContainer.mActivityDisplay.mDisplay.getSize(out);
403 }
404
Filip Gruszczynskie5390e72015-08-18 16:39:00 -0700405 void setBounds(Rect bounds) {
406 mBounds = mFullscreen ? null : new Rect(bounds);
Filip Gruszczynski9ac01a72015-09-04 11:12:17 -0700407 if (mTaskPositioner != null) {
408 mTaskPositioner.configure(bounds);
409 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700410 }
Craig Mautner5962b122012-10-05 14:45:52 -0700411
Amith Yamasani734983f2014-03-04 16:48:05 -0800412 boolean okToShowLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700413 return mStackSupervisor.okToShowLocked(r);
Craig Mautner5962b122012-10-05 14:45:52 -0700414 }
415
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700416 final ActivityRecord topRunningActivityLocked() {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800417 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700418 ActivityRecord r = mTaskHistory.get(taskNdx).topRunningActivityLocked();
Craig Mautner6b74cb52013-09-27 17:02:21 -0700419 if (r != null) {
420 return r;
Craig Mautner11bf9a52013-02-19 14:08:51 -0800421 }
422 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700423 return null;
424 }
425
426 final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800427 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
428 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner11bf9a52013-02-19 14:08:51 -0800429 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd74f7d72013-02-26 13:41:02 -0800430 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
431 ActivityRecord r = activities.get(activityNdx);
Amith Yamasani734983f2014-03-04 16:48:05 -0800432 if (!r.finishing && !r.delayedResume && r != notTop && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800433 return r;
434 }
435 }
436 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700437 return null;
438 }
439
440 /**
441 * This is a simplified version of topRunningActivityLocked that provides a number of
442 * optional skip-over modes. It is intended for use with the ActivityController hook only.
Craig Mautner9658b312013-02-28 10:55:59 -0800443 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700444 * @param token If non-null, any history records matching this token will be skipped.
445 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
Craig Mautner9658b312013-02-28 10:55:59 -0800446 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700447 * @return Returns the HistoryRecord of the next activity on the stack.
448 */
449 final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800450 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
451 TaskRecord task = mTaskHistory.get(taskNdx);
452 if (task.taskId == taskId) {
453 continue;
454 }
455 ArrayList<ActivityRecord> activities = task.mActivities;
456 for (int i = activities.size() - 1; i >= 0; --i) {
457 final ActivityRecord r = activities.get(i);
458 // Note: the taskId check depends on real taskId fields being non-zero
Amith Yamasani734983f2014-03-04 16:48:05 -0800459 if (!r.finishing && (token != r.appToken) && okToShowLocked(r)) {
Craig Mautner11bf9a52013-02-19 14:08:51 -0800460 return r;
461 }
462 }
463 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700464 return null;
465 }
466
Craig Mautner8849a5e2013-04-02 16:41:03 -0700467 final ActivityRecord topActivity() {
Craig Mautner8849a5e2013-04-02 16:41:03 -0700468 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
469 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
Craig Mautner0175b882014-09-07 18:05:31 -0700470 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
471 final ActivityRecord r = activities.get(activityNdx);
472 if (!r.finishing) {
473 return r;
474 }
Craig Mautner8849a5e2013-04-02 16:41:03 -0700475 }
476 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700477 return null;
Craig Mautner8849a5e2013-04-02 16:41:03 -0700478 }
479
Craig Mautner9e14d0f2013-05-01 11:26:09 -0700480 final TaskRecord topTask() {
481 final int size = mTaskHistory.size();
482 if (size > 0) {
483 return mTaskHistory.get(size - 1);
484 }
485 return null;
486 }
487
Craig Mautnerd2328952013-03-05 12:46:26 -0800488 TaskRecord taskForIdLocked(int id) {
489 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
490 final TaskRecord task = mTaskHistory.get(taskNdx);
491 if (task.taskId == id) {
492 return task;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800493 }
494 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700495 return null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700496 }
497
Craig Mautnerd2328952013-03-05 12:46:26 -0800498 ActivityRecord isInStackLocked(IBinder token) {
Wale Ogunwale7d701172015-03-11 15:36:30 -0700499 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Wale Ogunwale60454db2015-01-23 16:05:07 -0800500 return isInStackLocked(r);
501 }
502
503 ActivityRecord isInStackLocked(ActivityRecord r) {
504 if (r == null) {
505 return null;
506 }
507 final TaskRecord task = r.task;
Wale Ogunwale7d701172015-03-11 15:36:30 -0700508 if (task != null && task.stack != null
509 && task.mActivities.contains(r) && mTaskHistory.contains(task)) {
Wale Ogunwale60454db2015-01-23 16:05:07 -0800510 if (task.stack != this) Slog.w(TAG,
Craig Mautnerd2328952013-03-05 12:46:26 -0800511 "Illegal state! task does not point to stack it is in.");
Wale Ogunwale60454db2015-01-23 16:05:07 -0800512 return r;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800513 }
Craig Mautnerd2328952013-03-05 12:46:26 -0800514 return null;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800515 }
516
Craig Mautner2420ead2013-04-01 17:13:20 -0700517 final boolean updateLRUListLocked(ActivityRecord r) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700518 final boolean hadit = mLRUActivities.remove(r);
519 mLRUActivities.add(r);
520 return hadit;
521 }
522
Craig Mautnerde4ef022013-04-07 19:01:33 -0700523 final boolean isHomeStack() {
524 return mStackId == HOME_STACK_ID;
525 }
526
Winson Chung0583d3d2015-12-18 10:03:12 -0500527 final boolean isDockedStack() {
528 return mStackId == DOCKED_STACK_ID;
529 }
530
531 final boolean isPinnedStack() {
532 return mStackId == PINNED_STACK_ID;
533 }
534
Craig Mautnere0a38842013-12-16 16:14:02 -0800535 final boolean isOnHomeDisplay() {
536 return isAttached() &&
537 mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
538 }
539
Wale Ogunwaleeae451e2015-08-04 15:20:50 -0700540 void moveToFront(String reason) {
541 moveToFront(reason, null);
542 }
543
544 /**
545 * @param reason The reason for moving the stack to the front.
546 * @param task If non-null, the task will be moved to the top of the stack.
547 * */
548 void moveToFront(String reason, TaskRecord task) {
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700549 if (!isAttached()) {
550 return;
551 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -0700552
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700553 mStacks.remove(this);
554 int addIndex = mStacks.size();
555
556 if (addIndex > 0) {
557 final ActivityStack topStack = mStacks.get(addIndex - 1);
Filip Gruszczynski114d5ca2015-12-04 09:05:00 -0800558 if (StackId.isAlwaysOnTop(topStack.mStackId) && topStack != this) {
559 // If the top stack is always on top, we move this stack just below it.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700560 addIndex--;
Wale Ogunwalecb82f302015-02-25 07:53:40 -0800561 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700562 }
563
564 mStacks.add(addIndex, this);
565
566 // TODO(multi-display): Needs to also work if focus is moving to the non-home display.
567 if (isOnHomeDisplay()) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800568 mStackSupervisor.setFocusStackUnchecked(reason, this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700569 }
570 if (task != null) {
571 insertTaskAtTop(task, null);
572 } else {
573 task = topTask();
574 }
575 if (task != null) {
576 mWindowManager.moveTaskToTop(task.taskId);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800577 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800578 }
579
Wale Ogunwaled046a012015-12-24 13:05:59 -0800580 boolean isFocusable() {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -0800581 if (StackId.canReceiveKeys(mStackId)) {
582 return true;
583 }
584 // The stack isn't focusable. See if its top activity is focusable to force focus on the
585 // stack.
586 final ActivityRecord r = topRunningActivityLocked();
587 return r != null && r.isFocusable();
Wale Ogunwaled046a012015-12-24 13:05:59 -0800588 }
589
Craig Mautnere0a38842013-12-16 16:14:02 -0800590 final boolean isAttached() {
591 return mStacks != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800592 }
593
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700594 /**
Wale Ogunwale39381972015-12-17 17:15:29 -0800595 * Returns the top activity in any existing task matching the given Intent in the input result.
596 * Returns null if no such task is found.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700597 */
Wale Ogunwale39381972015-12-17 17:15:29 -0800598 void findTaskLocked(ActivityRecord target, FindTaskResult result) {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700599 Intent intent = target.intent;
600 ActivityInfo info = target.info;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700601 ComponentName cls = intent.getComponent();
602 if (info.targetActivity != null) {
603 cls = new ComponentName(info.packageName, info.targetActivity);
604 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700605 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Craig Mautnerd00f4742014-03-12 14:17:26 -0700606 boolean isDocument = intent != null & intent.isDocument();
607 // If documentData is non-null then it must match the existing task data.
608 Uri documentData = isDocument ? intent.getData() : null;
Craig Mautner000f0022013-02-26 15:04:29 -0800609
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700610 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + target + " in " + this);
Craig Mautner000f0022013-02-26 15:04:29 -0800611 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
612 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700613 if (task.voiceSession != null) {
614 // We never match voice sessions; those always run independently.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700615 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": voice session");
Dianne Hackborn91097de2014-04-04 18:02:06 -0700616 continue;
617 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700618 if (task.userId != userId) {
619 // Looking for a different task.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700620 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": different user");
Craig Mautnerac6f8432013-07-17 13:24:59 -0700621 continue;
622 }
Craig Mautner000f0022013-02-26 15:04:29 -0800623 final ActivityRecord r = task.getTopActivity();
624 if (r == null || r.finishing || r.userId != userId ||
625 r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700626 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch root " + r);
Craig Mautner000f0022013-02-26 15:04:29 -0800627 continue;
628 }
Chong Zhangb546f7e2015-08-05 14:21:36 -0700629 if (r.mActivityType != target.mActivityType) {
630 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Skipping " + task + ": mismatch activity type");
631 continue;
632 }
Craig Mautner000f0022013-02-26 15:04:29 -0800633
Craig Mautnerd00f4742014-03-12 14:17:26 -0700634 final Intent taskIntent = task.intent;
635 final Intent affinityIntent = task.affinityIntent;
636 final boolean taskIsDocument;
637 final Uri taskDocumentData;
638 if (taskIntent != null && taskIntent.isDocument()) {
639 taskIsDocument = true;
640 taskDocumentData = taskIntent.getData();
641 } else if (affinityIntent != null && affinityIntent.isDocument()) {
642 taskIsDocument = true;
643 taskDocumentData = affinityIntent.getData();
644 } else {
645 taskIsDocument = false;
646 taskDocumentData = null;
647 }
648
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700649 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Comparing existing cls="
Craig Mautnerd00f4742014-03-12 14:17:26 -0700650 + taskIntent.getComponent().flattenToShortString()
Dianne Hackborn79228822014-09-16 11:11:23 -0700651 + "/aff=" + r.task.rootAffinity + " to new cls="
Dianne Hackborn2a272d42013-10-16 13:34:33 -0700652 + intent.getComponent().flattenToShortString() + "/aff=" + info.taskAffinity);
Wale Ogunwale39381972015-12-17 17:15:29 -0800653 if (!isDocument && !taskIsDocument
654 && result.r == null && task.canMatchRootAffinity()) {
Dianne Hackborn79228822014-09-16 11:11:23 -0700655 if (task.rootAffinity.equals(target.taskAffinity)) {
Wale Ogunwale39381972015-12-17 17:15:29 -0800656 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching affinity candidate!");
657 // It is possible for multiple tasks to have the same root affinity especially
658 // if they are in separate stacks. We save off this candidate, but keep looking
659 // to see if there is a better candidate.
660 result.r = r;
661 result.matchedByRootAffinity = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700662 }
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700663 } else if (taskIntent != null && taskIntent.getComponent() != null &&
664 taskIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700665 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700666 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800667 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700668 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
669 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800670 result.r = r;
671 result.matchedByRootAffinity = false;
672 break;
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700673 } else if (affinityIntent != null && affinityIntent.getComponent() != null &&
674 affinityIntent.getComponent().compareTo(cls) == 0 &&
Craig Mautnerd00f4742014-03-12 14:17:26 -0700675 Objects.equals(documentData, taskDocumentData)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700676 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Found matching class!");
Craig Mautner000f0022013-02-26 15:04:29 -0800677 //dump();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700678 if (DEBUG_TASKS) Slog.d(TAG_TASKS,
679 "For Intent " + intent + " bringing to top: " + r.intent);
Wale Ogunwale39381972015-12-17 17:15:29 -0800680 result.r = r;
681 result.matchedByRootAffinity = false;
682 break;
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700683 } else if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Not a match: " + task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700684 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700685 }
686
687 /**
688 * Returns the first activity (starting from the top of the stack) that
689 * is the same as the given activity. Returns null if no such activity
690 * is found.
691 */
Craig Mautner8849a5e2013-04-02 16:41:03 -0700692 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700693 ComponentName cls = intent.getComponent();
694 if (info.targetActivity != null) {
695 cls = new ComponentName(info.packageName, info.targetActivity);
696 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700697 final int userId = UserHandle.getUserId(info.applicationInfo.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700698
Craig Mautner000f0022013-02-26 15:04:29 -0800699 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700700 final TaskRecord task = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700701 final boolean notCurrentUserTask =
702 !mStackSupervisor.isCurrentProfileLocked(task.userId);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700703 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700704
Craig Mautner000f0022013-02-26 15:04:29 -0800705 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
706 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -0700707 if (notCurrentUserTask && (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0) {
Wale Ogunwale25073dd2015-07-21 16:54:54 -0700708 continue;
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700709 }
Craig Mautner000f0022013-02-26 15:04:29 -0800710 if (!r.finishing && r.intent.getComponent().equals(cls) && r.userId == userId) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700711 return r;
712 }
713 }
714 }
715
716 return null;
717 }
718
Amith Yamasani742a6712011-05-04 14:49:28 -0700719 /*
Craig Mautnerac6f8432013-07-17 13:24:59 -0700720 * Move the activities around in the stack to bring a user to the foreground.
Amith Yamasani742a6712011-05-04 14:49:28 -0700721 */
Craig Mautner93529a42013-10-04 15:03:13 -0700722 final void switchUserLocked(int userId) {
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800723 if (mCurrentUser == userId) {
Craig Mautner93529a42013-10-04 15:03:13 -0700724 return;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800725 }
726 mCurrentUser = userId;
727
728 // Move userId's tasks to the top.
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800729 int index = mTaskHistory.size();
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800730 for (int i = 0; i < index; ) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -0700731 final TaskRecord task = mTaskHistory.get(i);
732
733 // NOTE: If {@link TaskRecord#topRunningActivityLocked} return is not null then it is
734 // okay to show the activity when locked.
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -0700735 if (mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -0700736 || task.topRunningActivityLocked() != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700737 if (DEBUG_TASKS) Slog.d(TAG_TASKS, "switchUserLocked: stack=" + getStackId() +
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700738 " moving " + task + " to top");
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800739 mTaskHistory.remove(i);
740 mTaskHistory.add(task);
741 --index;
Craig Mautnerdb5c4fb2013-11-06 13:55:08 -0800742 // Use same value for i.
743 } else {
744 ++i;
Craig Mautner5d9c7be2013-02-15 14:02:56 -0800745 }
746 }
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700747 if (VALIDATE_TOKENS) {
748 validateAppTokensLocked();
749 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700750 }
751
Craig Mautner2420ead2013-04-01 17:13:20 -0700752 void minimalResumeActivityLocked(ActivityRecord r) {
753 r.state = ActivityState.RESUMED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700754 if (DEBUG_STATES) Slog.v(TAG_STATES,
755 "Moving to RESUMED: " + r + " (starting new instance)");
Craig Mautner2420ead2013-04-01 17:13:20 -0700756 r.stopped = false;
757 mResumedActivity = r;
758 r.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800759 mRecentTasks.addLocked(r.task);
Craig Mautner2420ead2013-04-01 17:13:20 -0700760 completeResumeLocked(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700761 mStackSupervisor.checkReadyForSleepLocked();
Craig Mautner1e8b8722013-10-14 18:24:52 -0700762 setLaunchTime(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700763 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
764 "Launch completed; removing icicle of " + r.icicle);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700765 }
766
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -0800767 void addRecentActivityLocked(ActivityRecord r) {
Chong Zhang45c25ce2015-08-10 22:18:26 -0700768 if (r != null) {
769 mRecentTasks.addLocked(r.task);
770 r.task.touchActiveTime();
771 }
772 }
773
Narayan Kamath7829c812015-06-08 17:39:43 +0100774 private void startLaunchTraces(String packageName) {
Dianne Hackborncee04b52013-07-03 17:01:28 -0700775 if (mFullyDrawnStartTime != 0) {
776 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
777 }
Narayan Kamath7829c812015-06-08 17:39:43 +0100778 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " + packageName, 0);
Dianne Hackborncee04b52013-07-03 17:01:28 -0700779 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
780 }
781
782 private void stopFullyDrawnTraceIfNeeded() {
783 if (mFullyDrawnStartTime != 0 && mLaunchStartTime == 0) {
784 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
785 mFullyDrawnStartTime = 0;
786 }
787 }
788
Craig Mautnere79d42682013-04-01 19:01:53 -0700789 void setLaunchTime(ActivityRecord r) {
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700790 if (r.displayStartTime == 0) {
791 r.fullyDrawnStartTime = r.displayStartTime = SystemClock.uptimeMillis();
792 if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100793 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700794 mLaunchStartTime = mFullyDrawnStartTime = r.displayStartTime;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700795 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700796 } else if (mLaunchStartTime == 0) {
Narayan Kamath7829c812015-06-08 17:39:43 +0100797 startLaunchTraces(r.packageName);
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700798 mLaunchStartTime = mFullyDrawnStartTime = SystemClock.uptimeMillis();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700799 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700800 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800801
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700802 void clearLaunchTime(ActivityRecord r) {
Craig Mautner5c494542013-09-06 11:59:38 -0700803 // Make sure that there is no activity waiting for this to launch.
804 if (mStackSupervisor.mWaitingActivityLaunched.isEmpty()) {
805 r.displayStartTime = r.fullyDrawnStartTime = 0;
806 } else {
807 mStackSupervisor.removeTimeoutsForActivityLocked(r);
808 mStackSupervisor.scheduleIdleTimeoutLocked(r);
809 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700810 }
811
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800812 void awakeFromSleepingLocked() {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800813 // Ensure activities are no longer sleeping.
Craig Mautnerd44711d2013-02-23 11:24:36 -0800814 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
815 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
816 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
817 activities.get(activityNdx).setSleeping(false);
818 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800819 }
Craig Mautnerf49b0a42014-11-20 15:06:40 -0800820 if (mPausingActivity != null) {
821 Slog.d(TAG, "awakeFromSleepingLocked: previously pausing activity didn't pause");
822 activityPausedLocked(mPausingActivity.appToken, true);
823 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800824 }
825
Craig Mautner0eea92c2013-05-16 13:35:39 -0700826 /**
827 * @return true if something must be done before going to sleep.
828 */
829 boolean checkReadyForSleepLocked() {
830 if (mResumedActivity != null) {
831 // Still have something resumed; can't sleep until it is paused.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700832 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
833 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
834 "Sleep => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700835 startPausingLocked(false, true, false, false);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700836 return true;
837 }
838 if (mPausingActivity != null) {
839 // Still waiting for something to pause; can't sleep yet.
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700840 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep still waiting to pause " + mPausingActivity);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700841 return true;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800842 }
843
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700844 if (hasVisibleBehindActivity()) {
845 // Stop visible behind activity before going to sleep.
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800846 final ActivityRecord r = getVisibleBehindActivity();
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700847 mStackSupervisor.mStoppingActivities.add(r);
Wale Ogunwale1f544be2015-12-17 10:27:23 -0800848 if (DEBUG_STATES) Slog.v(TAG_STATES, "Sleep still waiting to stop visible behind " + r);
Wale Ogunwale5c42e502015-04-08 09:18:54 -0700849 return true;
850 }
851
Craig Mautner0eea92c2013-05-16 13:35:39 -0700852 return false;
853 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800854
Craig Mautner0eea92c2013-05-16 13:35:39 -0700855 void goToSleep() {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -0700856 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800857
Craig Mautner0eea92c2013-05-16 13:35:39 -0700858 // Make sure any stopped but visible activities are now sleeping.
859 // This ensures that the activity's onStop() is called.
860 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
861 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
862 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
863 final ActivityRecord r = activities.get(activityNdx);
864 if (r.state == ActivityState.STOPPING || r.state == ActivityState.STOPPED) {
865 r.setSleeping(true);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800866 }
867 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800868 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700869 }
Craig Mautner59c00972012-07-30 12:10:24 -0700870
Winson8b1871d2015-11-20 09:56:20 -0800871 public final Bitmap screenshotActivitiesLocked(ActivityRecord who) {
872 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "screenshotActivitiesLocked: " + who);
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800873 if (who.noDisplay) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700874 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tNo display");
Dianne Hackbornff801ec2011-01-22 18:05:38 -0800875 return null;
876 }
Craig Mautneraab647e2013-02-28 16:31:36 -0800877
Winson Chung083baf92014-07-11 10:32:42 -0700878 if (isHomeStack()) {
Winson Chung376543b2014-05-21 17:43:28 -0700879 // This is an optimization -- since we never show Home or Recents within Recents itself,
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800880 // we can just go ahead and skip taking the screenshot if this is the home stack.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700881 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tHome stack");
Dianne Hackborn4d03fe62013-10-04 17:26:37 -0700882 return null;
883 }
884
Winson Chung48a10a52014-08-27 14:36:51 -0700885 int w = mService.mThumbnailWidth;
886 int h = mService.mThumbnailHeight;
Winson8b1871d2015-11-20 09:56:20 -0800887 float scale = 1f;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800888 if (w > 0) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700889 if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
Winson8b1871d2015-11-20 09:56:20 -0800890
891 // When this flag is set, we currently take the fullscreen screenshot of the activity
Winson387aac62015-11-25 11:18:56 -0800892 // but scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to
893 // use within SystemUI while keeping memory usage low.
Winson Chungead5c0f2015-12-14 11:18:57 -0500894 if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
Winson8b1871d2015-11-20 09:56:20 -0800895 w = h = -1;
Winson387aac62015-11-25 11:18:56 -0800896 scale = 0.5f;
Winson8b1871d2015-11-20 09:56:20 -0800897 }
Wale Ogunwaleeacdf2c2014-12-09 14:02:27 -0800898 return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
Winson8b1871d2015-11-20 09:56:20 -0800899 w, h, scale);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800900 }
Winson Chung376543b2014-05-21 17:43:28 -0700901 Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800902 return null;
903 }
904
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700905 /**
906 * Start pausing the currently resumed activity. It is an error to call this if there
907 * is already an activity being paused or there is no resumed activity.
908 *
909 * @param userLeaving True if this should result in an onUserLeaving to the current activity.
910 * @param uiSleeping True if this is happening with the user interface going to sleep (the
911 * screen turning off).
912 * @param resuming True if this is being called as part of resuming the top activity, so
913 * we shouldn't try to instigate a resume here.
914 * @param dontWait True if the caller does not want to wait for the pause to complete. If
915 * set to true, we will immediately complete the pause here before returning.
916 * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
917 * it to tell us when it is done.
918 */
919 final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
920 boolean dontWait) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800921 if (mPausingActivity != null) {
riddle_hsu7dfe4d72015-02-16 18:43:49 +0800922 Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
923 + " state=" + mPausingActivity.state);
924 if (!mService.isSleeping()) {
925 // Avoid recursion among check for sleep and complete pause during sleeping.
926 // Because activity will be paused immediately after resume, just let pause
927 // be completed by the order of activity paused from clients.
928 completePauseLocked(false);
929 }
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800930 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700931 ActivityRecord prev = mResumedActivity;
932 if (prev == null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700933 if (!resuming) {
934 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
Wale Ogunwaled046a012015-12-24 13:05:59 -0800935 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700936 }
937 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700938 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800939
940 if (mActivityContainer.mParentActivity == null) {
941 // Top level stack, not a child. Look for child stacks.
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700942 mStackSupervisor.pauseChildStacks(prev, userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800943 }
944
Wale Ogunwale0fc365c2015-05-25 19:35:42 -0700945 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSING: " + prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700946 else if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Start pausing: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700947 mResumedActivity = null;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800948 mPausingActivity = prev;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700949 mLastPausedActivity = prev;
Craig Mautner0f922742013-08-06 08:44:42 -0700950 mLastNoHistoryActivity = (prev.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
951 || (prev.info.flags & ActivityInfo.FLAG_NO_HISTORY) != 0 ? prev : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700952 prev.state = ActivityState.PAUSING;
953 prev.task.touchActiveTime();
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700954 clearLaunchTime(prev);
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700955 final ActivityRecord next = mStackSupervisor.topRunningActivityLocked();
Wale Ogunwale979f5ed2015-10-12 11:22:50 -0700956 if (mService.mHasRecents
957 && (next == null || next.noDisplay || next.task != prev.task || uiSleeping)) {
Jorim Jaggic2f262b2015-12-07 16:59:10 -0800958 prev.mUpdateTaskThumbnailWhenHidden = true;
Craig Mautner6f6d56f2013-10-24 16:02:07 -0700959 }
Dianne Hackborncee04b52013-07-03 17:01:28 -0700960 stopFullyDrawnTraceIfNeeded();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700961
962 mService.updateCpuStats();
Craig Mautneraab647e2013-02-28 16:31:36 -0800963
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700964 if (prev.app != null && prev.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700965 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending pause: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700966 try {
967 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700968 prev.userId, System.identityHashCode(prev),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700969 prev.shortComponentName);
Jeff Sharkey5782da72013-04-25 14:32:30 -0700970 mService.updateUsageStats(prev, false);
Dianne Hackbornbe707852011-11-11 14:32:10 -0800971 prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700972 userLeaving, prev.configChangeFlags, dontWait);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700973 } catch (Exception e) {
974 // Ignore exception, if process died other code will cleanup.
975 Slog.w(TAG, "Exception thrown during pause", e);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800976 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700977 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700978 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700979 }
980 } else {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800981 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700982 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -0700983 mLastNoHistoryActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700984 }
985
986 // If we are not going to sleep, we want to ensure the device is
987 // awake until the next activity is started.
Dianne Hackborn3d07c942015-03-13 18:02:54 -0700988 if (!uiSleeping && !mService.isSleepingOrShuttingDown()) {
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700989 mStackSupervisor.acquireLaunchWakelock();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700990 }
991
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800992 if (mPausingActivity != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700993 // Have the window manager pause its key dispatching until the new
994 // activity has started. If we're pausing the activity just because
995 // the screen is being turned off and the UI is sleeping, don't interrupt
996 // key dispatch; the same activity will pick it up again on wakeup.
997 if (!uiSleeping) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -0800998 prev.pauseKeyDispatchingLocked();
Wale Ogunwaleee006da2015-03-30 14:49:25 -0700999 } else if (DEBUG_PAUSE) {
1000 Slog.v(TAG_PAUSE, "Key dispatch not paused for screen off");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001001 }
1002
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001003 if (dontWait) {
1004 // If the caller said they don't want to wait for the pause, then complete
1005 // the pause now.
1006 completePauseLocked(false);
1007 return false;
1008
1009 } else {
1010 // Schedule a pause timeout in case the app doesn't respond.
1011 // We don't give it much time because this directly impacts the
1012 // responsiveness seen by the user.
1013 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
1014 msg.obj = prev;
1015 prev.pauseTime = SystemClock.uptimeMillis();
1016 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001017 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Waiting for pause to complete...");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001018 return true;
1019 }
1020
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001021 } else {
1022 // This activity failed to schedule the
1023 // pause, so just treat it as being paused now.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001024 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001025 if (!resuming) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001026 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001027 }
1028 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001029 }
1030 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001031
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001032 final void activityPausedLocked(IBinder token, boolean timeout) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001033 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1034 "Activity paused: token=" + token + ", timeout=" + timeout);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001035
Craig Mautnerd2328952013-03-05 12:46:26 -08001036 final ActivityRecord r = isInStackLocked(token);
1037 if (r != null) {
1038 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
1039 if (mPausingActivity == r) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001040 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
Craig Mautnerd2328952013-03-05 12:46:26 -08001041 + (timeout ? " (due to timeout)" : " (pause complete)"));
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001042 completePauseLocked(true);
Craig Mautnerd2328952013-03-05 12:46:26 -08001043 } else {
1044 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
1045 r.userId, System.identityHashCode(r), r.shortComponentName,
1046 mPausingActivity != null
1047 ? mPausingActivity.shortComponentName : "(none)");
riddle_hsu9caeef72015-10-20 16:34:05 +08001048 if (r.state == ActivityState.PAUSING) {
1049 r.state = ActivityState.PAUSED;
1050 if (r.finishing) {
1051 if (DEBUG_PAUSE) Slog.v(TAG,
1052 "Executing finish of failed to pause activity: " + r);
1053 finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false);
1054 }
louis_chang047dfd42015-04-08 16:35:55 +08001055 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001056 }
1057 }
1058 }
1059
Craig Mautnera0026042014-04-23 11:45:37 -07001060 final void activityStoppedLocked(ActivityRecord r, Bundle icicle,
1061 PersistableBundle persistentState, CharSequence description) {
Dianne Hackbornb61a0262012-05-14 17:19:18 -07001062 if (r.state != ActivityState.STOPPING) {
1063 Slog.i(TAG, "Activity reported stop, but no longer stopping: " + r);
1064 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1065 return;
1066 }
Craig Mautner21d24a22014-04-23 11:45:37 -07001067 if (persistentState != null) {
1068 r.persistentState = persistentState;
1069 mService.notifyTaskPersisterLocked(r.task, false);
1070 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001071 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE, "Saving icicle of " + r + ": " + icicle);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001072 if (icicle != null) {
1073 // If icicle is null, this is happening due to a timeout, so we
1074 // haven't really saved the state.
1075 r.icicle = icicle;
1076 r.haveState = true;
Dianne Hackborn07981492013-01-28 11:36:23 -08001077 r.launchCount = 0;
Winson Chung740c3ac2014-11-12 16:14:38 -08001078 r.updateThumbnailLocked(null, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001079 }
1080 if (!r.stopped) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001081 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to STOPPED: " + r + " (stop complete)");
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001082 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
1083 r.stopped = true;
1084 r.state = ActivityState.STOPPED;
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001085 if (getVisibleBehindActivity() == r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001086 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001087 }
Dianne Hackborn6e3d6da2012-06-15 12:05:27 -07001088 if (r.finishing) {
1089 r.clearOptionsLocked();
1090 } else {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001091 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07001092 destroyActivityLocked(r, true, "stop-config");
Wale Ogunwaled046a012015-12-24 13:05:59 -08001093 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07001094 } else {
Dianne Hackborna413dc02013-07-12 12:02:55 -07001095 mStackSupervisor.updatePreviousProcessLocked(r);
Dianne Hackborn50685602011-12-01 12:23:37 -08001096 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001097 }
1098 }
1099 }
1100
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001101 private void completePauseLocked(boolean resumeNext) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001102 ActivityRecord prev = mPausingActivity;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001103 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
Craig Mautneraab647e2013-02-28 16:31:36 -08001104
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001105 if (prev != null) {
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001106 final boolean wasStopping = prev.state == ActivityState.STOPPING;
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001107 prev.state = ActivityState.PAUSED;
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001108 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001109 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Executing finish of activity: " + prev);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07001110 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001111 } else if (prev.app != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001112 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Enqueueing pending stop: " + prev);
Craig Mautner8c14c152015-01-15 17:32:07 -08001113 if (mStackSupervisor.mWaitingVisibleActivities.remove(prev)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001114 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(TAG_PAUSE,
1115 "Complete pause, no longer waiting: " + prev);
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001116 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08001117 if (prev.deferRelaunchUntilPaused) {
1118 // Complete the deferred relaunch that was waiting for pause to complete.
1119 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Re-launching after pause: " + prev);
1120 relaunchActivityLocked(prev, prev.configChangeFlags, false,
1121 prev.preserveWindowOnDeferredRelaunch);
Filip Gruszczynskidba623a2015-12-04 15:45:35 -08001122 } else if (wasStopping) {
1123 // We are also stopping, the stop request must have gone soon after the pause.
1124 // We can't clobber it, because the stop confirmation will not be handled.
1125 // We don't need to schedule another stop, we only need to let it happen.
1126 prev.state = ActivityState.STOPPING;
Wale Ogunwale5c42e502015-04-08 09:18:54 -07001127 } else if (!hasVisibleBehindActivity() || mService.isSleepingOrShuttingDown()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07001128 // If we were visible then resumeTopActivities will release resources before
Craig Mautneree2e45a2014-06-27 12:10:03 -07001129 // stopping.
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001130 addToStopping(prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001131 }
1132 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001133 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "App died during pause, not stopping: " + prev);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001134 prev = null;
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001135 }
Wale Ogunwale07927bf2015-03-28 17:21:05 -07001136 // It is possible the activity was freezing the screen before it was paused.
1137 // In that case go ahead and remove the freeze this activity has on the screen
1138 // since it is no longer visible.
1139 prev.stopFreezingScreenLocked(true /*force*/);
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001140 mPausingActivity = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001141 }
Dianne Hackborncbb722e2012-02-07 18:33:49 -08001142
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001143 if (resumeNext) {
1144 final ActivityStack topStack = mStackSupervisor.getFocusedStack();
1145 if (!mService.isSleepingOrShuttingDown()) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001146 mStackSupervisor.resumeFocusedStackTopActivityLocked(topStack, prev, null);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001147 } else {
1148 mStackSupervisor.checkReadyForSleepLocked();
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001149 ActivityRecord top = topStack.topRunningActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001150 if (top == null || (prev != null && top != prev)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001151 // If there are no more activities available to run, do resume anyway to start
1152 // something. Also if the top activity on the stack is not the just paused
1153 // activity, we need to go ahead and resume it to ensure we complete an
1154 // in-flight app switch.
1155 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001156 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07001157 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001158 }
Craig Mautneraab647e2013-02-28 16:31:36 -08001159
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001160 if (prev != null) {
1161 prev.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001162
Craig Mautner525f3d92013-05-07 14:01:50 -07001163 if (prev.app != null && prev.cpuTimeAtResume > 0
1164 && mService.mBatteryStatsService.isOnBattery()) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001165 long diff = mService.mProcessCpuTracker.getCpuTimeForPid(prev.app.pid)
1166 - prev.cpuTimeAtResume;
Craig Mautner525f3d92013-05-07 14:01:50 -07001167 if (diff > 0) {
1168 BatteryStatsImpl bsi = mService.mBatteryStatsService.getActiveStatistics();
1169 synchronized (bsi) {
1170 BatteryStatsImpl.Uid.Proc ps =
1171 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
Dianne Hackbornd2932242013-08-05 18:18:42 -07001172 prev.info.packageName);
Craig Mautner525f3d92013-05-07 14:01:50 -07001173 if (ps != null) {
1174 ps.addForegroundTimeLocked(diff);
1175 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001176 }
1177 }
1178 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001179 prev.cpuTimeAtResume = 0; // reset it
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001180 }
Winson Chung740c3ac2014-11-12 16:14:38 -08001181
1182 // Notfiy when the task stack has changed
1183 mService.notifyTaskStackChangedLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001184 }
1185
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08001186 private void addToStopping(ActivityRecord r) {
1187 mStackSupervisor.mStoppingActivities.add(r);
1188 if (mStackSupervisor.mStoppingActivities.size() > MAX_STOPPING_TO_FORCE ||
1189 r.frontOfTask && mTaskHistory.size() <= 1) {
1190 // If we already have a few activities waiting to stop,
1191 // then give up on things going idle and start clearing
1192 // them out. Or if r is the last of activity of the last task the stack
1193 // will be empty and must be cleared immediately.
1194 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "To many pending stops, forcing idle");
1195 mStackSupervisor.scheduleIdleLocked();
1196 } else {
1197 mStackSupervisor.checkReadyForSleepLocked();
1198 }
1199 }
1200
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001201 /**
1202 * Once we know that we have asked an application to put an activity in
1203 * the resumed state (either by launching it or explicitly telling it),
1204 * this function updates the rest of our state to match that fact.
1205 */
Craig Mautner525f3d92013-05-07 14:01:50 -07001206 private void completeResumeLocked(ActivityRecord next) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001207 next.idle = false;
1208 next.results = null;
1209 next.newIntents = null;
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001210
Chong Zhang85ee6542015-10-02 13:36:38 -07001211 if (next.isHomeActivity()) {
Craig Mautnerf33f4d72014-07-16 17:25:48 +00001212 ProcessRecord app = next.task.mActivities.get(0).app;
1213 if (app != null && app != mService.mHomeProcess) {
1214 mService.mHomeProcess = app;
1215 }
1216 }
1217
Craig Mautner07566322013-09-26 16:42:55 -07001218 if (next.nowVisible) {
1219 // We won't get a call to reportActivityVisibleLocked() so dismiss lockscreen now.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001220 mStackSupervisor.notifyActivityDrawnForKeyguard();
Craig Mautner07566322013-09-26 16:42:55 -07001221 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001222
1223 // schedule an idle timeout in case the app doesn't do it for us.
Craig Mautnerf3333272013-04-22 10:55:53 -07001224 mStackSupervisor.scheduleIdleTimeoutLocked(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001225
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001226 mStackSupervisor.reportResumedActivityLocked(next);
1227
1228 next.resumeKeyDispatchingLocked();
1229 mNoAnimActivities.clear();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001230
1231 // Mark the point when the activity is resuming
1232 // TODO: To be more accurate, the mark should be before the onCreate,
1233 // not after the onResume. But for subsequent starts, onResume is fine.
1234 if (next.app != null) {
Dianne Hackborn652973f2014-09-10 17:08:48 -07001235 next.cpuTimeAtResume = mService.mProcessCpuTracker.getCpuTimeForPid(next.app.pid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001236 } else {
1237 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
1238 }
Winson Chung376543b2014-05-21 17:43:28 -07001239
George Mount6ba042b2014-07-28 11:12:28 -07001240 next.returningOptions = null;
Craig Mautner64ccb702014-10-01 09:38:40 -07001241
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001242 if (getVisibleBehindActivity() == next) {
Craig Mautner64ccb702014-10-01 09:38:40 -07001243 // When resuming an activity, require it to call requestVisibleBehind() again.
Wale Ogunwale1f544be2015-12-17 10:27:23 -08001244 setVisibleBehindActivity(null);
Craig Mautner64ccb702014-10-01 09:38:40 -07001245 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001246 }
1247
Craig Mautner2568c3a2015-03-26 14:22:34 -07001248 private void setVisible(ActivityRecord r, boolean visible) {
Craig Mautnere3a00d72014-04-16 08:31:19 -07001249 r.visible = visible;
Jorim Jaggic2f262b2015-12-07 16:59:10 -08001250 if (!visible && r.mUpdateTaskThumbnailWhenHidden) {
1251 r.updateThumbnailLocked(r.task.stack.screenshotActivitiesLocked(r), null);
1252 r.mUpdateTaskThumbnailWhenHidden = false;
1253 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07001254 mWindowManager.setAppVisibility(r.appToken, visible);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001255 final ArrayList<ActivityContainer> containers = r.mChildContainers;
Craig Mautnere3a00d72014-04-16 08:31:19 -07001256 for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001257 ActivityContainer container = containers.get(containerNdx);
Craig Mautnere3a00d72014-04-16 08:31:19 -07001258 container.setVisible(visible);
1259 }
1260 }
1261
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001262 // Find the first visible activity above the passed activity and if it is translucent return it
1263 // otherwise return null;
1264 ActivityRecord findNextTranslucentActivity(ActivityRecord r) {
1265 TaskRecord task = r.task;
1266 if (task == null) {
1267 return null;
1268 }
1269
1270 ActivityStack stack = task.stack;
1271 if (stack == null) {
1272 return null;
1273 }
1274
1275 int stackNdx = mStacks.indexOf(stack);
1276
1277 ArrayList<TaskRecord> tasks = stack.mTaskHistory;
1278 int taskNdx = tasks.indexOf(task);
1279
1280 ArrayList<ActivityRecord> activities = task.mActivities;
1281 int activityNdx = activities.indexOf(r) + 1;
1282
1283 final int numStacks = mStacks.size();
1284 while (stackNdx < numStacks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001285 final ActivityStack historyStack = mStacks.get(stackNdx);
Todd Kennedyaab56db2015-01-30 09:39:53 -08001286 tasks = historyStack.mTaskHistory;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001287 final int numTasks = tasks.size();
1288 while (taskNdx < numTasks) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001289 final TaskRecord currentTask = tasks.get(taskNdx);
1290 activities = currentTask.mActivities;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001291 final int numActivities = activities.size();
1292 while (activityNdx < numActivities) {
1293 final ActivityRecord activity = activities.get(activityNdx);
1294 if (!activity.finishing) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001295 return historyStack.mFullscreen
1296 && currentTask.mFullscreen && activity.fullscreen ? null : activity;
Craig Mautnerfa387ad2014-08-05 11:16:41 -07001297 }
1298 ++activityNdx;
1299 }
1300 activityNdx = 0;
1301 ++taskNdx;
1302 }
1303 taskNdx = 0;
1304 ++stackNdx;
1305 }
1306
1307 return null;
1308 }
1309
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001310 ActivityStack getNextFocusableStackLocked() {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001311 ArrayList<ActivityStack> stacks = mStacks;
1312 final ActivityRecord parent = mActivityContainer.mParentActivity;
1313 if (parent != null) {
1314 stacks = parent.task.stack.mStacks;
1315 }
1316 if (stacks != null) {
1317 for (int i = stacks.size() - 1; i >= 0; --i) {
1318 ActivityStack stack = stacks.get(i);
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001319 if (stack != this && stack.isFocusable() && stack.isStackVisibleLocked()) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001320 return stack;
1321 }
1322 }
1323 }
1324 return null;
1325 }
1326
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001327 /** Returns true if the stack contains a fullscreen task. */
1328 private boolean hasFullscreenTask() {
1329 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
1330 final TaskRecord task = mTaskHistory.get(i);
1331 if (task.mFullscreen) {
1332 return true;
1333 }
1334 }
1335 return false;
1336 }
1337
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001338 private boolean hasTranslucentActivity(ActivityStack stack) {
1339 final ArrayList<TaskRecord> tasks = stack.getAllTasks();
1340 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
1341 final TaskRecord task = tasks.get(taskNdx);
1342 final ArrayList<ActivityRecord> activities = task.mActivities;
1343 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1344 final ActivityRecord r = activities.get(activityNdx);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001345
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001346 // Conditions for an activity to obscure the stack we're
1347 // examining:
1348 // 1. Not Finishing AND Visible AND:
1349 // 2. Either:
1350 // - Full Screen Activity OR
1351 // - On top of Home and our stack is NOT home
1352 if (!r.finishing && r.visible && (r.fullscreen ||
1353 (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()))) {
1354 return false;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001355 }
1356 }
1357 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001358 return true;
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001359 }
1360
1361 /** Returns true if the stack is considered visible. */
Filip Gruszczynski77d94482015-12-11 13:59:52 -08001362 boolean isStackVisibleLocked() {
Jose Lima7ba71252014-04-30 12:59:27 -07001363 if (!isAttached()) {
1364 return false;
1365 }
1366
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001367 if (mStackSupervisor.isFrontStack(this) || mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwale99db1862015-10-23 20:08:22 -07001368 return true;
1369 }
1370
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001371 final int stackIndex = mStacks.indexOf(this);
1372
1373 if (stackIndex == mStacks.size() - 1) {
1374 Slog.wtf(TAG,
1375 "Stack=" + this + " isn't front stack but is at the top of the stack list");
1376 return false;
1377 }
1378
Chong Zhang75b37202015-12-04 14:16:36 -08001379 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
1380 if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
1381 return false;
1382 }
1383
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001384 final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
1385 final int focusedStackId = focusedStack.mStackId;
1386
Wale Ogunwalecff4aa32015-12-11 10:44:25 -08001387 if (mStackId == FULLSCREEN_WORKSPACE_STACK_ID
1388 && hasVisibleBehindActivity() && focusedStackId == HOME_STACK_ID) {
1389 // The fullscreen stack should be visible if it has a visible behind activity behind
1390 // the home stack that will be translucent.
1391 return true;
1392 }
1393
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001394 if (mStackId == DOCKED_STACK_ID) {
1395 // Docked stack is always visible, except in the case where the home activity
1396 // is the top running activity in the focused home stack.
1397 if (focusedStackId != HOME_STACK_ID) {
1398 return true;
1399 }
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001400 ActivityRecord topHomeActivity = focusedStack.topRunningActivityLocked();
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001401 return topHomeActivity == null || !topHomeActivity.isHomeActivity();
1402 }
1403
Chong Zhangb16cf342015-11-12 17:22:40 -08001404 // Find the first stack below focused stack that actually got something visible.
1405 int belowFocusedIndex = mStacks.indexOf(focusedStack) - 1;
1406 while (belowFocusedIndex >= 0 &&
1407 mStacks.get(belowFocusedIndex).topRunningActivityLocked() == null) {
1408 belowFocusedIndex--;
1409 }
Wale Ogunwale99db1862015-10-23 20:08:22 -07001410 if ((focusedStackId == DOCKED_STACK_ID || focusedStackId == PINNED_STACK_ID)
1411 && stackIndex == belowFocusedIndex) {
1412 // Stacks directly behind the docked or pinned stack are always visible.
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001413 return true;
1414 }
1415
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001416 if (focusedStackId == FULLSCREEN_WORKSPACE_STACK_ID
1417 && hasTranslucentActivity(focusedStack)) {
1418 // Stacks behind the fullscreen stack with a translucent activity are always
1419 // visible so they can act as a backdrop to the translucent activity.
1420 // For example, dialog activities
1421 if (stackIndex == belowFocusedIndex) {
1422 return true;
1423 }
1424 if (belowFocusedIndex >= 0) {
1425 final ActivityStack stack = mStacks.get(belowFocusedIndex);
Wale Ogunwale99db1862015-10-23 20:08:22 -07001426 if ((stack.mStackId == DOCKED_STACK_ID || stack.mStackId == PINNED_STACK_ID)
1427 && stackIndex == (belowFocusedIndex - 1)) {
1428 // The stack behind the docked or pinned stack is also visible so we can have a
1429 // complete backdrop to the translucent activity when the docked stack is up.
Wale Ogunwalebd26d2a2015-09-22 22:08:37 -07001430 return true;
1431 }
1432 }
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001433 }
1434
Wale Ogunwale3797c222015-10-27 14:21:58 -07001435 if (StackId.isStaticStack(mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001436 // Visibility of any static stack should have been determined by the conditions above.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001437 return false;
1438 }
1439
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001440 for (int i = stackIndex + 1; i < mStacks.size(); i++) {
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001441 final ActivityStack stack = mStacks.get(i);
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07001442
1443 if (!stack.mFullscreen && !stack.hasFullscreenTask()) {
1444 continue;
1445 }
1446
Wale Ogunwale3797c222015-10-27 14:21:58 -07001447 if (!StackId.isDynamicStacksVisibleBehindAllowed(stack.mStackId)) {
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001448 // These stacks can't have any dynamic stacks visible behind them.
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001449 return false;
Todd Kennedyaab56db2015-01-30 09:39:53 -08001450 }
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07001451
Wale Ogunwale1e3523c2015-09-16 13:11:10 -07001452 if (!hasTranslucentActivity(stack)) {
1453 return false;
Jose Lima7ba71252014-04-30 12:59:27 -07001454 }
1455 }
1456
1457 return true;
1458 }
1459
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001460 final int rankTaskLayers(int baseLayer) {
1461 int layer = 0;
1462 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1463 final TaskRecord task = mTaskHistory.get(taskNdx);
1464 ActivityRecord r = task.topRunningActivityLocked();
1465 if (r == null || r.finishing || !r.visible) {
1466 task.mLayerRank = -1;
1467 } else {
1468 task.mLayerRank = baseLayer + layer++;
1469 }
1470 }
1471 return layer;
1472 }
1473
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001474 /**
1475 * Make sure that all activities that need to be visible (that is, they
1476 * currently can be seen by the user) actually are.
1477 */
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001478 final void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
1479 boolean preserveWindows) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001480 ActivityRecord top = topRunningActivityLocked();
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001481 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + top
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001482 + " configChanges=0x" + Integer.toHexString(configChanges));
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001483 if (top != null) {
1484 checkTranslucentActivityWaiting(top);
1485 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001486
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001487 // If the top activity is not fullscreen, then we need to
1488 // make sure any activities under it are now visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001489 boolean aboveTop = top != null;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001490 final boolean stackInvisible = !isStackVisibleLocked();
1491 boolean behindFullscreenActivity = stackInvisible;
Wale Ogunwaled046a012015-12-24 13:05:59 -08001492 boolean resumeNextActivity = isFocusable() && (isInStackLocked(starting) == null);
Jose Lima7ba71252014-04-30 12:59:27 -07001493
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001494 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07001495 final TaskRecord task = mTaskHistory.get(taskNdx);
1496 final ArrayList<ActivityRecord> activities = task.mActivities;
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001497
Craig Mautnerd44711d2013-02-23 11:24:36 -08001498 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1499 final ActivityRecord r = activities.get(activityNdx);
1500 if (r.finishing) {
1501 continue;
1502 }
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001503 final boolean isTop = r == top;
1504 if (aboveTop && !isTop) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08001505 continue;
1506 }
1507 aboveTop = false;
Craig Mautnerbb742462014-07-07 15:28:55 -07001508 // mLaunchingBehind: Activities launching behind are at the back of the task stack
1509 // but must be drawn initially for the animation as though they were visible.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001510 if ((!behindFullscreenActivity || r.mLaunchTaskBehind)
1511 && okToShowLocked(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001512 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1513 "Make visible? " + r + " finishing=" + r.finishing
Craig Mautnerd44711d2013-02-23 11:24:36 -08001514 + " state=" + r.state);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001515 // First: if this is not the current activity being started, make
1516 // sure it matches the current configuration.
Craig Mautnerbb742462014-07-07 15:28:55 -07001517 if (r != starting) {
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07001518 ensureActivityConfigurationLocked(r, 0, preserveWindows);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001519 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001520
1521 if (r.app == null || r.app.thread == null) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001522 if (makeVisibleAndRestartIfNeeded(starting, configChanges, isTop,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001523 resumeNextActivity, r)) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001524 if (activityNdx >= activities.size()) {
1525 // Record may be removed if its process needs to restart.
1526 activityNdx = activities.size() - 1;
1527 } else {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001528 resumeNextActivity = false;
riddle_hsu36ee73d2015-06-05 16:38:38 +08001529 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001530 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001531 } else if (r.visible) {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001532 // If this activity is already visible, then there is nothing to do here.
1533 if (handleAlreadyVisible(r)) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001534 resumeNextActivity = false;
Wale Ogunwale85b90ab2015-04-27 20:54:47 -07001535 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001536 } else {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001537 makeVisible(starting, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001538 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08001539 // Aggregate current change flags.
1540 configChanges |= r.configChangeFlags;
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001541 behindFullscreenActivity = updateBehindFullscreen(stackInvisible,
1542 behindFullscreenActivity, task, r);
Craig Mautnerd44711d2013-02-23 11:24:36 -08001543 } else {
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001544 makeInvisible(stackInvisible, behindFullscreenActivity, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001545 }
1546 }
Wale Ogunwalec219c0b2015-09-12 09:18:07 -07001547 if (mStackId == FREEFORM_WORKSPACE_STACK_ID) {
1548 // The visibility of tasks and the activities they contain in freeform stack are
1549 // determined individually unlike other stacks where the visibility or fullscreen
1550 // status of an activity in a previous task affects other.
1551 behindFullscreenActivity = stackInvisible;
1552 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001553 }
Craig Mautnereb8abf72014-07-02 15:04:09 -07001554
1555 if (mTranslucentActivityWaiting != null &&
1556 mUndrawnActivitiesBelowTopTranslucent.isEmpty()) {
1557 // Nothing is getting drawn or everything was already visible, don't wait for timeout.
1558 notifyActivityDrawnLocked(null);
1559 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001560 }
Craig Mautner58547802013-03-05 08:23:53 -08001561
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001562 private void checkTranslucentActivityWaiting(ActivityRecord top) {
1563 if (mTranslucentActivityWaiting != top) {
1564 mUndrawnActivitiesBelowTopTranslucent.clear();
1565 if (mTranslucentActivityWaiting != null) {
1566 // Call the callback with a timeout indication.
1567 notifyActivityDrawnLocked(null);
1568 mTranslucentActivityWaiting = null;
1569 }
1570 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1571 }
1572 }
1573
1574 private boolean makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
Wale Ogunwaled046a012015-12-24 13:05:59 -08001575 boolean isTop, boolean andResume, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001576 // We need to make sure the app is running if it's the top, or it is just made visible from
1577 // invisible. If the app is already visible, it must have died while it was visible. In this
1578 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
1579 // thrashing.
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001580 if (isTop || !r.visible) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001581 // This activity needs to be visible, but isn't even running...
1582 // get it started and resume if no other stack in this stack is resumed.
1583 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
1584 if (r != starting) {
1585 r.startFreezingScreenLocked(r.app, configChanges);
1586 }
1587 if (!r.visible || r.mLaunchTaskBehind) {
1588 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
1589 setVisible(r, true);
1590 }
1591 if (r != starting) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08001592 mStackSupervisor.startSpecificActivityLocked(r, andResume, false);
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001593 return true;
1594 }
1595 }
1596 return false;
1597 }
1598
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001599 private void makeInvisible(boolean stackInvisible, boolean behindFullscreenActivity,
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001600 ActivityRecord r) {
1601 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r + " finishing="
1602 + r.finishing + " state=" + r.state + " stackInvisible=" + stackInvisible
1603 + " behindFullscreenActivity=" + behindFullscreenActivity);
1604 if (!r.visible) {
1605 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Already invisible: " + r);
1606 return;
1607 }
1608 // Now for any activities that aren't visible to the user, make sure they no longer are
1609 // keeping the screen frozen.
1610 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Making invisible: " + r);
1611 try {
1612 setVisible(r, false);
1613 switch (r.state) {
1614 case STOPPING:
1615 case STOPPED:
1616 if (r.app != null && r.app.thread != null) {
1617 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1618 "Scheduling invisibility: " + r);
1619 r.app.thread.scheduleWindowVisibility(r.appToken, false);
1620 }
1621 break;
1622
1623 case INITIALIZING:
1624 case RESUMED:
1625 case PAUSING:
1626 case PAUSED:
1627 // This case created for transitioning activities from
1628 // translucent to opaque {@link Activity#convertToOpaque}.
1629 if (getVisibleBehindActivity() == r) {
1630 releaseBackgroundResources(r);
1631 } else {
1632 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
1633 mStackSupervisor.mStoppingActivities.add(r);
1634 }
1635 mStackSupervisor.scheduleIdleLocked();
1636 }
1637 break;
1638
1639 default:
1640 break;
1641 }
1642 } catch (Exception e) {
1643 // Just skip on any failure; we'll make it visible when it next restarts.
1644 Slog.w(TAG, "Exception thrown making hidden: " + r.intent.getComponent(), e);
1645 }
1646 }
1647
1648 private boolean updateBehindFullscreen(boolean stackInvisible, boolean behindFullscreenActivity,
1649 TaskRecord task, ActivityRecord r) {
1650 if (r.fullscreen) {
1651 // At this point, nothing else needs to be shown in this task.
1652 behindFullscreenActivity = true;
1653 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
1654 + " stackInvisible=" + stackInvisible
1655 + " behindFullscreenActivity=" + behindFullscreenActivity);
1656 } else if (!isHomeStack() && r.frontOfTask && task.isOverHomeStack()) {
1657 behindFullscreenActivity = true;
1658 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Showing home: at " + r
1659 + " stackInvisible=" + stackInvisible
1660 + " behindFullscreenActivity=" + behindFullscreenActivity);
1661 }
1662 return behindFullscreenActivity;
1663 }
1664
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001665 private void makeVisible(ActivityRecord starting, ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001666 // This activity is not currently visible, but is running. Tell it to become visible.
1667 r.visible = true;
1668 if (r.state != ActivityState.RESUMED && r != starting) {
1669 // If this activity is paused, tell it to now show its window.
1670 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
1671 "Making visible and scheduling visibility: " + r);
1672 try {
1673 if (mTranslucentActivityWaiting != null) {
1674 r.updateOptionsLocked(r.returningOptions);
1675 mUndrawnActivitiesBelowTopTranslucent.add(r);
1676 }
1677 setVisible(r, true);
1678 r.sleeping = false;
1679 r.app.pendingUiClean = true;
1680 r.app.thread.scheduleWindowVisibility(r.appToken, true);
1681 r.stopFreezingScreenLocked(false);
1682 } catch (Exception e) {
1683 // Just skip on any failure; we'll make it
1684 // visible when it next restarts.
1685 Slog.w(TAG, "Exception thrown making visibile: " + r.intent.getComponent(), e);
1686 }
1687 }
1688 }
1689
Filip Gruszczynski21199bd2015-12-02 12:49:58 -08001690 private boolean handleAlreadyVisible(ActivityRecord r) {
Filip Gruszczynski9104aea2015-11-13 16:42:05 -08001691 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Skipping: already visible at " + r);
1692 r.stopFreezingScreenLocked(false);
1693 try {
1694 if (r.returningOptions != null) {
1695 r.app.thread.scheduleOnNewActivityOptions(r.appToken, r.returningOptions);
1696 }
1697 } catch(RemoteException e) {
1698 }
1699 return r.state == ActivityState.RESUMED;
1700 }
1701
Todd Kennedyaab56db2015-01-30 09:39:53 -08001702 void convertActivityToTranslucent(ActivityRecord r) {
Craig Mautner5eda9b32013-07-02 11:58:16 -07001703 mTranslucentActivityWaiting = r;
1704 mUndrawnActivitiesBelowTopTranslucent.clear();
1705 mHandler.sendEmptyMessageDelayed(TRANSLUCENT_TIMEOUT_MSG, TRANSLUCENT_CONVERSION_TIMEOUT);
1706 }
1707
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07001708 void clearOtherAppTimeTrackers(AppTimeTracker except) {
1709 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1710 final TaskRecord task = mTaskHistory.get(taskNdx);
1711 final ArrayList<ActivityRecord> activities = task.mActivities;
1712 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1713 final ActivityRecord r = activities.get(activityNdx);
1714 if ( r.appTimeTracker != except) {
1715 r.appTimeTracker = null;
1716 }
1717 }
1718 }
1719 }
1720
Craig Mautner5eda9b32013-07-02 11:58:16 -07001721 /**
1722 * Called as activities below the top translucent activity are redrawn. When the last one is
1723 * redrawn notify the top activity by calling
1724 * {@link Activity#onTranslucentConversionComplete}.
1725 *
1726 * @param r The most recent background activity to be drawn. Or, if r is null then a timeout
1727 * occurred and the activity will be notified immediately.
1728 */
1729 void notifyActivityDrawnLocked(ActivityRecord r) {
Craig Mautner6985bad2014-04-21 15:22:06 -07001730 mActivityContainer.setDrawn();
Craig Mautner5eda9b32013-07-02 11:58:16 -07001731 if ((r == null)
1732 || (mUndrawnActivitiesBelowTopTranslucent.remove(r) &&
1733 mUndrawnActivitiesBelowTopTranslucent.isEmpty())) {
1734 // The last undrawn activity below the top has just been drawn. If there is an
1735 // opaque activity at the top, notify it that it can become translucent safely now.
1736 final ActivityRecord waitingActivity = mTranslucentActivityWaiting;
1737 mTranslucentActivityWaiting = null;
1738 mUndrawnActivitiesBelowTopTranslucent.clear();
1739 mHandler.removeMessages(TRANSLUCENT_TIMEOUT_MSG);
1740
Craig Mautner71dd1b62014-02-18 15:48:52 -08001741 if (waitingActivity != null) {
1742 mWindowManager.setWindowOpaque(waitingActivity.appToken, false);
1743 if (waitingActivity.app != null && waitingActivity.app.thread != null) {
1744 try {
1745 waitingActivity.app.thread.scheduleTranslucentConversionComplete(
1746 waitingActivity.appToken, r != null);
1747 } catch (RemoteException e) {
1748 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07001749 }
1750 }
1751 }
1752 }
1753
Craig Mautnera61bc652013-10-28 15:43:18 -07001754 /** If any activities below the top running one are in the INITIALIZING state and they have a
1755 * starting window displayed then remove that starting window. It is possible that the activity
1756 * in this state will never resumed in which case that starting window will be orphaned. */
1757 void cancelInitializingActivities() {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001758 final ActivityRecord topActivity = topRunningActivityLocked();
Craig Mautnera61bc652013-10-28 15:43:18 -07001759 boolean aboveTop = true;
1760 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
1761 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
1762 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
1763 final ActivityRecord r = activities.get(activityNdx);
1764 if (aboveTop) {
1765 if (r == topActivity) {
1766 aboveTop = false;
1767 }
1768 continue;
1769 }
1770
1771 if (r.state == ActivityState.INITIALIZING && r.mStartingWindowShown) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001772 if (DEBUG_VISIBILITY) Slog.w(TAG_VISIBILITY,
1773 "Found orphaned starting window " + r);
Craig Mautnera61bc652013-10-28 15:43:18 -07001774 r.mStartingWindowShown = false;
1775 mWindowManager.removeAppStartingWindow(r.appToken);
1776 }
1777 }
1778 }
1779 }
1780
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001781 /**
1782 * Ensure that the top activity in the stack is resumed.
1783 *
1784 * @param prev The previously resumed activity, for when in the process
1785 * of pausing; can be null to call from elsewhere.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001786 * @param options Activity options.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001787 *
1788 * @return Returns true if something is being resumed, or false if
1789 * nothing happened.
Wale Ogunwaled046a012015-12-24 13:05:59 -08001790 *
1791 * NOTE: It is not safe to call this method directly as it can cause an activity in a
1792 * non-focused stack to be resumed.
1793 * Use {@link ActivityStackSupervisor#resumeFocusedStackTopActivityLocked} to resume the
1794 * right activity for the current system state.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001795 */
Wale Ogunwaled046a012015-12-24 13:05:59 -08001796 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
Craig Mautner42d04db2014-11-06 12:13:23 -08001797 if (mStackSupervisor.inResumeTopActivity) {
Craig Mautner544efa72014-09-04 16:41:20 -07001798 // Don't even start recursing.
1799 return false;
1800 }
1801
1802 boolean result = false;
1803 try {
1804 // Protect against recursion.
Craig Mautner42d04db2014-11-06 12:13:23 -08001805 mStackSupervisor.inResumeTopActivity = true;
1806 if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
1807 mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
Jeff Brown9ef94012014-11-21 13:04:42 -08001808 mService.updateSleepIfNeededLocked();
Craig Mautner42d04db2014-11-06 12:13:23 -08001809 }
Craig Mautner544efa72014-09-04 16:41:20 -07001810 result = resumeTopActivityInnerLocked(prev, options);
1811 } finally {
Craig Mautner42d04db2014-11-06 12:13:23 -08001812 mStackSupervisor.inResumeTopActivity = false;
Craig Mautner544efa72014-09-04 16:41:20 -07001813 }
1814 return result;
1815 }
1816
Chong Zhang280d3322015-11-03 17:27:26 -08001817 private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001818 if (DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner5314a402013-09-26 12:40:16 -07001819
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07001820 if (!mService.mBooting && !mService.mBooted) {
1821 // Not ready yet!
1822 return false;
1823 }
1824
Craig Mautnerdf88d732014-01-27 09:21:32 -08001825 ActivityRecord parent = mActivityContainer.mParentActivity;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001826 if ((parent != null && parent.state != ActivityState.RESUMED) ||
Craig Mautnerd163e752014-06-13 17:18:47 -07001827 !mActivityContainer.isAttachedLocked()) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08001828 // Do not resume this stack if its parent is not resumed.
1829 // TODO: If in a loop, make sure that parent stack resumeTopActivity is called 1st.
1830 return false;
1831 }
1832
Craig Mautnera61bc652013-10-28 15:43:18 -07001833 cancelInitializingActivities();
1834
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001835 // Find the first activity that is not finishing.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07001836 final ActivityRecord next = topRunningActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001837
1838 // Remember how we'll process this pause/resume situation, and ensure
1839 // that the state is reset however we wind up proceeding.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001840 final boolean userLeaving = mStackSupervisor.mUserLeaving;
1841 mStackSupervisor.mUserLeaving = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001842
Craig Mautner84984fa2014-06-19 11:19:20 -07001843 final TaskRecord prevTask = prev != null ? prev.task : null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001844 if (next == null) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001845 // There are no more activities!
1846 final String reason = "noMoreActivities";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001847 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(reason)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001848 // Try to move focus to the next visible stack with a running activity if this
1849 // stack is not covering the entire screen.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001850 return mStackSupervisor.resumeFocusedStackTopActivityLocked(
1851 mStackSupervisor.getFocusedStack(), prev, null);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001852 }
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08001853
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001854 // Let's just start up the Launcher...
Craig Mautnerde4ef022013-04-07 19:01:33 -07001855 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001856 if (DEBUG_STATES) Slog.d(TAG_STATES,
1857 "resumeTopActivityLocked: No more activities go home");
Craig Mautnercf910b02013-04-23 11:23:27 -07001858 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnere0a38842013-12-16 16:14:02 -08001859 // Only resume home if on home display
Craig Mautner84984fa2014-06-19 11:19:20 -07001860 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1861 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1862 return isOnHomeDisplay() &&
Wale Ogunwaled697cea2015-02-20 17:19:49 -08001863 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001864 }
1865
1866 next.delayedResume = false;
Craig Mautner58547802013-03-05 08:23:53 -08001867
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001868 // If the top activity is the resumed one, nothing to do.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001869 if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
1870 mStackSupervisor.allResumedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001871 // Make sure we have executed any pending transitions, since there
1872 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001873 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001874 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001875 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001876 if (DEBUG_STATES) Slog.d(TAG_STATES,
1877 "resumeTopActivityLocked: Top activity resumed " + next);
Craig Mautnercf910b02013-04-23 11:23:27 -07001878 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001879 return false;
1880 }
1881
Craig Mautner525f3d92013-05-07 14:01:50 -07001882 final TaskRecord nextTask = next.task;
bulicccd230712014-05-12 14:34:50 -07001883 if (prevTask != null && prevTask.stack == this &&
Craig Mautner84984fa2014-06-19 11:19:20 -07001884 prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
Craig Mautnercf910b02013-04-23 11:23:27 -07001885 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautner525f3d92013-05-07 14:01:50 -07001886 if (prevTask == nextTask) {
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08001887 prevTask.setFrontOfTask();
Craig Mautner525f3d92013-05-07 14:01:50 -07001888 } else if (prevTask != topTask()) {
Craig Mautner84984fa2014-06-19 11:19:20 -07001889 // This task is going away but it was supposed to return to the home stack.
Craig Mautnere418ecd2013-05-01 17:02:29 -07001890 // Now the task above it has to return to the home task instead.
Craig Mautner525f3d92013-05-07 14:01:50 -07001891 final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07001892 mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
louis_chang2d094e92015-01-21 19:01:52 +08001893 } else if (!isOnHomeDisplay()) {
1894 return false;
1895 } else if (!isHomeStack()){
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001896 if (DEBUG_STATES) Slog.d(TAG_STATES,
Craig Mautnere0a38842013-12-16 16:14:02 -08001897 "resumeTopActivityLocked: Launching home next");
Craig Mautner84984fa2014-06-19 11:19:20 -07001898 final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ?
1899 HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo();
1900 return isOnHomeDisplay() &&
Craig Mautner3c878f22015-01-26 13:57:19 -08001901 mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
Craig Mautnere418ecd2013-05-01 17:02:29 -07001902 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001903 }
1904
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001905 // If we are sleeping, and there is no resumed activity, and the top
1906 // activity is paused, well that is the state we want.
Craig Mautner5314a402013-09-26 12:40:16 -07001907 if (mService.isSleepingOrShuttingDown()
p13451dbad2872012-04-18 11:39:23 +09001908 && mLastPausedActivity == next
Craig Mautner5314a402013-09-26 12:40:16 -07001909 && mStackSupervisor.allPausedActivitiesComplete()) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001910 // Make sure we have executed any pending transitions, since there
1911 // should be nothing left to do at this point.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001912 mWindowManager.executeAppTransition();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001913 mNoAnimActivities.clear();
Dianne Hackborn84375872012-06-01 19:03:50 -07001914 ActivityOptions.abort(options);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001915 if (DEBUG_STATES) Slog.d(TAG_STATES,
1916 "resumeTopActivityLocked: Going to sleep and all paused");
Craig Mautnercf910b02013-04-23 11:23:27 -07001917 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001918 return false;
1919 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001920
1921 // Make sure that the user who owns this activity is started. If not,
1922 // we will just leave it as is because someone should be bringing
1923 // another user's activities to the top of the stack.
Fyodor Kupolov610acda2015-10-19 18:44:07 -07001924 if (!mService.mUserController.hasStartedUserState(next.userId)) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001925 Slog.w(TAG, "Skipping resume of top activity " + next
1926 + ": user " + next.userId + " is stopped");
Craig Mautnercf910b02013-04-23 11:23:27 -07001927 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001928 return false;
1929 }
1930
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001931 // The activity may be waiting for stop, but that is no longer
1932 // appropriate for it.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001933 mStackSupervisor.mStoppingActivities.remove(next);
Craig Mautner0eea92c2013-05-16 13:35:39 -07001934 mStackSupervisor.mGoingToSleepActivities.remove(next);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001935 next.sleeping = false;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001936 mStackSupervisor.mWaitingVisibleActivities.remove(next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001937
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001938 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001939
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001940 // If we are currently pausing an activity, then don't do anything
1941 // until that is done.
Craig Mautner69ada552013-04-18 13:51:51 -07001942 if (!mStackSupervisor.allPausedActivitiesComplete()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07001943 if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001944 "resumeTopActivityLocked: Skip resume: some activity pausing.");
Craig Mautnercf910b02013-04-23 11:23:27 -07001945 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08001946 return false;
1947 }
1948
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001949 // Okay we are now going to start a switch, to 'next'. We may first
1950 // have to pause the current activity, but this is an important point
1951 // where we have decided to go to 'next' so keep track of that.
Dianne Hackborn034093a42010-09-20 22:24:38 -07001952 // XXX "App Redirected" dialog is getting too many false positives
1953 // at this point, so turn off for now.
1954 if (false) {
1955 if (mLastStartedActivity != null && !mLastStartedActivity.finishing) {
1956 long now = SystemClock.uptimeMillis();
1957 final boolean inTime = mLastStartedActivity.startTime != 0
1958 && (mLastStartedActivity.startTime + START_WARN_TIME) >= now;
1959 final int lastUid = mLastStartedActivity.info.applicationInfo.uid;
1960 final int nextUid = next.info.applicationInfo.uid;
1961 if (inTime && lastUid != nextUid
1962 && lastUid != next.launchedFromUid
1963 && mService.checkPermission(
1964 android.Manifest.permission.STOP_APP_SWITCHES,
1965 -1, next.launchedFromUid)
1966 != PackageManager.PERMISSION_GRANTED) {
1967 mService.showLaunchWarningLocked(mLastStartedActivity, next);
1968 } else {
1969 next.startTime = now;
1970 mLastStartedActivity = next;
1971 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001972 } else {
Dianne Hackborn034093a42010-09-20 22:24:38 -07001973 next.startTime = SystemClock.uptimeMillis();
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001974 mLastStartedActivity = next;
1975 }
Dianne Hackborn0dad3642010-09-09 21:25:35 -07001976 }
Craig Mautner58547802013-03-05 08:23:53 -08001977
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001978 mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
1979
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001980 // We need to start pausing the current activity so the top one
1981 // can be resumed...
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001982 boolean dontWaitForPause = (next.info.flags&ActivityInfo.FLAG_RESUME_WHILE_PAUSING) != 0;
1983 boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001984 if (mResumedActivity != null) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001985 if (DEBUG_STATES) Slog.d(TAG_STATES,
1986 "resumeTopActivityLocked: Pausing " + mResumedActivity);
Craig Mautnere042bf22014-11-11 11:28:43 -08001987 pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
Craig Mautnereb957862013-04-24 15:34:32 -07001988 }
1989 if (pausing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07001990 if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
Craig Mautnerac6f8432013-07-17 13:24:59 -07001991 "resumeTopActivityLocked: Skip resume: need to start pausing");
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001992 // At this point we want to put the upcoming activity's process
1993 // at the top of the LRU list, since we know we will be needing it
1994 // very soon and it would be a waste to let it get killed if it
1995 // happens to be sitting towards the end.
1996 if (next.app != null && next.app.thread != null) {
Dianne Hackborndb926082013-10-31 16:32:44 -07001997 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001998 }
Craig Mautnercf910b02013-04-23 11:23:27 -07001999 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002000 return true;
2001 }
2002
Christopher Tated3f175c2012-06-14 14:16:54 -07002003 // If the most recent activity was noHistory but was only stopped rather
2004 // than stopped+finished because the device went to sleep, we need to make
2005 // sure to finish it as we're making a new activity topmost.
Dianne Hackborn91097de2014-04-04 18:02:06 -07002006 if (mService.isSleeping() && mLastNoHistoryActivity != null &&
Craig Mautner0f922742013-08-06 08:44:42 -07002007 !mLastNoHistoryActivity.finishing) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002008 if (DEBUG_STATES) Slog.d(TAG_STATES,
2009 "no-history finish of " + mLastNoHistoryActivity + " on new resume");
Craig Mautner0f922742013-08-06 08:44:42 -07002010 requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
Todd Kennedy539db512014-12-15 09:57:55 -08002011 null, "resume-no-history", false);
Craig Mautner0f922742013-08-06 08:44:42 -07002012 mLastNoHistoryActivity = null;
Christopher Tated3f175c2012-06-14 14:16:54 -07002013 }
2014
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002015 if (prev != null && prev != next) {
Craig Mautner8c14c152015-01-15 17:32:07 -08002016 if (!mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2017 && next != null && !next.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002018 mStackSupervisor.mWaitingVisibleActivities.add(prev);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002019 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2020 "Resuming top, waiting visible to hide: " + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002021 } else {
2022 // The next activity is already visible, so hide the previous
2023 // activity's windows right now so we can show the new one ASAP.
2024 // We only do this if the previous is finishing, which should mean
2025 // it is on top of the one being resumed so hiding it quickly
2026 // is good. Otherwise, we want to do the normal route of allowing
2027 // the resumed activity to be shown so we can decide if the
2028 // previous should actually be hidden depending on whether the
2029 // new one is found to be full-screen or not.
2030 if (prev.finishing) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002031 mWindowManager.setAppVisibility(prev.appToken, false);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002032 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
2033 "Not waiting for visible to hide: " + prev + ", waitingVisible="
Craig Mautner8c14c152015-01-15 17:32:07 -08002034 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002035 + ", nowVisible=" + next.nowVisible);
2036 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002037 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
Craig Mautner8c14c152015-01-15 17:32:07 -08002038 "Previous already visible but still waiting to hide: " + prev
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002039 + ", waitingVisible="
2040 + mStackSupervisor.mWaitingVisibleActivities.contains(prev)
2041 + ", nowVisible=" + next.nowVisible);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002042 }
2043 }
2044 }
2045
Dianne Hackborne7f97212011-02-24 14:40:20 -08002046 // Launching this app's activity, make sure the app is no longer
2047 // considered stopped.
2048 try {
2049 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07002050 next.packageName, false, next.userId); /* TODO: Verify if correct userid */
Dianne Hackborne7f97212011-02-24 14:40:20 -08002051 } catch (RemoteException e1) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08002052 } catch (IllegalArgumentException e) {
2053 Slog.w(TAG, "Failed trying to unstop package "
2054 + next.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08002055 }
2056
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002057 // We are starting up the next activity, so tell the window manager
2058 // that the previous one will be hidden soon. This way it can know
2059 // to ignore it when computing the desired screen orientation.
Craig Mautner525f3d92013-05-07 14:01:50 -07002060 boolean anim = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002061 if (prev != null) {
2062 if (prev.finishing) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002063 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002064 "Prepare close transition: prev=" + prev);
2065 if (mNoAnimActivities.contains(prev)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002066 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002067 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002068 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002069 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002070 ? AppTransition.TRANSIT_ACTIVITY_CLOSE
2071 : AppTransition.TRANSIT_TASK_CLOSE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002072 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002073 mWindowManager.setAppVisibility(prev.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002074 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002075 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
2076 "Prepare open transition: prev=" + prev);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002077 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002078 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002079 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002080 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002081 mWindowManager.prepareAppTransition(prev.task == next.task
Craig Mautner4b71aa12012-12-27 17:20:01 -08002082 ? AppTransition.TRANSIT_ACTIVITY_OPEN
Craig Mautnerbb742462014-07-07 15:28:55 -07002083 : next.mLaunchTaskBehind
2084 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2085 : AppTransition.TRANSIT_TASK_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002086 }
2087 }
Craig Mautner967212c2013-04-13 21:10:58 -07002088 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002089 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002090 if (mNoAnimActivities.contains(next)) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002091 anim = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002092 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002093 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002094 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_ACTIVITY_OPEN, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002095 }
2096 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002097
2098 Bundle resumeAnimOptions = null;
Craig Mautner525f3d92013-05-07 14:01:50 -07002099 if (anim) {
Adam Powellcfbe9be2013-11-06 14:58:58 -08002100 ActivityOptions opts = next.getOptionsForTargetActivityLocked();
2101 if (opts != null) {
2102 resumeAnimOptions = opts.toBundle();
2103 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002104 next.applyOptionsLocked();
2105 } else {
2106 next.clearOptionsLocked();
2107 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002108
Craig Mautnercf910b02013-04-23 11:23:27 -07002109 ActivityStack lastStack = mStackSupervisor.getLastStack();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002110 if (next.app != null && next.app.thread != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002111 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002112
2113 // This activity is now becoming visible.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002114 mWindowManager.setAppVisibility(next.appToken, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002115
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07002116 // schedule launch ticks to collect information about slow apps.
2117 next.startLaunchTickingLocked();
2118
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002119 ActivityRecord lastResumedActivity =
2120 lastStack == null ? null :lastStack.mResumedActivity;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002121 ActivityState lastState = next.state;
2122
2123 mService.updateCpuStats();
Craig Mautner58547802013-03-05 08:23:53 -08002124
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002125 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED: " + next + " (in existing)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002126 next.state = ActivityState.RESUMED;
2127 mResumedActivity = next;
2128 next.task.touchActiveTime();
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08002129 mRecentTasks.addLocked(next.task);
Dianne Hackborndb926082013-10-31 16:32:44 -07002130 mService.updateLruProcessLocked(next.app, true, null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002131 updateLRUListLocked(next);
Dianne Hackborndb926082013-10-31 16:32:44 -07002132 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002133
2134 // Have the window manager re-evaluate the orientation of
2135 // the screen based on the new activity order.
Craig Mautner525f3d92013-05-07 14:01:50 -07002136 boolean notUpdated = true;
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002137 if (mStackSupervisor.isFocusedStack(this)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002138 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner8d341ef2013-03-26 09:03:27 -07002139 mService.mConfiguration,
2140 next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
2141 if (config != null) {
2142 next.frozenBeforeDestroy = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002143 }
Maxim Bogatov05075302015-05-19 18:33:08 -07002144 notUpdated = !mService.updateConfigurationLocked(config, next, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002145 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002146
Craig Mautner525f3d92013-05-07 14:01:50 -07002147 if (notUpdated) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002148 // The configuration update wasn't able to keep the existing
2149 // instance of the activity, and instead started a new one.
2150 // We should be all done, but let's just make sure our activity
2151 // is still at the top and schedule another run if something
2152 // weird happened.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002153 ActivityRecord nextNext = topRunningActivityLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002154 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002155 "Activity config changed during resume: " + next
2156 + ", new next: " + nextNext);
2157 if (nextNext != next) {
2158 // Do over!
Craig Mautner05d29032013-05-03 13:40:13 -07002159 mStackSupervisor.scheduleResumeTopActivities();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002160 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002161 if (mStackSupervisor.reportResumedActivityLocked(next)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002162 mNoAnimActivities.clear();
Craig Mautnercf910b02013-04-23 11:23:27 -07002163 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002164 return true;
2165 }
Craig Mautnercf910b02013-04-23 11:23:27 -07002166 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002167 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002168 }
Craig Mautner58547802013-03-05 08:23:53 -08002169
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002170 try {
2171 // Deliver all pending results.
Craig Mautner05d6272ba2013-02-11 09:39:27 -08002172 ArrayList<ResultInfo> a = next.results;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002173 if (a != null) {
2174 final int N = a.size();
2175 if (!next.finishing && N > 0) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002176 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
2177 "Delivering results to " + next + ": " + a);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002178 next.app.thread.scheduleSendResult(next.appToken, a);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002179 }
2180 }
2181
2182 if (next.newIntents != null) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002183 next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002184 }
2185
Craig Mautner299f9602015-01-26 09:47:33 -08002186 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
2187 System.identityHashCode(next), next.task.taskId, next.shortComponentName);
Craig Mautner58547802013-03-05 08:23:53 -08002188
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002189 next.sleeping = false;
Craig Mautner2420ead2013-04-01 17:13:20 -07002190 mService.showAskCompatModeDialogLocked(next);
Dianne Hackborn905577f2011-09-07 18:31:28 -07002191 next.app.pendingUiClean = true;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002192 next.app.forceProcessStateUpTo(mService.mTopProcessState);
George Mount2c92c972014-03-20 09:38:23 -07002193 next.clearOptionsLocked();
Dianne Hackborna413dc02013-07-12 12:02:55 -07002194 next.app.thread.scheduleResumeActivity(next.appToken, next.app.repProcState,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002195 mService.isNextTransitionForward(), resumeAnimOptions);
Craig Mautner58547802013-03-05 08:23:53 -08002196
Craig Mautner0eea92c2013-05-16 13:35:39 -07002197 mStackSupervisor.checkReadyForSleepLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002198
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002199 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002200 } catch (Exception e) {
2201 // Whoops, need to restart this activity!
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002202 if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to "
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002203 + lastState + ": " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002204 next.state = lastState;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002205 if (lastStack != null) {
2206 lastStack.mResumedActivity = lastResumedActivity;
2207 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002208 Slog.i(TAG, "Restarting because process died: " + next);
2209 if (!next.hasBeenLaunched) {
2210 next.hasBeenLaunched = true;
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002211 } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
2212 mStackSupervisor.isFrontStack(lastStack)) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002213 mWindowManager.setAppStartingWindow(
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002214 next.appToken, next.packageName, next.theme,
2215 mService.compatibilityInfoForPackageLocked(next.info.applicationInfo),
Adam Powell04fe6eb2013-05-31 14:39:48 -07002216 next.nonLocalizedLabel, next.labelRes, next.icon, next.logo,
2217 next.windowFlags, null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002218 }
George Mount2c92c972014-03-20 09:38:23 -07002219 mStackSupervisor.startSpecificActivityLocked(next, true, false);
Craig Mautnercf910b02013-04-23 11:23:27 -07002220 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002221 return true;
2222 }
2223
2224 // From this point on, if something goes wrong there is no way
2225 // to recover the activity.
2226 try {
2227 next.visible = true;
2228 completeResumeLocked(next);
2229 } catch (Exception e) {
2230 // If any exception gets thrown, toss away this
2231 // activity and try the next one.
2232 Slog.w(TAG, "Exception thrown during resume of " + next, e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002233 requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002234 "resume-exception", true);
Craig Mautnercf910b02013-04-23 11:23:27 -07002235 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002236 return true;
2237 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002238 next.stopped = false;
2239
2240 } else {
2241 // Whoops, need to restart this activity!
2242 if (!next.hasBeenLaunched) {
2243 next.hasBeenLaunched = true;
2244 } else {
2245 if (SHOW_APP_STARTING_PREVIEW) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002246 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002247 next.appToken, next.packageName, next.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002248 mService.compatibilityInfoForPackageLocked(
2249 next.info.applicationInfo),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002250 next.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002251 next.labelRes, next.icon, next.logo, next.windowFlags,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08002252 null, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002253 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002254 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002255 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002256 if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting " + next);
George Mount2c92c972014-03-20 09:38:23 -07002257 mStackSupervisor.startSpecificActivityLocked(next, true, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002258 }
2259
Craig Mautnercf910b02013-04-23 11:23:27 -07002260 if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002261 return true;
2262 }
2263
riddle_hsuc215a4f2014-12-27 12:10:45 +08002264 private TaskRecord getNextTask(TaskRecord targetTask) {
2265 final int index = mTaskHistory.indexOf(targetTask);
2266 if (index >= 0) {
2267 final int numTasks = mTaskHistory.size();
2268 for (int i = index + 1; i < numTasks; ++i) {
2269 TaskRecord task = mTaskHistory.get(i);
2270 if (task.userId == targetTask.userId) {
2271 return task;
2272 }
2273 }
2274 }
2275 return null;
2276 }
2277
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002278 private void insertTaskAtPosition(TaskRecord task, int position) {
2279 if (position >= mTaskHistory.size()) {
2280 insertTaskAtTop(task, null);
2281 return;
2282 }
2283 // Calculate maximum possible position for this task.
2284 int maxPosition = mTaskHistory.size();
2285 if (!mStackSupervisor.isCurrentProfileLocked(task.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002286 && task.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002287 // Put non-current user tasks below current user tasks.
2288 while (maxPosition > 0) {
2289 final TaskRecord tmpTask = mTaskHistory.get(maxPosition - 1);
2290 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002291 || tmpTask.topRunningActivityLocked() == null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07002292 break;
2293 }
2294 maxPosition--;
2295 }
2296 }
2297 position = Math.min(position, maxPosition);
2298 mTaskHistory.remove(task);
2299 mTaskHistory.add(position, task);
2300 updateTaskMovement(task, true);
2301 }
2302
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002303 private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08002304 // If the moving task is over home stack, transfer its return type to next task
2305 if (task.isOverHomeStack()) {
2306 final TaskRecord nextTask = getNextTask(task);
2307 if (nextTask != null) {
2308 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
2309 }
2310 }
2311
Craig Mautner9c85c202013-10-04 20:11:26 -07002312 // If this is being moved to the top by another activity or being launched from the home
riddle_hsuc215a4f2014-12-27 12:10:45 +08002313 // activity, set mTaskToReturnTo accordingly.
Craig Mautnere0a38842013-12-16 16:14:02 -08002314 if (isOnHomeDisplay()) {
2315 ActivityStack lastStack = mStackSupervisor.getLastStack();
2316 final boolean fromHome = lastStack.isHomeStack();
2317 if (!isHomeStack() && (fromHome || topTask() != task)) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002318 int returnToType = APPLICATION_ACTIVITY_TYPE;
2319 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
2320 returnToType = lastStack.topTask() == null
2321 ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
2322 }
2323 task.setTaskToReturnTo(returnToType);
Craig Mautnere0a38842013-12-16 16:14:02 -08002324 }
2325 } else {
Craig Mautner84984fa2014-06-19 11:19:20 -07002326 task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner9c85c202013-10-04 20:11:26 -07002327 }
Craig Mautnerd99384d2013-10-14 07:09:18 -07002328
Craig Mautnerac6f8432013-07-17 13:24:59 -07002329 mTaskHistory.remove(task);
2330 // Now put task at top.
Craig Mautnerbb742462014-07-07 15:28:55 -07002331 int taskNdx = mTaskHistory.size();
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002332 final boolean notShownWhenLocked =
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07002333 (newActivity != null && (newActivity.info.flags & FLAG_SHOW_FOR_ALL_USERS) == 0)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002334 || (newActivity == null && task.topRunningActivityLocked() == null);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002335 if (!mStackSupervisor.isCurrentProfileLocked(task.userId) && notShownWhenLocked) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002336 // Put non-current user tasks below current user tasks.
Craig Mautnerbb742462014-07-07 15:28:55 -07002337 while (--taskNdx >= 0) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002338 final TaskRecord tmpTask = mTaskHistory.get(taskNdx);
Wale Ogunwale0bd2aa72015-04-16 13:50:44 -07002339 if (!mStackSupervisor.isCurrentProfileLocked(tmpTask.userId)
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07002340 || tmpTask.topRunningActivityLocked() == null) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07002341 break;
2342 }
2343 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002344 ++taskNdx;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002345 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002346 mTaskHistory.add(taskNdx, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07002347 updateTaskMovement(task, true);
Craig Mautnerac6f8432013-07-17 13:24:59 -07002348 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08002349
Filip Gruszczynski3d82ed62015-12-10 10:41:39 -08002350 final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
2351 ActivityOptions options) {
Craig Mautnerd2328952013-03-05 12:46:26 -08002352 TaskRecord rTask = r.task;
2353 final int taskId = rTask.taskId;
Craig Mautnerbb742462014-07-07 15:28:55 -07002354 // mLaunchTaskBehind tasks get placed at the back of the task stack.
2355 if (!r.mLaunchTaskBehind && (taskForIdLocked(taskId) == null || newTask)) {
Craig Mautner77878772013-03-04 19:46:24 -08002356 // Last activity in task had been removed or ActivityManagerService is reusing task.
2357 // Insert or replace.
Craig Mautner77878772013-03-04 19:46:24 -08002358 // Might not even be in.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07002359 insertTaskAtTop(rTask, r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002360 mWindowManager.moveTaskToTop(taskId);
Craig Mautner77878772013-03-04 19:46:24 -08002361 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002362 TaskRecord task = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002363 if (!newTask) {
2364 // If starting in an existing task, find where that is...
Craig Mautner70a86932013-02-28 22:37:44 -08002365 boolean startIt = true;
2366 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
2367 task = mTaskHistory.get(taskNdx);
riddle_hsu9bcc6e82014-07-31 00:26:51 +08002368 if (task.getTopActivity() == null) {
2369 // All activities in task are finishing.
2370 continue;
2371 }
Craig Mautner70a86932013-02-28 22:37:44 -08002372 if (task == r.task) {
2373 // Here it is! Now, if this is not yet visible to the
2374 // user, then just add it without starting; it will
2375 // get started when the user navigates back to it.
Craig Mautner70a86932013-02-28 22:37:44 -08002376 if (!startIt) {
2377 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to task "
2378 + task, new RuntimeException("here").fillInStackTrace());
2379 task.addActivityToTop(r);
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002380 r.putInHistory();
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002381 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002382 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002383 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002384 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002385 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002386 return;
2387 }
2388 break;
Craig Mautner70a86932013-02-28 22:37:44 -08002389 } else if (task.numFullscreen > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002390 startIt = false;
2391 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002392 }
2393 }
2394
2395 // Place a new activity at top of stack, so it is next to interact
2396 // with the user.
Craig Mautner70a86932013-02-28 22:37:44 -08002397
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002398 // If we are not placing the new activity frontmost, we do not want
2399 // to deliver the onUserLeaving callback to the actual frontmost
2400 // activity
Craig Mautner70a86932013-02-28 22:37:44 -08002401 if (task == r.task && mTaskHistory.indexOf(task) != (mTaskHistory.size() - 1)) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002402 mStackSupervisor.mUserLeaving = false;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002403 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
Craig Mautner70a86932013-02-28 22:37:44 -08002404 "startActivity() behind front, mUserLeaving=false");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002405 }
Craig Mautner70a86932013-02-28 22:37:44 -08002406
2407 task = r.task;
2408
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002409 // Slot the activity into the history stack and proceed
Craig Mautner70a86932013-02-28 22:37:44 -08002410 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "Adding activity " + r + " to stack to task " + task,
Craig Mautner56f52db2013-02-25 10:03:01 -08002411 new RuntimeException("here").fillInStackTrace());
Craig Mautner70a86932013-02-28 22:37:44 -08002412 task.addActivityToTop(r);
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08002413 task.setFrontOfTask();
Craig Mautner70a86932013-02-28 22:37:44 -08002414
Dianne Hackbornf26fd992011-04-08 18:14:09 -07002415 r.putInHistory();
Craig Mautnerde4ef022013-04-07 19:01:33 -07002416 if (!isHomeStack() || numActivities() > 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002417 // We want to show the starting preview window if we are
2418 // switching to a new task, or the next activity's process is
2419 // not currently running.
2420 boolean showStartingIcon = newTask;
2421 ProcessRecord proc = r.app;
2422 if (proc == null) {
2423 proc = mService.mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2424 }
2425 if (proc == null || proc.thread == null) {
2426 showStartingIcon = true;
2427 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002428 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002429 "Prepare open transition: starting " + r);
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002430 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002431 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002432 mNoAnimActivities.add(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002433 } else {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002434 mWindowManager.prepareAppTransition(newTask
Craig Mautnerbb742462014-07-07 15:28:55 -07002435 ? r.mLaunchTaskBehind
2436 ? AppTransition.TRANSIT_TASK_OPEN_BEHIND
2437 : AppTransition.TRANSIT_TASK_OPEN
Craig Mautner4b71aa12012-12-27 17:20:01 -08002438 : AppTransition.TRANSIT_ACTIVITY_OPEN, keepCurTransition);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002439 mNoAnimActivities.remove(r);
2440 }
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002441 addConfigOverride(r, task);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002442 boolean doShow = true;
2443 if (newTask) {
2444 // Even though this activity is starting fresh, we still need
2445 // to reset it to make sure we apply affinities to move any
2446 // existing activities from other tasks in to it.
2447 // If the caller has requested that the target task be
2448 // reset, then do so.
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002449 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002450 resetTaskIfNeededLocked(r, r);
2451 doShow = topRunningNonDelayedActivityLocked(null) == r;
2452 }
Chong Zhang280d3322015-11-03 17:27:26 -08002453 } else if (options != null && options.getAnimationType()
George Mount70778d72014-07-01 16:33:45 -07002454 == ActivityOptions.ANIM_SCENE_TRANSITION) {
2455 doShow = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002456 }
Craig Mautnerbb742462014-07-07 15:28:55 -07002457 if (r.mLaunchTaskBehind) {
2458 // Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
2459 // tell WindowManager that r is visible even though it is at the back of the stack.
2460 mWindowManager.setAppVisibility(r.appToken, true);
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07002461 ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
Craig Mautnerbb742462014-07-07 15:28:55 -07002462 } else if (SHOW_APP_STARTING_PREVIEW && doShow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002463 // Figure out if we are transitioning from another activity that is
2464 // "has the same starting icon" as the next one. This allows the
2465 // window manager to keep the previous window it had previously
2466 // created, if it still had one.
2467 ActivityRecord prev = mResumedActivity;
2468 if (prev != null) {
2469 // We don't want to reuse the previous starting preview if:
2470 // (1) The current activity is in a different task.
Craig Mautner29219d92013-04-16 20:19:12 -07002471 if (prev.task != r.task) {
2472 prev = null;
2473 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002474 // (2) The current activity is already displayed.
Craig Mautner29219d92013-04-16 20:19:12 -07002475 else if (prev.nowVisible) {
2476 prev = null;
2477 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002478 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002479 mWindowManager.setAppStartingWindow(
Dianne Hackbornbe707852011-11-11 14:32:10 -08002480 r.appToken, r.packageName, r.theme,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07002481 mService.compatibilityInfoForPackageLocked(
2482 r.info.applicationInfo), r.nonLocalizedLabel,
Adam Powell04fe6eb2013-05-31 14:39:48 -07002483 r.labelRes, r.icon, r.logo, r.windowFlags,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002484 prev != null ? prev.appToken : null, showStartingIcon);
Craig Mautnera61bc652013-10-28 15:43:18 -07002485 r.mStartingWindowShown = true;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002486 }
2487 } else {
2488 // If this is the first activity, don't do any fancy animations,
2489 // because there is nothing for it to animate on top of.
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07002490 addConfigOverride(r, task);
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002491 ActivityOptions.abort(options);
Craig Mautner233ceee2014-05-09 17:05:11 -07002492 options = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002493 }
2494 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002495 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002496 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002497 }
2498
Dianne Hackbornbe707852011-11-11 14:32:10 -08002499 final void validateAppTokensLocked() {
2500 mValidateAppTokens.clear();
Craig Mautner000f0022013-02-26 15:04:29 -08002501 mValidateAppTokens.ensureCapacity(numActivities());
2502 final int numTasks = mTaskHistory.size();
2503 for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
2504 TaskRecord task = mTaskHistory.get(taskNdx);
2505 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerc8143c62013-09-03 12:15:57 -07002506 if (activities.isEmpty()) {
Craig Mautner000f0022013-02-26 15:04:29 -08002507 continue;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002508 }
Craig Mautner000f0022013-02-26 15:04:29 -08002509 TaskGroup group = new TaskGroup();
2510 group.taskId = task.taskId;
2511 mValidateAppTokens.add(group);
2512 final int numActivities = activities.size();
2513 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
2514 final ActivityRecord r = activities.get(activityNdx);
2515 group.tokens.add(r.appToken);
2516 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002517 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002518 mWindowManager.validateAppTokens(mStackId, mValidateAppTokens);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002519 }
2520
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002521 /**
2522 * Perform a reset of the given task, if needed as part of launching it.
2523 * Returns the new HistoryRecord at the top of the task.
2524 */
Craig Mautnere3a74d52013-02-22 14:14:58 -08002525 /**
2526 * Helper method for #resetTaskIfNeededLocked.
2527 * We are inside of the task being reset... we'll either finish this activity, push it out
2528 * for another task, or leave it as-is.
2529 * @param task The task containing the Activity (taskTop) that might be reset.
2530 * @param forceReset
2531 * @return An ActivityOptions that needs to be processed.
2532 */
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002533 final ActivityOptions resetTargetTaskIfNeededLocked(TaskRecord task, boolean forceReset) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002534 ActivityOptions topOptions = null;
2535
2536 int replyChainEnd = -1;
2537 boolean canMoveOptions = true;
2538
2539 // We only do this for activities that are not the root of the task (since if we finish
2540 // the root, we may no longer have the task!).
2541 final ArrayList<ActivityRecord> activities = task.mActivities;
2542 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002543 final int rootActivityNdx = task.findEffectiveRootIndex();
2544 for (int i = numActivities - 1; i > rootActivityNdx; --i ) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002545 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002546 if (target.frontOfTask)
2547 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002548
2549 final int flags = target.info.flags;
2550 final boolean finishOnTaskLaunch =
2551 (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2552 final boolean allowTaskReparenting =
2553 (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2554 final boolean clearWhenTaskReset =
2555 (target.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
2556
2557 if (!finishOnTaskLaunch
2558 && !clearWhenTaskReset
2559 && target.resultTo != null) {
2560 // If this activity is sending a reply to a previous
2561 // activity, we can't do anything with it now until
2562 // we reach the start of the reply chain.
2563 // XXX note that we are assuming the result is always
2564 // to the previous activity, which is almost always
2565 // the case but we really shouldn't count on.
2566 if (replyChainEnd < 0) {
2567 replyChainEnd = i;
2568 }
2569 } else if (!finishOnTaskLaunch
2570 && !clearWhenTaskReset
2571 && allowTaskReparenting
2572 && target.taskAffinity != null
2573 && !target.taskAffinity.equals(task.affinity)) {
2574 // If this activity has an affinity for another
2575 // task, then we need to move it out of here. We will
2576 // move it as far out of the way as possible, to the
2577 // bottom of the activity stack. This also keeps it
2578 // correctly ordered with any activities we previously
2579 // moved.
Craig Mautner329f4122013-11-07 09:10:42 -08002580 final TaskRecord targetTask;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002581 final ActivityRecord bottom =
2582 !mTaskHistory.isEmpty() && !mTaskHistory.get(0).mActivities.isEmpty() ?
Craig Mautner329f4122013-11-07 09:10:42 -08002583 mTaskHistory.get(0).mActivities.get(0) : null;
Craig Mautnerdccb7702013-09-17 15:53:34 -07002584 if (bottom != null && target.taskAffinity != null
2585 && target.taskAffinity.equals(bottom.task.affinity)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002586 // If the activity currently at the bottom has the
2587 // same task affinity as the one we are moving,
2588 // then merge it into the same task.
Craig Mautner329f4122013-11-07 09:10:42 -08002589 targetTask = bottom.task;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002590 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnerdccb7702013-09-17 15:53:34 -07002591 + " out to bottom task " + bottom.task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002592 } else {
Craig Mautner329f4122013-11-07 09:10:42 -08002593 targetTask = createTaskRecord(mStackSupervisor.getNextTaskId(), target.info,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002594 null, null, null, false);
Craig Mautner329f4122013-11-07 09:10:42 -08002595 targetTask.affinityIntent = target.intent;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002596 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Start pushing activity " + target
Craig Mautnere3a74d52013-02-22 14:14:58 -08002597 + " out to new task " + target.task);
2598 }
2599
Wale Ogunwale706ed792015-08-02 10:29:44 -07002600 setAppTask(target, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002601
Craig Mautner525f3d92013-05-07 14:01:50 -07002602 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002603 final int start = replyChainEnd < 0 ? i : replyChainEnd;
2604 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnerdccb7702013-09-17 15:53:34 -07002605 final ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002606 if (p.finishing) {
2607 continue;
2608 }
2609
Craig Mautnere3a74d52013-02-22 14:14:58 -08002610 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002611 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002612 topOptions = p.takeOptionsLocked();
2613 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002614 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002615 }
2616 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002617 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2618 "Removing activity " + p + " from task=" + task + " adding to task="
2619 + targetTask + " Callers=" + Debug.getCallers(4));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002620 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2621 "Pushing next activity " + p + " out to target's task " + target.task);
Craig Mautnera228ae92014-07-09 05:44:55 -07002622 p.setTask(targetTask, null);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002623 targetTask.addActivityAtBottom(p);
Craig Mautner0247fc82013-02-28 14:32:06 -08002624
Wale Ogunwale706ed792015-08-02 10:29:44 -07002625 setAppTask(p, targetTask);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002626 }
2627
Wale Ogunwale706ed792015-08-02 10:29:44 -07002628 mWindowManager.moveTaskToBottom(targetTask.taskId);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002629 if (VALIDATE_TOKENS) {
2630 validateAppTokensLocked();
2631 }
2632
2633 replyChainEnd = -1;
2634 } else if (forceReset || finishOnTaskLaunch || clearWhenTaskReset) {
2635 // If the activity should just be removed -- either
2636 // because it asks for it, or the task should be
2637 // cleared -- then finish it and anything that is
2638 // part of its reply chain.
2639 int end;
2640 if (clearWhenTaskReset) {
2641 // In this case, we want to finish this activity
2642 // and everything above it, so be sneaky and pretend
2643 // like these are all in the reply chain.
Mark Lu4b5a9a02014-12-09 14:47:13 +08002644 end = activities.size() - 1;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002645 } else if (replyChainEnd < 0) {
2646 end = i;
2647 } else {
2648 end = replyChainEnd;
2649 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002650 boolean noOptions = canMoveOptions;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002651 for (int srcPos = i; srcPos <= end; srcPos++) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002652 ActivityRecord p = activities.get(srcPos);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002653 if (p.finishing) {
2654 continue;
2655 }
2656 canMoveOptions = false;
Craig Mautner525f3d92013-05-07 14:01:50 -07002657 if (noOptions && topOptions == null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002658 topOptions = p.takeOptionsLocked();
2659 if (topOptions != null) {
Craig Mautner525f3d92013-05-07 14:01:50 -07002660 noOptions = false;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002661 }
2662 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002663 if (DEBUG_TASKS) Slog.w(TAG_TASKS,
Craig Mautner58547802013-03-05 08:23:53 -08002664 "resetTaskIntendedTask: calling finishActivity on " + p);
Todd Kennedy539db512014-12-15 09:57:55 -08002665 if (finishActivityLocked(
2666 p, Activity.RESULT_CANCELED, null, "reset-task", false)) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002667 end--;
2668 srcPos--;
2669 }
2670 }
2671 replyChainEnd = -1;
2672 } else {
2673 // If we were in the middle of a chain, well the
2674 // activity that started it all doesn't want anything
2675 // special, so leave it all as-is.
2676 replyChainEnd = -1;
2677 }
2678 }
2679
2680 return topOptions;
2681 }
2682
2683 /**
2684 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given
2685 * TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
2686 * @param affinityTask The task we are looking for an affinity to.
2687 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
2688 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
2689 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
2690 */
Craig Mautner525f3d92013-05-07 14:01:50 -07002691 private int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask, TaskRecord task,
Craig Mautner77878772013-03-04 19:46:24 -08002692 boolean topTaskIsHigher, boolean forceReset, int taskInsertionPoint) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002693 int replyChainEnd = -1;
2694 final int taskId = task.taskId;
2695 final String taskAffinity = task.affinity;
2696
2697 final ArrayList<ActivityRecord> activities = affinityTask.mActivities;
2698 final int numActivities = activities.size();
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07002699 final int rootActivityNdx = affinityTask.findEffectiveRootIndex();
2700
2701 // Do not operate on or below the effective root Activity.
2702 for (int i = numActivities - 1; i > rootActivityNdx; --i) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002703 ActivityRecord target = activities.get(i);
Craig Mautner76ae2f02014-07-16 16:16:19 +00002704 if (target.frontOfTask)
2705 break;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002706
2707 final int flags = target.info.flags;
2708 boolean finishOnTaskLaunch = (flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
2709 boolean allowTaskReparenting = (flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
2710
2711 if (target.resultTo != null) {
2712 // If this activity is sending a reply to a previous
2713 // activity, we can't do anything with it now until
2714 // we reach the start of the reply chain.
2715 // XXX note that we are assuming the result is always
2716 // to the previous activity, which is almost always
2717 // the case but we really shouldn't count on.
2718 if (replyChainEnd < 0) {
2719 replyChainEnd = i;
2720 }
2721 } else if (topTaskIsHigher
2722 && allowTaskReparenting
2723 && taskAffinity != null
2724 && taskAffinity.equals(target.taskAffinity)) {
2725 // This activity has an affinity for our task. Either remove it if we are
2726 // clearing or move it over to our task. Note that
2727 // we currently punt on the case where we are resetting a
2728 // task that is not at the top but who has activities above
2729 // with an affinity to it... this is really not a normal
2730 // case, and we will need to later pull that task to the front
2731 // and usually at that point we will do the reset and pick
2732 // up those remaining activities. (This only happens if
2733 // someone starts an activity in a new task from an activity
2734 // in a task that is not currently on top.)
2735 if (forceReset || finishOnTaskLaunch) {
2736 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002737 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2738 "Finishing task at index " + start + " to " + i);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002739 for (int srcPos = start; srcPos >= i; --srcPos) {
2740 final ActivityRecord p = activities.get(srcPos);
2741 if (p.finishing) {
2742 continue;
2743 }
Todd Kennedy539db512014-12-15 09:57:55 -08002744 finishActivityLocked(
2745 p, Activity.RESULT_CANCELED, null, "move-affinity", false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002746 }
2747 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002748 if (taskInsertionPoint < 0) {
2749 taskInsertionPoint = task.mActivities.size();
Craig Mautnerd2328952013-03-05 12:46:26 -08002750
Craig Mautner77878772013-03-04 19:46:24 -08002751 }
Craig Mautner77878772013-03-04 19:46:24 -08002752
2753 final int start = replyChainEnd >= 0 ? replyChainEnd : i;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002754 if (DEBUG_TASKS) Slog.v(TAG_TASKS,
2755 "Reparenting from task=" + affinityTask + ":" + start + "-" + i
2756 + " to task=" + task + ":" + taskInsertionPoint);
Craig Mautner77878772013-03-04 19:46:24 -08002757 for (int srcPos = start; srcPos >= i; --srcPos) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002758 final ActivityRecord p = activities.get(srcPos);
Craig Mautnera228ae92014-07-09 05:44:55 -07002759 p.setTask(task, null);
Craig Mautner77878772013-03-04 19:46:24 -08002760 task.addActivityAtIndex(taskInsertionPoint, p);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002761
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002762 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
2763 "Removing and adding activity " + p + " to stack at " + task
2764 + " callers=" + Debug.getCallers(3));
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002765 if (DEBUG_TASKS) Slog.v(TAG_TASKS, "Pulling activity " + p
2766 + " from " + srcPos + " in to resetting task " + task);
Wale Ogunwale706ed792015-08-02 10:29:44 -07002767 setAppTask(p, task);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002768 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002769 mWindowManager.moveTaskToTop(taskId);
Craig Mautner9658b312013-02-28 10:55:59 -08002770 if (VALIDATE_TOKENS) {
2771 validateAppTokensLocked();
2772 }
Craig Mautnere3a74d52013-02-22 14:14:58 -08002773
2774 // Now we've moved it in to place... but what if this is
2775 // a singleTop activity and we have put it on top of another
2776 // instance of the same activity? Then we drop the instance
2777 // below so it remains singleTop.
2778 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
2779 ArrayList<ActivityRecord> taskActivities = task.mActivities;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002780 int targetNdx = taskActivities.indexOf(target);
2781 if (targetNdx > 0) {
2782 ActivityRecord p = taskActivities.get(targetNdx - 1);
2783 if (p.intent.getComponent().equals(target.intent.getComponent())) {
Craig Mautner58547802013-03-05 08:23:53 -08002784 finishActivityLocked(p, Activity.RESULT_CANCELED, null, "replace",
2785 false);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002786 }
2787 }
2788 }
2789 }
2790
2791 replyChainEnd = -1;
2792 }
2793 }
Craig Mautner77878772013-03-04 19:46:24 -08002794 return taskInsertionPoint;
Craig Mautnere3a74d52013-02-22 14:14:58 -08002795 }
2796
Craig Mautner8849a5e2013-04-02 16:41:03 -07002797 final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
Craig Mautnere3a74d52013-02-22 14:14:58 -08002798 ActivityRecord newActivity) {
2799 boolean forceReset =
2800 (newActivity.info.flags & ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
2801 if (ACTIVITY_INACTIVE_RESET_TIME > 0
2802 && taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
2803 if ((newActivity.info.flags & ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
2804 forceReset = true;
2805 }
2806 }
2807
2808 final TaskRecord task = taskTop.task;
2809
2810 /** False until we evaluate the TaskRecord associated with taskTop. Switches to true
2811 * for remaining tasks. Used for later tasks to reparent to task. */
2812 boolean taskFound = false;
2813
2814 /** If ActivityOptions are moved out and need to be aborted or moved to taskTop. */
2815 ActivityOptions topOptions = null;
2816
Craig Mautner77878772013-03-04 19:46:24 -08002817 // Preserve the location for reparenting in the new task.
2818 int reparentInsertionPoint = -1;
2819
Craig Mautnere3a74d52013-02-22 14:14:58 -08002820 for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
2821 final TaskRecord targetTask = mTaskHistory.get(i);
2822
2823 if (targetTask == task) {
2824 topOptions = resetTargetTaskIfNeededLocked(task, forceReset);
2825 taskFound = true;
2826 } else {
Craig Mautner77878772013-03-04 19:46:24 -08002827 reparentInsertionPoint = resetAffinityTaskIfNeededLocked(targetTask, task,
2828 taskFound, forceReset, reparentInsertionPoint);
Craig Mautnere3a74d52013-02-22 14:14:58 -08002829 }
2830 }
2831
Craig Mautner70a86932013-02-28 22:37:44 -08002832 int taskNdx = mTaskHistory.indexOf(task);
riddle_hsu1d7919a2015-03-11 17:09:50 +08002833 if (taskNdx >= 0) {
2834 do {
2835 taskTop = mTaskHistory.get(taskNdx--).getTopActivity();
2836 } while (taskTop == null && taskNdx >= 0);
2837 }
Craig Mautner70a86932013-02-28 22:37:44 -08002838
Craig Mautnere3a74d52013-02-22 14:14:58 -08002839 if (topOptions != null) {
2840 // If we got some ActivityOptions from an activity on top that
2841 // was removed from the task, propagate them to the new real top.
2842 if (taskTop != null) {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002843 taskTop.updateOptionsLocked(topOptions);
2844 } else {
Craig Mautnere3a74d52013-02-22 14:14:58 -08002845 topOptions.abort();
2846 }
2847 }
2848
2849 return taskTop;
2850 }
2851
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002852 void sendActivityResultLocked(int callingUid, ActivityRecord r,
2853 String resultWho, int requestCode, int resultCode, Intent data) {
2854
2855 if (callingUid > 0) {
2856 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002857 data, r.getUriPermissionsLocked(), r.userId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002858 }
2859
2860 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
2861 + " : who=" + resultWho + " req=" + requestCode
2862 + " res=" + resultCode + " data=" + data);
2863 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
2864 try {
2865 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2866 list.add(new ResultInfo(resultWho, requestCode,
2867 resultCode, data));
Dianne Hackbornbe707852011-11-11 14:32:10 -08002868 r.app.thread.scheduleSendResult(r.appToken, list);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002869 return;
2870 } catch (Exception e) {
2871 Slog.w(TAG, "Exception thrown sending result to " + r, e);
2872 }
2873 }
2874
2875 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
2876 }
2877
Craig Mautner299f9602015-01-26 09:47:33 -08002878 private void adjustFocusedActivityLocked(ActivityRecord r, String reason) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002879 if (!mStackSupervisor.isFocusedStack(this) || mService.mFocusedActivity != r) {
2880 return;
2881 }
2882
2883 final ActivityRecord next = topRunningActivityLocked();
2884 final String myReason = reason + " adjustFocus";
2885 if (next != r) {
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002886 if (next != null && StackId.keepFocusInStackIfPossible(mStackId) && isFocusable()) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002887 // For freeform, docked, and pinned stacks we always keep the focus within the
2888 // stack as long as there is a running activity in the stack that we can adjust
2889 // focus to.
2890 mService.setFocusedActivityLocked(next, myReason);
2891 return;
2892 } else {
2893 final TaskRecord task = r.task;
2894 if (r.frontOfTask && task == topTask() && task.isOverHomeStack()) {
2895 // For non-fullscreen stack, we want to move the focus to the next visible
2896 // stack to prevent the home screen from moving to the top and obscuring
2897 // other visible stacks.
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002898 if (!mFullscreen && adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwaled045c822015-12-02 09:14:28 -08002899 return;
2900 }
2901 // Move the home stack to the top if this stack is fullscreen or there is no
2902 // other visible stack.
2903 if (mStackSupervisor.moveHomeStackTaskToTop(
2904 task.getTaskToReturnTo(), myReason)) {
2905 // Activity focus was already adjusted. Nothing else to do...
2906 return;
Wale Ogunwaled80c2632015-03-13 10:26:26 -07002907 }
Craig Mautner04f0b702013-10-22 12:31:01 -07002908 }
2909 }
Wale Ogunwaled045c822015-12-02 09:14:28 -08002910 }
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002911
Wale Ogunwaled046a012015-12-24 13:05:59 -08002912 mService.setFocusedActivityLocked(mStackSupervisor.topRunningActivityLocked(), myReason);
Craig Mautner04f0b702013-10-22 12:31:01 -07002913 }
2914
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08002915 private boolean adjustFocusToNextFocusableStackLocked(String reason) {
2916 final ActivityStack stack = getNextFocusableStackLocked();
2917 final String myReason = reason + " adjustFocusToNextFocusableStack";
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002918 if (stack == null) {
2919 return false;
2920 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08002921 return mService.setFocusedActivityLocked(stack.topRunningActivityLocked(), myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08002922 }
2923
Craig Mautnerf3333272013-04-22 10:55:53 -07002924 final void stopActivityLocked(ActivityRecord r) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002925 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Stopping: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002926 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
2927 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
2928 if (!r.finishing) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07002929 if (!mService.isSleeping()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002930 if (DEBUG_STATES) Slog.d(TAG_STATES, "no-history finish of " + r);
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07002931 if (requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
2932 "stop-no-history", false)) {
2933 // Activity was finished, no need to continue trying to schedule stop.
2934 adjustFocusedActivityLocked(r, "stopActivityFinished");
2935 r.resumeKeyDispatchingLocked();
2936 return;
2937 }
Christopher Tated3f175c2012-06-14 14:16:54 -07002938 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002939 if (DEBUG_STATES) Slog.d(TAG_STATES, "Not finishing noHistory " + r
Christopher Tated3f175c2012-06-14 14:16:54 -07002940 + " on stop because we're just sleeping");
2941 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002942 }
Christopher Tate5007ddd2012-06-12 13:08:18 -07002943 }
2944
2945 if (r.app != null && r.app.thread != null) {
Craig Mautner299f9602015-01-26 09:47:33 -08002946 adjustFocusedActivityLocked(r, "stopActivity");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002947 r.resumeKeyDispatchingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002948 try {
2949 r.stopped = false;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002950 if (DEBUG_STATES) Slog.v(TAG_STATES,
2951 "Moving to STOPPING: " + r + " (stop requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002952 r.state = ActivityState.STOPPING;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07002953 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
2954 "Stopping visible=" + r.visible + " for " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002955 if (!r.visible) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002956 mWindowManager.setAppVisibility(r.appToken, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002957 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002958 r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
Craig Mautnere11f2b72013-04-01 12:37:17 -07002959 if (mService.isSleepingOrShuttingDown()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002960 r.setSleeping(true);
2961 }
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07002962 Message msg = mHandler.obtainMessage(STOP_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002963 mHandler.sendMessageDelayed(msg, STOP_TIMEOUT);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002964 } catch (Exception e) {
2965 // Maybe just ignore exceptions here... if the process
2966 // has crashed, our death notification will clean things
2967 // up.
2968 Slog.w(TAG, "Exception thrown during pause", e);
2969 // Just in case, assume it to be stopped.
2970 r.stopped = true;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002971 if (DEBUG_STATES) Slog.v(TAG_STATES, "Stop failed; moving to STOPPED: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002972 r.state = ActivityState.STOPPED;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08002973 if (r.deferRelaunchUntilPaused) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002974 destroyActivityLocked(r, true, "stop-except");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002975 }
2976 }
2977 }
2978 }
Craig Mautner23ac33b2013-04-01 16:26:35 -07002979
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002980 /**
2981 * @return Returns true if the activity is being finished, false if for
2982 * some reason it is being left as-is.
2983 */
2984 final boolean requestFinishActivityLocked(IBinder token, int resultCode,
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07002985 Intent resultData, String reason, boolean oomAdj) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002986 ActivityRecord r = isInStackLocked(token);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07002987 if (DEBUG_RESULTS || DEBUG_STATES) Slog.v(TAG_STATES,
2988 "Finishing activity token=" + token + " r="
Christopher Tated3f175c2012-06-14 14:16:54 -07002989 + ", result=" + resultCode + ", data=" + resultData
2990 + ", reason=" + reason);
Craig Mautnerd74f7d72013-02-26 13:41:02 -08002991 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002992 return false;
2993 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002994
Craig Mautnerd44711d2013-02-23 11:24:36 -08002995 finishActivityLocked(r, resultCode, resultData, reason, oomAdj);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002996 return true;
2997 }
2998
Craig Mautnerd2328952013-03-05 12:46:26 -08002999 final void finishSubActivityLocked(ActivityRecord self, String resultWho, int requestCode) {
Craig Mautner9658b312013-02-28 10:55:59 -08003000 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3001 ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3002 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3003 ActivityRecord r = activities.get(activityNdx);
3004 if (r.resultTo == self && r.requestCode == requestCode) {
3005 if ((r.resultWho == null && resultWho == null) ||
3006 (r.resultWho != null && r.resultWho.equals(resultWho))) {
3007 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "request-sub",
3008 false);
3009 }
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003010 }
3011 }
3012 }
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003013 mService.updateOomAdjLocked();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003014 }
3015
Todd Kennedy539db512014-12-15 09:57:55 -08003016 final void finishTopRunningActivityLocked(ProcessRecord app, String reason) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003017 ActivityRecord r = topRunningActivityLocked();
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003018 if (r != null && r.app == app) {
3019 // If the top running activity is from this crashing
3020 // process, then terminate it to avoid getting in a loop.
3021 Slog.w(TAG, " Force finishing activity "
3022 + r.intent.getComponent().flattenToShortString());
Craig Mautner9658b312013-02-28 10:55:59 -08003023 int taskNdx = mTaskHistory.indexOf(r.task);
3024 int activityNdx = r.task.mActivities.indexOf(r);
Todd Kennedy539db512014-12-15 09:57:55 -08003025 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003026 // Also terminate any activities below it that aren't yet
3027 // stopped, to avoid a situation where one will get
3028 // re-start our crashing activity once it gets resumed again.
Craig Mautner9658b312013-02-28 10:55:59 -08003029 --activityNdx;
3030 if (activityNdx < 0) {
3031 do {
3032 --taskNdx;
3033 if (taskNdx < 0) {
3034 break;
3035 }
3036 activityNdx = mTaskHistory.get(taskNdx).mActivities.size() - 1;
3037 } while (activityNdx < 0);
3038 }
3039 if (activityNdx >= 0) {
3040 r = mTaskHistory.get(taskNdx).mActivities.get(activityNdx);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003041 if (r.state == ActivityState.RESUMED
3042 || r.state == ActivityState.PAUSING
3043 || r.state == ActivityState.PAUSED) {
Craig Mautner4ef26932013-09-18 15:15:52 -07003044 if (!r.isHomeActivity() || mService.mHomeProcess != r.app) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003045 Slog.w(TAG, " Force finishing activity "
3046 + r.intent.getComponent().flattenToShortString());
Todd Kennedy539db512014-12-15 09:57:55 -08003047 finishActivityLocked(r, Activity.RESULT_CANCELED, null, reason, false);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003048 }
3049 }
3050 }
3051 }
3052 }
3053
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07003054 final void finishVoiceTask(IVoiceInteractionSession session) {
3055 IBinder sessionBinder = session.asBinder();
3056 boolean didOne = false;
3057 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3058 TaskRecord tr = mTaskHistory.get(taskNdx);
3059 if (tr.voiceSession != null && tr.voiceSession.asBinder() == sessionBinder) {
3060 for (int activityNdx = tr.mActivities.size() - 1; activityNdx >= 0; --activityNdx) {
3061 ActivityRecord r = tr.mActivities.get(activityNdx);
3062 if (!r.finishing) {
3063 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "finish-voice",
3064 false);
3065 didOne = true;
3066 }
3067 }
3068 }
3069 }
3070 if (didOne) {
3071 mService.updateOomAdjLocked();
3072 }
3073 }
3074
Craig Mautnerd2328952013-03-05 12:46:26 -08003075 final boolean finishActivityAffinityLocked(ActivityRecord r) {
Craig Mautnerd74f7d72013-02-26 13:41:02 -08003076 ArrayList<ActivityRecord> activities = r.task.mActivities;
3077 for (int index = activities.indexOf(r); index >= 0; --index) {
3078 ActivityRecord cur = activities.get(index);
Kenny Roote6585b32013-12-13 12:00:26 -08003079 if (!Objects.equals(cur.taskAffinity, r.taskAffinity)) {
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003080 break;
3081 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003082 finishActivityLocked(cur, Activity.RESULT_CANCELED, null, "request-affinity", true);
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07003083 }
3084 return true;
3085 }
3086
Dianne Hackborn5c607432012-02-28 14:44:19 -08003087 final void finishActivityResultsLocked(ActivityRecord r, int resultCode, Intent resultData) {
3088 // send the result
3089 ActivityRecord resultTo = r.resultTo;
3090 if (resultTo != null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003091 if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "Adding result to " + resultTo
Dianne Hackborn5c607432012-02-28 14:44:19 -08003092 + " who=" + r.resultWho + " req=" + r.requestCode
3093 + " res=" + resultCode + " data=" + resultData);
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003094 if (resultTo.userId != r.userId) {
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003095 if (resultData != null) {
Nicolas Prevot107f7b72015-07-01 16:31:48 +01003096 resultData.prepareToLeaveUser(r.userId);
Nicolas Prevot6b942b82014-06-02 15:20:42 +01003097 }
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003098 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08003099 if (r.info.applicationInfo.uid > 0) {
3100 mService.grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
3101 resultTo.packageName, resultData,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01003102 resultTo.getUriPermissionsLocked(), resultTo.userId);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003103 }
3104 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
3105 resultData);
3106 r.resultTo = null;
3107 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003108 else if (DEBUG_RESULTS) Slog.v(TAG_RESULTS, "No result destination from " + r);
Dianne Hackborn5c607432012-02-28 14:44:19 -08003109
3110 // Make sure this HistoryRecord is not holding on to other resources,
3111 // because clients have remote IPC references to this object so we
3112 // can't assume that will go away and want to avoid circular IPC refs.
3113 r.results = null;
3114 r.pendingResults = null;
3115 r.newIntents = null;
3116 r.icicle = null;
3117 }
3118
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003119 /**
3120 * @return Returns true if this activity has been removed from the history
3121 * list, or false if it is still in the list and will be removed later.
3122 */
Craig Mautnerf3333272013-04-22 10:55:53 -07003123 final boolean finishActivityLocked(ActivityRecord r, int resultCode, Intent resultData,
3124 String reason, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003125 if (r.finishing) {
3126 Slog.w(TAG, "Duplicate finish request for " + r);
3127 return false;
3128 }
3129
Wale Ogunwale7d701172015-03-11 15:36:30 -07003130 r.makeFinishingLocked();
Craig Mautneraea74a52014-03-08 14:23:10 -08003131 final TaskRecord task = r.task;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003132 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003133 r.userId, System.identityHashCode(r),
Craig Mautneraea74a52014-03-08 14:23:10 -08003134 task.taskId, r.shortComponentName, reason);
3135 final ArrayList<ActivityRecord> activities = task.mActivities;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003136 final int index = activities.indexOf(r);
3137 if (index < (activities.size() - 1)) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003138 task.setFrontOfTask();
Craig Mautnerd00f4742014-03-12 14:17:26 -07003139 if ((r.intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003140 // If the caller asked that this activity (and all above it)
3141 // be cleared when the task is reset, don't lose that information,
3142 // but propagate it up to the next activity.
Craig Mautner1aa9d0d3f2013-12-16 15:58:31 -08003143 ActivityRecord next = activities.get(index+1);
Craig Mautnerd44711d2013-02-23 11:24:36 -08003144 next.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003145 }
3146 }
3147
3148 r.pauseKeyDispatchingLocked();
Craig Mautner04f0b702013-10-22 12:31:01 -07003149
Craig Mautner299f9602015-01-26 09:47:33 -08003150 adjustFocusedActivityLocked(r, "finishActivity");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003151
Dianne Hackborn5c607432012-02-28 14:44:19 -08003152 finishActivityResultsLocked(r, resultCode, resultData);
Craig Mautner2420ead2013-04-01 17:13:20 -07003153
Craig Mautnerde4ef022013-04-07 19:01:33 -07003154 if (mResumedActivity == r) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003155 boolean endTask = index <= 0;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003156 if (DEBUG_VISIBILITY || DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003157 "Prepare close transition: finishing " + r);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003158 mWindowManager.prepareAppTransition(endTask
Craig Mautner4b71aa12012-12-27 17:20:01 -08003159 ? AppTransition.TRANSIT_TASK_CLOSE
3160 : AppTransition.TRANSIT_ACTIVITY_CLOSE, false);
Craig Mautner0247fc82013-02-28 14:32:06 -08003161
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003162 // Tell window manager to prepare for this one to be removed.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003163 mWindowManager.setAppVisibility(r.appToken, false);
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003164
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08003165 if (mPausingActivity == null) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003166 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
3167 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
3168 "finish() => pause with userLeaving=false");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003169 startPausingLocked(false, false, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003170 }
3171
Craig Mautneraea74a52014-03-08 14:23:10 -08003172 if (endTask) {
Craig Mautner15df08a2015-04-01 12:17:18 -07003173 mStackSupervisor.removeLockedTaskLocked(task);
Craig Mautneraea74a52014-03-08 14:23:10 -08003174 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003175 } else if (r.state != ActivityState.PAUSING) {
3176 // If the activity is PAUSING, we will complete the finish once
3177 // it is done pausing; else we can just directly finish it here.
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003178 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish not pausing: " + r);
Chong Zhang66ed4c62015-11-12 14:56:28 -08003179 if (r.visible) {
3180 mWindowManager.setAppVisibility(r.appToken, false);
3181 }
Craig Mautnerd44711d2013-02-23 11:24:36 -08003182 return finishCurrentActivityLocked(r, FINISH_AFTER_PAUSE, oomAdj) == null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003183 } else {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003184 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish waiting for pause of: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003185 }
3186
3187 return false;
3188 }
3189
Craig Mautnerf3333272013-04-22 10:55:53 -07003190 static final int FINISH_IMMEDIATELY = 0;
3191 static final int FINISH_AFTER_PAUSE = 1;
3192 static final int FINISH_AFTER_VISIBLE = 2;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003193
Craig Mautnerf3333272013-04-22 10:55:53 -07003194 final ActivityRecord finishCurrentActivityLocked(ActivityRecord r, int mode, boolean oomAdj) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003195 // First things first: if this activity is currently visible,
3196 // and the resumed activity is not yet visible, then hold off on
3197 // finishing until the resumed one becomes visible.
3198 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07003199 if (!mStackSupervisor.mStoppingActivities.contains(r)) {
Filip Gruszczynskief2f72b2015-12-04 14:52:25 -08003200 addToStopping(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003201 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003202 if (DEBUG_STATES) Slog.v(TAG_STATES,
3203 "Moving to STOPPING: "+ r + " (finish requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003204 r.state = ActivityState.STOPPING;
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003205 if (oomAdj) {
3206 mService.updateOomAdjLocked();
3207 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003208 return r;
3209 }
3210
3211 // make sure the record is cleaned out of other places.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003212 mStackSupervisor.mStoppingActivities.remove(r);
Craig Mautner0eea92c2013-05-16 13:35:39 -07003213 mStackSupervisor.mGoingToSleepActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003214 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003215 if (mResumedActivity == r) {
3216 mResumedActivity = null;
3217 }
3218 final ActivityState prevState = r.state;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003219 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to FINISHING: " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003220 r.state = ActivityState.FINISHING;
3221
3222 if (mode == FINISH_IMMEDIATELY
Wale Ogunwale3f529ee2015-07-12 15:14:01 -07003223 || (mode == FINISH_AFTER_PAUSE && prevState == ActivityState.PAUSED)
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003224 || prevState == ActivityState.STOPPED
3225 || prevState == ActivityState.INITIALIZING) {
3226 // If this activity is already stopped, we can just finish
3227 // it right now.
Wale Ogunwale7d701172015-03-11 15:36:30 -07003228 r.makeFinishingLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003229 boolean activityRemoved = destroyActivityLocked(r, true, "finish-imm");
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003230 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003231 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003232 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003233 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS,
Craig Mautnerd163e752014-06-13 17:18:47 -07003234 "destroyActivityLocked: finishCurrentActivityLocked r=" + r +
3235 " destroy returned removed=" + activityRemoved);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003236 return activityRemoved ? null : r;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003237 }
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003238
3239 // Need to go through the full pause cycle to get this
3240 // activity into the stopped state and then finish it.
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003241 if (DEBUG_ALL) Slog.v(TAG, "Enqueueing pending finish: " + r);
Craig Mautnerf3333272013-04-22 10:55:53 -07003242 mStackSupervisor.mFinishingActivities.add(r);
Martin Wallgrenc8733b82011-08-31 12:39:31 +02003243 r.resumeKeyDispatchingLocked();
Wale Ogunwaled046a012015-12-24 13:05:59 -08003244 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003245 return r;
3246 }
3247
Craig Mautneree36c772014-07-16 14:56:05 -07003248 void finishAllActivitiesLocked(boolean immediately) {
3249 boolean noActivitiesInStack = true;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003250 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3251 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3252 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3253 final ActivityRecord r = activities.get(activityNdx);
Craig Mautneree36c772014-07-16 14:56:05 -07003254 noActivitiesInStack = false;
3255 if (r.finishing && !immediately) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003256 continue;
3257 }
Craig Mautneree36c772014-07-16 14:56:05 -07003258 Slog.d(TAG, "finishAllActivitiesLocked: finishing " + r + " immediately");
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003259 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
3260 }
3261 }
Craig Mautneree36c772014-07-16 14:56:05 -07003262 if (noActivitiesInStack) {
3263 mActivityContainer.onTaskListEmptyLocked();
3264 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003265 }
3266
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003267 final boolean shouldUpRecreateTaskLocked(ActivityRecord srec, String destAffinity) {
3268 // Basic case: for simple app-centric recents, we need to recreate
3269 // the task if the affinity has changed.
3270 if (srec == null || srec.task.affinity == null ||
3271 !srec.task.affinity.equals(destAffinity)) {
3272 return true;
3273 }
3274 // Document-centric case: an app may be split in to multiple documents;
3275 // they need to re-create their task if this current activity is the root
3276 // of a document, unless simply finishing it will return them to the the
3277 // correct app behind.
Dianne Hackbornf3eb8432014-09-19 17:21:46 -07003278 if (srec.frontOfTask && srec.task != null && srec.task.getBaseIntent() != null
3279 && srec.task.getBaseIntent().isDocument()) {
Dianne Hackborn6f4d61f2014-08-21 17:50:42 -07003280 // Okay, this activity is at the root of its task. What to do, what to do...
3281 if (srec.task.getTaskToReturnTo() != ActivityRecord.APPLICATION_ACTIVITY_TYPE) {
3282 // Finishing won't return to an application, so we need to recreate.
3283 return true;
3284 }
3285 // We now need to get the task below it to determine what to do.
3286 int taskIdx = mTaskHistory.indexOf(srec.task);
3287 if (taskIdx <= 0) {
3288 Slog.w(TAG, "shouldUpRecreateTask: task not in history for " + srec);
3289 return false;
3290 }
3291 if (taskIdx == 0) {
3292 // At the bottom of the stack, nothing to go back to.
3293 return true;
3294 }
3295 TaskRecord prevTask = mTaskHistory.get(taskIdx);
3296 if (!srec.task.affinity.equals(prevTask.affinity)) {
3297 // These are different apps, so need to recreate.
3298 return true;
3299 }
3300 }
3301 return false;
3302 }
3303
Wale Ogunwale7d701172015-03-11 15:36:30 -07003304 final boolean navigateUpToLocked(ActivityRecord srec, Intent destIntent, int resultCode,
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003305 Intent resultData) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003306 final TaskRecord task = srec.task;
3307 final ArrayList<ActivityRecord> activities = task.mActivities;
3308 final int start = activities.indexOf(srec);
3309 if (!mTaskHistory.contains(task) || (start < 0)) {
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003310 return false;
3311 }
3312 int finishTo = start - 1;
Craig Mautner0247fc82013-02-28 14:32:06 -08003313 ActivityRecord parent = finishTo < 0 ? null : activities.get(finishTo);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003314 boolean foundParentInTask = false;
Craig Mautner0247fc82013-02-28 14:32:06 -08003315 final ComponentName dest = destIntent.getComponent();
3316 if (start > 0 && dest != null) {
3317 for (int i = finishTo; i >= 0; i--) {
3318 ActivityRecord r = activities.get(i);
3319 if (r.info.packageName.equals(dest.getPackageName()) &&
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003320 r.info.name.equals(dest.getClassName())) {
3321 finishTo = i;
3322 parent = r;
3323 foundParentInTask = true;
3324 break;
3325 }
3326 }
3327 }
3328
3329 IActivityController controller = mService.mController;
3330 if (controller != null) {
3331 ActivityRecord next = topRunningActivityLocked(srec.appToken, 0);
3332 if (next != null) {
3333 // ask watcher if this is allowed
3334 boolean resumeOK = true;
3335 try {
3336 resumeOK = controller.activityResuming(next.packageName);
3337 } catch (RemoteException e) {
3338 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07003339 Watchdog.getInstance().setActivityController(null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003340 }
3341
3342 if (!resumeOK) {
3343 return false;
3344 }
3345 }
3346 }
3347 final long origId = Binder.clearCallingIdentity();
3348 for (int i = start; i > finishTo; i--) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003349 ActivityRecord r = activities.get(i);
3350 requestFinishActivityLocked(r.appToken, resultCode, resultData, "navigate-up", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003351 // Only return the supplied result for the first activity finished
3352 resultCode = Activity.RESULT_CANCELED;
3353 resultData = null;
3354 }
3355
3356 if (parent != null && foundParentInTask) {
3357 final int parentLaunchMode = parent.info.launchMode;
3358 final int destIntentFlags = destIntent.getFlags();
3359 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
3360 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
3361 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
3362 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Dianne Hackborn85d558c2014-11-04 10:31:54 -08003363 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent,
3364 srec.packageName);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003365 } else {
3366 try {
3367 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
3368 destIntent.getComponent(), 0, srec.userId);
Filip Gruszczynski303210b2016-01-08 16:28:08 -08003369 int res = mService.mActivityStarter.startActivityLocked(srec.app.thread,
3370 destIntent, null /*ephemeralIntent*/, null, aInfo, null /*rInfo*/, null,
3371 null, parent.appToken, null, 0, -1, parent.launchedFromUid,
Todd Kennedy7440f172015-12-09 14:31:22 -08003372 parent.launchedFromPackage, -1, parent.launchedFromUid, 0, null,
3373 false, true, null, null, null);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003374 foundParentInTask = res == ActivityManager.START_SUCCESS;
3375 } catch (RemoteException e) {
3376 foundParentInTask = false;
3377 }
3378 requestFinishActivityLocked(parent.appToken, resultCode,
Todd Kennedy539db512014-12-15 09:57:55 -08003379 resultData, "navigate-top", true);
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003380 }
3381 }
3382 Binder.restoreCallingIdentity(origId);
3383 return foundParentInTask;
3384 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003385 /**
3386 * Perform the common clean-up of an activity record. This is called both
3387 * as part of destroyActivityLocked() (when destroying the client-side
3388 * representation) and cleaning things up as a result of its hosting
3389 * processing going away, in which case there is no remaining client-side
3390 * state to destroy so only the cleanup here is needed.
Craig Mautneracebdc82015-02-24 10:53:03 -08003391 *
3392 * Note: Call before #removeActivityFromHistoryLocked.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003393 */
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003394 final void cleanUpActivityLocked(ActivityRecord r, boolean cleanServices,
3395 boolean setState) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003396 if (mResumedActivity == r) {
3397 mResumedActivity = null;
3398 }
Craig Mautner1872ce32014-03-28 23:05:42 +00003399 if (mPausingActivity == r) {
3400 mPausingActivity = null;
3401 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003402 mService.resetFocusedActivityIfNeededLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003403
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08003404 r.deferRelaunchUntilPaused = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003405 r.frozenBeforeDestroy = false;
3406
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003407 if (setState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003408 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (cleaning up)");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003409 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003410 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during cleanUp for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003411 r.app = null;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003412 }
3413
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003414 // Make sure this record is no longer in the pending finishes list.
3415 // This could happen, for example, if we are trimming activities
3416 // down to the max limit while they are still waiting to finish.
Craig Mautnerf3333272013-04-22 10:55:53 -07003417 mStackSupervisor.mFinishingActivities.remove(r);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003418 mStackSupervisor.mWaitingVisibleActivities.remove(r);
Craig Mautner2420ead2013-04-01 17:13:20 -07003419
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003420 // Remove any pending results.
3421 if (r.finishing && r.pendingResults != null) {
3422 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
3423 PendingIntentRecord rec = apr.get();
3424 if (rec != null) {
3425 mService.cancelIntentSenderLocked(rec, false);
3426 }
3427 }
3428 r.pendingResults = null;
3429 }
3430
3431 if (cleanServices) {
Craig Mautner2420ead2013-04-01 17:13:20 -07003432 cleanUpActivityServicesLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003433 }
3434
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003435 // Get rid of any pending idle timeouts.
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003436 removeTimeoutsForActivityLocked(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003437 if (getVisibleBehindActivity() == r) {
3438 mStackSupervisor.requestVisibleBehindLocked(r, false);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003439 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003440 }
3441
3442 private void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003443 mStackSupervisor.removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003444 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003445 mHandler.removeMessages(STOP_TIMEOUT_MSG, r);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003446 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003447 r.finishLaunchTickingLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003448 }
3449
Craig Mautner299f9602015-01-26 09:47:33 -08003450 private void removeActivityFromHistoryLocked(ActivityRecord r, String reason) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003451 mStackSupervisor.removeChildActivityContainers(r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003452 finishActivityResultsLocked(r, Activity.RESULT_CANCELED, null);
Wale Ogunwale7d701172015-03-11 15:36:30 -07003453 r.makeFinishingLocked();
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003454 if (DEBUG_ADD_REMOVE) Slog.i(TAG_ADD_REMOVE,
3455 "Removing activity " + r + " from stack callers=" + Debug.getCallers(5));
3456
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003457 r.takeFromHistory();
3458 removeTimeoutsForActivityLocked(r);
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003459 if (DEBUG_STATES) Slog.v(TAG_STATES,
3460 "Moving to DESTROYED: " + r + " (removed from history)");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003461 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003462 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during remove for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003463 r.app = null;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003464 mWindowManager.removeAppToken(r.appToken);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003465 if (VALIDATE_TOKENS) {
3466 validateAppTokensLocked();
3467 }
Craig Mautner312ba862014-02-10 17:55:01 -08003468 final TaskRecord task = r.task;
3469 if (task != null && task.removeActivity(r)) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003470 if (DEBUG_STACK) Slog.i(TAG_STACK,
Craig Mautner312ba862014-02-10 17:55:01 -08003471 "removeActivityFromHistoryLocked: last activity removed from " + this);
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07003472 if (mStackSupervisor.isFocusedStack(this) && task == topTask() &&
Craig Mautner84984fa2014-06-19 11:19:20 -07003473 task.isOverHomeStack()) {
Craig Mautner299f9602015-01-26 09:47:33 -08003474 mStackSupervisor.moveHomeStackTaskToTop(task.getTaskToReturnTo(), reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003475 }
Craig Mautner299f9602015-01-26 09:47:33 -08003476 removeTask(task, reason);
Craig Mautner312ba862014-02-10 17:55:01 -08003477 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003478 cleanUpActivityServicesLocked(r);
3479 r.removeUriPermissionsLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003480 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003481
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003482 /**
3483 * Perform clean-up of service connections in an activity record.
3484 */
3485 final void cleanUpActivityServicesLocked(ActivityRecord r) {
3486 // Throw away any services that have been bound by this activity.
3487 if (r.connections != null) {
3488 Iterator<ConnectionRecord> it = r.connections.iterator();
3489 while (it.hasNext()) {
3490 ConnectionRecord c = it.next();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003491 mService.mServices.removeConnectionLocked(c, null, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003492 }
3493 r.connections = null;
3494 }
3495 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003496
Craig Mautneree2e45a2014-06-27 12:10:03 -07003497 final void scheduleDestroyActivities(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003498 Message msg = mHandler.obtainMessage(DESTROY_ACTIVITIES_MSG);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003499 msg.obj = new ScheduleDestroyArgs(owner, reason);
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003500 mHandler.sendMessage(msg);
3501 }
3502
Craig Mautneree2e45a2014-06-27 12:10:03 -07003503 final void destroyActivitiesLocked(ProcessRecord owner, String reason) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07003504 boolean lastIsOpaque = false;
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003505 boolean activityRemoved = false;
Craig Mautnerd44711d2013-02-23 11:24:36 -08003506 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3507 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3508 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3509 final ActivityRecord r = activities.get(activityNdx);
3510 if (r.finishing) {
3511 continue;
3512 }
3513 if (r.fullscreen) {
3514 lastIsOpaque = true;
3515 }
3516 if (owner != null && r.app != owner) {
3517 continue;
3518 }
3519 if (!lastIsOpaque) {
3520 continue;
3521 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003522 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003523 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Destroying " + r + " in state " + r.state
Craig Mautnerd44711d2013-02-23 11:24:36 -08003524 + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003525 + " pausing=" + mPausingActivity + " for reason " + reason);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003526 if (destroyActivityLocked(r, true, reason)) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08003527 activityRemoved = true;
3528 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003529 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003530 }
3531 }
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003532 if (activityRemoved) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08003533 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003534 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003535 }
3536
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003537 final boolean safelyDestroyActivityLocked(ActivityRecord r, String reason) {
3538 if (r.isDestroyable()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003539 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
3540 "Destroying " + r + " in state " + r.state + " resumed=" + mResumedActivity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003541 + " pausing=" + mPausingActivity + " for reason " + reason);
3542 return destroyActivityLocked(r, true, reason);
3543 }
3544 return false;
3545 }
3546
3547 final int releaseSomeActivitiesLocked(ProcessRecord app, ArraySet<TaskRecord> tasks,
3548 String reason) {
3549 // Iterate over tasks starting at the back (oldest) first.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003550 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Trying to release some activities in " + app);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003551 int maxTasks = tasks.size() / 4;
3552 if (maxTasks < 1) {
3553 maxTasks = 1;
3554 }
3555 int numReleased = 0;
3556 for (int taskNdx = 0; taskNdx < mTaskHistory.size() && maxTasks > 0; taskNdx++) {
3557 final TaskRecord task = mTaskHistory.get(taskNdx);
3558 if (!tasks.contains(task)) {
3559 continue;
3560 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003561 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE, "Looking for activities to release in " + task);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003562 int curNum = 0;
3563 final ArrayList<ActivityRecord> activities = task.mActivities;
3564 for (int actNdx = 0; actNdx < activities.size(); actNdx++) {
3565 final ActivityRecord activity = activities.get(actNdx);
3566 if (activity.app == app && activity.isDestroyable()) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003567 if (DEBUG_RELEASE) Slog.v(TAG_RELEASE, "Destroying " + activity
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003568 + " in state " + activity.state + " resumed=" + mResumedActivity
3569 + " pausing=" + mPausingActivity + " for reason " + reason);
3570 destroyActivityLocked(activity, true, reason);
3571 if (activities.get(actNdx) != activity) {
3572 // Was removed from list, back up so we don't miss the next one.
3573 actNdx--;
3574 }
3575 curNum++;
3576 }
3577 }
3578 if (curNum > 0) {
3579 numReleased += curNum;
3580 maxTasks--;
3581 if (mTaskHistory.get(taskNdx) != task) {
3582 // The entire task got removed, back up so we don't miss the next one.
3583 taskNdx--;
3584 }
3585 }
3586 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003587 if (DEBUG_RELEASE) Slog.d(TAG_RELEASE,
3588 "Done releasing: did " + numReleased + " activities");
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003589 return numReleased;
3590 }
3591
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003592 /**
3593 * Destroy the current CLIENT SIDE instance of an activity. This may be
3594 * called both when actually finishing an activity, or when performing
3595 * a configuration switch where we destroy the current client-side object
3596 * but then create a new client-side object for this same HistoryRecord.
3597 */
Craig Mautneree2e45a2014-06-27 12:10:03 -07003598 final boolean destroyActivityLocked(ActivityRecord r, boolean removeFromApp, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003599 if (DEBUG_SWITCH || DEBUG_CLEANUP) Slog.v(TAG_SWITCH,
3600 "Removing activity from " + reason + ": token=" + r
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003601 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003602 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003603 r.userId, System.identityHashCode(r),
Dianne Hackborn28695e02011-11-02 21:59:51 -07003604 r.task.taskId, r.shortComponentName, reason);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003605
3606 boolean removedFromHistory = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003607
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003608 cleanUpActivityLocked(r, false, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003609
3610 final boolean hadApp = r.app != null;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003611
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003612 if (hadApp) {
3613 if (removeFromApp) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003614 r.app.activities.remove(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003615 if (mService.mHeavyWeightProcess == r.app && r.app.activities.size() <= 0) {
3616 mService.mHeavyWeightProcess = null;
3617 mService.mHandler.sendEmptyMessage(
3618 ActivityManagerService.CANCEL_HEAVY_NOTIFICATION_MSG);
3619 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07003620 if (r.app.activities.isEmpty()) {
Dianne Hackborn465fa392014-09-14 14:21:18 -07003621 // Update any services we are bound to that might care about whether
3622 // their client may have activities.
3623 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07003624 // No longer have activities, so update LRU list and oom adj.
Dianne Hackborndb926082013-10-31 16:32:44 -07003625 mService.updateLruProcessLocked(r.app, false, null);
Dianne Hackborn2d1b3782012-09-09 17:49:39 -07003626 mService.updateOomAdjLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003627 }
3628 }
3629
3630 boolean skipDestroy = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003631
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003632 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003633 if (DEBUG_SWITCH) Slog.i(TAG_SWITCH, "Destroying: " + r);
Dianne Hackbornbe707852011-11-11 14:32:10 -08003634 r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003635 r.configChangeFlags);
3636 } catch (Exception e) {
3637 // We can just ignore exceptions here... if the process
3638 // has crashed, our death notification will clean things
3639 // up.
3640 //Slog.w(TAG, "Exception thrown during finish", e);
3641 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003642 removeActivityFromHistoryLocked(r, reason + " exceptionInScheduleDestroy");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003643 removedFromHistory = true;
3644 skipDestroy = true;
3645 }
3646 }
3647
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003648 r.nowVisible = false;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003649
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003650 // If the activity is finishing, we need to wait on removing it
3651 // from the list to give it a chance to do its cleanup. During
3652 // that time it may make calls back with its token so we need to
3653 // be able to find it on the list and so we don't want to remove
3654 // it from the list yet. Otherwise, we can just immediately put
3655 // it in the destroyed state since we are not removing it from the
3656 // list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003657 if (r.finishing && !skipDestroy) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003658 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYING: " + r
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003659 + " (destroy requested)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003660 r.state = ActivityState.DESTROYING;
Craig Mautnerf7bfefb2013-05-16 17:30:44 -07003661 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003662 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
3663 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003664 if (DEBUG_STATES) Slog.v(TAG_STATES,
3665 "Moving to DESTROYED: " + r + " (destroy skipped)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003666 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003667 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003668 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003669 }
3670 } else {
3671 // remove this record from the history.
3672 if (r.finishing) {
Craig Mautner299f9602015-01-26 09:47:33 -08003673 removeActivityFromHistoryLocked(r, reason + " hadNoApp");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003674 removedFromHistory = true;
3675 } else {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003676 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to DESTROYED: " + r + " (no app)");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003677 r.state = ActivityState.DESTROYED;
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003678 if (DEBUG_APP) Slog.v(TAG_APP, "Clearing app during destroy for activity " + r);
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003679 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003680 }
3681 }
3682
3683 r.configChangeFlags = 0;
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003684
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003685 if (!mLRUActivities.remove(r) && hadApp) {
3686 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
3687 }
Craig Mautnerdbcb31f2013-04-02 12:32:53 -07003688
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003689 return removedFromHistory;
3690 }
3691
Craig Mautner299f9602015-01-26 09:47:33 -08003692 final void activityDestroyedLocked(IBinder token, String reason) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003693 final long origId = Binder.clearCallingIdentity();
3694 try {
Wale Ogunwale7d701172015-03-11 15:36:30 -07003695 ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautnerd2328952013-03-05 12:46:26 -08003696 if (r != null) {
3697 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003698 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003699 if (DEBUG_CONTAINERS) Slog.d(TAG_CONTAINERS, "activityDestroyedLocked: r=" + r);
Craig Mautnerd2328952013-03-05 12:46:26 -08003700
Wale Ogunwale60454db2015-01-23 16:05:07 -08003701 if (isInStackLocked(r) != null) {
Craig Mautnerd2328952013-03-05 12:46:26 -08003702 if (r.state == ActivityState.DESTROYING) {
3703 cleanUpActivityLocked(r, true, false);
Craig Mautner299f9602015-01-26 09:47:33 -08003704 removeActivityFromHistoryLocked(r, reason);
Craig Mautnerd2328952013-03-05 12:46:26 -08003705 }
3706 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003707 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautnerd2328952013-03-05 12:46:26 -08003708 } finally {
3709 Binder.restoreCallingIdentity(origId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003710 }
3711 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07003712
Todd Kennedyaab56db2015-01-30 09:39:53 -08003713 void releaseBackgroundResources(ActivityRecord r) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003714 if (hasVisibleBehindActivity() &&
3715 !mHandler.hasMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG)) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003716 if (r == topRunningActivityLocked()) {
Craig Mautner64ccb702014-10-01 09:38:40 -07003717 // Don't release the top activity if it has requested to run behind the next
3718 // activity.
3719 return;
3720 }
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003721 if (DEBUG_STATES) Slog.d(TAG_STATES, "releaseBackgroundResources activtyDisplay=" +
Jose Lima4b6c6692014-08-12 17:41:12 -07003722 mActivityContainer.mActivityDisplay + " visibleBehind=" + r + " app=" + r.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07003723 " thread=" + r.app.thread);
3724 if (r != null && r.app != null && r.app.thread != null) {
3725 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07003726 r.app.thread.scheduleCancelVisibleBehind(r.appToken);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003727 } catch (RemoteException e) {
3728 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003729 mHandler.sendEmptyMessageDelayed(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG, 500);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003730 } else {
Jose Lima4b6c6692014-08-12 17:41:12 -07003731 Slog.e(TAG, "releaseBackgroundResources: activity " + r + " no longer running");
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003732 backgroundResourcesReleased();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003733 }
3734 }
3735 }
3736
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003737 final void backgroundResourcesReleased() {
Jose Lima4b6c6692014-08-12 17:41:12 -07003738 mHandler.removeMessages(RELEASE_BACKGROUND_RESOURCES_TIMEOUT_MSG);
3739 final ActivityRecord r = getVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003740 if (r != null) {
3741 mStackSupervisor.mStoppingActivities.add(r);
Jose Lima4b6c6692014-08-12 17:41:12 -07003742 setVisibleBehindActivity(null);
Wale Ogunwale1f4c02b2014-11-13 14:22:32 -08003743 mStackSupervisor.scheduleIdleTimeoutLocked(null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003744 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08003745 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003746 }
3747
Jose Lima4b6c6692014-08-12 17:41:12 -07003748 boolean hasVisibleBehindActivity() {
3749 return isAttached() && mActivityContainer.mActivityDisplay.hasVisibleBehindActivity();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003750 }
3751
Jose Lima4b6c6692014-08-12 17:41:12 -07003752 void setVisibleBehindActivity(ActivityRecord r) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003753 if (isAttached()) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003754 mActivityContainer.mActivityDisplay.setVisibleBehindActivity(r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003755 }
3756 }
3757
Jose Lima4b6c6692014-08-12 17:41:12 -07003758 ActivityRecord getVisibleBehindActivity() {
3759 return isAttached() ? mActivityContainer.mActivityDisplay.mVisibleBehindActivity : null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003760 }
3761
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003762 private void removeHistoryRecordsForAppLocked(ArrayList<ActivityRecord> list,
3763 ProcessRecord app, String listName) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003764 int i = list.size();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003765 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3766 "Removing app " + app + " from list " + listName + " with " + i + " entries");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003767 while (i > 0) {
3768 i--;
Craig Mautner05d6272ba2013-02-11 09:39:27 -08003769 ActivityRecord r = list.get(i);
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003770 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "Record #" + i + " " + r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003771 if (r.app == app) {
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003772 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP, "---> REMOVING this entry!");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003773 list.remove(i);
Dianne Hackborn42e620c2012-06-24 13:20:51 -07003774 removeTimeoutsForActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003775 }
3776 }
3777 }
3778
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003779 boolean removeHistoryRecordsForAppLocked(ProcessRecord app) {
3780 removeHistoryRecordsForAppLocked(mLRUActivities, app, "mLRUActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003781 removeHistoryRecordsForAppLocked(mStackSupervisor.mStoppingActivities, app,
3782 "mStoppingActivities");
Craig Mautner0eea92c2013-05-16 13:35:39 -07003783 removeHistoryRecordsForAppLocked(mStackSupervisor.mGoingToSleepActivities, app,
3784 "mGoingToSleepActivities");
Craig Mautnerde4ef022013-04-07 19:01:33 -07003785 removeHistoryRecordsForAppLocked(mStackSupervisor.mWaitingVisibleActivities, app,
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003786 "mWaitingVisibleActivities");
Craig Mautnerf3333272013-04-22 10:55:53 -07003787 removeHistoryRecordsForAppLocked(mStackSupervisor.mFinishingActivities, app,
3788 "mFinishingActivities");
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003789
3790 boolean hasVisibleActivities = false;
3791
3792 // Clean out the history list.
Craig Mautner0247fc82013-02-28 14:32:06 -08003793 int i = numActivities();
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003794 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3795 "Removing app " + app + " from history with " + i + " entries");
Craig Mautner0247fc82013-02-28 14:32:06 -08003796 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
3797 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
3798 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
3799 final ActivityRecord r = activities.get(activityNdx);
3800 --i;
Wale Ogunwalee23149f2015-03-06 15:39:44 -08003801 if (DEBUG_CLEANUP) Slog.v(TAG_CLEANUP,
3802 "Record #" + i + " " + r + ": app=" + r.app);
Craig Mautner0247fc82013-02-28 14:32:06 -08003803 if (r.app == app) {
riddle_hsu558e8492015-04-02 16:43:13 +08003804 if (r.visible) {
3805 hasVisibleActivities = true;
3806 }
Craig Mautneracebdc82015-02-24 10:53:03 -08003807 final boolean remove;
Craig Mautner0247fc82013-02-28 14:32:06 -08003808 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
3809 // Don't currently have state for the activity, or
3810 // it is finishing -- always remove it.
3811 remove = true;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003812 } else if (!r.visible && r.launchCount > 2 &&
3813 r.lastLaunchTime > (SystemClock.uptimeMillis() - 60000)) {
Craig Mautner0247fc82013-02-28 14:32:06 -08003814 // We have launched this activity too many times since it was
3815 // able to run, so give up and remove it.
Chong Zhang112eb8c2015-11-02 11:17:00 -08003816 // (Note if the activity is visible, we don't remove the record.
3817 // We leave the dead window on the screen but the process will
3818 // not be restarted unless user explicitly tap on it.)
Craig Mautner0247fc82013-02-28 14:32:06 -08003819 remove = true;
3820 } else {
3821 // The process may be gone, but the activity lives on!
3822 remove = false;
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003823 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003824 if (remove) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003825 if (DEBUG_ADD_REMOVE || DEBUG_CLEANUP) Slog.i(TAG_ADD_REMOVE,
3826 "Removing activity " + r + " from stack at " + i
3827 + ": haveState=" + r.haveState
3828 + " stateNotNeeded=" + r.stateNotNeeded
3829 + " finishing=" + r.finishing
3830 + " state=" + r.state + " callers=" + Debug.getCallers(5));
Craig Mautner0247fc82013-02-28 14:32:06 -08003831 if (!r.finishing) {
3832 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
3833 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
3834 r.userId, System.identityHashCode(r),
3835 r.task.taskId, r.shortComponentName,
3836 "proc died without state saved");
Jeff Sharkey5782da72013-04-25 14:32:30 -07003837 if (r.state == ActivityState.RESUMED) {
3838 mService.updateUsageStats(r, false);
3839 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003840 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003841 } else {
3842 // We have the current state for this activity, so
3843 // it can be restarted later when needed.
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003844 if (DEBUG_ALL) Slog.v(TAG, "Keeping entry, setting app to null");
3845 if (DEBUG_APP) Slog.v(TAG_APP,
3846 "Clearing app during removeHistory for activity " + r);
Craig Mautner0247fc82013-02-28 14:32:06 -08003847 r.app = null;
Chong Zhang112eb8c2015-11-02 11:17:00 -08003848 // Set nowVisible to previous visible state. If the app was visible while
3849 // it died, we leave the dead window on screen so it's basically visible.
3850 // This is needed when user later tap on the dead window, we need to stop
3851 // other apps when user transfers focus to the restarted activity.
3852 r.nowVisible = r.visible;
Craig Mautner0247fc82013-02-28 14:32:06 -08003853 if (!r.haveState) {
Wale Ogunwale0fc365c2015-05-25 19:35:42 -07003854 if (DEBUG_SAVED_STATE) Slog.i(TAG_SAVED_STATE,
Craig Mautner0247fc82013-02-28 14:32:06 -08003855 "App died, clearing saved state of " + r);
3856 r.icicle = null;
3857 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003858 }
Craig Mautnerd2328952013-03-05 12:46:26 -08003859 cleanUpActivityLocked(r, true, true);
Craig Mautneracebdc82015-02-24 10:53:03 -08003860 if (remove) {
3861 removeActivityFromHistoryLocked(r, "appDied");
3862 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003863 }
Dianne Hackborncc5a0552012-10-01 16:32:39 -07003864 }
3865 }
3866
3867 return hasVisibleActivities;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003868 }
Craig Mautner0247fc82013-02-28 14:32:06 -08003869
Chong Zhang280d3322015-11-03 17:27:26 -08003870 final void updateTransitLocked(int transit, ActivityOptions options) {
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003871 if (options != null) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003872 ActivityRecord r = topRunningActivityLocked();
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003873 if (r != null && r.state != ActivityState.RESUMED) {
3874 r.updateOptionsLocked(options);
3875 } else {
3876 ActivityOptions.abort(options);
3877 }
3878 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003879 mWindowManager.prepareAppTransition(transit, false);
Dianne Hackborn7f58b952012-04-18 12:59:29 -07003880 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003881
Craig Mautner21d24a22014-04-23 11:45:37 -07003882 void updateTaskMovement(TaskRecord task, boolean toFront) {
3883 if (task.isPersistable) {
3884 task.mLastTimeMoved = System.currentTimeMillis();
3885 // Sign is used to keep tasks sorted when persisted. Tasks sent to the bottom most
3886 // recently will be most negative, tasks sent to the bottom before that will be less
3887 // negative. Similarly for recent tasks moved to the top which will be most positive.
3888 if (!toFront) {
3889 task.mLastTimeMoved *= -1;
3890 }
3891 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07003892 mStackSupervisor.invalidateTaskLayers();
Craig Mautner21d24a22014-04-23 11:45:37 -07003893 }
3894
Craig Mautner84984fa2014-06-19 11:19:20 -07003895 void moveHomeStackTaskToTop(int homeStackTaskType) {
Craig Mautnera82aa092013-09-13 15:34:08 -07003896 final int top = mTaskHistory.size() - 1;
3897 for (int taskNdx = top; taskNdx >= 0; --taskNdx) {
3898 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07003899 if (task.taskType == homeStackTaskType) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003900 if (DEBUG_TASKS || DEBUG_STACK) Slog.d(TAG_STACK,
3901 "moveHomeStackTaskToTop: moving " + task);
Craig Mautnera82aa092013-09-13 15:34:08 -07003902 mTaskHistory.remove(taskNdx);
3903 mTaskHistory.add(top, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07003904 updateTaskMovement(task, true);
Craig Mautnera82aa092013-09-13 15:34:08 -07003905 return;
3906 }
3907 }
3908 }
3909
Chong Zhang280d3322015-11-03 17:27:26 -08003910 final void moveTaskToFrontLocked(TaskRecord tr, boolean noAnimation, ActivityOptions options,
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003911 AppTimeTracker timeTracker, String reason) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003912 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "moveTaskToFront: " + tr);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003913
Craig Mautner11bf9a52013-02-19 14:08:51 -08003914 final int numTasks = mTaskHistory.size();
3915 final int index = mTaskHistory.indexOf(tr);
Craig Mautner86d67a42013-05-14 10:34:38 -07003916 if (numTasks == 0 || index < 0) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003917 // nothing to do!
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003918 if (noAnimation) {
Craig Mautner11bf9a52013-02-19 14:08:51 -08003919 ActivityOptions.abort(options);
3920 } else {
3921 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
3922 }
3923 return;
3924 }
3925
Dianne Hackbornb5a380d2015-05-20 18:18:46 -07003926 if (timeTracker != null) {
3927 // The caller wants a time tracker associated with this task.
3928 for (int i = tr.mActivities.size() - 1; i >= 0; i--) {
3929 tr.mActivities.get(i).appTimeTracker = timeTracker;
3930 }
3931 }
3932
Craig Mautner11bf9a52013-02-19 14:08:51 -08003933 // Shift all activities with this task up to the top
3934 // of the stack, keeping them in the same internal order.
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07003935 insertTaskAtTop(tr, null);
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003936
Chong Zhang45c25ce2015-08-10 22:18:26 -07003937 // Don't refocus if invisible to current user
3938 ActivityRecord top = tr.getTopActivity();
3939 if (!okToShowLocked(top)) {
3940 addRecentActivityLocked(top);
3941 ActivityOptions.abort(options);
3942 return;
3943 }
3944
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003945 // Set focus to the top running activity of this stack.
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07003946 ActivityRecord r = topRunningActivityLocked();
Wale Ogunwaled80c2632015-03-13 10:26:26 -07003947 mService.setFocusedActivityLocked(r, reason);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003948
Wale Ogunwaleee006da2015-03-30 14:49:25 -07003949 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to front transition: task=" + tr);
Craig Mautner8f5f7e92015-01-26 18:03:13 -08003950 if (noAnimation) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003951 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003952 if (r != null) {
3953 mNoAnimActivities.add(r);
3954 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003955 ActivityOptions.abort(options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003956 } else {
Craig Mautner4b71aa12012-12-27 17:20:01 -08003957 updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003958 }
Craig Mautner30e2d722013-02-12 11:30:16 -08003959
Wale Ogunwaled046a012015-12-24 13:05:59 -08003960 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Craig Mautner58547802013-03-05 08:23:53 -08003961 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, tr.taskId);
Craig Mautner11bf9a52013-02-19 14:08:51 -08003962
3963 if (VALIDATE_TOKENS) {
3964 validateAppTokensLocked();
3965 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003966 }
3967
3968 /**
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003969 * Worker method for rearranging history stack. Implements the function of moving all
3970 * activities for a specific task (gathering them if disjoint) into a single group at the
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003971 * bottom of the stack.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003972 *
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003973 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
3974 * to premeptively cancel the move.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003975 *
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003976 * @param taskId The taskId to collect and move to the bottom.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003977 * @return Returns true if the move completed, false if not.
3978 */
Craig Mautner299f9602015-01-26 09:47:33 -08003979 final boolean moveTaskToBackLocked(int taskId) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003980 final TaskRecord tr = taskForIdLocked(taskId);
3981 if (tr == null) {
3982 Slog.i(TAG, "moveTaskToBack: bad taskId=" + taskId);
3983 return false;
3984 }
3985
3986 Slog.i(TAG, "moveTaskToBack: " + tr);
Craig Mautner15df08a2015-04-01 12:17:18 -07003987 mStackSupervisor.removeLockedTaskLocked(tr);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08003988
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003989 // If we have a watcher, preflight the move before committing to it. First check
3990 // for *other* available tasks, but if none are available, then try again allowing the
3991 // current task to be selected.
Craig Mautnerde4ef022013-04-07 19:01:33 -07003992 if (mStackSupervisor.isFrontStack(this) && mService.mController != null) {
Craig Mautnerae7ecab2013-09-18 11:48:14 -07003993 ActivityRecord next = topRunningActivityLocked(null, taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003994 if (next == null) {
3995 next = topRunningActivityLocked(null, 0);
3996 }
3997 if (next != null) {
3998 // ask watcher if this is allowed
3999 boolean moveOK = true;
4000 try {
4001 moveOK = mService.mController.activityResuming(next.packageName);
4002 } catch (RemoteException e) {
4003 mService.mController = null;
Kenny Rootadd58212013-05-07 09:47:34 -07004004 Watchdog.getInstance().setActivityController(null);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004005 }
4006 if (!moveOK) {
4007 return false;
4008 }
4009 }
4010 }
4011
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004012 if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare to back transition: task=" + taskId);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004013
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004014 if (mStackId == HOME_STACK_ID && topTask().isHomeTask()) {
4015 // For the case where we are moving the home task back and there is an activity visible
4016 // behind it on the fullscreen stack, we want to move the focus to the visible behind
4017 // activity to maintain order with what the user is seeing.
4018 final ActivityStack fullscreenStack =
4019 mStackSupervisor.getStack(FULLSCREEN_WORKSPACE_STACK_ID);
4020 if (fullscreenStack != null && fullscreenStack.hasVisibleBehindActivity()) {
4021 final ActivityRecord visibleBehind = fullscreenStack.getVisibleBehindActivity();
4022 mService.setFocusedActivityLocked(visibleBehind, "moveTaskToBack");
Wale Ogunwaled046a012015-12-24 13:05:59 -08004023 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwale285ccef2015-12-11 14:56:37 -08004024 return true;
4025 }
4026 }
4027
riddle_hsuc215a4f2014-12-27 12:10:45 +08004028 boolean prevIsHome = false;
Wale Ogunwale42709242015-08-11 13:54:42 -07004029
4030 // If true, we should resume the home activity next if the task we are moving to the
4031 // back is over the home stack. We force to false if the task we are moving to back
4032 // is the home task and we don't want it resumed after moving to the back.
4033 final boolean canGoHome = !tr.isHomeTask() && tr.isOverHomeStack();
4034 if (canGoHome) {
riddle_hsuc215a4f2014-12-27 12:10:45 +08004035 final TaskRecord nextTask = getNextTask(tr);
4036 if (nextTask != null) {
4037 nextTask.setTaskToReturnTo(tr.getTaskToReturnTo());
4038 } else {
4039 prevIsHome = true;
4040 }
4041 }
Craig Mautner11bf9a52013-02-19 14:08:51 -08004042 mTaskHistory.remove(tr);
4043 mTaskHistory.add(0, tr);
Craig Mautner21d24a22014-04-23 11:45:37 -07004044 updateTaskMovement(tr, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004045
Craig Mautnerc8143c62013-09-03 12:15:57 -07004046 // There is an assumption that moving a task to the back moves it behind the home activity.
4047 // We make sure here that some activity in the stack will launch home.
Craig Mautnerc8143c62013-09-03 12:15:57 -07004048 int numTasks = mTaskHistory.size();
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004049 for (int taskNdx = numTasks - 1; taskNdx >= 1; --taskNdx) {
4050 final TaskRecord task = mTaskHistory.get(taskNdx);
Craig Mautner84984fa2014-06-19 11:19:20 -07004051 if (task.isOverHomeStack()) {
Craig Mautnerc8143c62013-09-03 12:15:57 -07004052 break;
4053 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004054 if (taskNdx == 1) {
4055 // Set the last task before tr to go to home.
Craig Mautner84984fa2014-06-19 11:19:20 -07004056 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004057 }
Craig Mautnerc8143c62013-09-03 12:15:57 -07004058 }
4059
Craig Mautner299f9602015-01-26 09:47:33 -08004060 mWindowManager.prepareAppTransition(AppTransition.TRANSIT_TASK_TO_BACK, false);
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004061 mWindowManager.moveTaskToBottom(taskId);
Craig Mautnerb44de0d2013-02-21 20:00:58 -08004062
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004063 if (VALIDATE_TOKENS) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08004064 validateAppTokensLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004065 }
4066
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004067 final TaskRecord task = mResumedActivity != null ? mResumedActivity.task : null;
Wale Ogunwale42709242015-08-11 13:54:42 -07004068 if (prevIsHome || (task == tr && canGoHome) || (numTasks <= 1 && isOnHomeDisplay())) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07004069 if (!mService.mBooting && !mService.mBooted) {
4070 // Not ready yet!
4071 return false;
4072 }
Craig Mautner84984fa2014-06-19 11:19:20 -07004073 final int taskToReturnTo = tr.getTaskToReturnTo();
4074 tr.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
Craig Mautner299f9602015-01-26 09:47:33 -08004075 return mStackSupervisor.resumeHomeStackTask(taskToReturnTo, null, "moveTaskToBack");
Craig Mautnerde4ef022013-04-07 19:01:33 -07004076 }
4077
Wale Ogunwaled046a012015-12-24 13:05:59 -08004078 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004079 return true;
4080 }
Dianne Hackborn15491c62012-09-19 10:59:14 -07004081
Craig Mautner8849a5e2013-04-02 16:41:03 -07004082 static final void logStartActivity(int tag, ActivityRecord r,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004083 TaskRecord task) {
Santos Cordon73ff7d82013-03-06 17:24:11 -08004084 final Uri data = r.intent.getData();
4085 final String strData = data != null ? data.toSafeString() : null;
4086
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004087 EventLog.writeEvent(tag,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004088 r.userId, System.identityHashCode(r), task.taskId,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004089 r.shortComponentName, r.intent.getAction(),
Santos Cordon73ff7d82013-03-06 17:24:11 -08004090 r.intent.getType(), strData, r.intent.getFlags());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004091 }
4092
4093 /**
4094 * Make sure the given activity matches the current configuration. Returns
4095 * false if the activity had to be destroyed. Returns true if the
4096 * configuration is the same, or the activity will remain running as-is
4097 * for whatever reason. Ensures the HistoryRecord is updated with the
4098 * correct configuration and all other bookkeeping is handled.
4099 */
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004100 final boolean ensureActivityConfigurationLocked(ActivityRecord r, int globalChanges,
4101 boolean preserveWindow) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004102 if (mConfigWillChange) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004103 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004104 "Skipping config check (will change): " + r);
4105 return true;
4106 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004107
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004108 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004109 "Ensuring correct configuration: " + r);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004110
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004111 // Short circuit: if the two configurations are the exact same
4112 // object (the common case), then there is nothing to do.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004113 final Configuration newConfig = mService.mConfiguration;
4114 final Configuration taskConfig = r.task.mOverrideConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004115 if (r.configuration == newConfig
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004116 && r.taskConfigOverride == taskConfig
Wale Ogunwale60454db2015-01-23 16:05:07 -08004117 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004118 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004119 "Configuration unchanged in " + r);
4120 return true;
4121 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004122
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004123 // We don't worry about activities that are finishing.
4124 if (r.finishing) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004125 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004126 "Configuration doesn't matter in finishing " + r);
4127 r.stopFreezingScreenLocked(false);
4128 return true;
4129 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004130
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004131 // Okay we now are going to make this activity have the new config.
4132 // But then we need to figure out how it needs to deal with that.
Wale Ogunwale60454db2015-01-23 16:05:07 -08004133 final Configuration oldConfig = r.configuration;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004134 final Configuration oldTaskOverride = r.taskConfigOverride;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004135 r.configuration = newConfig;
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004136 r.taskConfigOverride = taskConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004137
Filip Gruszczynski23493322015-07-29 17:02:59 -07004138 int taskChanges = getTaskConfigurationChanges(r, taskConfig, oldTaskOverride);
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004139 final int changes = oldConfig.diff(newConfig) | taskChanges;
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004140 if (changes == 0 && !r.forceNewConfig) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004141 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004142 "Configuration no differences in " + r);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004143 // There are no significant differences, so we won't relaunch but should still deliver
4144 // the new configuration to the client process.
4145 r.scheduleConfigurationChanged(taskConfig, false);
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004146 return true;
4147 }
4148
Filip Gruszczynskibc5a6c52015-09-22 13:13:24 -07004149 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4150 "Configuration changes for " + r + " ; taskChanges="
4151 + Configuration.configurationDiffToString(taskChanges) + ", allChanges="
4152 + Configuration.configurationDiffToString(changes));
4153
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004154 // If the activity isn't currently running, just leave the new
4155 // configuration and it will pick that up next time it starts.
4156 if (r.app == null || r.app.thread == null) {
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 "Configuration doesn't matter not running " + r);
4159 r.stopFreezingScreenLocked(false);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004160 r.forceNewConfig = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004161 return true;
4162 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004163
Dianne Hackborn58f42a52011-10-10 13:46:34 -07004164 // Figure out how to handle the changes between the configurations.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004165 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
4166 "Checking to restart " + r.info.name + ": changed=0x"
4167 + Integer.toHexString(changes) + ", handles=0x"
Filip Gruszczynskica664812015-12-04 12:43:36 -08004168 + Integer.toHexString(r.info.getRealConfigChanged()) + ", newConfig=" + newConfig
4169 + ", taskConfig=" + taskConfig);
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004170
Dianne Hackborne6676352011-06-01 16:51:20 -07004171 if ((changes&(~r.info.getRealConfigChanged())) != 0 || r.forceNewConfig) {
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004172 // Aha, the activity isn't handling the change, so DIE DIE DIE.
4173 r.configChangeFlags |= changes;
4174 r.startFreezingScreenLocked(r.app, globalChanges);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004175 r.forceNewConfig = false;
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004176 preserveWindow &= isResizeOnlyChange(changes);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004177 if (r.app == null || r.app.thread == null) {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004178 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004179 "Config is destroying non-running " + r);
Craig Mautneree2e45a2014-06-27 12:10:03 -07004180 destroyActivityLocked(r, true, "config");
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004181 } else if (r.state == ActivityState.PAUSING) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004182 // A little annoying: we are waiting for this activity to finish pausing. Let's not
4183 // do anything now, but just flag that it needs to be restarted when done pausing.
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004184 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004185 "Config is skipping already pausing " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004186 r.deferRelaunchUntilPaused = true;
4187 r.preserveWindowOnDeferredRelaunch = preserveWindow;
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004188 return true;
4189 } else if (r.state == ActivityState.RESUMED) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004190 // Try to optimize this case: the configuration is changing and we need to restart
4191 // the top, resumed activity. Instead of doing the normal handshaking, just say
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004192 // "restart!".
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004193 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004194 "Config is relaunching resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004195 relaunchActivityLocked(r, r.configChangeFlags, true, preserveWindow);
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004196 } else {
Wale Ogunwale3ab9a272015-03-16 09:55:45 -07004197 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004198 "Config is relaunching non-resumed " + r);
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004199 relaunchActivityLocked(r, r.configChangeFlags, false, preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004200 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004201
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004202 // All done... tell the caller we weren't able to keep this activity around.
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -07004203 return false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004204 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004205
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004206 // Default case: the activity can handle this new configuration, so hand it over.
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07004207 // NOTE: We only forward the task override configuration as the system level configuration
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004208 // changes is always sent to all processes when they happen so it can just use whatever
4209 // system level configuration it last got.
Filip Gruszczynskica664812015-12-04 12:43:36 -08004210 r.scheduleConfigurationChanged(taskConfig, true);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004211 r.stopFreezingScreenLocked(false);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07004212
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004213 return true;
4214 }
4215
Filip Gruszczynski23493322015-07-29 17:02:59 -07004216 private int getTaskConfigurationChanges(ActivityRecord record, Configuration taskConfig,
4217 Configuration oldTaskOverride) {
4218 // Determine what has changed. May be nothing, if this is a config
4219 // that has come back from the app after going idle. In that case
4220 // we just want to leave the official config object now in the
4221 // activity and do nothing else.
4222 int taskChanges = oldTaskOverride.diff(taskConfig);
4223 // We don't want to use size changes if they don't cross boundaries that are important to
4224 // the app.
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004225 if ((taskChanges & CONFIG_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004226 final boolean crosses = record.crossesHorizontalSizeThreshold(
4227 oldTaskOverride.screenWidthDp, taskConfig.screenWidthDp)
4228 || record.crossesVerticalSizeThreshold(
4229 oldTaskOverride.screenHeightDp, taskConfig.screenHeightDp);
4230 if (!crosses) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004231 taskChanges &= ~CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004232 }
4233 }
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004234 if ((taskChanges & CONFIG_SMALLEST_SCREEN_SIZE) != 0) {
Filip Gruszczynski23493322015-07-29 17:02:59 -07004235 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4236 final int newSmallest = taskConfig.smallestScreenWidthDp;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07004237 if (!record.crossesSmallestSizeThreshold(oldSmallest, newSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004238 taskChanges &= ~CONFIG_SMALLEST_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004239 }
4240 }
4241 return catchConfigChangesFromUnset(taskConfig, oldTaskOverride, taskChanges);
4242 }
4243
4244 private static int catchConfigChangesFromUnset(Configuration taskConfig,
4245 Configuration oldTaskOverride, int taskChanges) {
4246 if (taskChanges == 0) {
4247 // {@link Configuration#diff} doesn't catch changes from unset values.
4248 // Check for changes we care about.
4249 if (oldTaskOverride.orientation != taskConfig.orientation) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004250 taskChanges |= CONFIG_ORIENTATION;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004251 }
4252 // We want to explicitly track situations where the size configuration goes from
4253 // undefined to defined. We don't care about crossing the threshold in that case,
4254 // because there is no threshold.
4255 final int oldHeight = oldTaskOverride.screenHeightDp;
4256 final int newHeight = taskConfig.screenHeightDp;
4257 final int undefinedHeight = Configuration.SCREEN_HEIGHT_DP_UNDEFINED;
4258 if ((oldHeight == undefinedHeight && newHeight != undefinedHeight)
4259 || (oldHeight != undefinedHeight && newHeight == undefinedHeight)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004260 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004261 }
4262 final int oldWidth = oldTaskOverride.screenWidthDp;
4263 final int newWidth = taskConfig.screenWidthDp;
4264 final int undefinedWidth = Configuration.SCREEN_WIDTH_DP_UNDEFINED;
4265 if ((oldWidth == undefinedWidth && newWidth != undefinedWidth)
4266 || (oldWidth != undefinedWidth && newWidth == undefinedWidth)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004267 taskChanges |= CONFIG_SCREEN_SIZE;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004268 }
4269 final int oldSmallest = oldTaskOverride.smallestScreenWidthDp;
4270 final int newSmallest = taskConfig.smallestScreenWidthDp;
4271 final int undefinedSmallest = Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED;
4272 if ((oldSmallest == undefinedSmallest && newSmallest != undefinedSmallest)
4273 || (oldSmallest != undefinedSmallest && newSmallest == undefinedSmallest)) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004274 taskChanges |= CONFIG_SMALLEST_SCREEN_SIZE;
4275 }
4276 final int oldLayout = oldTaskOverride.screenLayout;
4277 final int newLayout = taskConfig.screenLayout;
4278 if ((oldLayout == SCREENLAYOUT_UNDEFINED && newLayout != SCREENLAYOUT_UNDEFINED)
4279 || (oldLayout != SCREENLAYOUT_UNDEFINED && newLayout == SCREENLAYOUT_UNDEFINED)) {
4280 taskChanges |= CONFIG_SCREEN_LAYOUT;
Filip Gruszczynski23493322015-07-29 17:02:59 -07004281 }
4282 }
4283 return taskChanges;
4284 }
4285
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004286 private static boolean isResizeOnlyChange(int change) {
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004287 return (change & ~(CONFIG_SCREEN_SIZE | CONFIG_SMALLEST_SCREEN_SIZE | CONFIG_ORIENTATION
4288 | CONFIG_SCREEN_LAYOUT)) == 0;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004289 }
4290
Wale Ogunwale83301a92015-09-24 15:54:08 -07004291 private void relaunchActivityLocked(
4292 ActivityRecord r, int changes, boolean andResume, boolean preserveWindow) {
4293 if (mService.mSuppressResizeConfigChanges && preserveWindow) {
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004294 r.configChangeFlags = 0;
Wale Ogunwale83301a92015-09-24 15:54:08 -07004295 return;
4296 }
4297
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004298 List<ResultInfo> results = null;
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004299 List<ReferrerIntent> newIntents = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004300 if (andResume) {
4301 results = r.results;
4302 newIntents = r.newIntents;
4303 }
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004304 if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
4305 "Relaunching: " + r + " with results=" + results + " newIntents=" + newIntents
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004306 + " andResume=" + andResume + " preserveWindow=" + preserveWindow);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004307 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
Dianne Hackbornb12e1352012-09-26 11:39:20 -07004308 : EventLogTags.AM_RELAUNCH_ACTIVITY, r.userId, System.identityHashCode(r),
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004309 r.task.taskId, r.shortComponentName);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004310
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004311 r.startFreezingScreenLocked(r.app, 0);
Craig Mautner9db9a0b2013-04-29 17:05:56 -07004312
Craig Mautner34b73df2014-01-12 21:11:08 -08004313 mStackSupervisor.removeChildActivityContainers(r);
4314
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004315 try {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004316 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH,
4317 "Moving to " + (andResume ? "RESUMED" : "PAUSED") + " Relaunching " + r);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004318 r.forceNewConfig = false;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004319 r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents, changes,
4320 !andResume, new Configuration(mService.mConfiguration),
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004321 new Configuration(r.task.mOverrideConfig), preserveWindow);
Jorim Jaggife89d122015-12-22 16:28:44 +01004322 mStackSupervisor.activityRelaunchingLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004323 // Note: don't need to call pauseIfSleepingLocked() here, because
4324 // the caller will only pass in 'andResume' if this activity is
4325 // currently resumed, which implies we aren't sleeping.
4326 } catch (RemoteException e) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004327 if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_SWITCH, "Relaunch failed", e);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004328 }
4329
4330 if (andResume) {
4331 r.results = null;
4332 r.newIntents = null;
Dianne Hackbornb61a0262012-05-14 17:19:18 -07004333 r.state = ActivityState.RESUMED;
4334 } else {
4335 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4336 r.state = ActivityState.PAUSED;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004337 }
Wale Ogunwalef81c1d12016-01-12 12:20:18 -08004338
4339 r.configChangeFlags = 0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004340 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004341
4342 boolean willActivityBeVisibleLocked(IBinder token) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004343 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4344 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4345 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4346 final ActivityRecord r = activities.get(activityNdx);
4347 if (r.appToken == token) {
Craig Mautner34b73df2014-01-12 21:11:08 -08004348 return true;
Craig Mautnerd44711d2013-02-23 11:24:36 -08004349 }
4350 if (r.fullscreen && !r.finishing) {
4351 return false;
4352 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004353 }
4354 }
Wale Ogunwale7d701172015-03-11 15:36:30 -07004355 final ActivityRecord r = ActivityRecord.forTokenLocked(token);
Craig Mautner34b73df2014-01-12 21:11:08 -08004356 if (r == null) {
4357 return false;
4358 }
4359 if (r.finishing) Slog.e(TAG, "willActivityBeVisibleLocked: Returning false,"
4360 + " would have returned true for r=" + r);
4361 return !r.finishing;
Craig Mautnercae015f2013-02-08 14:31:27 -08004362 }
4363
4364 void closeSystemDialogsLocked() {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004365 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4366 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4367 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4368 final ActivityRecord r = activities.get(activityNdx);
4369 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Craig Mautnerd2328952013-03-05 12:46:26 -08004370 finishActivityLocked(r, Activity.RESULT_CANCELED, null, "close-sys", true);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004371 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004372 }
4373 }
4374 }
4375
Wale Ogunwale540e1232015-05-01 15:35:39 -07004376 boolean finishDisabledPackageActivitiesLocked(String packageName, Set<String> filterByClasses,
4377 boolean doit, boolean evenPersistent, int userId) {
Craig Mautnercae015f2013-02-08 14:31:27 -08004378 boolean didSomething = false;
4379 TaskRecord lastTask = null;
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004380 ComponentName homeActivity = null;
Craig Mautner56f52db2013-02-25 10:03:01 -08004381 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4382 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4383 int numActivities = activities.size();
4384 for (int activityNdx = 0; activityNdx < numActivities; ++activityNdx) {
4385 ActivityRecord r = activities.get(activityNdx);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004386 final boolean sameComponent =
4387 (r.packageName.equals(packageName) && (filterByClasses == null
4388 || filterByClasses.contains(r.realActivity.getClassName())))
4389 || (packageName == null && r.userId == userId);
Craig Mautner56f52db2013-02-25 10:03:01 -08004390 if ((userId == UserHandle.USER_ALL || r.userId == userId)
Wale Ogunwale540e1232015-05-01 15:35:39 -07004391 && (sameComponent || r.task == lastTask)
Craig Mautner56f52db2013-02-25 10:03:01 -08004392 && (r.app == null || evenPersistent || !r.app.persistent)) {
4393 if (!doit) {
4394 if (r.finishing) {
4395 // If this activity is just finishing, then it is not
4396 // interesting as far as something to stop.
4397 continue;
4398 }
4399 return true;
Craig Mautnercae015f2013-02-08 14:31:27 -08004400 }
Craig Mautner9d8a30d2014-07-07 17:26:20 +00004401 if (r.isHomeActivity()) {
4402 if (homeActivity != null && homeActivity.equals(r.realActivity)) {
4403 Slog.i(TAG, "Skip force-stop again " + r);
4404 continue;
4405 } else {
4406 homeActivity = r.realActivity;
4407 }
4408 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004409 didSomething = true;
4410 Slog.i(TAG, " Force finishing activity " + r);
Wale Ogunwale540e1232015-05-01 15:35:39 -07004411 if (sameComponent) {
Craig Mautner56f52db2013-02-25 10:03:01 -08004412 if (r.app != null) {
4413 r.app.removed = true;
4414 }
4415 r.app = null;
Craig Mautnercae015f2013-02-08 14:31:27 -08004416 }
Craig Mautner56f52db2013-02-25 10:03:01 -08004417 lastTask = r.task;
Craig Mautnerd94b1b42013-05-01 11:58:03 -07004418 if (finishActivityLocked(r, Activity.RESULT_CANCELED, null, "force-stop",
4419 true)) {
4420 // r has been deleted from mActivities, accommodate.
4421 --numActivities;
4422 --activityNdx;
4423 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004424 }
4425 }
4426 }
4427 return didSomething;
4428 }
4429
Dianne Hackborn09233282014-04-30 11:33:59 -07004430 void getTasksLocked(List<RunningTaskInfo> list, int callingUid, boolean allowed) {
riddle_hsuddc74152015-04-07 11:30:09 +08004431 boolean focusedStack = mStackSupervisor.getFocusedStack() == this;
4432 boolean topTask = true;
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004433 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004434 final TaskRecord task = mTaskHistory.get(taskNdx);
riddle_hsuddc74152015-04-07 11:30:09 +08004435 if (task.getTopActivity() == null) {
4436 continue;
4437 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004438 ActivityRecord r = null;
4439 ActivityRecord top = null;
Wale Ogunwale6035e012015-04-14 15:54:10 -07004440 ActivityRecord tmp;
Craig Mautneraab647e2013-02-28 16:31:36 -08004441 int numActivities = 0;
4442 int numRunning = 0;
4443 final ArrayList<ActivityRecord> activities = task.mActivities;
Dianne Hackborn885fbe52014-08-23 15:23:58 -07004444 if (!allowed && !task.isHomeTask() && task.effectiveUid != callingUid) {
Dianne Hackborn09233282014-04-30 11:33:59 -07004445 continue;
4446 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004447 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
Wale Ogunwale6035e012015-04-14 15:54:10 -07004448 tmp = activities.get(activityNdx);
4449 if (tmp.finishing) {
4450 continue;
4451 }
4452 r = tmp;
Craig Mautnercae015f2013-02-08 14:31:27 -08004453
Craig Mautneraab647e2013-02-28 16:31:36 -08004454 // Initialize state for next task if needed.
4455 if (top == null || (top.state == ActivityState.INITIALIZING)) {
4456 top = r;
4457 numActivities = numRunning = 0;
Craig Mautnercae015f2013-02-08 14:31:27 -08004458 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004459
4460 // Add 'r' into the current task.
4461 numActivities++;
4462 if (r.app != null && r.app.thread != null) {
4463 numRunning++;
4464 }
4465
Wale Ogunwalee23149f2015-03-06 15:39:44 -08004466 if (DEBUG_ALL) Slog.v(
Craig Mautneraab647e2013-02-28 16:31:36 -08004467 TAG, r.intent.getComponent().flattenToShortString()
4468 + ": task=" + r.task);
4469 }
4470
4471 RunningTaskInfo ci = new RunningTaskInfo();
4472 ci.id = task.taskId;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -07004473 ci.stackId = mStackId;
Craig Mautneraab647e2013-02-28 16:31:36 -08004474 ci.baseActivity = r.intent.getComponent();
4475 ci.topActivity = top.intent.getComponent();
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004476 ci.lastActiveTime = task.lastActiveTime;
riddle_hsuddc74152015-04-07 11:30:09 +08004477 if (focusedStack && topTask) {
4478 // Give the latest time to ensure foreground task can be sorted
4479 // at the first, because lastActiveTime of creating task is 0.
4480 ci.lastActiveTime = System.currentTimeMillis();
4481 topTask = false;
4482 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -07004483
Craig Mautnerc0ffce52014-07-01 12:38:52 -07004484 if (top.task != null) {
4485 ci.description = top.task.lastDescription;
Craig Mautneraab647e2013-02-28 16:31:36 -08004486 }
4487 ci.numActivities = numActivities;
4488 ci.numRunning = numRunning;
Craig Mautneraab647e2013-02-28 16:31:36 -08004489 list.add(ci);
Craig Mautnercae015f2013-02-08 14:31:27 -08004490 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004491 }
4492
4493 public void unhandledBackLocked() {
Craig Mautneraab647e2013-02-28 16:31:36 -08004494 final int top = mTaskHistory.size() - 1;
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004495 if (DEBUG_SWITCH) Slog.d(TAG_SWITCH, "Performing unhandledBack(): top activity at " + top);
Craig Mautneraab647e2013-02-28 16:31:36 -08004496 if (top >= 0) {
4497 final ArrayList<ActivityRecord> activities = mTaskHistory.get(top).mActivities;
4498 int activityTop = activities.size() - 1;
4499 if (activityTop > 0) {
4500 finishActivityLocked(activities.get(activityTop), Activity.RESULT_CANCELED, null,
4501 "unhandled-back", true);
4502 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004503 }
4504 }
4505
Craig Mautner6b74cb52013-09-27 17:02:21 -07004506 /**
4507 * Reset local parameters because an app's activity died.
4508 * @param app The app of the activity that died.
Craig Mautner19091252013-10-05 00:03:53 -07004509 * @return result from removeHistoryRecordsForAppLocked.
Craig Mautner6b74cb52013-09-27 17:02:21 -07004510 */
4511 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautnere79d42682013-04-01 19:01:53 -07004512 if (mPausingActivity != null && mPausingActivity.app == app) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004513 if (DEBUG_PAUSE || DEBUG_CLEANUP) Slog.v(TAG_PAUSE,
Craig Mautnere79d42682013-04-01 19:01:53 -07004514 "App died while pausing: " + mPausingActivity);
4515 mPausingActivity = null;
4516 }
4517 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4518 mLastPausedActivity = null;
Craig Mautner0f922742013-08-06 08:44:42 -07004519 mLastNoHistoryActivity = null;
Craig Mautnere79d42682013-04-01 19:01:53 -07004520 }
4521
Craig Mautner19091252013-10-05 00:03:53 -07004522 return removeHistoryRecordsForAppLocked(app);
Craig Mautnere79d42682013-04-01 19:01:53 -07004523 }
4524
Craig Mautnercae015f2013-02-08 14:31:27 -08004525 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnerd44711d2013-02-23 11:24:36 -08004526 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4527 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4528 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4529 final ActivityRecord r = activities.get(activityNdx);
4530 if (r.app == app) {
4531 Slog.w(TAG, " Force finishing activity "
4532 + r.intent.getComponent().flattenToShortString());
Craig Mautner8e5b1332014-07-24 13:32:37 -07004533 // Force the destroy to skip right to removal.
4534 r.app = null;
4535 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY, false);
Craig Mautnerd44711d2013-02-23 11:24:36 -08004536 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004537 }
4538 }
4539 }
4540
Dianne Hackborn390517b2013-05-30 15:03:32 -07004541 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004542 boolean dumpClient, String dumpPackage, boolean needSep, String header) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07004543 boolean printed = false;
Craig Mautneraab647e2013-02-28 16:31:36 -08004544 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4545 final TaskRecord task = mTaskHistory.get(taskNdx);
Dianne Hackborn390517b2013-05-30 15:03:32 -07004546 printed |= ActivityStackSupervisor.dumpHistoryList(fd, pw,
4547 mTaskHistory.get(taskNdx).mActivities, " ", "Hist", true, !dumpAll,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004548 dumpClient, dumpPackage, needSep, header,
Wale Ogunwaleb34a7ad2015-08-14 11:05:30 -07004549 " Task id #" + task.taskId + "\n" +
4550 " mFullscreen=" + task.mFullscreen + "\n" +
4551 " mBounds=" + task.mBounds + "\n" +
4552 " mLastNonFullscreenBounds=" + task.mLastNonFullscreenBounds);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07004553 if (printed) {
4554 header = null;
4555 }
Craig Mautneraab647e2013-02-28 16:31:36 -08004556 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07004557 return printed;
Craig Mautnercae015f2013-02-08 14:31:27 -08004558 }
4559
4560 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
4561 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
4562
4563 if ("all".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004564 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4565 activities.addAll(mTaskHistory.get(taskNdx).mActivities);
Craig Mautnercae015f2013-02-08 14:31:27 -08004566 }
4567 } else if ("top".equals(name)) {
Craig Mautneraab647e2013-02-28 16:31:36 -08004568 final int top = mTaskHistory.size() - 1;
4569 if (top >= 0) {
4570 final ArrayList<ActivityRecord> list = mTaskHistory.get(top).mActivities;
4571 int listTop = list.size() - 1;
4572 if (listTop >= 0) {
4573 activities.add(list.get(listTop));
4574 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004575 }
4576 } else {
4577 ItemMatcher matcher = new ItemMatcher();
4578 matcher.build(name);
4579
Craig Mautneraab647e2013-02-28 16:31:36 -08004580 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4581 for (ActivityRecord r1 : mTaskHistory.get(taskNdx).mActivities) {
4582 if (matcher.match(r1, r1.intent.getComponent())) {
4583 activities.add(r1);
4584 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004585 }
4586 }
4587 }
4588
4589 return activities;
4590 }
4591
4592 ActivityRecord restartPackage(String packageName) {
Filip Gruszczynski3e85ba22015-10-05 22:48:30 -07004593 ActivityRecord starting = topRunningActivityLocked();
Craig Mautnercae015f2013-02-08 14:31:27 -08004594
4595 // All activities that came from the package must be
4596 // restarted as if there was a config change.
Craig Mautneraab647e2013-02-28 16:31:36 -08004597 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4598 final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
4599 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
4600 final ActivityRecord a = activities.get(activityNdx);
4601 if (a.info.packageName.equals(packageName)) {
4602 a.forceNewConfig = true;
4603 if (starting != null && a == starting && a.visible) {
4604 a.startFreezingScreenLocked(starting.app,
Filip Gruszczynski3c2db1d12016-01-06 17:39:14 -08004605 CONFIG_SCREEN_LAYOUT);
Craig Mautneraab647e2013-02-28 16:31:36 -08004606 }
Craig Mautnercae015f2013-02-08 14:31:27 -08004607 }
4608 }
4609 }
4610
4611 return starting;
4612 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004613
Craig Mautner299f9602015-01-26 09:47:33 -08004614 void removeTask(TaskRecord task, String reason) {
Wale Ogunwale040b4702015-08-06 18:10:50 -07004615 removeTask(task, reason, !MOVING);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004616 }
4617
Wale Ogunwale000957c2015-04-03 08:19:12 -07004618 /**
4619 * Removes the input task from this stack.
4620 * @param task to remove.
4621 * @param reason for removal.
Wale Ogunwale040b4702015-08-06 18:10:50 -07004622 * @param moving task to another stack. In the case we are moving we don't want to perform
4623 * some operations on the task like removing it from window manager or recents.
Wale Ogunwale000957c2015-04-03 08:19:12 -07004624 */
Wale Ogunwale040b4702015-08-06 18:10:50 -07004625 void removeTask(TaskRecord task, String reason, boolean moving) {
4626 if (!moving) {
Craig Mautner15df08a2015-04-01 12:17:18 -07004627 mStackSupervisor.removeLockedTaskLocked(task);
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004628 mWindowManager.removeTask(task.taskId);
Wale Ogunwaleb317b222015-11-09 11:14:10 -08004629 if (!StackId.persistTaskBounds(mStackId)) {
4630 // Reset current bounds for task whose bounds shouldn't be persisted so it uses
4631 // default configuration the next time it launches.
4632 task.updateOverrideConfiguration(null);
4633 }
Wale Ogunwale53a29a92015-02-23 15:42:52 -08004634 }
Wale Ogunwale000957c2015-04-03 08:19:12 -07004635
Craig Mautner04a0ea62014-01-13 12:51:26 -08004636 final ActivityRecord r = mResumedActivity;
4637 if (r != null && r.task == task) {
4638 mResumedActivity = null;
4639 }
4640
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004641 final int taskNdx = mTaskHistory.indexOf(task);
4642 final int topTaskNdx = mTaskHistory.size() - 1;
Craig Mautner84984fa2014-06-19 11:19:20 -07004643 if (task.isOverHomeStack() && taskNdx < topTaskNdx) {
4644 final TaskRecord nextTask = mTaskHistory.get(taskNdx + 1);
4645 if (!nextTask.isOverHomeStack()) {
4646 nextTask.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
4647 }
Craig Mautnerae7ecab2013-09-18 11:48:14 -07004648 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004649 mTaskHistory.remove(task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004650 updateTaskMovement(task, true);
Craig Mautner41db4a72014-05-07 17:20:56 -07004651
Wale Ogunwale040b4702015-08-06 18:10:50 -07004652 if (!moving && task.mActivities.isEmpty()) {
Craig Mautner41db4a72014-05-07 17:20:56 -07004653 final boolean isVoiceSession = task.voiceSession != null;
4654 if (isVoiceSession) {
4655 try {
4656 task.voiceSession.taskFinished(task.intent, task.taskId);
4657 } catch (RemoteException e) {
4658 }
Dianne Hackbornc03c9162014-05-02 10:45:59 -07004659 }
Craig Mautner41db4a72014-05-07 17:20:56 -07004660 if (task.autoRemoveFromRecents() || isVoiceSession) {
4661 // Task creator asked to remove this when done, or this task was a voice
4662 // interaction, so it should not remain on the recent tasks list.
Wale Ogunwalec82f2f52014-12-09 09:32:50 -08004663 mRecentTasks.remove(task);
Winson Chung740c3ac2014-11-12 16:14:38 -08004664 task.removedFromRecents();
Craig Mautner41db4a72014-05-07 17:20:56 -07004665 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07004666 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004667
4668 if (mTaskHistory.isEmpty()) {
Wale Ogunwaleee006da2015-03-30 14:49:25 -07004669 if (DEBUG_STACK) Slog.i(TAG_STACK, "removeTask: removing stack=" + this);
Chong Zhanga8be5e52015-10-05 17:06:28 -07004670 // We only need to adjust focused stack if this stack is in focus.
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004671 if (isOnHomeDisplay() && mStackSupervisor.isFocusedStack(this)) {
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004672 String myReason = reason + " leftTaskHistoryEmpty";
Wale Ogunwale4cea0f52015-12-25 06:30:31 -08004673 if (mFullscreen || !adjustFocusToNextFocusableStackLocked(myReason)) {
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004674 mStackSupervisor.moveHomeStackToFront(myReason);
Wale Ogunwaled697cea2015-02-20 17:19:49 -08004675 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004676 }
Craig Mautner593a4e62014-01-15 17:55:51 -08004677 if (mStacks != null) {
4678 mStacks.remove(this);
4679 mStacks.add(0, this);
4680 }
Wale Ogunwale925d0d12015-09-23 15:40:07 -07004681 if (!isHomeStack()) {
Wale Ogunwale49853bf2015-02-23 09:24:42 -08004682 mActivityContainer.onTaskListEmptyLocked();
4683 }
Craig Mautner04a0ea62014-01-13 12:51:26 -08004684 }
Wale Ogunwale04f4d6b2015-03-11 09:23:25 -07004685
4686 task.stack = null;
Craig Mautner0247fc82013-02-28 14:32:06 -08004687 }
4688
Dianne Hackborn91097de2014-04-04 18:02:06 -07004689 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
4690 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
4691 boolean toTop) {
Craig Mautner21d24a22014-04-23 11:45:37 -07004692 TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
4693 voiceInteractor);
Chong Zhang0fa656b2015-08-31 15:17:21 -07004694 // add the task to stack first, mTaskPositioner might need the stack association
Wale Ogunwale5f986092015-12-04 15:35:38 -08004695 addTask(task, toTop, "createTaskRecord");
Chong Zhang75b37202015-12-04 14:16:36 -08004696 final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
4697 if (!layoutTaskInStack(task, info.layout) && mBounds != null && task.mResizeable
4698 && !isLockscreenShown) {
Wale Ogunwalea6e902e2015-09-21 18:37:15 -07004699 task.updateOverrideConfiguration(mBounds);
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004700 }
Craig Mautner5d9c7be2013-02-15 14:02:56 -08004701 return task;
4702 }
Craig Mautnerc00204b2013-03-05 15:02:14 -08004703
Wale Ogunwale935e5022015-11-10 12:36:10 -08004704 boolean layoutTaskInStack(TaskRecord task, ActivityInfo.Layout layout) {
4705 if (mTaskPositioner == null) {
4706 return false;
4707 }
4708 mTaskPositioner.updateDefaultBounds(task, mTaskHistory, layout);
4709 return true;
4710 }
4711
Craig Mautnerc00204b2013-03-05 15:02:14 -08004712 ArrayList<TaskRecord> getAllTasks() {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08004713 return new ArrayList<>(mTaskHistory);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004714 }
4715
Wale Ogunwale5f986092015-12-04 15:35:38 -08004716 void addTask(final TaskRecord task, final boolean toTop, String reason) {
4717 final ActivityStack prevStack = preAddTask(task, reason);
4718
Craig Mautnerc00204b2013-03-05 15:02:14 -08004719 task.stack = this;
4720 if (toTop) {
Wale Ogunwale3fcb4a82015-04-06 14:00:13 -07004721 insertTaskAtTop(task, null);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004722 } else {
4723 mTaskHistory.add(0, task);
Craig Mautner21d24a22014-04-23 11:45:37 -07004724 updateTaskMovement(task, false);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004725 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08004726 postAddTask(task, prevStack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08004727 }
4728
Wale Ogunwale5f986092015-12-04 15:35:38 -08004729 void positionTask(final TaskRecord task, int position) {
4730 final ActivityStack prevStack = preAddTask(task, "positionTask");
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004731 task.stack = this;
4732 insertTaskAtPosition(task, position);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004733 postAddTask(task, prevStack);
4734 }
4735
4736 private ActivityStack preAddTask(TaskRecord task, String reason) {
4737 final ActivityStack prevStack = task.stack;
4738 if (prevStack != null && prevStack != this) {
4739 prevStack.removeTask(task, reason, MOVING);
4740 }
4741 return prevStack;
4742 }
4743
4744 private void postAddTask(TaskRecord task, ActivityStack prevStack) {
4745 if (prevStack != null) {
Wale Ogunwaled046a012015-12-24 13:05:59 -08004746 task.reportPictureInPictureModeChangeIfNeeded(prevStack);
Wale Ogunwale5f986092015-12-04 15:35:38 -08004747 } else if (task.voiceSession != null) {
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -07004748 try {
4749 task.voiceSession.taskStarted(task.intent, task.taskId);
4750 } catch (RemoteException e) {
4751 }
4752 }
4753 }
4754
Filip Gruszczynskie5390e72015-08-18 16:39:00 -07004755 void addConfigOverride(ActivityRecord r, TaskRecord task) {
Wale Ogunwale706ed792015-08-02 10:29:44 -07004756 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004757 task.updateOverrideConfiguration(bounds);
Filip Gruszczynskiebcc8752015-08-25 16:51:05 -07004758 mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken,
4759 r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen,
Wale Ogunwale6cae7652015-12-26 07:36:26 -08004760 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
4761 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
4762 !r.isHomeActivity(), r.isAlwaysFocusable());
Chong Zhangb15758a2015-11-17 12:12:03 -08004763 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004764 r.taskConfigOverride = task.mOverrideConfig;
4765 }
4766
Wale Ogunwaled046a012015-12-24 13:05:59 -08004767 void moveToFrontAndResumeStateIfNeeded(
4768 ActivityRecord r, boolean moveToFront, boolean setResume, String reason) {
4769 if (!moveToFront) {
4770 return;
Wale Ogunwale079a0042015-10-24 11:44:07 -07004771 }
Wale Ogunwaled046a012015-12-24 13:05:59 -08004772
4773 // If the activity owns the last resumed activity, transfer that together,
4774 // so that we don't resume the same activity again in the new stack.
4775 // Apps may depend on onResume()/onPause() being called in pairs.
4776 if (setResume) {
4777 mResumedActivity = r;
4778 }
4779 // Move the stack in which we are placing the activity to the front. The call will also
4780 // make sure the activity focus is set.
4781 moveToFront(reason);
Wale Ogunwale079a0042015-10-24 11:44:07 -07004782 }
4783
4784 /**
4785 * Moves the input activity from its current stack to this one.
4786 * NOTE: The current task of the activity isn't moved to this stack. Instead a new task is
4787 * created on this stack which the activity is added to.
4788 * */
4789 void moveActivityToStack(ActivityRecord r) {
4790 final ActivityStack prevStack = r.task.stack;
4791 if (prevStack.mStackId == mStackId) {
4792 // You are already in the right stack silly...
4793 return;
4794 }
4795
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07004796 final boolean wasFocused = mStackSupervisor.isFocusedStack(prevStack)
Wale Ogunwale079a0042015-10-24 11:44:07 -07004797 && (mStackSupervisor.topRunningActivityLocked() == r);
4798 final boolean wasResumed = wasFocused && (prevStack.mResumedActivity == r);
4799
4800 final TaskRecord task = createTaskRecord(
4801 mStackSupervisor.getNextTaskId(), r.info, r.intent, null, null, true);
4802 r.setTask(task, null);
4803 task.addActivityToTop(r);
4804 setAppTask(r, task);
Wale Ogunwaled046a012015-12-24 13:05:59 -08004805 task.reportPictureInPictureModeChangeIfNeeded(prevStack);
4806 moveToFrontAndResumeStateIfNeeded(r, wasFocused, wasResumed, "moveActivityToStack");
4807 if (wasResumed) {
4808 prevStack.mResumedActivity = null;
4809 }
Wale Ogunwale079a0042015-10-24 11:44:07 -07004810 }
4811
Wale Ogunwale706ed792015-08-02 10:29:44 -07004812 private void setAppTask(ActivityRecord r, TaskRecord task) {
4813 final Rect bounds = task.getLaunchBounds();
Chong Zhang0fa656b2015-08-31 15:17:21 -07004814 task.updateOverrideConfiguration(bounds);
4815 mWindowManager.setAppTask(
Wale Ogunwale2998eef2015-12-02 19:46:29 -08004816 r.appToken, task.taskId, mStackId, task.getLaunchBounds(), task.mOverrideConfig);
Chong Zhangb15758a2015-11-17 12:12:03 -08004817 mWindowManager.setTaskResizeable(task.taskId, task.mResizeable);
Wale Ogunwale706ed792015-08-02 10:29:44 -07004818 r.taskConfigOverride = task.mOverrideConfig;
4819 }
4820
Craig Mautnerc00204b2013-03-05 15:02:14 -08004821 public int getStackId() {
4822 return mStackId;
4823 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07004824
4825 @Override
4826 public String toString() {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07004827 return "ActivityStack{" + Integer.toHexString(System.identityHashCode(this))
4828 + " stackId=" + mStackId + ", " + mTaskHistory.size() + " tasks}";
Craig Mautnerde4ef022013-04-07 19:01:33 -07004829 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004830
Craig Mautner15df08a2015-04-01 12:17:18 -07004831 void onLockTaskPackagesUpdatedLocked() {
4832 for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
4833 mTaskHistory.get(taskNdx).setLockTaskAuth();
4834 }
4835 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004836}