blob: e9f055866bd269acaa4a3ca496326cce09a95a8b [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 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
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Craig Mautner29219d92013-04-16 20:19:12 -070020import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
21import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Craig Mautner6170f732013-04-02 13:05:23 -070022import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Craig Mautner2420ead2013-04-01 17:13:20 -070023import static com.android.server.am.ActivityManagerService.localLOGV;
Craig Mautner23ac33b2013-04-01 16:26:35 -070024import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
Craig Mautnere7c58b62013-06-12 20:19:00 -070025import static com.android.server.am.ActivityManagerService.DEBUG_FOCUS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070026import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
Craig Mautner6170f732013-04-02 13:05:23 -070027import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070028import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
Craig Mautner2420ead2013-04-01 17:13:20 -070029import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
Craig Mautner8849a5e2013-04-02 16:41:03 -070030import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
31import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
Craig Mautner05d29032013-05-03 13:40:13 -070032import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner8d341ef2013-03-26 09:03:27 -070033import static com.android.server.am.ActivityManagerService.TAG;
Craig Mautner84984fa2014-06-19 11:19:20 -070034import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
35import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
36import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner8d341ef2013-03-26 09:03:27 -070037
Craig Mautner2420ead2013-04-01 17:13:20 -070038import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070039import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080040import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityOptions;
42import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080043import android.app.IActivityContainer;
44import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070045import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070047import android.app.PendingIntent;
Jeff Hao1b012d32014-08-20 10:35:34 -070048import android.app.ProfilerInfo;
Craig Mautner20e72272013-04-01 13:45:53 -070049import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070050import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070051import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040052import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040053import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070054import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070055import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070056import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070057import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070058import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070059import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070060import android.content.pm.ApplicationInfo;
61import android.content.pm.PackageManager;
62import android.content.pm.ResolveInfo;
63import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080064import android.graphics.Point;
65import android.hardware.display.DisplayManager;
66import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080067import android.hardware.display.DisplayManagerGlobal;
68import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080069import android.hardware.input.InputManager;
70import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070071import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070072import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070073import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070074import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070075import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070076import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070077import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070078import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070079import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070080import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070081import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040082import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070083import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070084import android.os.UserHandle;
Jason Monk62515be2014-05-21 16:06:19 -040085import android.provider.Settings;
86import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070087import android.service.voice.IVoiceInteractionSession;
Dianne Hackborn89ad4562014-08-24 16:45:38 -070088import android.util.ArraySet;
Craig Mautner2420ead2013-04-01 17:13:20 -070089import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070090import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080091import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070092
Craig Mautner4a1cb222013-12-04 16:14:06 -080093import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080094import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080095import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080096import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080097import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -070098import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -070099import com.android.internal.app.IVoiceInteractor;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -0700100import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -0400101import com.android.internal.statusbar.IStatusBarService;
Jason Monke0697792014-08-04 16:28:09 -0400102import com.android.internal.widget.LockPatternUtils;
Jeff Brownca9bc702014-02-11 14:32:56 -0800103import com.android.server.LocalServices;
Craig Mautner2420ead2013-04-01 17:13:20 -0700104import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700105import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700106
justinzhang5286d3f2014-05-12 17:06:01 -0400107
Craig Mautner8d341ef2013-03-26 09:03:27 -0700108import java.io.FileDescriptor;
109import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700110import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700111import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700112import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700113
Craig Mautner4a1cb222013-12-04 16:14:06 -0800114public final class ActivityStackSupervisor implements DisplayListener {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700115 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
116 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
117 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700118 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700119 static final boolean DEBUG_IDLE = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700120 static final boolean DEBUG_RELEASE = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700121 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
122 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
123 static final boolean DEBUG_STATES = DEBUG || false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700124 static final boolean DEBUG_VISIBLE_BEHIND = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700125
Craig Mautner2219a1b2013-03-25 09:44:30 -0700126 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700127
Craig Mautnerf3333272013-04-22 10:55:53 -0700128 /** How long we wait until giving up on the last activity telling us it is idle. */
129 static final int IDLE_TIMEOUT = 10*1000;
130
Craig Mautner0eea92c2013-05-16 13:35:39 -0700131 /** How long we can hold the sleep wake lock before giving up. */
132 static final int SLEEP_TIMEOUT = 5*1000;
133
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700134 // How long we can hold the launch wake lock before giving up.
135 static final int LAUNCH_TIMEOUT = 10*1000;
136
Craig Mautner05d29032013-05-03 13:40:13 -0700137 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
138 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
139 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700140 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700141 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800142 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
143 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
144 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700145 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400146 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
147 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700148 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4c07d022014-06-11 17:12:59 -0700149 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerbb742462014-07-07 15:28:55 -0700150 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 13;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800151
Craig Mautner4504de52013-12-20 09:06:56 -0800152 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700153
Jason Monk62515be2014-05-21 16:06:19 -0400154 private static final String LOCK_TASK_TAG = "Lock-to-App";
155
justinzhang5286d3f2014-05-12 17:06:01 -0400156 /** Status Bar Service **/
157 private IBinder mToken = new Binder();
158 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400159 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400160
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700161 // For debugging to make sure the caller when acquiring/releasing our
162 // wake lock is the system process.
163 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700164
Craig Mautner27084302013-03-25 08:05:25 -0700165 final ActivityManagerService mService;
166
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700167 final ActivityStackSupervisorHandler mHandler;
168
169 /** Short cut */
170 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800171 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700172
Craig Mautner8d341ef2013-03-26 09:03:27 -0700173 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700174 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700175
176 /** Task identifier that activities are currently being started in. Incremented each time a
177 * new task is created. */
178 private int mCurTaskId = 0;
179
Craig Mautner2420ead2013-04-01 17:13:20 -0700180 /** The current user */
181 private int mCurrentUser;
182
Craig Mautnere0a38842013-12-16 16:14:02 -0800183 /** The stack containing the launcher app. Assumed to always be attached to
184 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700185 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700186
Craig Mautnere0a38842013-12-16 16:14:02 -0800187 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700188 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700189
Craig Mautner4a1cb222013-12-04 16:14:06 -0800190 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
191 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800192 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800193 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700194
195 /** List of activities that are waiting for a new activity to become visible before completing
196 * whatever operation they are supposed to do. */
197 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();
198
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700199 /** List of processes waiting to find out about the next visible activity. */
200 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
201 new ArrayList<IActivityManager.WaitResult>();
202
203 /** List of processes waiting to find out about the next launched activity. */
204 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
205 new ArrayList<IActivityManager.WaitResult>();
206
Craig Mautnerde4ef022013-04-07 19:01:33 -0700207 /** List of activities that are ready to be stopped, but waiting for the next activity to
208 * settle down before doing so. */
209 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
210
Craig Mautnerf3333272013-04-22 10:55:53 -0700211 /** List of activities that are ready to be finished, but waiting for the previous activity to
212 * settle down before doing so. It contains ActivityRecord objects. */
213 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
214
Craig Mautner0eea92c2013-05-16 13:35:39 -0700215 /** List of activities that are in the process of going to sleep. */
216 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
217
Craig Mautnerf3333272013-04-22 10:55:53 -0700218 /** Used on user changes */
219 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<UserStartedState>();
220
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700221 /** Used to queue up any background users being started */
222 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<UserStartedState>();
223
Craig Mautnerde4ef022013-04-07 19:01:33 -0700224 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
225 * is being brought in front of us. */
226 boolean mUserLeaving = false;
227
Craig Mautner0eea92c2013-05-16 13:35:39 -0700228 /** Set when we have taken too long waiting to go to sleep. */
229 boolean mSleepTimeout = false;
230
Jose Lima58e66d62014-05-27 20:00:27 -0700231 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
232 * setWindowManager is called. **/
233 private boolean mLeanbackOnlyDevice;
234
Craig Mautner0eea92c2013-05-16 13:35:39 -0700235 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700236 * We don't want to allow the device to go to sleep while in the process
237 * of launching an activity. This is primarily to allow alarm intent
238 * receivers to launch an activity and get that to run before the device
239 * goes back to sleep.
240 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700241 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700242
243 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700244 * Set when the system is going to sleep, until we have
245 * successfully paused the current activity and released our wake lock.
246 * At that point the system is allowed to actually sleep.
247 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700248 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700249
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700250 /** Stack id of the front stack when user switched, indexed by userId. */
251 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700252
Craig Mautner4504de52013-12-20 09:06:56 -0800253 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800254 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700255 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800256
257 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700258 private final SparseArray<ActivityDisplay> mActivityDisplays =
259 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800260
Jeff Brownca9bc702014-02-11 14:32:56 -0800261 InputManagerInternal mInputManagerInternal;
262
Craig Mautneraea74a52014-03-08 14:23:10 -0800263 /** If non-null then the task specified remains in front and no other tasks may be started
264 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400265 TaskRecord mLockTaskModeTask;
Jason Monka8f569c2014-07-07 12:15:21 -0400266 /** Whether lock task has been entered by an authorized app and cannot
267 * be exited. */
268 private boolean mLockTaskIsLocked;
Jason Monk62515be2014-05-21 16:06:19 -0400269 /**
270 * Notifies the user when entering/exiting lock-task.
271 */
272 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800273
Craig Mautneree36c772014-07-16 14:56:05 -0700274 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
275 = new ArrayList<PendingActivityLaunch>();
276
277 /**
278 * Description of a request to start a new activity, which has been held
279 * due to app switches being disabled.
280 */
281 static class PendingActivityLaunch {
282 final ActivityRecord r;
283 final ActivityRecord sourceRecord;
284 final int startFlags;
285 final ActivityStack stack;
286
287 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
288 int _startFlags, ActivityStack _stack) {
289 r = _r;
290 sourceRecord = _sourceRecord;
291 startFlags = _startFlags;
292 stack = _stack;
293 }
294 }
295
Craig Mautner4a1cb222013-12-04 16:14:06 -0800296 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700297 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700298 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
299 }
300
301 /**
302 * At the time when the constructor runs, the power manager has not yet been
303 * initialized. So we initialize our wakelocks afterwards.
304 */
305 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800306 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700307 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700308 mLaunchingActivity =
309 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
310 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700311 }
312
justinzhang5286d3f2014-05-12 17:06:01 -0400313 // This function returns a IStatusBarService. The value is from ServiceManager.
314 // getService and is cached.
315 private IStatusBarService getStatusBarService() {
316 synchronized (mService) {
317 if (mStatusBarService == null) {
318 mStatusBarService = IStatusBarService.Stub.asInterface(
319 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
320 if (mStatusBarService == null) {
321 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
322 }
323 }
324 return mStatusBarService;
325 }
326 }
327
Jason Monk35c62a42014-06-17 10:24:47 -0400328 private IDevicePolicyManager getDevicePolicyManager() {
329 synchronized (mService) {
330 if (mDevicePolicyManager == null) {
331 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
332 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
333 if (mDevicePolicyManager == null) {
334 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
335 }
336 }
337 return mDevicePolicyManager;
338 }
339 }
340
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700341 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800342 synchronized (mService) {
343 mWindowManager = wm;
344
345 mDisplayManager =
346 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
347 mDisplayManager.registerDisplayListener(this, null);
348
349 Display[] displays = mDisplayManager.getDisplays();
350 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
351 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800352 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -0700353 if (activityDisplay.mDisplay == null) {
354 throw new IllegalStateException("Default Display does not exist");
355 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800356 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800357 }
358
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700359 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800360 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800361
362 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700363
364 // Initialize this here, now that we can get a valid reference to PackageManager.
365 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800366 }
Craig Mautner27084302013-03-25 08:05:25 -0700367 }
368
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200369 void notifyActivityDrawnForKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700370 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200371 mWindowManager.notifyActivityDrawnForKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700372 }
373
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700374 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800375 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700376 }
377
Craig Mautnerde4ef022013-04-07 19:01:33 -0700378 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800379 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700380 }
381
Craig Mautner4a1cb222013-12-04 16:14:06 -0800382 // TODO: Split into two methods isFrontStack for any visible stack and isFrontmostStack for the
383 // top of all visible stacks.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700384 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800385 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
386 if (parent != null) {
387 stack = parent.task.stack;
388 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800389 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800390 if (stacks != null && !stacks.isEmpty()) {
391 return stack == stacks.get(stacks.size() - 1);
392 }
393 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700394 }
395
Craig Mautnerde4ef022013-04-07 19:01:33 -0700396 void moveHomeStack(boolean toFront) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800397 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800398 int topNdx = stacks.size() - 1;
399 if (topNdx <= 0) {
400 return;
401 }
402 ActivityStack topStack = stacks.get(topNdx);
403 final boolean homeInFront = topStack == mHomeStack;
404 if (homeInFront != toFront) {
405 mLastFocusedStack = topStack;
406 stacks.remove(mHomeStack);
407 stacks.add(toFront ? topNdx : 0, mHomeStack);
408 mFocusedStack = stacks.get(topNdx);
409 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
410 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700411 }
412 }
413
Craig Mautner84984fa2014-06-19 11:19:20 -0700414 void moveHomeStackTaskToTop(int homeStackTaskType) {
415 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
416 mWindowManager.showRecentApps();
417 return;
418 }
Craig Mautner69ada552013-04-18 13:51:51 -0700419 moveHomeStack(true);
Craig Mautner84984fa2014-06-19 11:19:20 -0700420 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Craig Mautner8e569572013-10-11 17:36:59 -0700421 }
422
Craig Mautner84984fa2014-06-19 11:19:20 -0700423 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev) {
Dianne Hackborn7622a0f2014-09-30 14:31:42 -0700424 if (!mService.mBooting && !mService.mBooted) {
425 // Not ready yet!
426 return false;
427 }
428
Craig Mautner84984fa2014-06-19 11:19:20 -0700429 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
430 mWindowManager.showRecentApps();
431 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700432 }
Craig Mautner84984fa2014-06-19 11:19:20 -0700433 moveHomeStackTaskToTop(homeStackTaskType);
434 if (prev != null) {
435 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
436 }
437
Craig Mautnera8a90e02013-06-28 15:24:50 -0700438 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700439 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000440 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700441 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700442 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700443 }
444 return mService.startHomeActivityLocked(mCurrentUser);
445 }
446
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200447 void keyguardWaitingForActivityDrawn() {
448 mWindowManager.keyguardWaitingForActivityDrawn();
Craig Mautner27084302013-03-25 08:05:25 -0700449 }
450
Craig Mautner8d341ef2013-03-26 09:03:27 -0700451 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800452 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800453 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800454 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800455 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
456 ActivityStack stack = stacks.get(stackNdx);
457 TaskRecord task = stack.taskForIdLocked(id);
458 if (task != null) {
459 return task;
460 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700461 }
462 }
463 return null;
464 }
465
Craig Mautner6170f732013-04-02 13:05:23 -0700466 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800467 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800468 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800469 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800470 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
471 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
472 if (r != null) {
473 return r;
474 }
Craig Mautner6170f732013-04-02 13:05:23 -0700475 }
476 }
477 return null;
478 }
479
Craig Mautneref73ee12014-04-23 11:45:37 -0700480 void setNextTaskId(int taskId) {
481 if (taskId > mCurTaskId) {
482 mCurTaskId = taskId;
483 }
484 }
485
Craig Mautner8d341ef2013-03-26 09:03:27 -0700486 int getNextTaskId() {
487 do {
488 mCurTaskId++;
489 if (mCurTaskId <= 0) {
490 mCurTaskId = 1;
491 }
492 } while (anyTaskForIdLocked(mCurTaskId) != null);
493 return mCurTaskId;
494 }
495
Craig Mautnerde4ef022013-04-07 19:01:33 -0700496 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700497 ActivityStack stack = getFocusedStack();
498 if (stack == null) {
499 return null;
500 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700501 ActivityRecord resumedActivity = stack.mResumedActivity;
502 if (resumedActivity == null || resumedActivity.app == null) {
503 resumedActivity = stack.mPausingActivity;
504 if (resumedActivity == null || resumedActivity.app == null) {
505 resumedActivity = stack.topRunningActivityLocked(null);
506 }
507 }
508 return resumedActivity;
509 }
510
Dianne Hackbornff072722014-09-24 10:56:28 -0700511 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700512 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800513 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800514 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
515 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800516 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
517 final ActivityStack stack = stacks.get(stackNdx);
518 if (!isFrontStack(stack)) {
519 continue;
520 }
521 ActivityRecord hr = stack.topRunningActivityLocked(null);
522 if (hr != null) {
523 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
524 && processName.equals(hr.processName)) {
525 try {
George Mount2c92c972014-03-20 09:38:23 -0700526 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800527 didSomething = true;
528 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700529 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800530 Slog.w(TAG, "Exception in new application when starting activity "
531 + hr.intent.getComponent().flattenToShortString(), e);
532 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700533 }
Craig Mautner20e72272013-04-01 13:45:53 -0700534 }
Craig Mautner20e72272013-04-01 13:45:53 -0700535 }
536 }
537 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700538 if (!didSomething) {
539 ensureActivitiesVisibleLocked(null, 0);
540 }
Craig Mautner20e72272013-04-01 13:45:53 -0700541 return didSomething;
542 }
543
544 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800545 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
546 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800547 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
548 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800549 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800550 continue;
551 }
552 final ActivityRecord resumedActivity = stack.mResumedActivity;
553 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800554 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
555 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800556 return false;
557 }
Craig Mautner20e72272013-04-01 13:45:53 -0700558 }
559 }
560 return true;
561 }
562
Craig Mautnerde4ef022013-04-07 19:01:33 -0700563 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800564 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
565 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800566 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
567 final ActivityStack stack = stacks.get(stackNdx);
568 if (isFrontStack(stack)) {
569 final ActivityRecord r = stack.mResumedActivity;
570 if (r != null && r.state != ActivityState.RESUMED) {
571 return false;
572 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700573 }
574 }
575 }
576 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800577 if (DEBUG_STACK) Slog.d(TAG,
578 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
579 mLastFocusedStack + " to=" + mFocusedStack);
580 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700581 return true;
582 }
583
584 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800585 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
586 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800587 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
588 final ActivityStack stack = stacks.get(stackNdx);
589 final ActivityRecord r = stack.mResumedActivity;
590 if (r != null && (!r.nowVisible || r.waitingVisible)) {
591 return false;
592 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700593 }
594 }
595 return true;
596 }
597
Craig Mautner2acc3892013-09-23 10:28:14 -0700598 /**
599 * Pause all activities in either all of the stacks or just the back stacks.
600 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700601 * @return true if any activity was paused as a result of this call.
602 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700603 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700604 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800605 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
606 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800607 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
608 final ActivityStack stack = stacks.get(stackNdx);
609 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
610 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
611 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700612 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
613 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800614 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700615 }
616 }
617 return someActivityPaused;
618 }
619
Craig Mautnerde4ef022013-04-07 19:01:33 -0700620 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700621 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800622 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
623 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800624 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
625 final ActivityStack stack = stacks.get(stackNdx);
626 final ActivityRecord r = stack.mPausingActivity;
627 if (r != null && r.state != ActivityState.PAUSED
628 && r.state != ActivityState.STOPPED
629 && r.state != ActivityState.STOPPING) {
630 if (DEBUG_STATES) {
631 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
632 pausing = false;
633 } else {
634 return false;
635 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700636 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700637 }
638 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700639 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700640 }
641
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700642 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
643 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500644 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800645 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
646 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
647 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
648 final ActivityStack stack = stacks.get(stackNdx);
649 if (stack.mResumedActivity != null &&
650 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700651 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800652 }
653 }
654 }
655 }
656
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700657 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700658 sendWaitingVisibleReportLocked(r);
659 notifyActivityDrawnForKeyguard();
660 }
661
662 void sendWaitingVisibleReportLocked(ActivityRecord r) {
663 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700664 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700665 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700666 if (w.who == null) {
667 changed = true;
668 w.timeout = false;
669 if (r != null) {
670 w.who = new ComponentName(r.info.packageName, r.info.name);
671 }
672 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
673 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700674 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700675 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700676 if (changed) {
677 mService.notifyAll();
678 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700679 }
680
681 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
682 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700683 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700684 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700685 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700686 if (w.who == null) {
687 changed = true;
688 w.timeout = timeout;
689 if (r != null) {
690 w.who = new ComponentName(r.info.packageName, r.info.name);
691 }
692 w.thisTime = thisTime;
693 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700694 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700695 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700696 if (changed) {
697 mService.notifyAll();
698 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700699 }
700
Craig Mautner29219d92013-04-16 20:19:12 -0700701 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700702 final ActivityStack focusedStack = getFocusedStack();
703 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
704 if (r != null) {
705 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700706 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700707
Craig Mautner4a1cb222013-12-04 16:14:06 -0800708 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800709 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800710 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
711 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700712 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700713 r = stack.topRunningActivityLocked(null);
714 if (r != null) {
715 return r;
716 }
717 }
718 }
719 return null;
720 }
721
Dianne Hackborn09233282014-04-30 11:33:59 -0700722 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700723 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
725 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800726 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800727 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800728 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800729 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
730 final ActivityStack stack = stacks.get(stackNdx);
731 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
732 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700733 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700734 }
735 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700736
737 // The lists are already sorted from most recent to oldest. Just pull the most recent off
738 // each list and add it to list. Stop when all lists are empty or maxNum reached.
739 while (maxNum > 0) {
740 long mostRecentActiveTime = Long.MIN_VALUE;
741 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800742 final int numTaskLists = runningTaskLists.size();
743 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
744 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700745 if (!stackTaskList.isEmpty()) {
746 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
747 if (lastActiveTime > mostRecentActiveTime) {
748 mostRecentActiveTime = lastActiveTime;
749 selectedStackList = stackTaskList;
750 }
751 }
752 }
753 if (selectedStackList != null) {
754 list.add(selectedStackList.remove(0));
755 --maxNum;
756 } else {
757 break;
758 }
759 }
Craig Mautner20e72272013-04-01 13:45:53 -0700760 }
761
Craig Mautner23ac33b2013-04-01 16:26:35 -0700762 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700763 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700764 // Collect information about the target of the Intent.
765 ActivityInfo aInfo;
766 try {
767 ResolveInfo rInfo =
768 AppGlobals.getPackageManager().resolveIntent(
769 intent, resolvedType,
770 PackageManager.MATCH_DEFAULT_ONLY
771 | ActivityManagerService.STOCK_PM_FLAGS, userId);
772 aInfo = rInfo != null ? rInfo.activityInfo : null;
773 } catch (RemoteException e) {
774 aInfo = null;
775 }
776
777 if (aInfo != null) {
778 // Store the found target back into the intent, because now that
779 // we have it we never want to do this again. For example, if the
780 // user navigates back to this point in the history, we should
781 // always restart the exact same activity.
782 intent.setComponent(new ComponentName(
783 aInfo.applicationInfo.packageName, aInfo.name));
784
785 // Don't debug things in the system process
786 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
787 if (!aInfo.processName.equals("system")) {
788 mService.setDebugApp(aInfo.processName, true, false);
789 }
790 }
791
792 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
793 if (!aInfo.processName.equals("system")) {
794 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
795 }
796 }
797
Jeff Hao1b012d32014-08-20 10:35:34 -0700798 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700799 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700800 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700801 }
802 }
803 }
804 return aInfo;
805 }
806
Craig Mautner2219a1b2013-03-25 09:44:30 -0700807 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700808 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE);
Dianne Hackborn95465202014-09-15 16:21:55 -0700809 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
810 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700811 }
812
Craig Mautner23ac33b2013-04-01 16:26:35 -0700813 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700814 String callingPackage, Intent intent, String resolvedType,
815 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700816 IBinder resultTo, String resultWho, int requestCode, int startFlags,
817 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700818 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700819 // Refuse possible leaked file descriptors
820 if (intent != null && intent.hasFileDescriptors()) {
821 throw new IllegalArgumentException("File descriptors passed in Intent");
822 }
823 boolean componentSpecified = intent.getComponent() != null;
824
825 // Don't modify the client's object!
826 intent = new Intent(intent);
827
828 // Collect information about the target of the Intent.
829 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700830 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700831
Craig Mautnere0a38842013-12-16 16:14:02 -0800832 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700833 synchronized (mService) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700834 final int realCallingPid = Binder.getCallingPid();
835 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700836 int callingPid;
837 if (callingUid >= 0) {
838 callingPid = -1;
839 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700840 callingPid = realCallingPid;
841 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700842 } else {
843 callingPid = callingUid = -1;
844 }
845
Craig Mautnere0a38842013-12-16 16:14:02 -0800846 final ActivityStack stack;
847 if (container == null || container.mStack.isOnHomeDisplay()) {
848 stack = getFocusedStack();
849 } else {
850 stack = container.mStack;
851 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700852 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700853 && mService.mConfiguration.diff(config) != 0;
854 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700855 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700856
857 final long origId = Binder.clearCallingIdentity();
858
859 if (aInfo != null &&
860 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
861 // This may be a heavy-weight process! Check to see if we already
862 // have another, different heavy-weight process running.
863 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
864 if (mService.mHeavyWeightProcess != null &&
865 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
866 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700867 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700868 if (caller != null) {
869 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
870 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700871 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700872 } else {
873 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700874 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700875 + intent.toString());
876 ActivityOptions.abort(options);
877 return ActivityManager.START_PERMISSION_DENIED;
878 }
879 }
880
881 IIntentSender target = mService.getIntentSenderLocked(
882 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700883 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700884 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
885 | PendingIntent.FLAG_ONE_SHOT, null);
886
887 Intent newIntent = new Intent();
888 if (requestCode >= 0) {
889 // Caller is requesting a result.
890 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
891 }
892 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
893 new IntentSender(target));
894 if (mService.mHeavyWeightProcess.activities.size() > 0) {
895 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
896 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
897 hist.packageName);
898 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
899 hist.task.taskId);
900 }
901 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
902 aInfo.packageName);
903 newIntent.setFlags(intent.getFlags());
904 newIntent.setClassName("android",
905 HeavyWeightSwitcherActivity.class.getName());
906 intent = newIntent;
907 resolvedType = null;
908 caller = null;
909 callingUid = Binder.getCallingUid();
910 callingPid = Binder.getCallingPid();
911 componentSpecified = true;
912 try {
913 ResolveInfo rInfo =
914 AppGlobals.getPackageManager().resolveIntent(
915 intent, null,
916 PackageManager.MATCH_DEFAULT_ONLY
917 | ActivityManagerService.STOCK_PM_FLAGS, userId);
918 aInfo = rInfo != null ? rInfo.activityInfo : null;
919 aInfo = mService.getActivityInfoForUser(aInfo, userId);
920 } catch (RemoteException e) {
921 aInfo = null;
922 }
923 }
924 }
925 }
926
Dianne Hackborn91097de2014-04-04 18:02:06 -0700927 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
928 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -0700929 requestCode, callingPid, callingUid, callingPackage,
930 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700931 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700932
Craig Mautner85c11a82014-07-17 12:38:18 -0700933 Binder.restoreCallingIdentity(origId);
934
Craig Mautnerde4ef022013-04-07 19:01:33 -0700935 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700936 // If the caller also wants to switch to a new configuration,
937 // do so now. This allows a clean switch, as we are waiting
938 // for the current activity to pause (so we will not destroy
939 // it), and have not yet started the next activity.
940 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
941 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700942 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700943 if (DEBUG_CONFIGURATION) Slog.v(TAG,
944 "Updating to new configuration after starting activity.");
945 mService.updateConfigurationLocked(config, null, false, false);
946 }
947
Craig Mautner23ac33b2013-04-01 16:26:35 -0700948 if (outResult != null) {
949 outResult.result = res;
950 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700951 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700952 do {
953 try {
954 mService.wait();
955 } catch (InterruptedException e) {
956 }
957 } while (!outResult.timeout && outResult.who == null);
958 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700959 ActivityRecord r = stack.topRunningActivityLocked(null);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700960 if (r.nowVisible && r.state == ActivityState.RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700961 outResult.timeout = false;
962 outResult.who = new ComponentName(r.info.packageName, r.info.name);
963 outResult.totalTime = 0;
964 outResult.thisTime = 0;
965 } else {
966 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700967 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700968 do {
969 try {
970 mService.wait();
971 } catch (InterruptedException e) {
972 }
973 } while (!outResult.timeout && outResult.who == null);
974 }
975 }
976 }
977
978 return res;
979 }
980 }
981
982 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
983 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
984 Bundle options, int userId) {
985 if (intents == null) {
986 throw new NullPointerException("intents is null");
987 }
988 if (resolvedTypes == null) {
989 throw new NullPointerException("resolvedTypes is null");
990 }
991 if (intents.length != resolvedTypes.length) {
992 throw new IllegalArgumentException("intents are length different than resolvedTypes");
993 }
994
Craig Mautner23ac33b2013-04-01 16:26:35 -0700995
996 int callingPid;
997 if (callingUid >= 0) {
998 callingPid = -1;
999 } else if (caller == null) {
1000 callingPid = Binder.getCallingPid();
1001 callingUid = Binder.getCallingUid();
1002 } else {
1003 callingPid = callingUid = -1;
1004 }
1005 final long origId = Binder.clearCallingIdentity();
1006 try {
1007 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001008 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001009 for (int i=0; i<intents.length; i++) {
1010 Intent intent = intents[i];
1011 if (intent == null) {
1012 continue;
1013 }
1014
1015 // Refuse possible leaked file descriptors
1016 if (intent != null && intent.hasFileDescriptors()) {
1017 throw new IllegalArgumentException("File descriptors passed in Intent");
1018 }
1019
1020 boolean componentSpecified = intent.getComponent() != null;
1021
1022 // Don't modify the client's object!
1023 intent = new Intent(intent);
1024
1025 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001026 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001027 // TODO: New, check if this is correct
1028 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1029
1030 if (aInfo != null &&
1031 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
1032 != 0) {
1033 throw new IllegalArgumentException(
1034 "FLAG_CANT_SAVE_STATE not supported here");
1035 }
1036
1037 Bundle theseOptions;
1038 if (options != null && i == intents.length-1) {
1039 theseOptions = options;
1040 } else {
1041 theseOptions = null;
1042 }
Craig Mautner6170f732013-04-02 13:05:23 -07001043 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001044 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1045 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001046 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001047 if (res < 0) {
1048 return res;
1049 }
1050
1051 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1052 }
1053 }
1054 } finally {
1055 Binder.restoreCallingIdentity(origId);
1056 }
1057
1058 return ActivityManager.START_SUCCESS;
1059 }
1060
Craig Mautner2420ead2013-04-01 17:13:20 -07001061 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001062 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001063 throws RemoteException {
1064
1065 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001066 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001067 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001068
1069 // schedule launch ticks to collect information about slow apps.
1070 r.startLaunchTickingLocked();
1071
1072 // Have the window manager re-evaluate the orientation of
1073 // the screen based on the new activity order. Note that
1074 // as a result of this, it can call back into the activity
1075 // manager with a new orientation. We don't care about that,
1076 // because the activity is not currently running so we are
1077 // just restarting it anyway.
1078 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001079 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001080 mService.mConfiguration,
1081 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1082 mService.updateConfigurationLocked(config, r, false, false);
1083 }
1084
1085 r.app = app;
1086 app.waitingToKill = null;
1087 r.launchCount++;
1088 r.lastLaunchTime = SystemClock.uptimeMillis();
1089
1090 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1091
1092 int idx = app.activities.indexOf(r);
1093 if (idx < 0) {
1094 app.activities.add(r);
1095 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001096 mService.updateLruProcessLocked(app, true, null);
1097 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001098
1099 final ActivityStack stack = r.task.stack;
1100 try {
1101 if (app.thread == null) {
1102 throw new RemoteException();
1103 }
1104 List<ResultInfo> results = null;
1105 List<Intent> newIntents = null;
1106 if (andResume) {
1107 results = r.results;
1108 newIntents = r.newIntents;
1109 }
1110 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1111 + " icicle=" + r.icicle
1112 + " with results=" + results + " newIntents=" + newIntents
1113 + " andResume=" + andResume);
1114 if (andResume) {
1115 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1116 r.userId, System.identityHashCode(r),
1117 r.task.taskId, r.shortComponentName);
1118 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001119 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001120 // Home process is the root process of the task.
1121 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001122 }
1123 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1124 r.sleeping = false;
1125 r.forceNewConfig = false;
1126 mService.showAskCompatModeDialogLocked(r);
1127 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1128 String profileFile = null;
1129 ParcelFileDescriptor profileFd = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001130 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1131 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1132 mService.mProfileProc = app;
1133 profileFile = mService.mProfileFile;
1134 profileFd = mService.mProfileFd;
Craig Mautner2420ead2013-04-01 17:13:20 -07001135 }
1136 }
1137 app.hasShownUi = true;
1138 app.pendingUiClean = true;
1139 if (profileFd != null) {
1140 try {
1141 profileFd = profileFd.dup();
1142 } catch (IOException e) {
1143 if (profileFd != null) {
1144 try {
1145 profileFd.close();
1146 } catch (IOException o) {
1147 }
1148 profileFd = null;
1149 }
1150 }
1151 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001152
Jeff Hao1b012d32014-08-20 10:35:34 -07001153 ProfilerInfo profilerInfo = profileFile != null
1154 ? new ProfilerInfo(profileFile, profileFd, mService.mSamplingInterval,
1155 mService.mAutoStopProfiler) : null;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001156 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001157 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001158 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
1159 r.compat, r.task.voiceInteractor, app.repProcState, r.icicle, r.persistentState,
1160 results, newIntents, !andResume, mService.isNextTransitionForward(),
1161 profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001162
1163 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1164 // This may be a heavy-weight process! Note that the package
1165 // manager will ensure that only activity can run in the main
1166 // process of the .apk, which is the only thing that will be
1167 // considered heavy-weight.
1168 if (app.processName.equals(app.info.packageName)) {
1169 if (mService.mHeavyWeightProcess != null
1170 && mService.mHeavyWeightProcess != app) {
1171 Slog.w(TAG, "Starting new heavy weight process " + app
1172 + " when already running "
1173 + mService.mHeavyWeightProcess);
1174 }
1175 mService.mHeavyWeightProcess = app;
1176 Message msg = mService.mHandler.obtainMessage(
1177 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1178 msg.obj = r;
1179 mService.mHandler.sendMessage(msg);
1180 }
1181 }
1182
1183 } catch (RemoteException e) {
1184 if (r.launchFailed) {
1185 // This is the second time we failed -- finish activity
1186 // and give up.
1187 Slog.e(TAG, "Second failure launching "
1188 + r.intent.getComponent().flattenToShortString()
1189 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001190 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001191 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1192 "2nd-crash", false);
1193 return false;
1194 }
1195
1196 // This is the first time we failed -- restart process and
1197 // retry.
1198 app.activities.remove(r);
1199 throw e;
1200 }
1201
1202 r.launchFailed = false;
1203 if (stack.updateLRUListLocked(r)) {
1204 Slog.w(TAG, "Activity " + r
1205 + " being launched, but already in LRU list");
1206 }
1207
1208 if (andResume) {
1209 // As part of the process of launching, ActivityThread also performs
1210 // a resume.
1211 stack.minimalResumeActivityLocked(r);
1212 } else {
1213 // This activity is not starting in the resumed state... which
1214 // should look like we asked it to pause+stop (but remain visible),
1215 // and it has done so and reported back the current icicle and
1216 // other state.
1217 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1218 + " (starting in stopped state)");
1219 r.state = ActivityState.STOPPED;
1220 r.stopped = true;
1221 }
1222
1223 // Launch the new version setup screen if needed. We do this -after-
1224 // launching the initial activity (that is, home), so that it can have
1225 // a chance to initialize itself while in the background, making the
1226 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001227 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001228 mService.startSetupActivityLocked();
1229 }
1230
Dianne Hackborn465fa392014-09-14 14:21:18 -07001231 // Update any services we are bound to that might care about whether
1232 // their client may have activities.
1233 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1234
Craig Mautner2420ead2013-04-01 17:13:20 -07001235 return true;
1236 }
1237
Craig Mautnere79d42682013-04-01 19:01:53 -07001238 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001239 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001240 // Is this activity's application already running?
1241 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001242 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001243
1244 r.task.stack.setLaunchTime(r);
1245
1246 if (app != null && app.thread != null) {
1247 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001248 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1249 || !"android".equals(r.info.packageName)) {
1250 // Don't add this if it is a platform component that is marked
1251 // to run in multiple processes, because this is actually
1252 // part of the framework so doesn't make sense to track as a
1253 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001254 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1255 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001256 }
George Mount2c92c972014-03-20 09:38:23 -07001257 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001258 return;
1259 } catch (RemoteException e) {
1260 Slog.w(TAG, "Exception when starting activity "
1261 + r.intent.getComponent().flattenToShortString(), e);
1262 }
1263
1264 // If a dead object exception was thrown -- fall through to
1265 // restart the application.
1266 }
1267
1268 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001269 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001270 }
1271
Craig Mautner6170f732013-04-02 13:05:23 -07001272 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001273 Intent intent, String resolvedType, ActivityInfo aInfo,
1274 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1275 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001276 int callingPid, int callingUid, String callingPackage,
1277 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001278 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1279 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001280 int err = ActivityManager.START_SUCCESS;
1281
1282 ProcessRecord callerApp = null;
1283 if (caller != null) {
1284 callerApp = mService.getRecordForAppLocked(caller);
1285 if (callerApp != null) {
1286 callingPid = callerApp.pid;
1287 callingUid = callerApp.info.uid;
1288 } else {
1289 Slog.w(TAG, "Unable to find app for caller " + caller
1290 + " (pid=" + callingPid + ") when starting: "
1291 + intent.toString());
1292 err = ActivityManager.START_PERMISSION_DENIED;
1293 }
1294 }
1295
1296 if (err == ActivityManager.START_SUCCESS) {
1297 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1298 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001299 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001300 + " on display " + (container == null ? (mFocusedStack == null ?
1301 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1302 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1303 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001304 }
1305
1306 ActivityRecord sourceRecord = null;
1307 ActivityRecord resultRecord = null;
1308 if (resultTo != null) {
1309 sourceRecord = isInAnyStackLocked(resultTo);
1310 if (DEBUG_RESULTS) Slog.v(
1311 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1312 if (sourceRecord != null) {
1313 if (requestCode >= 0 && !sourceRecord.finishing) {
1314 resultRecord = sourceRecord;
1315 }
1316 }
1317 }
1318 ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1319
Dianne Hackborn91097de2014-04-04 18:02:06 -07001320 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001321
Dianne Hackborn95465202014-09-15 16:21:55 -07001322 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001323 // Transfer the result target from the source activity to the new
1324 // one being started, including any failures.
1325 if (requestCode >= 0) {
1326 ActivityOptions.abort(options);
1327 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1328 }
1329 resultRecord = sourceRecord.resultTo;
1330 resultWho = sourceRecord.resultWho;
1331 requestCode = sourceRecord.requestCode;
1332 sourceRecord.resultTo = null;
1333 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001334 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001335 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001336 if (sourceRecord.launchedFromUid == callingUid) {
1337 // The new activity is being launched from the same uid as the previous
1338 // activity in the flow, and asking to forward its result back to the
1339 // previous. In this case the activity is serving as a trampoline between
1340 // the two, so we also want to update its launchedFromPackage to be the
1341 // same as the previous activity. Note that this is safe, since we know
1342 // these two packages come from the same uid; the caller could just as
1343 // well have supplied that same package name itself. This specifially
1344 // deals with the case of an intent picker/chooser being launched in the app
1345 // flow to redirect to an activity picked by the user, where we want the final
1346 // activity to consider it to have been launched by the previous app activity.
1347 callingPackage = sourceRecord.launchedFromPackage;
1348 }
Craig Mautner6170f732013-04-02 13:05:23 -07001349 }
1350
1351 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1352 // We couldn't find a class that can handle the given Intent.
1353 // That's the end of that!
1354 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1355 }
1356
1357 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1358 // We couldn't find the specific class specified in the Intent.
1359 // Also the end of the line.
1360 err = ActivityManager.START_CLASS_NOT_FOUND;
1361 }
1362
Dianne Hackborn91097de2014-04-04 18:02:06 -07001363 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1364 && sourceRecord.task.voiceSession != null) {
1365 // If this activity is being launched as part of a voice session, we need
1366 // to ensure that it is safe to do so. If the upcoming activity will also
1367 // be part of the voice session, we can only launch it if it has explicitly
1368 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001369 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001370 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1371 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001372 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1373 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001374 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1375 }
1376 } catch (RemoteException e) {
1377 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1378 }
1379 }
1380 }
1381
1382 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1383 // If the caller is starting a new voice session, just make sure the target
1384 // is actually allowing it to run this way.
1385 try {
1386 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1387 intent, resolvedType)) {
1388 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1389 }
1390 } catch (RemoteException e) {
1391 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1392 }
1393 }
1394
Craig Mautner6170f732013-04-02 13:05:23 -07001395 if (err != ActivityManager.START_SUCCESS) {
1396 if (resultRecord != null) {
1397 resultStack.sendActivityResultLocked(-1,
1398 resultRecord, resultWho, requestCode,
1399 Activity.RESULT_CANCELED, null);
1400 }
Craig Mautner6170f732013-04-02 13:05:23 -07001401 ActivityOptions.abort(options);
1402 return err;
1403 }
1404
1405 final int startAnyPerm = mService.checkPermission(
1406 START_ANY_ACTIVITY, callingPid, callingUid);
1407 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1408 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1409 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1410 if (resultRecord != null) {
1411 resultStack.sendActivityResultLocked(-1,
1412 resultRecord, resultWho, requestCode,
1413 Activity.RESULT_CANCELED, null);
1414 }
Craig Mautner6170f732013-04-02 13:05:23 -07001415 String msg;
1416 if (!aInfo.exported) {
1417 msg = "Permission Denial: starting " + intent.toString()
1418 + " from " + callerApp + " (pid=" + callingPid
1419 + ", uid=" + callingUid + ")"
1420 + " not exported from uid " + aInfo.applicationInfo.uid;
1421 } else {
1422 msg = "Permission Denial: starting " + intent.toString()
1423 + " from " + callerApp + " (pid=" + callingPid
1424 + ", uid=" + callingUid + ")"
1425 + " requires " + aInfo.permission;
1426 }
1427 Slog.w(TAG, msg);
1428 throw new SecurityException(msg);
1429 }
1430
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001431 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001432 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001433
Craig Mautner6170f732013-04-02 13:05:23 -07001434 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001435 try {
1436 // The Intent we give to the watcher has the extra data
1437 // stripped off, since it can contain private information.
1438 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001439 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001440 aInfo.applicationInfo.packageName);
1441 } catch (RemoteException e) {
1442 mService.mController = null;
1443 }
Ben Gruver5e207332013-04-03 17:41:37 -07001444 }
Craig Mautner6170f732013-04-02 13:05:23 -07001445
Ben Gruver5e207332013-04-03 17:41:37 -07001446 if (abort) {
1447 if (resultRecord != null) {
1448 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001449 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001450 }
Ben Gruver5e207332013-04-03 17:41:37 -07001451 // We pretend to the caller that it was really started, but
1452 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001453 ActivityOptions.abort(options);
1454 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001455 }
1456
1457 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001458 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001459 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001460 if (outActivity != null) {
1461 outActivity[0] = r;
1462 }
1463
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001464 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001465 if (voiceSession == null && (stack.mResumedActivity == null
1466 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001467 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1468 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001469 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001470 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001471 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001472 ActivityOptions.abort(options);
1473 return ActivityManager.START_SWITCHES_CANCELED;
1474 }
1475 }
1476
1477 if (mService.mDidAppSwitch) {
1478 // This is the second allowed switch since we stopped switches,
1479 // so now just generally allow switches. Use case: user presses
1480 // home (switches disabled, switch to home, mDidAppSwitch now true);
1481 // user taps a home icon (coming from home so allowed, we hit here
1482 // and now allow anyone to switch again).
1483 mService.mAppSwitchesAllowedTime = 0;
1484 } else {
1485 mService.mDidAppSwitch = true;
1486 }
1487
Craig Mautneree36c772014-07-16 14:56:05 -07001488 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001489
Dianne Hackborn91097de2014-04-04 18:02:06 -07001490 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001491 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001492
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001493 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001494 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001495 // activity start, but we are not actually doing an activity
1496 // switch... just dismiss the keyguard now, because we
1497 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001498 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001499 }
1500 return err;
1501 }
1502
Craig Mautner1b4bf852014-05-26 15:06:32 -07001503 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001504 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001505
1506 // On leanback only devices we should keep all activities in the same stack.
1507 if (!mLeanbackOnlyDevice &&
1508 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001509 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001510 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001511 if (taskStack.isOnHomeDisplay()) {
1512 if (mFocusedStack != taskStack) {
1513 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1514 "focused stack to r=" + r + " task=" + task);
1515 mFocusedStack = taskStack;
1516 } else {
1517 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1518 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1519 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001520 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001521 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001522 }
1523
Craig Mautnere0a38842013-12-16 16:14:02 -08001524 final ActivityContainer container = r.mInitialActivityContainer;
1525 if (container != null) {
1526 // The first time put it on the desired stack, after this put on task stack.
1527 r.mInitialActivityContainer = null;
1528 return container.mStack;
1529 }
1530
Craig Mautner1b4bf852014-05-26 15:06:32 -07001531 if (mFocusedStack != mHomeStack && (!newTask ||
1532 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001533 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1534 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1535 return mFocusedStack;
1536 }
1537
Craig Mautnere0a38842013-12-16 16:14:02 -08001538 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1539 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1540 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1541 if (!stack.isHomeStack()) {
1542 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1543 "adjustStackFocus: Setting focused stack=" + stack);
1544 mFocusedStack = stack;
1545 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001546 }
1547 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001548
Craig Mautner4a1cb222013-12-04 16:14:06 -08001549 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001550 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001551 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1552 " stackId=" + stackId);
1553 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001554 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001555 }
1556 return mHomeStack;
1557 }
1558
Craig Mautner29219d92013-04-16 20:19:12 -07001559 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001560 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001561 final TaskRecord task = r.task;
1562 boolean isHomeActivity = !r.isApplicationActivity();
1563 if (!isHomeActivity && task != null) {
1564 isHomeActivity = !task.isApplicationTask();
1565 }
1566 if (!isHomeActivity && task != null) {
1567 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1568 isHomeActivity = parent != null && parent.isHomeActivity();
1569 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001570 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001571 }
1572 }
1573
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001574 final int startActivityUncheckedLocked(ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001575 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001576 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001577 final Intent intent = r.intent;
1578 final int callingUid = r.launchedFromUid;
1579
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001580 // In some flows in to this function, we retrieve the task record and hold on to it
1581 // without a lock before calling back in to here... so the task at this point may
1582 // not actually be in recents. Check for that, and if it isn't in recents just
1583 // consider it invalid.
1584 if (inTask != null && !inTask.inRecents) {
1585 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1586 inTask = null;
1587 }
1588
Craig Mautnerad400af2014-08-13 16:41:36 -07001589 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001590 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1591 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001592
Craig Mautnera228ae92014-07-09 05:44:55 -07001593 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001594 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001595 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001596 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1597 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1598 "\"singleInstance\" or \"singleTask\"");
1599 launchFlags &=
1600 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1601 } else {
1602 switch (r.info.documentLaunchMode) {
1603 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1604 break;
1605 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1606 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1607 break;
1608 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1609 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1610 break;
1611 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1612 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1613 break;
1614 }
1615 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001616
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001617 final boolean launchTaskBehind = r.mLaunchTaskBehind
1618 && !launchSingleTask && !launchSingleInstance
1619 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001620
1621 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1622 // For whatever reason this activity is being launched into a new
1623 // task... yet the caller has requested a result back. Well, that
1624 // is pretty messed up, so instead immediately send back a cancel
1625 // and let the new task continue launched as normal without a
1626 // dependency on its originator.
1627 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1628 r.resultTo.task.stack.sendActivityResultLocked(-1,
1629 r.resultTo, r.resultWho, r.requestCode,
1630 Activity.RESULT_CANCELED, null);
1631 r.resultTo = null;
1632 }
1633
1634 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1635 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1636 }
1637
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001638 // If we are actually going to launch in to a new task, there are some cases where
1639 // we further want to do multiple task.
1640 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1641 if (launchTaskBehind
1642 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1643 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1644 }
1645 }
1646
Craig Mautner8849a5e2013-04-02 16:41:03 -07001647 // We'll invoke onUserLeaving before onPause only if the launching
1648 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001649 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001650 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001651
1652 // If the caller has asked not to resume at this point, we make note
1653 // of this in the record so that we can skip it when trying to find
1654 // the top running activity.
1655 if (!doResume) {
1656 r.delayedResume = true;
1657 }
1658
Craig Mautnera254cd72014-05-25 16:47:39 -07001659 ActivityRecord notTop =
1660 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001661
1662 // If the onlyIfNeeded flag is set, then we can do this if the activity
1663 // being launched is the same as the one making the call... or, as
1664 // a special case, if we do not know the caller then we count the
1665 // current top activity as the caller.
1666 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1667 ActivityRecord checkedCaller = sourceRecord;
1668 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001669 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001670 }
1671 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1672 // Caller is not the same as launcher, so always needed.
1673 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1674 }
1675 }
1676
Craig Mautner8849a5e2013-04-02 16:41:03 -07001677 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001678 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001679
1680 // If the caller is not coming from another activity, but has given us an
1681 // explicit task into which they would like us to launch the new activity,
1682 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001683 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1684 final Intent baseIntent = inTask.getBaseIntent();
1685 final ActivityRecord root = inTask.getRootActivity();
1686 if (baseIntent == null) {
1687 ActivityOptions.abort(options);
1688 throw new IllegalArgumentException("Launching into task without base intent: "
1689 + inTask);
1690 }
1691
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001692 // If this task is empty, then we are adding the first activity -- it
1693 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001694 if (launchSingleInstance || launchSingleTask) {
1695 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1696 ActivityOptions.abort(options);
1697 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1698 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001699 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001700 if (root != null) {
1701 ActivityOptions.abort(options);
1702 throw new IllegalArgumentException("Caller with inTask " + inTask
1703 + " has root " + root + " but target is singleInstance/Task");
1704 }
1705 }
1706
1707 // If task is empty, then adopt the interesting intent launch flags in to the
1708 // activity being started.
1709 if (root == null) {
1710 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1711 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1712 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1713 launchFlags = (launchFlags&~flagsOfInterest)
1714 | (baseIntent.getFlags()&flagsOfInterest);
1715 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001716 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001717 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001718
Dianne Hackborn962d5352014-08-29 16:27:40 -07001719 // If the task is not empty and the caller is asking to start it as the root
1720 // of a new task, then we don't actually want to start this on the task. We
1721 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001722 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001723 addingToTask = false;
1724
1725 } else {
1726 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001727 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001728
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001729 reuseTask = inTask;
1730 } else {
1731 inTask = null;
1732 }
1733
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001734 if (inTask == null) {
1735 if (sourceRecord == null) {
1736 // This activity is not being started from another... in this
1737 // case we -always- start a new task.
1738 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1739 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1740 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1741 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1742 }
1743 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1744 // The original activity who is starting us is running as a single
1745 // instance... this new activity it is starting must go on its
1746 // own task.
1747 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1748 } else if (launchSingleInstance || launchSingleTask) {
1749 // The activity being started is a single instance... it always
1750 // gets launched into its own task.
1751 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1752 }
1753 }
1754
1755 ActivityInfo newTaskInfo = null;
1756 Intent newTaskIntent = null;
1757 ActivityStack sourceStack;
1758 if (sourceRecord != null) {
1759 if (sourceRecord.finishing) {
1760 // If the source is finishing, we can't further count it as our source. This
1761 // is because the task it is associated with may now be empty and on its way out,
1762 // so we don't want to blindly throw it in to that task. Instead we will take
1763 // the NEW_TASK flow and try to find a task for it. But save the task information
1764 // so it can be used when creating the new task.
1765 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1766 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1767 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1768 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1769 newTaskInfo = sourceRecord.info;
1770 newTaskIntent = sourceRecord.task.intent;
1771 }
1772 sourceRecord = null;
1773 sourceStack = null;
1774 } else {
1775 sourceStack = sourceRecord.task.stack;
1776 }
1777 } else {
1778 sourceStack = null;
1779 }
1780
1781 boolean movedHome = false;
1782 ActivityStack targetStack;
1783
1784 intent.setFlags(launchFlags);
1785
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001786 // We may want to try to place the new activity in to an existing task. We always
1787 // do this if the target activity is singleTask or singleInstance; we will also do
1788 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1789 // us to still place it in a new task: multi task, always doc mode, or being asked to
1790 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001791 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1792 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001793 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001794 // If bring to front is requested, and no result is requested and we have not
1795 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001796 // we can find a task that was started with this same
1797 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001798 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001799 // See if there is a task to bring to the front. If this is
1800 // a SINGLE_INSTANCE activity, there can be one and only one
1801 // instance of it in the history, and it is always in its own
1802 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001803 ActivityRecord intentActivity = !launchSingleInstance ?
1804 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001805 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001806 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001807 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001808 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001809 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1810 }
Craig Mautner29219d92013-04-16 20:19:12 -07001811 if (r.task == null) {
1812 r.task = intentActivity.task;
1813 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001814 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001815 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001816 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1817 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001818 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001819 if (intentActivity.task.intent == null) {
1820 // This task was started because of movement of
1821 // the activity based on affinity... now that we
1822 // are actually launching it, we can assign the
1823 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001824 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001825 }
1826 // If the target task is not in the front, then we need
1827 // to bring it to the front... except... well, with
1828 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1829 // to have the same behavior as if a new instance was
1830 // being started, which means not bringing it to the front
1831 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001832 final ActivityStack lastStack = getLastStack();
1833 ActivityRecord curTop = lastStack == null?
1834 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner7504d7b2013-09-17 10:50:53 -07001835 if (curTop != null && (curTop.task != intentActivity.task ||
1836 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001837 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001838 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1839 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001840 // We really do want to push this one into the
1841 // user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001842 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001843 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1844 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001845 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001846 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001847 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001848 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1849 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001850 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001851 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001852 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001853 options = null;
1854 }
1855 }
1856 // If the caller has requested that the target task be
1857 // reset, then do so.
1858 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1859 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1860 }
1861 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1862 // We don't need to start a new activity, and
1863 // the client said not to do anything if that
1864 // is the case, so this is it! And for paranoia, make
1865 // sure we have correctly resumed the top activity.
1866 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001867 resumeTopActivitiesLocked(targetStack, null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001868 } else {
1869 ActivityOptions.abort(options);
1870 }
1871 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1872 }
1873 if ((launchFlags &
1874 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1875 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1876 // The caller has requested to completely replace any
1877 // existing task with its new activity. Well that should
1878 // not be too hard...
1879 reuseTask = intentActivity.task;
1880 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001881 reuseTask.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001882 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001883 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001884 // In this situation we want to remove all activities
1885 // from the task up to the one being started. In most
1886 // cases this means we are resetting the task to its
1887 // initial state.
1888 ActivityRecord top =
1889 intentActivity.task.performClearTaskLocked(r, launchFlags);
1890 if (top != null) {
1891 if (top.frontOfTask) {
1892 // Activity aliases may mean we use different
1893 // intents for the top activity, so make sure
1894 // the task now has the identity of the new
1895 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001896 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001897 }
1898 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1899 r, top.task);
1900 top.deliverNewIntentLocked(callingUid, r.intent);
1901 } else {
1902 // A special case: we need to
1903 // start the activity because it is not currently
1904 // running, and the caller has asked to clear the
1905 // current task to have this activity at the top.
1906 addingToTask = true;
1907 // Now pretend like this activity is being started
1908 // by the top of its task, so it is put in the
1909 // right place.
1910 sourceRecord = intentActivity;
1911 }
1912 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1913 // In this case the top activity on the task is the
1914 // same as the one being launched, so we take that
1915 // as a request to bring the task to the foreground.
1916 // If the top activity in the task is the root
1917 // activity, deliver this new intent to it if it
1918 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07001919 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001920 && intentActivity.realActivity.equals(r.realActivity)) {
1921 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1922 intentActivity.task);
1923 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07001924 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001925 }
1926 intentActivity.deliverNewIntentLocked(callingUid, r.intent);
1927 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1928 // In this case we are launching the root activity
1929 // of the task, but with a different intent. We
1930 // should start a new instance on top.
1931 addingToTask = true;
1932 sourceRecord = intentActivity;
1933 }
1934 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1935 // In this case an activity is being launched in to an
1936 // existing task, without resetting that task. This
1937 // is typically the situation of launching an activity
1938 // from a notification or shortcut. We want to place
1939 // the new activity on top of the current task.
1940 addingToTask = true;
1941 sourceRecord = intentActivity;
1942 } else if (!intentActivity.task.rootWasReset) {
1943 // In this case we are launching in to an existing task
1944 // that has not yet been started from its front door.
1945 // The current task has been brought to the front.
1946 // Ideally, we'd probably like to place this new task
1947 // at the bottom of its stack, but that's a little hard
1948 // to do with the current organization of the code so
1949 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07001950 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001951 }
1952 if (!addingToTask && reuseTask == null) {
1953 // We didn't do anything... but it was needed (a.k.a., client
1954 // don't use that intent!) And for paranoia, make
1955 // sure we have correctly resumed the top activity.
1956 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001957 targetStack.resumeTopActivityLocked(null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001958 } else {
1959 ActivityOptions.abort(options);
1960 }
1961 return ActivityManager.START_TASK_TO_FRONT;
1962 }
1963 }
1964 }
1965 }
1966
1967 //String uri = r.intent.toURI();
1968 //Intent intent2 = new Intent(uri);
1969 //Slog.i(TAG, "Given intent: " + r.intent);
1970 //Slog.i(TAG, "URI is: " + uri);
1971 //Slog.i(TAG, "To intent: " + intent2);
1972
1973 if (r.packageName != null) {
1974 // If the activity being launched is the same as the one currently
1975 // at the top, then we need to check if it should only be launched
1976 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001977 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001978 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001979 if (top != null && r.resultTo == null) {
1980 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
1981 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07001982 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07001983 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001984 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
1985 top.task);
1986 // For paranoia, make sure we have correctly
1987 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001988 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001989 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001990 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001991 }
1992 ActivityOptions.abort(options);
1993 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1994 // We don't need to start a new activity, and
1995 // the client said not to do anything if that
1996 // is the case, so this is it!
1997 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1998 }
1999 top.deliverNewIntentLocked(callingUid, r.intent);
2000 return ActivityManager.START_DELIVERED_TO_TOP;
2001 }
2002 }
2003 }
2004 }
2005
2006 } else {
2007 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002008 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2009 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002010 }
2011 ActivityOptions.abort(options);
2012 return ActivityManager.START_CLASS_NOT_FOUND;
2013 }
2014
2015 boolean newTask = false;
2016 boolean keepCurTransition = false;
2017
Craig Mautnerbb742462014-07-07 15:28:55 -07002018 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002019 sourceRecord.task : null;
2020
Craig Mautner8849a5e2013-04-02 16:41:03 -07002021 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002022 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002023 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002024 if (isLockTaskModeViolation(reuseTask)) {
2025 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2026 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2027 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002028 newTask = true;
2029 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerbb742462014-07-07 15:28:55 -07002030 if (!launchTaskBehind) {
2031 targetStack.moveToFront();
2032 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002033 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002034 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2035 newTaskInfo != null ? newTaskInfo : r.info,
2036 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002037 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2038 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002039 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
2040 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002041 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002042 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002043 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002044 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002045 if ((launchFlags &
2046 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2047 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2048 // Caller wants to appear on home activity, so before starting
2049 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002050 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002051 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002052 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002053 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002054 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002055 if (isLockTaskModeViolation(sourceTask)) {
2056 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2057 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2058 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002059 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07002060 targetStack.moveToFront();
Craig Mautnerabcc59c2014-05-17 15:13:28 -07002061 mWindowManager.moveTaskToTop(targetStack.topTask().taskId);
Craig Mautnera228ae92014-07-09 05:44:55 -07002062 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002063 // In this case, we are adding the activity to an existing
2064 // task, but the caller has asked to clear that task if the
2065 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002066 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002067 keepCurTransition = true;
2068 if (top != null) {
2069 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
2070 top.deliverNewIntentLocked(callingUid, r.intent);
2071 // For paranoia, make sure we have correctly
2072 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002073 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002074 if (doResume) {
2075 targetStack.resumeTopActivityLocked(null);
2076 }
2077 ActivityOptions.abort(options);
2078 return ActivityManager.START_DELIVERED_TO_TOP;
2079 }
2080 } else if (!addingToTask &&
2081 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2082 // In this case, we are launching an activity in our own task
2083 // that may already be running somewhere in the history, and
2084 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002085 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002086 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002087 final TaskRecord task = top.task;
2088 task.moveActivityToFrontLocked(top);
2089 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002090 top.updateOptionsLocked(options);
2091 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner0f922742013-08-06 08:44:42 -07002092 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002093 if (doResume) {
2094 targetStack.resumeTopActivityLocked(null);
2095 }
2096 return ActivityManager.START_DELIVERED_TO_TOP;
2097 }
2098 }
2099 // An existing activity is starting this new activity, so we want
2100 // to keep the new one in the same task as the one that is starting
2101 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002102 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002103 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002104 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002105
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002106 } else if (inTask != null) {
2107 // The calling is asking that the new activity be started in an explicit
2108 // task it has provided to us.
2109 if (isLockTaskModeViolation(inTask)) {
2110 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2111 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2112 }
2113 targetStack = inTask.stack;
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002114 targetStack.moveTaskToFrontLocked(inTask, r, options);
Craig Mautner02a4aa22014-09-03 10:01:24 -07002115 targetStack.moveToFront();
Dianne Hackborn2c924fb2014-08-29 13:02:03 -07002116 mWindowManager.moveTaskToTop(inTask.taskId);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002117
2118 // Check whether we should actually launch the new activity in to the task,
2119 // or just reuse the current activity on top.
2120 ActivityRecord top = inTask.getTopActivity();
2121 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2122 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2123 || launchSingleTop || launchSingleTask) {
2124 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2125 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2126 // We don't need to start a new activity, and
2127 // the client said not to do anything if that
2128 // is the case, so this is it!
2129 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2130 }
2131 top.deliverNewIntentLocked(callingUid, r.intent);
2132 return ActivityManager.START_DELIVERED_TO_TOP;
2133 }
2134 }
2135
Dianne Hackborn962d5352014-08-29 16:27:40 -07002136 if (!addingToTask) {
2137 // We don't actually want to have this activity added to the task, so just
2138 // stop here but still tell the caller that we consumed the intent.
2139 ActivityOptions.abort(options);
2140 return ActivityManager.START_TASK_TO_FRONT;
2141 }
2142
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002143 r.setTask(inTask, null);
2144 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2145 + " in explicit task " + r.task);
2146
Craig Mautner8849a5e2013-04-02 16:41:03 -07002147 } else {
2148 // This not being started from an existing activity, and not part
2149 // of a new task... just put it in the top task, though these days
2150 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07002151 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerdd221b32014-03-19 19:53:45 -07002152 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07002153 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002154 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002155 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002156 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002157 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2158 + " in new guessed " + r.task);
2159 }
2160
2161 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002162 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002163
Craig Mautner76e2a762014-06-24 09:05:43 -07002164 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2165 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2166 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002167 if (newTask) {
2168 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2169 }
2170 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002171 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002172 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002173 if (!launchTaskBehind) {
2174 // Don't set focus on an activity that's going to the back.
2175 mService.setFocusedActivityLocked(r);
2176 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002177 return ActivityManager.START_SUCCESS;
2178 }
2179
Craig Mautneree36c772014-07-16 14:56:05 -07002180 final void doPendingActivityLaunchesLocked(boolean doResume) {
2181 while (!mPendingActivityLaunches.isEmpty()) {
2182 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002183 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002184 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002185 }
2186 }
2187
Craig Mautner7f13ed32014-07-28 14:00:35 -07002188 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002189 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2190 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002191 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002192 mPendingActivityLaunches.remove(palNdx);
2193 }
2194 }
2195 }
2196
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002197 void acquireLaunchWakelock() {
2198 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2199 throw new IllegalStateException("Calling must be system uid");
2200 }
2201 mLaunchingActivity.acquire();
2202 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2203 // To be safe, don't allow the wake lock to be held for too long.
2204 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2205 }
2206 }
2207
Craig Mautnerf3333272013-04-22 10:55:53 -07002208 // Checked.
2209 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2210 Configuration config) {
2211 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2212
Craig Mautnerf3333272013-04-22 10:55:53 -07002213 ArrayList<ActivityRecord> stops = null;
2214 ArrayList<ActivityRecord> finishes = null;
2215 ArrayList<UserStartedState> startingUsers = null;
2216 int NS = 0;
2217 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002218 boolean booting = false;
2219 boolean enableScreen = false;
2220 boolean activityRemoved = false;
2221
2222 ActivityRecord r = ActivityRecord.forToken(token);
2223 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002224 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2225 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002226 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2227 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002228 if (fromTimeout) {
2229 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002230 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002231
2232 // This is a hack to semi-deal with a race condition
2233 // in the client where it can be constructed with a
2234 // newer configuration from when we asked it to launch.
2235 // We'll update with whatever configuration it now says
2236 // it used to launch.
2237 if (config != null) {
2238 r.configuration = config;
2239 }
2240
2241 // We are now idle. If someone is waiting for a thumbnail from
2242 // us, we can now deliver.
2243 r.idle = true;
2244
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002245 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
2246 if (!mService.mBooted && isFrontStack(r.task.stack)) {
2247 mService.mBooted = true;
2248 enableScreen = true;
2249 }
2250 }
2251
2252 if (allResumedActivitiesIdle()) {
2253 if (r != null) {
2254 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002255 }
2256
2257 if (mLaunchingActivity.isHeld()) {
2258 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2259 if (VALIDATE_WAKE_LOCK_CALLER &&
2260 Binder.getCallingUid() != Process.myUid()) {
2261 throw new IllegalStateException("Calling must be system uid");
2262 }
2263 mLaunchingActivity.release();
2264 }
2265 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002266 }
2267
2268 // Atomically retrieve all of the other things to do.
2269 stops = processStoppingActivitiesLocked(true);
2270 NS = stops != null ? stops.size() : 0;
2271 if ((NF=mFinishingActivities.size()) > 0) {
2272 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2273 mFinishingActivities.clear();
2274 }
2275
Prashant Malani84b08ad2014-07-15 16:28:24 -07002276 booting = mService.mBooting;
2277 mService.mBooting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002278
2279 if (mStartingUsers.size() > 0) {
2280 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2281 mStartingUsers.clear();
2282 }
2283
Craig Mautnerf3333272013-04-22 10:55:53 -07002284 // Stop any activities that are scheduled to do so but have been
2285 // waiting for the next one to start.
2286 for (int i = 0; i < NS; i++) {
2287 r = stops.get(i);
2288 final ActivityStack stack = r.task.stack;
2289 if (r.finishing) {
2290 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2291 } else {
2292 stack.stopActivityLocked(r);
2293 }
2294 }
2295
2296 // Finish any activities that are scheduled to do so but have been
2297 // waiting for the next one to start.
2298 for (int i = 0; i < NF; i++) {
2299 r = finishes.get(i);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002300 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07002301 }
2302
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002303 if (!booting) {
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002304 // Complete user switch
2305 if (startingUsers != null) {
2306 for (int i = 0; i < startingUsers.size(); i++) {
2307 mService.finishUserSwitch(startingUsers.get(i));
2308 }
2309 }
2310 // Complete starting up of background users
2311 if (mStartingBackgroundUsers.size() > 0) {
2312 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2313 mStartingBackgroundUsers.clear();
2314 for (int i = 0; i < startingUsers.size(); i++) {
2315 mService.finishUserBoot(startingUsers.get(i));
2316 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002317 }
2318 }
2319
2320 mService.trimApplications();
2321 //dump();
2322 //mWindowManager.dump();
2323
Dianne Hackborn7622a0f2014-09-30 14:31:42 -07002324 if (booting || enableScreen) {
2325 mService.postFinishBooting(booting, enableScreen);
Craig Mautnerf3333272013-04-22 10:55:53 -07002326 }
2327
2328 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002329 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002330 }
2331
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002332 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002333 }
2334
Craig Mautner8e569572013-10-11 17:36:59 -07002335 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002336 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002337 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2338 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002339 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2340 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2341 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002342 }
Craig Mautner19091252013-10-05 00:03:53 -07002343 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002344 }
2345
2346 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002347 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2348 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002349 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2350 stacks.get(stackNdx).closeSystemDialogsLocked();
2351 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002352 }
2353 }
2354
Craig Mautner93529a42013-10-04 15:03:13 -07002355 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002356 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002357 }
2358
Craig Mautner8d341ef2013-03-26 09:03:27 -07002359 /**
2360 * @return true if some activity was finished (or would have finished if doit were true).
2361 */
2362 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2363 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002364 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2365 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002366 final int numStacks = stacks.size();
2367 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2368 final ActivityStack stack = stacks.get(stackNdx);
2369 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2370 didSomething = true;
2371 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002372 }
2373 }
2374 return didSomething;
2375 }
2376
Dianne Hackborna413dc02013-07-12 12:02:55 -07002377 void updatePreviousProcessLocked(ActivityRecord r) {
2378 // Now that this process has stopped, we may want to consider
2379 // it to be the previous app to try to keep around in case
2380 // the user wants to return to it.
2381
2382 // First, found out what is currently the foreground app, so that
2383 // we don't blow away the previous app if this activity is being
2384 // hosted by the process that is actually still the foreground.
2385 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002386 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2387 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002388 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2389 final ActivityStack stack = stacks.get(stackNdx);
2390 if (isFrontStack(stack)) {
2391 if (stack.mResumedActivity != null) {
2392 fgApp = stack.mResumedActivity.app;
2393 } else if (stack.mPausingActivity != null) {
2394 fgApp = stack.mPausingActivity.app;
2395 }
2396 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002397 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002398 }
2399 }
2400
2401 // Now set this one as the previous process, only if that really
2402 // makes sense to.
2403 if (r.app != null && fgApp != null && r.app != fgApp
2404 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002405 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002406 mService.mPreviousProcess = r.app;
2407 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2408 }
2409 }
2410
Craig Mautner05d29032013-05-03 13:40:13 -07002411 boolean resumeTopActivitiesLocked() {
2412 return resumeTopActivitiesLocked(null, null, null);
2413 }
2414
2415 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2416 Bundle targetOptions) {
2417 if (targetStack == null) {
2418 targetStack = getFocusedStack();
2419 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002420 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002421 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002422 if (isFrontStack(targetStack)) {
2423 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2424 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002425 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2426 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002427 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2428 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002429 if (stack == targetStack) {
2430 // Already started above.
2431 continue;
2432 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002433 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002434 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002435 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002436 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002437 }
Craig Mautner05d29032013-05-03 13:40:13 -07002438 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002439 }
2440
2441 void finishTopRunningActivityLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002442 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2443 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002444 final int numStacks = stacks.size();
2445 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2446 final ActivityStack stack = stacks.get(stackNdx);
2447 stack.finishTopRunningActivityLocked(app);
2448 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002449 }
2450 }
2451
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002452 void finishVoiceTask(IVoiceInteractionSession session) {
2453 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2454 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2455 final int numStacks = stacks.size();
2456 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2457 final ActivityStack stack = stacks.get(stackNdx);
2458 stack.finishVoiceTask(session);
2459 }
2460 }
2461 }
2462
Craig Mautneraea74a52014-03-08 14:23:10 -08002463 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2464 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2465 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002466 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002467 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2468 // Caller wants the home activity moved with it. To accomplish this,
2469 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002470 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002471 }
2472 task.stack.moveTaskToFrontLocked(task, null, options);
2473 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2474 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002475 }
2476
Craig Mautner967212c2013-04-13 21:10:58 -07002477 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002478 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2479 if (activityContainer != null) {
2480 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002481 }
2482 return null;
2483 }
2484
Craig Mautner967212c2013-04-13 21:10:58 -07002485 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002486 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002487 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2488 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002489 }
2490 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002491 }
2492
Craig Mautner4a1cb222013-12-04 16:14:06 -08002493 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002494 ActivityRecord homeActivity = getHomeActivity();
2495 if (homeActivity != null) {
2496 return homeActivity.appToken;
2497 }
2498 return null;
2499 }
2500
2501 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002502 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2503 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2504 final TaskRecord task = tasks.get(taskNdx);
2505 if (task.isHomeTask()) {
2506 final ArrayList<ActivityRecord> activities = task.mActivities;
2507 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2508 final ActivityRecord r = activities.get(activityNdx);
2509 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002510 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002511 }
2512 }
2513 }
2514 }
2515 return null;
2516 }
2517
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002518 ActivityContainer createActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002519 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002520 ActivityContainer activityContainer =
2521 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002522 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002523 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002524 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002525 return activityContainer;
2526 }
2527
Craig Mautner34b73df2014-01-12 21:11:08 -08002528 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002529 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2530 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2531 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002532 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2533 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002534 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002535 }
2536 }
2537
Craig Mautner95da1082014-02-24 17:54:35 -08002538 void deleteActivityContainer(IActivityContainer container) {
2539 ActivityContainer activityContainer = (ActivityContainer)container;
2540 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002541 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2542 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002543 final int stackId = activityContainer.mStackId;
2544 mActivityContainers.remove(stackId);
2545 mWindowManager.removeStack(stackId);
2546 }
2547 }
2548
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002549 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002550 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2551 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002552 return -1;
2553 }
2554
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002555 ActivityContainer activityContainer = new ActivityContainer(stackId);
2556 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002557 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002558 return stackId;
2559 }
2560
2561 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002562 while (true) {
2563 if (++mLastStackId <= HOME_STACK_ID) {
2564 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002565 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002566 if (getStack(mLastStackId) == null) {
2567 break;
2568 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002569 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002570 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002571 }
2572
Craig Mautneref73ee12014-04-23 11:45:37 -07002573 void createStackForRestoredTaskHistory(ArrayList<TaskRecord> tasks) {
2574 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
2575 final ActivityStack stack = getStack(stackId);
2576 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2577 final TaskRecord task = tasks.get(taskNdx);
2578 stack.addTask(task, false, false);
2579 final int taskId = task.taskId;
2580 final ArrayList<ActivityRecord> activities = task.mActivities;
2581 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2582 final ActivityRecord r = activities.get(activityNdx);
2583 mWindowManager.addAppToken(0, r.appToken, taskId, stackId,
2584 r.info.screenOrientation, r.fullscreen,
2585 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002586 r.userId, r.info.configChanges, task.voiceSession != null,
2587 r.mLaunchTaskBehind);
Craig Mautneref73ee12014-04-23 11:45:37 -07002588 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002589 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002590 }
2591
Craig Mautner8d341ef2013-03-26 09:03:27 -07002592 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002593 final TaskRecord task = anyTaskForIdLocked(taskId);
2594 if (task == null) {
2595 return;
2596 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002597 final ActivityStack stack = getStack(stackId);
2598 if (stack == null) {
2599 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2600 return;
2601 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002602 task.stack.removeTask(task);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002603 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002604 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002605 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002606 }
2607
Craig Mautnerac6f8432013-07-17 13:24:59 -07002608 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002609 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002610 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2611 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002612 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2613 final ActivityStack stack = stacks.get(stackNdx);
2614 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002615 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2616 continue;
2617 }
2618 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2619 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2620 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002621 continue;
2622 }
2623 final ActivityRecord ar = stack.findTaskLocked(r);
2624 if (ar != null) {
2625 return ar;
2626 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002627 }
2628 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002629 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002630 return null;
2631 }
2632
2633 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002634 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2635 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002636 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2637 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2638 if (ar != null) {
2639 return ar;
2640 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002641 }
2642 }
2643 return null;
2644 }
2645
Craig Mautner8d341ef2013-03-26 09:03:27 -07002646 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002647 scheduleSleepTimeout();
2648 if (!mGoingToSleep.isHeld()) {
2649 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002650 if (mLaunchingActivity.isHeld()) {
2651 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2652 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002653 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002654 mLaunchingActivity.release();
2655 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002656 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002657 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002658 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002659 }
2660
2661 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002662 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002663
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002664 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002665 final long endTime = System.currentTimeMillis() + timeout;
2666 while (true) {
2667 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002668 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2669 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002670 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2671 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2672 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002673 }
2674 if (cantShutdown) {
2675 long timeRemaining = endTime - System.currentTimeMillis();
2676 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002677 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002678 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002679 } catch (InterruptedException e) {
2680 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002681 } else {
2682 Slog.w(TAG, "Activity manager shutdown timed out");
2683 timedout = true;
2684 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002685 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002686 } else {
2687 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002688 }
2689 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002690
2691 // Force checkReadyForSleep to complete.
2692 mSleepTimeout = true;
2693 checkReadyForSleepLocked();
2694
Craig Mautner8d341ef2013-03-26 09:03:27 -07002695 return timedout;
2696 }
2697
2698 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002699 removeSleepTimeouts();
2700 if (mGoingToSleep.isHeld()) {
2701 mGoingToSleep.release();
2702 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002703 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2704 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002705 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2706 final ActivityStack stack = stacks.get(stackNdx);
2707 stack.awakeFromSleepingLocked();
2708 if (isFrontStack(stack)) {
2709 resumeTopActivitiesLocked();
2710 }
Craig Mautner5314a402013-09-26 12:40:16 -07002711 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002712 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002713 mGoingToSleepActivities.clear();
2714 }
2715
2716 void activitySleptLocked(ActivityRecord r) {
2717 mGoingToSleepActivities.remove(r);
2718 checkReadyForSleepLocked();
2719 }
2720
2721 void checkReadyForSleepLocked() {
2722 if (!mService.isSleepingOrShuttingDown()) {
2723 // Do not care.
2724 return;
2725 }
2726
2727 if (!mSleepTimeout) {
2728 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002729 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2730 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002731 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2732 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2733 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002734 }
2735
2736 if (mStoppingActivities.size() > 0) {
2737 // Still need to tell some activities to stop; can't sleep yet.
2738 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2739 + mStoppingActivities.size() + " activities");
2740 scheduleIdleLocked();
2741 dontSleep = true;
2742 }
2743
2744 if (mGoingToSleepActivities.size() > 0) {
2745 // Still need to tell some activities to sleep; can't sleep yet.
2746 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2747 + mGoingToSleepActivities.size() + " activities");
2748 dontSleep = true;
2749 }
2750
2751 if (dontSleep) {
2752 return;
2753 }
2754 }
2755
Craig Mautnere0a38842013-12-16 16:14:02 -08002756 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2757 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002758 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2759 stacks.get(stackNdx).goToSleep();
2760 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002761 }
2762
2763 removeSleepTimeouts();
2764
2765 if (mGoingToSleep.isHeld()) {
2766 mGoingToSleep.release();
2767 }
2768 if (mService.mShuttingDown) {
2769 mService.notifyAll();
2770 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002771 }
2772
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002773 boolean reportResumedActivityLocked(ActivityRecord r) {
2774 final ActivityStack stack = r.task.stack;
2775 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002776 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002777 }
2778 if (allResumedActivitiesComplete()) {
2779 ensureActivitiesVisibleLocked(null, 0);
2780 mWindowManager.executeAppTransition();
2781 return true;
2782 }
2783 return false;
2784 }
2785
Craig Mautner8d341ef2013-03-26 09:03:27 -07002786 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002787 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2788 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002789 final int numStacks = stacks.size();
2790 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2791 final ActivityStack stack = stacks.get(stackNdx);
2792 stack.handleAppCrashLocked(app);
2793 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002794 }
2795 }
2796
Jose Lima4b6c6692014-08-12 17:41:12 -07002797 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002798 final ActivityStack stack = r.task.stack;
2799 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002800 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
2801 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002802 return false;
2803 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002804 final boolean isVisible = stack.hasVisibleBehindActivity();
2805 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
2806 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002807
2808 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002809 if (top == null || top == r || (visible == isVisible)) {
2810 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
2811 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002812 return true;
2813 }
2814
2815 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002816 if (visible && top.fullscreen) {
2817 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07002818 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07002819 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07002820 " top.app.thread=" + top.app.thread);
2821 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002822 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2823 // Only the activity set as currently visible behind should actively reset its
2824 // visible behind state.
2825 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
2826 + visible + " stack.getVisibleBehindActivity()=" +
2827 stack.getVisibleBehindActivity() + " r=" + r);
2828 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002829 }
2830
Jose Lima4b6c6692014-08-12 17:41:12 -07002831 stack.setVisibleBehindActivity(visible ? r : null);
2832 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002833 // Make the activity immediately above r opaque.
2834 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2835 if (next != null) {
2836 mService.convertFromTranslucent(next.appToken);
2837 }
2838 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002839 if (top.app != null && top.app.thread != null) {
2840 // Notify the top app of the change.
2841 try {
2842 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2843 } catch (RemoteException e) {
2844 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002845 }
2846 return true;
2847 }
2848
Craig Mautnerbb742462014-07-07 15:28:55 -07002849 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2850 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2851 r.mLaunchTaskBehind = false;
2852 final TaskRecord task = r.task;
2853 task.setLastThumbnail(task.stack.screenshotActivities(r));
2854 mService.addRecentTaskLocked(task);
2855 mWindowManager.setAppVisibility(r.appToken, false);
2856 }
2857
2858 void scheduleLaunchTaskBehindComplete(IBinder token) {
2859 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2860 }
2861
Craig Mautnerde4ef022013-04-07 19:01:33 -07002862 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002863 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002864 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2865 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002866 final int topStackNdx = stacks.size() - 1;
2867 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2868 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002869 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002870 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002871 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002872 }
2873
2874 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002875 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2876 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002877 final int numStacks = stacks.size();
2878 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2879 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002880 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002881 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002882 }
2883 }
2884
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002885 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2886 // Examine all activities currently running in the process.
2887 TaskRecord firstTask = null;
2888 // Tasks is non-null only if two or more tasks are found.
2889 ArraySet<TaskRecord> tasks = null;
2890 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
2891 for (int i=0; i<app.activities.size(); i++) {
2892 ActivityRecord r = app.activities.get(i);
2893 // First, if we find an activity that is in the process of being destroyed,
2894 // then we just aren't going to do anything for now; we want things to settle
2895 // down before we try to prune more activities.
2896 if (r.finishing || r.state == ActivityState.DESTROYING
2897 || r.state == ActivityState.DESTROYED) {
2898 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
2899 return;
2900 }
2901 // Don't consider any activies that are currently not in a state where they
2902 // can be destroyed.
2903 if (r.visible || !r.stopped || !r.haveState
2904 || r.state == ActivityState.RESUMED || r.state == ActivityState.PAUSING
2905 || r.state == ActivityState.PAUSED || r.state == ActivityState.STOPPING) {
2906 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
2907 continue;
2908 }
2909 if (r.task != null) {
2910 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
2911 + " from " + r);
2912 if (firstTask == null) {
2913 firstTask = r.task;
2914 } else if (firstTask != r.task) {
2915 if (tasks == null) {
2916 tasks = new ArraySet<>();
2917 tasks.add(firstTask);
2918 }
2919 tasks.add(r.task);
2920 }
2921 }
2922 }
2923 if (tasks == null) {
2924 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
2925 return;
2926 }
2927 // If we have activities in multiple tasks that are in a position to be destroyed,
2928 // let's iterate through the tasks and release the oldest one.
2929 final int numDisplays = mActivityDisplays.size();
2930 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2931 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2932 // Step through all stacks starting from behind, to hit the oldest things first.
2933 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2934 final ActivityStack stack = stacks.get(stackNdx);
2935 // Try to release activities in this stack; if we manage to, we are done.
2936 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2937 return;
2938 }
2939 }
2940 }
2941 }
2942
Craig Mautner8d341ef2013-03-26 09:03:27 -07002943 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002944 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
2945 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002946 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002947
Craig Mautner858d8a62013-04-23 17:08:34 -07002948 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002949 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2950 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002951 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002952 final ActivityStack stack = stacks.get(stackNdx);
2953 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002954 TaskRecord task = stack.topTask();
2955 if (task != null) {
2956 mWindowManager.moveTaskToTop(task.taskId);
2957 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002958 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002959 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002960
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002961 ActivityStack stack = getStack(restoreStackId);
2962 if (stack == null) {
2963 stack = mHomeStack;
2964 }
2965 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002966 if (stack.isOnHomeDisplay()) {
2967 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002968 TaskRecord task = stack.topTask();
2969 if (task != null) {
2970 mWindowManager.moveTaskToTop(task.taskId);
2971 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002972 } else {
2973 // Stack was moved to another display while user was swapped out.
Craig Mautner84984fa2014-06-19 11:19:20 -07002974 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
Craig Mautnere0a38842013-12-16 16:14:02 -08002975 }
Craig Mautner93529a42013-10-04 15:03:13 -07002976 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002977 }
2978
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002979 /**
2980 * Add background users to send boot completed events to.
2981 * @param userId The user being started in the background
2982 * @param uss The state object for the user.
2983 */
2984 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
2985 mStartingBackgroundUsers.add(uss);
2986 }
2987
Craig Mautnerde4ef022013-04-07 19:01:33 -07002988 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
2989 int N = mStoppingActivities.size();
2990 if (N <= 0) return null;
2991
2992 ArrayList<ActivityRecord> stops = null;
2993
2994 final boolean nowVisible = allResumedActivitiesVisible();
2995 for (int i=0; i<N; i++) {
2996 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07002997 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07002998 + nowVisible + " waitingVisible=" + s.waitingVisible
2999 + " finishing=" + s.finishing);
3000 if (s.waitingVisible && nowVisible) {
3001 mWaitingVisibleActivities.remove(s);
3002 s.waitingVisible = false;
3003 if (s.finishing) {
3004 // If this activity is finishing, it is sitting on top of
3005 // everyone else but we now know it is no longer needed...
3006 // so get rid of it. Otherwise, we need to go through the
3007 // normal flow and hide it once we determine that it is
3008 // hidden by the activities in front of it.
3009 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003010 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003011 }
3012 }
3013 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
3014 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
3015 if (stops == null) {
3016 stops = new ArrayList<ActivityRecord>();
3017 }
3018 stops.add(s);
3019 mStoppingActivities.remove(i);
3020 N--;
3021 i--;
3022 }
3023 }
3024
3025 return stops;
3026 }
3027
Craig Mautnercf910b02013-04-23 11:23:27 -07003028 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003029 // FIXME
3030/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3031 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07003032 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003033 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07003034 if (isFrontStack(stack)) {
3035 if (r == null) {
3036 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
3037 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003038 final ActivityRecord pausing = stack.mPausingActivity;
3039 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003040 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003041 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003042 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003043 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003044 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003045 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003046 }
3047 }
3048 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003049 final ActivityRecord resumed = stack.mResumedActivity;
3050 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003051 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003052 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003053 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003054 if (r != null && (state == ActivityState.INITIALIZING
3055 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003056 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003057 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003058 }
3059 }
3060 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003061*/
Craig Mautner76ea2242013-05-15 11:40:05 -07003062 }
3063
Craig Mautner27084302013-03-25 08:05:25 -07003064 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003065 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003066 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003067 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3068 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3069 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003070 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003071 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003072
Craig Mautner20e72272013-04-01 13:45:53 -07003073 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003074 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003075 }
3076
Dianne Hackborn390517b2013-05-30 15:03:32 -07003077 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3078 boolean needSep, String prefix) {
3079 if (activity != null) {
3080 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3081 if (needSep) {
3082 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003083 }
3084 pw.print(prefix);
3085 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003086 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003087 }
3088 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003089 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003090 }
3091
Craig Mautner8d341ef2013-03-26 09:03:27 -07003092 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3093 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003094 boolean printed = false;
3095 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003096 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3097 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003098 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
3099 pw.println(" (activities from bottom to top):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003100 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003101 final int numStacks = stacks.size();
3102 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3103 final ActivityStack stack = stacks.get(stackNdx);
3104 StringBuilder stackHeader = new StringBuilder(128);
3105 stackHeader.append(" Stack #");
3106 stackHeader.append(stack.mStackId);
3107 stackHeader.append(":");
3108 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3109 needSep, stackHeader.toString());
3110 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3111 !dumpAll, false, dumpPackage, true,
3112 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003113
Craig Mautner4a1cb222013-12-04 16:14:06 -08003114 needSep = printed;
3115 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3116 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003117 if (pr) {
3118 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003119 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003120 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003121 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3122 " mResumedActivity: ");
3123 if (pr) {
3124 printed = true;
3125 needSep = false;
3126 }
3127 if (dumpAll) {
3128 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3129 " mLastPausedActivity: ");
3130 if (pr) {
3131 printed = true;
3132 needSep = true;
3133 }
3134 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3135 needSep, " mLastNoHistoryActivity: ");
3136 }
3137 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003138 }
3139 }
3140
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003141 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3142 false, dumpPackage, true, " Activities waiting to finish:", null);
3143 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3144 false, dumpPackage, true, " Activities waiting to stop:", null);
3145 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3146 false, dumpPackage, true, " Activities waiting for another to become visible:",
3147 null);
3148 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3149 false, dumpPackage, true, " Activities waiting to sleep:", null);
3150 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3151 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003152
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003153 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003154 }
3155
Dianne Hackborn390517b2013-05-30 15:03:32 -07003156 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003157 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003158 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003159 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003160 String innerPrefix = null;
3161 String[] args = null;
3162 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003163 for (int i=list.size()-1; i>=0; i--) {
3164 final ActivityRecord r = list.get(i);
3165 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3166 continue;
3167 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003168 if (innerPrefix == null) {
3169 innerPrefix = prefix + " ";
3170 args = new String[0];
3171 }
3172 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003173 final boolean full = !brief && (complete || !r.isInHistory());
3174 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003175 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003176 needNL = false;
3177 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003178 if (header1 != null) {
3179 pw.println(header1);
3180 header1 = null;
3181 }
3182 if (header2 != null) {
3183 pw.println(header2);
3184 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003185 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003186 if (lastTask != r.task) {
3187 lastTask = r.task;
3188 pw.print(prefix);
3189 pw.print(full ? "* " : " ");
3190 pw.println(lastTask);
3191 if (full) {
3192 lastTask.dump(pw, prefix + " ");
3193 } else if (complete) {
3194 // Complete + brief == give a summary. Isn't that obvious?!?
3195 if (lastTask.intent != null) {
3196 pw.print(prefix); pw.print(" ");
3197 pw.println(lastTask.intent.toInsecureStringWithClip());
3198 }
3199 }
3200 }
3201 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3202 pw.print(" #"); pw.print(i); pw.print(": ");
3203 pw.println(r);
3204 if (full) {
3205 r.dump(pw, innerPrefix);
3206 } else if (complete) {
3207 // Complete + brief == give a summary. Isn't that obvious?!?
3208 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3209 if (r.app != null) {
3210 pw.print(innerPrefix); pw.println(r.app);
3211 }
3212 }
3213 if (client && r.app != null && r.app.thread != null) {
3214 // flush anything that is already in the PrintWriter since the thread is going
3215 // to write to the file descriptor directly
3216 pw.flush();
3217 try {
3218 TransferPipe tp = new TransferPipe();
3219 try {
3220 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3221 r.appToken, innerPrefix, args);
3222 // Short timeout, since blocking here can
3223 // deadlock with the application.
3224 tp.go(fd, 2000);
3225 } finally {
3226 tp.kill();
3227 }
3228 } catch (IOException e) {
3229 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3230 } catch (RemoteException e) {
3231 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3232 }
3233 needNL = true;
3234 }
3235 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003236 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003237 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003238
Craig Mautnerf3333272013-04-22 10:55:53 -07003239 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003240 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003241 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3242 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003243 }
3244
3245 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003246 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003247 }
3248
3249 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003250 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003251 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3252 }
3253
Craig Mautner05d29032013-05-03 13:40:13 -07003254 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003255 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3256 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3257 }
Craig Mautner05d29032013-05-03 13:40:13 -07003258 }
3259
Craig Mautner0eea92c2013-05-16 13:35:39 -07003260 void removeSleepTimeouts() {
3261 mSleepTimeout = false;
3262 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3263 }
3264
3265 final void scheduleSleepTimeout() {
3266 removeSleepTimeouts();
3267 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3268 }
3269
Craig Mautner4a1cb222013-12-04 16:14:06 -08003270 @Override
3271 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003272 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003273 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3274 }
3275
3276 @Override
3277 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003278 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003279 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3280 }
3281
3282 @Override
3283 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003284 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003285 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3286 }
3287
3288 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003289 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003290 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003291 newDisplay = mActivityDisplays.get(displayId) == null;
3292 if (newDisplay) {
3293 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003294 if (activityDisplay.mDisplay == null) {
3295 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3296 return;
3297 }
Craig Mautner4504de52013-12-20 09:06:56 -08003298 mActivityDisplays.put(displayId, activityDisplay);
3299 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003300 }
Craig Mautner4504de52013-12-20 09:06:56 -08003301 if (newDisplay) {
3302 mWindowManager.onDisplayAdded(displayId);
3303 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003304 }
3305
3306 public void handleDisplayRemovedLocked(int displayId) {
3307 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003308 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3309 if (activityDisplay != null) {
3310 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003311 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003312 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003313 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003314 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003315 }
3316 }
3317 mWindowManager.onDisplayRemoved(displayId);
3318 }
3319
3320 public void handleDisplayChangedLocked(int displayId) {
3321 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003322 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3323 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003324 // TODO: Update the bounds.
3325 }
3326 }
3327 mWindowManager.onDisplayChanged(displayId);
3328 }
3329
3330 StackInfo getStackInfo(ActivityStack stack) {
3331 StackInfo info = new StackInfo();
3332 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3333 info.displayId = Display.DEFAULT_DISPLAY;
3334 info.stackId = stack.mStackId;
3335
3336 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3337 final int numTasks = tasks.size();
3338 int[] taskIds = new int[numTasks];
3339 String[] taskNames = new String[numTasks];
3340 for (int i = 0; i < numTasks; ++i) {
3341 final TaskRecord task = tasks.get(i);
3342 taskIds[i] = task.taskId;
3343 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3344 : task.realActivity != null ? task.realActivity.flattenToString()
3345 : task.getTopActivity() != null ? task.getTopActivity().packageName
3346 : "unknown";
3347 }
3348 info.taskIds = taskIds;
3349 info.taskNames = taskNames;
3350 return info;
3351 }
3352
3353 StackInfo getStackInfoLocked(int stackId) {
3354 ActivityStack stack = getStack(stackId);
3355 if (stack != null) {
3356 return getStackInfo(stack);
3357 }
3358 return null;
3359 }
3360
3361 ArrayList<StackInfo> getAllStackInfosLocked() {
3362 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003363 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3364 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003365 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3366 list.add(getStackInfo(stacks.get(ndx)));
3367 }
3368 }
3369 return list;
3370 }
3371
Jason Monka8f569c2014-07-07 12:15:21 -04003372 void showLockTaskToast() {
3373 mLockTaskNotify.showToast(mLockTaskIsLocked);
3374 }
3375
3376 void setLockTaskModeLocked(TaskRecord task, boolean isLocked) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003377 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003378 // Take out of lock task mode if necessary
3379 if (mLockTaskModeTask != null) {
3380 final Message lockTaskMsg = Message.obtain();
3381 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3382 lockTaskMsg.what = LOCK_TASK_END_MSG;
3383 mLockTaskModeTask = null;
3384 mHandler.sendMessage(lockTaskMsg);
3385 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003386 return;
3387 }
3388 if (isLockTaskModeViolation(task)) {
3389 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3390 return;
3391 }
3392 mLockTaskModeTask = task;
3393 findTaskToMoveToFrontLocked(task, 0, null);
3394 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003395
3396 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003397 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3398 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003399 lockTaskMsg.what = LOCK_TASK_START_MSG;
Jason Monka8f569c2014-07-07 12:15:21 -04003400 lockTaskMsg.arg2 = !isLocked ? 1 : 0;
justinzhang5286d3f2014-05-12 17:06:01 -04003401 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003402 }
3403
3404 boolean isLockTaskModeViolation(TaskRecord task) {
3405 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3406 }
3407
3408 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3409 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003410 final Message lockTaskMsg = Message.obtain();
3411 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3412 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003413 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003414 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003415 }
3416 }
3417
3418 boolean isInLockTaskMode() {
3419 return mLockTaskModeTask != null;
3420 }
3421
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003422 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003423
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003424 public ActivityStackSupervisorHandler(Looper looper) {
3425 super(looper);
3426 }
3427
Craig Mautnerf3333272013-04-22 10:55:53 -07003428 void activityIdleInternal(ActivityRecord r) {
3429 synchronized (mService) {
3430 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3431 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003432 }
3433
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003434 @Override
3435 public void handleMessage(Message msg) {
3436 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003437 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003438 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003439 if (mService.mDidDexOpt) {
3440 mService.mDidDexOpt = false;
3441 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3442 nmsg.obj = msg.obj;
3443 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3444 return;
3445 }
3446 // We don't at this point know if the activity is fullscreen,
3447 // so we need to be conservative and assume it isn't.
3448 activityIdleInternal((ActivityRecord)msg.obj);
3449 } break;
3450 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003451 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003452 activityIdleInternal((ActivityRecord)msg.obj);
3453 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003454 case RESUME_TOP_ACTIVITY_MSG: {
3455 synchronized (mService) {
3456 resumeTopActivitiesLocked();
3457 }
3458 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003459 case SLEEP_TIMEOUT_MSG: {
3460 synchronized (mService) {
3461 if (mService.isSleepingOrShuttingDown()) {
3462 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3463 mSleepTimeout = true;
3464 checkReadyForSleepLocked();
3465 }
3466 }
3467 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003468 case LAUNCH_TIMEOUT_MSG: {
3469 if (mService.mDidDexOpt) {
3470 mService.mDidDexOpt = false;
3471 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3472 return;
3473 }
3474 synchronized (mService) {
3475 if (mLaunchingActivity.isHeld()) {
3476 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3477 if (VALIDATE_WAKE_LOCK_CALLER
3478 && Binder.getCallingUid() != Process.myUid()) {
3479 throw new IllegalStateException("Calling must be system uid");
3480 }
3481 mLaunchingActivity.release();
3482 }
3483 }
3484 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003485 case HANDLE_DISPLAY_ADDED: {
3486 handleDisplayAddedLocked(msg.arg1);
3487 } break;
3488 case HANDLE_DISPLAY_CHANGED: {
3489 handleDisplayChangedLocked(msg.arg1);
3490 } break;
3491 case HANDLE_DISPLAY_REMOVED: {
3492 handleDisplayRemovedLocked(msg.arg1);
3493 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003494 case CONTAINER_CALLBACK_VISIBILITY: {
3495 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003496 final IActivityContainerCallback callback = container.mCallback;
3497 if (callback != null) {
3498 try {
3499 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3500 } catch (RemoteException e) {
3501 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003502 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003503 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003504 case LOCK_TASK_START_MSG: {
3505 // When lock task starts, we disable the status bars.
3506 try {
Jason Monk62515be2014-05-21 16:06:19 -04003507 if (mLockTaskNotify == null) {
3508 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003509 }
Jason Monk62515be2014-05-21 16:06:19 -04003510 mLockTaskNotify.show(true);
Jason Monk2961ce52014-07-23 13:30:27 -04003511 mLockTaskIsLocked = msg.arg2 == 0;
Jason Monk62515be2014-05-21 16:06:19 -04003512 if (getStatusBarService() != null) {
3513 int flags =
3514 StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
Jason Monk2961ce52014-07-23 13:30:27 -04003515 if (!mLockTaskIsLocked) {
Jason Monk62515be2014-05-21 16:06:19 -04003516 flags ^= StatusBarManager.DISABLE_HOME
3517 | StatusBarManager.DISABLE_RECENT;
3518 }
3519 getStatusBarService().disable(flags, mToken,
3520 mService.mContext.getPackageName());
3521 }
3522 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003523 if (getDevicePolicyManager() != null) {
3524 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003525 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003526 }
justinzhang5286d3f2014-05-12 17:06:01 -04003527 } catch (RemoteException ex) {
3528 throw new RuntimeException(ex);
3529 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003530 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003531 case LOCK_TASK_END_MSG: {
3532 // When lock task ends, we enable the status bars.
3533 try {
Jason Monk62515be2014-05-21 16:06:19 -04003534 if (getStatusBarService() != null) {
3535 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3536 mService.mContext.getPackageName());
3537 }
3538 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003539 if (getDevicePolicyManager() != null) {
3540 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3541 msg.arg1);
3542 }
Jason Monk62515be2014-05-21 16:06:19 -04003543 if (mLockTaskNotify == null) {
3544 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3545 }
3546 mLockTaskNotify.show(false);
3547 try {
3548 boolean shouldLockKeyguard = Settings.System.getInt(
3549 mService.mContext.getContentResolver(),
3550 Settings.System.LOCK_TO_APP_EXIT_LOCKED) != 0;
Jason Monk2961ce52014-07-23 13:30:27 -04003551 if (!mLockTaskIsLocked && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003552 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003553 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003554 new LockPatternUtils(mService.mContext)
3555 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003556 }
3557 } catch (SettingNotFoundException e) {
3558 // No setting, don't lock.
3559 }
justinzhang5286d3f2014-05-12 17:06:01 -04003560 } catch (RemoteException ex) {
3561 throw new RuntimeException(ex);
3562 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003563 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003564 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3565 final ActivityContainer container = (ActivityContainer) msg.obj;
3566 final IActivityContainerCallback callback = container.mCallback;
3567 if (callback != null) {
3568 try {
3569 callback.onAllActivitiesComplete(container.asBinder());
3570 } catch (RemoteException e) {
3571 }
3572 }
3573 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003574 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3575 synchronized (mService) {
3576 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3577 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003578 final ActivityContainer container = (ActivityContainer) msg.obj;
3579 container.mStack.finishAllActivitiesLocked(true);
3580 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003581 }
3582 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003583 case LAUNCH_TASK_BEHIND_COMPLETE: {
3584 synchronized (mService) {
3585 ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
3586 if (r != null) {
3587 handleLaunchTaskBehindCompleteLocked(r);
3588 }
3589 }
3590 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003591 }
3592 }
3593 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003594
Ying Wangb081a592014-04-22 15:20:16 -07003595 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003596 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003597 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003598 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003599 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003600 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003601 ActivityRecord mParentActivity = null;
3602 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003603
Craig Mautnere3a00d72014-04-16 08:31:19 -07003604 boolean mVisible = true;
3605
Craig Mautner4a1cb222013-12-04 16:14:06 -08003606 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003607 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003608
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003609 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3610 final static int CONTAINER_STATE_NO_SURFACE = 1;
3611 final static int CONTAINER_STATE_FINISHING = 2;
3612 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3613
3614 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003615 synchronized (mService) {
3616 mStackId = stackId;
3617 mStack = new ActivityStack(this);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003618 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003619 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003620 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003621 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003622
Craig Mautnere0a38842013-12-16 16:14:02 -08003623 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003624 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3625 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003626 mActivityDisplay = activityDisplay;
3627 mStack.mDisplayId = activityDisplay.mDisplayId;
3628 mStack.mStacks = activityDisplay.mStacks;
3629
3630 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003631 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003632 }
3633
3634 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003635 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003636 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003637 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3638 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003639 return;
3640 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003641 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003642 }
3643 }
3644
3645 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003646 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003647 synchronized (mService) {
3648 if (mActivityDisplay != null) {
3649 return mActivityDisplay.mDisplayId;
3650 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003651 }
3652 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003653 }
3654
Jeff Brownca9bc702014-02-11 14:32:56 -08003655 @Override
3656 public boolean injectEvent(InputEvent event) {
3657 final long origId = Binder.clearCallingIdentity();
3658 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003659 synchronized (mService) {
3660 if (mActivityDisplay != null) {
3661 return mInputManagerInternal.injectInputEvent(event,
3662 mActivityDisplay.mDisplayId,
3663 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3664 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003665 }
3666 return false;
3667 } finally {
3668 Binder.restoreCallingIdentity(origId);
3669 }
3670 }
3671
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003672 @Override
3673 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003674 synchronized (mService) {
3675 if (mContainerState == CONTAINER_STATE_FINISHING) {
3676 return;
3677 }
3678 mContainerState = CONTAINER_STATE_FINISHING;
3679
3680 final Message msg =
3681 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003682 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003683
3684 long origId = Binder.clearCallingIdentity();
3685 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003686 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003687 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003688 } finally {
3689 Binder.restoreCallingIdentity(origId);
3690 }
3691 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003692 }
3693
Craig Mautner60257702014-09-17 15:02:33 -07003694 protected void detachLocked() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003695 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3696 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003697 if (mActivityDisplay != null) {
3698 mActivityDisplay.detachActivitiesLocked(mStack);
3699 mActivityDisplay = null;
3700 mStack.mDisplayId = -1;
3701 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003702 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003703 }
3704 }
3705
3706 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003707 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003708 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003709 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003710 Binder.getCallingUid(), mCurrentUser, false,
3711 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003712 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003713 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003714 String mimeType = intent.getType();
3715 if (mimeType == null && intent.getData() != null
3716 && "content".equals(intent.getData().getScheme())) {
3717 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3718 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003719 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0,
3720 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003721 }
3722
3723 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003724 public final int startActivityIntentSender(IIntentSender intentSender) {
3725 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3726
3727 if (!(intentSender instanceof PendingIntentRecord)) {
3728 throw new IllegalArgumentException("Bad PendingIntent object");
3729 }
3730
3731 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003732 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003733 }
3734
Craig Mautner247ab652014-04-25 10:09:00 -07003735 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3736 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003737 Binder.getCallingUid(), mCurrentUser, false,
3738 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautner247ab652014-04-25 10:09:00 -07003739 if (resolvedType == null) {
3740 resolvedType = intent.getType();
3741 if (resolvedType == null && intent.getData() != null
3742 && "content".equals(intent.getData().getScheme())) {
3743 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3744 }
3745 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003746 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003747 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003748 throw new SecurityException(
3749 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3750 }
3751 }
3752
3753 /** Throw a SecurityException if allowEmbedded is not true */
3754 @Override
3755 public final void checkEmbeddedAllowed(Intent intent) {
3756 checkEmbeddedAllowedInner(intent, null);
3757 }
3758
3759 /** Throw a SecurityException if allowEmbedded is not true */
3760 @Override
3761 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3762 if (!(intentSender instanceof PendingIntentRecord)) {
3763 throw new IllegalArgumentException("Bad PendingIntent object");
3764 }
3765 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3766 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3767 pendingIntent.key.requestResolvedType);
3768 }
3769
Craig Mautnerdf88d732014-01-27 09:21:32 -08003770 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003771 public IBinder asBinder() {
3772 return this;
3773 }
3774
Craig Mautner4504de52013-12-20 09:06:56 -08003775 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003776 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003777 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003778 }
3779
Craig Mautner4a1cb222013-12-04 16:14:06 -08003780 ActivityStackSupervisor getOuter() {
3781 return ActivityStackSupervisor.this;
3782 }
3783
Craig Mautnerd163e752014-06-13 17:18:47 -07003784 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003785 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003786 }
3787
3788 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003789 synchronized (mService) {
3790 if (mActivityDisplay != null) {
3791 mActivityDisplay.getBounds(outBounds);
3792 } else {
3793 outBounds.set(0, 0);
3794 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003795 }
3796 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003797
Craig Mautner6985bad2014-04-21 15:22:06 -07003798 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003799 void setVisible(boolean visible) {
3800 if (mVisible != visible) {
3801 mVisible = visible;
3802 if (mCallback != null) {
3803 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3804 0 /* unused */, this).sendToTarget();
3805 }
3806 }
3807 }
3808
Craig Mautner6985bad2014-04-21 15:22:06 -07003809 void setDrawn() {
3810 }
3811
Craig Mautner1b4bf852014-05-26 15:06:32 -07003812 // You can always start a new task on a regular ActivityStack.
3813 boolean isEligibleForNewTasks() {
3814 return true;
3815 }
3816
Craig Mautnerd163e752014-06-13 17:18:47 -07003817 void onTaskListEmptyLocked() {
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003818 }
3819
Craig Mautner34b73df2014-01-12 21:11:08 -08003820 @Override
3821 public String toString() {
3822 return mIdString + (mActivityDisplay == null ? "N" : "A");
3823 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003824 }
3825
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003826 private class VirtualActivityContainer extends ActivityContainer {
3827 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003828 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003829
3830 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3831 super(getNextStackId());
3832 mParentActivity = parent;
3833 mCallback = callback;
3834 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003835 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003836 }
3837
3838 @Override
3839 public void setSurface(Surface surface, int width, int height, int density) {
3840 super.setSurface(surface, width, height, density);
3841
3842 synchronized (mService) {
3843 final long origId = Binder.clearCallingIdentity();
3844 try {
3845 setSurfaceLocked(surface, width, height, density);
3846 } finally {
3847 Binder.restoreCallingIdentity(origId);
3848 }
3849 }
3850 }
3851
3852 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3853 if (mContainerState == CONTAINER_STATE_FINISHING) {
3854 return;
3855 }
3856 VirtualActivityDisplay virtualActivityDisplay =
3857 (VirtualActivityDisplay) mActivityDisplay;
3858 if (virtualActivityDisplay == null) {
3859 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003860 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003861 mActivityDisplay = virtualActivityDisplay;
3862 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3863 attachToDisplayLocked(virtualActivityDisplay);
3864 }
3865
3866 if (mSurface != null) {
3867 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003868 }
3869
Craig Mautner6985bad2014-04-21 15:22:06 -07003870 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003871 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003872 mStack.resumeTopActivityLocked(null);
3873 } else {
3874 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003875 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003876 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003877 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003878 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003879 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003880
Craig Mautnerd163e752014-06-13 17:18:47 -07003881 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003882
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003883 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3884 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003885 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003886
Craig Mautner6985bad2014-04-21 15:22:06 -07003887 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003888 boolean isAttachedLocked() {
3889 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003890 }
3891
3892 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003893 void setDrawn() {
3894 synchronized (mService) {
3895 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003896 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003897 }
3898 }
3899
Craig Mautner1b4bf852014-05-26 15:06:32 -07003900 // Never start a new task on an ActivityView if it isn't explicitly specified.
3901 @Override
3902 boolean isEligibleForNewTasks() {
3903 return false;
3904 }
3905
Craig Mautner60257702014-09-17 15:02:33 -07003906 void onTaskListEmptyLocked() {
3907 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3908 detachLocked();
3909 deleteActivityContainer(this);
3910 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
3911 }
3912
Craig Mautnerd163e752014-06-13 17:18:47 -07003913 private void setSurfaceIfReadyLocked() {
3914 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003915 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3916 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3917 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3918 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3919 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003920 }
3921 }
3922
Craig Mautner4a1cb222013-12-04 16:14:06 -08003923 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3924 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003925 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003926 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003927 int mDisplayId;
3928 Display mDisplay;
3929 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003930
Craig Mautner4a1cb222013-12-04 16:14:06 -08003931 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3932 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08003933 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003934
Jose Lima4b6c6692014-08-12 17:41:12 -07003935 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003936
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003937 ActivityDisplay() {
3938 }
Craig Mautner4504de52013-12-20 09:06:56 -08003939
Craig Mautner1a70a162014-09-13 12:09:31 -07003940 // After instantiation, check that mDisplay is not null before using this. The alternative
3941 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003942 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003943 final Display display = mDisplayManager.getDisplay(displayId);
3944 if (display == null) {
3945 return;
3946 }
3947 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003948 }
3949
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003950 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003951 mDisplay = display;
3952 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003953 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003954 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003955
3956 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003957 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
3958 + mDisplayId);
3959 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003960 }
3961
Craig Mautnere0a38842013-12-16 16:14:02 -08003962 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003963 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003964 + " from displayId=" + mDisplayId);
3965 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003966 }
3967
3968 void getBounds(Point bounds) {
3969 mDisplay.getDisplayInfo(mDisplayInfo);
3970 bounds.x = mDisplayInfo.appWidth;
3971 bounds.y = mDisplayInfo.appHeight;
3972 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003973
Jose Lima4b6c6692014-08-12 17:41:12 -07003974 void setVisibleBehindActivity(ActivityRecord r) {
3975 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003976 }
3977
Jose Lima4b6c6692014-08-12 17:41:12 -07003978 boolean hasVisibleBehindActivity() {
3979 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003980 }
3981
Craig Mautner34b73df2014-01-12 21:11:08 -08003982 @Override
3983 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003984 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3985 }
3986 }
3987
3988 class VirtualActivityDisplay extends ActivityDisplay {
3989 VirtualDisplay mVirtualDisplay;
3990
Craig Mautner6985bad2014-04-21 15:22:06 -07003991 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003992 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003993 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3994 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3995 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3996 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003997
3998 init(mVirtualDisplay.getDisplay());
3999
4000 mWindowManager.handleDisplayAdded(mDisplayId);
4001 }
4002
4003 void setSurface(Surface surface) {
4004 if (mVirtualDisplay != null) {
4005 mVirtualDisplay.setSurface(surface);
4006 }
4007 }
4008
4009 @Override
4010 void detachActivitiesLocked(ActivityStack stack) {
4011 super.detachActivitiesLocked(stack);
4012 if (mVirtualDisplay != null) {
4013 mVirtualDisplay.release();
4014 mVirtualDisplay = null;
4015 }
4016 }
4017
4018 @Override
4019 public String toString() {
4020 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004021 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004022 }
Jose Lima58e66d62014-05-27 20:00:27 -07004023
4024 private boolean isLeanbackOnlyDevice() {
4025 boolean onLeanbackOnly = false;
4026 try {
4027 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4028 PackageManager.FEATURE_LEANBACK_ONLY);
4029 } catch (RemoteException e) {
4030 // noop
4031 }
4032
4033 return onLeanbackOnly;
4034 }
Craig Mautner27084302013-03-25 08:05:25 -07004035}