blob: 83a8d45c5192b4462152e2a7befc1ea89671eeb7 [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) {
424 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
425 mWindowManager.showRecentApps();
426 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700427 }
Craig Mautner84984fa2014-06-19 11:19:20 -0700428 moveHomeStackTaskToTop(homeStackTaskType);
429 if (prev != null) {
430 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
431 }
432
Craig Mautnera8a90e02013-06-28 15:24:50 -0700433 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700434 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000435 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700436 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700437 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700438 }
439 return mService.startHomeActivityLocked(mCurrentUser);
440 }
441
Jorim Jaggi8de4311c2014-08-11 22:36:20 +0200442 void keyguardWaitingForActivityDrawn() {
443 mWindowManager.keyguardWaitingForActivityDrawn();
Craig Mautner27084302013-03-25 08:05:25 -0700444 }
445
Craig Mautner8d341ef2013-03-26 09:03:27 -0700446 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800447 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800448 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800449 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800450 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
451 ActivityStack stack = stacks.get(stackNdx);
452 TaskRecord task = stack.taskForIdLocked(id);
453 if (task != null) {
454 return task;
455 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700456 }
457 }
458 return null;
459 }
460
Craig Mautner6170f732013-04-02 13:05:23 -0700461 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800462 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800463 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800464 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800465 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
466 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
467 if (r != null) {
468 return r;
469 }
Craig Mautner6170f732013-04-02 13:05:23 -0700470 }
471 }
472 return null;
473 }
474
Craig Mautneref73ee12014-04-23 11:45:37 -0700475 void setNextTaskId(int taskId) {
476 if (taskId > mCurTaskId) {
477 mCurTaskId = taskId;
478 }
479 }
480
Craig Mautner8d341ef2013-03-26 09:03:27 -0700481 int getNextTaskId() {
482 do {
483 mCurTaskId++;
484 if (mCurTaskId <= 0) {
485 mCurTaskId = 1;
486 }
487 } while (anyTaskForIdLocked(mCurTaskId) != null);
488 return mCurTaskId;
489 }
490
Craig Mautnerde4ef022013-04-07 19:01:33 -0700491 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700492 ActivityStack stack = getFocusedStack();
493 if (stack == null) {
494 return null;
495 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700496 ActivityRecord resumedActivity = stack.mResumedActivity;
497 if (resumedActivity == null || resumedActivity.app == null) {
498 resumedActivity = stack.mPausingActivity;
499 if (resumedActivity == null || resumedActivity.app == null) {
500 resumedActivity = stack.topRunningActivityLocked(null);
501 }
502 }
503 return resumedActivity;
504 }
505
Dianne Hackbornff072722014-09-24 10:56:28 -0700506 boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
Craig Mautner20e72272013-04-01 13:45:53 -0700507 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800508 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800509 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
510 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800511 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
512 final ActivityStack stack = stacks.get(stackNdx);
513 if (!isFrontStack(stack)) {
514 continue;
515 }
516 ActivityRecord hr = stack.topRunningActivityLocked(null);
517 if (hr != null) {
518 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
519 && processName.equals(hr.processName)) {
520 try {
George Mount2c92c972014-03-20 09:38:23 -0700521 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800522 didSomething = true;
523 }
Dianne Hackbornff072722014-09-24 10:56:28 -0700524 } catch (RemoteException e) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800525 Slog.w(TAG, "Exception in new application when starting activity "
526 + hr.intent.getComponent().flattenToShortString(), e);
527 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700528 }
Craig Mautner20e72272013-04-01 13:45:53 -0700529 }
Craig Mautner20e72272013-04-01 13:45:53 -0700530 }
531 }
532 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700533 if (!didSomething) {
534 ensureActivitiesVisibleLocked(null, 0);
535 }
Craig Mautner20e72272013-04-01 13:45:53 -0700536 return didSomething;
537 }
538
539 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800540 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
541 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800542 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
543 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800544 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800545 continue;
546 }
547 final ActivityRecord resumedActivity = stack.mResumedActivity;
548 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800549 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
550 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800551 return false;
552 }
Craig Mautner20e72272013-04-01 13:45:53 -0700553 }
554 }
555 return true;
556 }
557
Craig Mautnerde4ef022013-04-07 19:01:33 -0700558 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800559 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
560 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800561 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
562 final ActivityStack stack = stacks.get(stackNdx);
563 if (isFrontStack(stack)) {
564 final ActivityRecord r = stack.mResumedActivity;
565 if (r != null && r.state != ActivityState.RESUMED) {
566 return false;
567 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700568 }
569 }
570 }
571 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800572 if (DEBUG_STACK) Slog.d(TAG,
573 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
574 mLastFocusedStack + " to=" + mFocusedStack);
575 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700576 return true;
577 }
578
579 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800580 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
581 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800582 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
583 final ActivityStack stack = stacks.get(stackNdx);
584 final ActivityRecord r = stack.mResumedActivity;
585 if (r != null && (!r.nowVisible || r.waitingVisible)) {
586 return false;
587 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700588 }
589 }
590 return true;
591 }
592
Craig Mautner2acc3892013-09-23 10:28:14 -0700593 /**
594 * Pause all activities in either all of the stacks or just the back stacks.
595 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700596 * @return true if any activity was paused as a result of this call.
597 */
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700598 boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700599 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800600 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
601 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800602 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
603 final ActivityStack stack = stacks.get(stackNdx);
604 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
605 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
606 " mResumedActivity=" + stack.mResumedActivity);
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700607 someActivityPaused |= stack.startPausingLocked(userLeaving, false, resuming,
608 dontWait);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800609 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700610 }
611 }
612 return someActivityPaused;
613 }
614
Craig Mautnerde4ef022013-04-07 19:01:33 -0700615 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700616 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800617 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
618 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800619 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
620 final ActivityStack stack = stacks.get(stackNdx);
621 final ActivityRecord r = stack.mPausingActivity;
622 if (r != null && r.state != ActivityState.PAUSED
623 && r.state != ActivityState.STOPPED
624 && r.state != ActivityState.STOPPING) {
625 if (DEBUG_STATES) {
626 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
627 pausing = false;
628 } else {
629 return false;
630 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700631 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700632 }
633 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700634 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700635 }
636
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700637 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
638 boolean resuming, boolean dontWait) {
John Spurlock8a985d22014-02-25 09:40:05 -0500639 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800640 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
641 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
642 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
643 final ActivityStack stack = stacks.get(stackNdx);
644 if (stack.mResumedActivity != null &&
645 stack.mActivityContainer.mParentActivity == parent) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700646 stack.startPausingLocked(userLeaving, uiSleeping, resuming, dontWait);
Craig Mautnerdf88d732014-01-27 09:21:32 -0800647 }
648 }
649 }
650 }
651
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700652 void reportActivityVisibleLocked(ActivityRecord r) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700653 sendWaitingVisibleReportLocked(r);
654 notifyActivityDrawnForKeyguard();
655 }
656
657 void sendWaitingVisibleReportLocked(ActivityRecord r) {
658 boolean changed = false;
Craig Mautner858d8a62013-04-23 17:08:34 -0700659 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700660 WaitResult w = mWaitingActivityVisible.get(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700661 if (w.who == null) {
662 changed = true;
663 w.timeout = false;
664 if (r != null) {
665 w.who = new ComponentName(r.info.packageName, r.info.name);
666 }
667 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
668 w.thisTime = w.totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700669 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700670 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700671 if (changed) {
672 mService.notifyAll();
673 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700674 }
675
676 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
677 long thisTime, long totalTime) {
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700678 boolean changed = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700679 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700680 WaitResult w = mWaitingActivityLaunched.remove(i);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700681 if (w.who == null) {
682 changed = true;
683 w.timeout = timeout;
684 if (r != null) {
685 w.who = new ComponentName(r.info.packageName, r.info.name);
686 }
687 w.thisTime = thisTime;
688 w.totalTime = totalTime;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700689 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700690 }
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700691 if (changed) {
692 mService.notifyAll();
693 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700694 }
695
Craig Mautner29219d92013-04-16 20:19:12 -0700696 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700697 final ActivityStack focusedStack = getFocusedStack();
698 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
699 if (r != null) {
700 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700701 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700702
Craig Mautner4a1cb222013-12-04 16:14:06 -0800703 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800704 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800705 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
706 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700707 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700708 r = stack.topRunningActivityLocked(null);
709 if (r != null) {
710 return r;
711 }
712 }
713 }
714 return null;
715 }
716
Dianne Hackborn09233282014-04-30 11:33:59 -0700717 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700718 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800719 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
720 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800721 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800722 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800723 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800724 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
725 final ActivityStack stack = stacks.get(stackNdx);
726 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
727 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700728 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700729 }
730 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700731
732 // The lists are already sorted from most recent to oldest. Just pull the most recent off
733 // each list and add it to list. Stop when all lists are empty or maxNum reached.
734 while (maxNum > 0) {
735 long mostRecentActiveTime = Long.MIN_VALUE;
736 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800737 final int numTaskLists = runningTaskLists.size();
738 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
739 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700740 if (!stackTaskList.isEmpty()) {
741 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
742 if (lastActiveTime > mostRecentActiveTime) {
743 mostRecentActiveTime = lastActiveTime;
744 selectedStackList = stackTaskList;
745 }
746 }
747 }
748 if (selectedStackList != null) {
749 list.add(selectedStackList.remove(0));
750 --maxNum;
751 } else {
752 break;
753 }
754 }
Craig Mautner20e72272013-04-01 13:45:53 -0700755 }
756
Craig Mautner23ac33b2013-04-01 16:26:35 -0700757 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700758 ProfilerInfo profilerInfo, int userId) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700759 // Collect information about the target of the Intent.
760 ActivityInfo aInfo;
761 try {
762 ResolveInfo rInfo =
763 AppGlobals.getPackageManager().resolveIntent(
764 intent, resolvedType,
765 PackageManager.MATCH_DEFAULT_ONLY
766 | ActivityManagerService.STOCK_PM_FLAGS, userId);
767 aInfo = rInfo != null ? rInfo.activityInfo : null;
768 } catch (RemoteException e) {
769 aInfo = null;
770 }
771
772 if (aInfo != null) {
773 // Store the found target back into the intent, because now that
774 // we have it we never want to do this again. For example, if the
775 // user navigates back to this point in the history, we should
776 // always restart the exact same activity.
777 intent.setComponent(new ComponentName(
778 aInfo.applicationInfo.packageName, aInfo.name));
779
780 // Don't debug things in the system process
781 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
782 if (!aInfo.processName.equals("system")) {
783 mService.setDebugApp(aInfo.processName, true, false);
784 }
785 }
786
787 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
788 if (!aInfo.processName.equals("system")) {
789 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
790 }
791 }
792
Jeff Hao1b012d32014-08-20 10:35:34 -0700793 if (profilerInfo != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700794 if (!aInfo.processName.equals("system")) {
Jeff Hao1b012d32014-08-20 10:35:34 -0700795 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName, profilerInfo);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700796 }
797 }
798 }
799 return aInfo;
800 }
801
Craig Mautner2219a1b2013-03-25 09:44:30 -0700802 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700803 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE);
Dianne Hackborn95465202014-09-15 16:21:55 -0700804 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null,
805 0, 0, 0, null, false, null, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700806 }
807
Craig Mautner23ac33b2013-04-01 16:26:35 -0700808 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700809 String callingPackage, Intent intent, String resolvedType,
810 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
Jeff Hao1b012d32014-08-20 10:35:34 -0700811 IBinder resultTo, String resultWho, int requestCode, int startFlags,
812 ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700813 Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700814 // Refuse possible leaked file descriptors
815 if (intent != null && intent.hasFileDescriptors()) {
816 throw new IllegalArgumentException("File descriptors passed in Intent");
817 }
818 boolean componentSpecified = intent.getComponent() != null;
819
820 // Don't modify the client's object!
821 intent = new Intent(intent);
822
823 // Collect information about the target of the Intent.
824 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
Jeff Hao1b012d32014-08-20 10:35:34 -0700825 profilerInfo, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700826
Craig Mautnere0a38842013-12-16 16:14:02 -0800827 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700828 synchronized (mService) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700829 final int realCallingPid = Binder.getCallingPid();
830 final int realCallingUid = Binder.getCallingUid();
Craig Mautner23ac33b2013-04-01 16:26:35 -0700831 int callingPid;
832 if (callingUid >= 0) {
833 callingPid = -1;
834 } else if (caller == null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700835 callingPid = realCallingPid;
836 callingUid = realCallingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700837 } else {
838 callingPid = callingUid = -1;
839 }
840
Craig Mautnere0a38842013-12-16 16:14:02 -0800841 final ActivityStack stack;
842 if (container == null || container.mStack.isOnHomeDisplay()) {
843 stack = getFocusedStack();
844 } else {
845 stack = container.mStack;
846 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700847 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700848 && mService.mConfiguration.diff(config) != 0;
849 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700850 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700851
852 final long origId = Binder.clearCallingIdentity();
853
854 if (aInfo != null &&
855 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
856 // This may be a heavy-weight process! Check to see if we already
857 // have another, different heavy-weight process running.
858 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
859 if (mService.mHeavyWeightProcess != null &&
860 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
861 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700862 int appCallingUid = callingUid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700863 if (caller != null) {
864 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
865 if (callerApp != null) {
Dianne Hackborn95465202014-09-15 16:21:55 -0700866 appCallingUid = callerApp.info.uid;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700867 } else {
868 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700869 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700870 + intent.toString());
871 ActivityOptions.abort(options);
872 return ActivityManager.START_PERMISSION_DENIED;
873 }
874 }
875
876 IIntentSender target = mService.getIntentSenderLocked(
877 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
Dianne Hackborn95465202014-09-15 16:21:55 -0700878 appCallingUid, userId, null, null, 0, new Intent[] { intent },
Craig Mautner23ac33b2013-04-01 16:26:35 -0700879 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
880 | PendingIntent.FLAG_ONE_SHOT, null);
881
882 Intent newIntent = new Intent();
883 if (requestCode >= 0) {
884 // Caller is requesting a result.
885 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
886 }
887 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
888 new IntentSender(target));
889 if (mService.mHeavyWeightProcess.activities.size() > 0) {
890 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
891 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
892 hist.packageName);
893 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
894 hist.task.taskId);
895 }
896 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
897 aInfo.packageName);
898 newIntent.setFlags(intent.getFlags());
899 newIntent.setClassName("android",
900 HeavyWeightSwitcherActivity.class.getName());
901 intent = newIntent;
902 resolvedType = null;
903 caller = null;
904 callingUid = Binder.getCallingUid();
905 callingPid = Binder.getCallingPid();
906 componentSpecified = true;
907 try {
908 ResolveInfo rInfo =
909 AppGlobals.getPackageManager().resolveIntent(
910 intent, null,
911 PackageManager.MATCH_DEFAULT_ONLY
912 | ActivityManagerService.STOCK_PM_FLAGS, userId);
913 aInfo = rInfo != null ? rInfo.activityInfo : null;
914 aInfo = mService.getActivityInfoForUser(aInfo, userId);
915 } catch (RemoteException e) {
916 aInfo = null;
917 }
918 }
919 }
920 }
921
Dianne Hackborn91097de2014-04-04 18:02:06 -0700922 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
923 voiceSession, voiceInteractor, resultTo, resultWho,
Dianne Hackborn95465202014-09-15 16:21:55 -0700924 requestCode, callingPid, callingUid, callingPackage,
925 realCallingPid, realCallingUid, startFlags, options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700926 componentSpecified, null, container, inTask);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700927
Craig Mautner85c11a82014-07-17 12:38:18 -0700928 Binder.restoreCallingIdentity(origId);
929
Craig Mautnerde4ef022013-04-07 19:01:33 -0700930 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700931 // If the caller also wants to switch to a new configuration,
932 // do so now. This allows a clean switch, as we are waiting
933 // for the current activity to pause (so we will not destroy
934 // it), and have not yet started the next activity.
935 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
936 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700937 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700938 if (DEBUG_CONFIGURATION) Slog.v(TAG,
939 "Updating to new configuration after starting activity.");
940 mService.updateConfigurationLocked(config, null, false, false);
941 }
942
Craig Mautner23ac33b2013-04-01 16:26:35 -0700943 if (outResult != null) {
944 outResult.result = res;
945 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700946 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700947 do {
948 try {
949 mService.wait();
950 } catch (InterruptedException e) {
951 }
952 } while (!outResult.timeout && outResult.who == null);
953 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700954 ActivityRecord r = stack.topRunningActivityLocked(null);
Dianne Hackborn6cfbb712014-09-17 12:47:35 -0700955 if (r.nowVisible && r.state == ActivityState.RESUMED) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700956 outResult.timeout = false;
957 outResult.who = new ComponentName(r.info.packageName, r.info.name);
958 outResult.totalTime = 0;
959 outResult.thisTime = 0;
960 } else {
961 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700962 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700963 do {
964 try {
965 mService.wait();
966 } catch (InterruptedException e) {
967 }
968 } while (!outResult.timeout && outResult.who == null);
969 }
970 }
971 }
972
973 return res;
974 }
975 }
976
977 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
978 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
979 Bundle options, int userId) {
980 if (intents == null) {
981 throw new NullPointerException("intents is null");
982 }
983 if (resolvedTypes == null) {
984 throw new NullPointerException("resolvedTypes is null");
985 }
986 if (intents.length != resolvedTypes.length) {
987 throw new IllegalArgumentException("intents are length different than resolvedTypes");
988 }
989
Craig Mautner23ac33b2013-04-01 16:26:35 -0700990
991 int callingPid;
992 if (callingUid >= 0) {
993 callingPid = -1;
994 } else if (caller == null) {
995 callingPid = Binder.getCallingPid();
996 callingUid = Binder.getCallingUid();
997 } else {
998 callingPid = callingUid = -1;
999 }
1000 final long origId = Binder.clearCallingIdentity();
1001 try {
1002 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -07001003 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -07001004 for (int i=0; i<intents.length; i++) {
1005 Intent intent = intents[i];
1006 if (intent == null) {
1007 continue;
1008 }
1009
1010 // Refuse possible leaked file descriptors
1011 if (intent != null && intent.hasFileDescriptors()) {
1012 throw new IllegalArgumentException("File descriptors passed in Intent");
1013 }
1014
1015 boolean componentSpecified = intent.getComponent() != null;
1016
1017 // Don't modify the client's object!
1018 intent = new Intent(intent);
1019
1020 // Collect information about the target of the Intent.
Jeff Hao1b012d32014-08-20 10:35:34 -07001021 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i], 0, null, userId);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001022 // TODO: New, check if this is correct
1023 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1024
1025 if (aInfo != null &&
1026 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
1027 != 0) {
1028 throw new IllegalArgumentException(
1029 "FLAG_CANT_SAVE_STATE not supported here");
1030 }
1031
1032 Bundle theseOptions;
1033 if (options != null && i == intents.length-1) {
1034 theseOptions = options;
1035 } else {
1036 theseOptions = null;
1037 }
Craig Mautner6170f732013-04-02 13:05:23 -07001038 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn95465202014-09-15 16:21:55 -07001039 aInfo, null, null, resultTo, null, -1, callingPid, callingUid,
1040 callingPackage, callingPid, callingUid,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001041 0, theseOptions, componentSpecified, outActivity, null, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001042 if (res < 0) {
1043 return res;
1044 }
1045
1046 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1047 }
1048 }
1049 } finally {
1050 Binder.restoreCallingIdentity(origId);
1051 }
1052
1053 return ActivityManager.START_SUCCESS;
1054 }
1055
Craig Mautner2420ead2013-04-01 17:13:20 -07001056 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001057 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001058 throws RemoteException {
1059
1060 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001061 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001062 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001063
1064 // schedule launch ticks to collect information about slow apps.
1065 r.startLaunchTickingLocked();
1066
1067 // Have the window manager re-evaluate the orientation of
1068 // the screen based on the new activity order. Note that
1069 // as a result of this, it can call back into the activity
1070 // manager with a new orientation. We don't care about that,
1071 // because the activity is not currently running so we are
1072 // just restarting it anyway.
1073 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001074 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001075 mService.mConfiguration,
1076 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1077 mService.updateConfigurationLocked(config, r, false, false);
1078 }
1079
1080 r.app = app;
1081 app.waitingToKill = null;
1082 r.launchCount++;
1083 r.lastLaunchTime = SystemClock.uptimeMillis();
1084
1085 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1086
1087 int idx = app.activities.indexOf(r);
1088 if (idx < 0) {
1089 app.activities.add(r);
1090 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001091 mService.updateLruProcessLocked(app, true, null);
1092 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001093
1094 final ActivityStack stack = r.task.stack;
1095 try {
1096 if (app.thread == null) {
1097 throw new RemoteException();
1098 }
1099 List<ResultInfo> results = null;
1100 List<Intent> newIntents = null;
1101 if (andResume) {
1102 results = r.results;
1103 newIntents = r.newIntents;
1104 }
1105 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1106 + " icicle=" + r.icicle
1107 + " with results=" + results + " newIntents=" + newIntents
1108 + " andResume=" + andResume);
1109 if (andResume) {
1110 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1111 r.userId, System.identityHashCode(r),
1112 r.task.taskId, r.shortComponentName);
1113 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001114 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001115 // Home process is the root process of the task.
1116 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001117 }
1118 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1119 r.sleeping = false;
1120 r.forceNewConfig = false;
1121 mService.showAskCompatModeDialogLocked(r);
1122 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1123 String profileFile = null;
1124 ParcelFileDescriptor profileFd = null;
Craig Mautner2420ead2013-04-01 17:13:20 -07001125 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1126 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1127 mService.mProfileProc = app;
1128 profileFile = mService.mProfileFile;
1129 profileFd = mService.mProfileFd;
Craig Mautner2420ead2013-04-01 17:13:20 -07001130 }
1131 }
1132 app.hasShownUi = true;
1133 app.pendingUiClean = true;
1134 if (profileFd != null) {
1135 try {
1136 profileFd = profileFd.dup();
1137 } catch (IOException e) {
1138 if (profileFd != null) {
1139 try {
1140 profileFd.close();
1141 } catch (IOException o) {
1142 }
1143 profileFd = null;
1144 }
1145 }
1146 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001147
Jeff Hao1b012d32014-08-20 10:35:34 -07001148 ProfilerInfo profilerInfo = profileFile != null
1149 ? new ProfilerInfo(profileFile, profileFd, mService.mSamplingInterval,
1150 mService.mAutoStopProfiler) : null;
Dianne Hackborna413dc02013-07-12 12:02:55 -07001151 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001152 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
Jeff Hao1b012d32014-08-20 10:35:34 -07001153 System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
1154 r.compat, r.task.voiceInteractor, app.repProcState, r.icicle, r.persistentState,
1155 results, newIntents, !andResume, mService.isNextTransitionForward(),
1156 profilerInfo);
Craig Mautner2420ead2013-04-01 17:13:20 -07001157
1158 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1159 // This may be a heavy-weight process! Note that the package
1160 // manager will ensure that only activity can run in the main
1161 // process of the .apk, which is the only thing that will be
1162 // considered heavy-weight.
1163 if (app.processName.equals(app.info.packageName)) {
1164 if (mService.mHeavyWeightProcess != null
1165 && mService.mHeavyWeightProcess != app) {
1166 Slog.w(TAG, "Starting new heavy weight process " + app
1167 + " when already running "
1168 + mService.mHeavyWeightProcess);
1169 }
1170 mService.mHeavyWeightProcess = app;
1171 Message msg = mService.mHandler.obtainMessage(
1172 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1173 msg.obj = r;
1174 mService.mHandler.sendMessage(msg);
1175 }
1176 }
1177
1178 } catch (RemoteException e) {
1179 if (r.launchFailed) {
1180 // This is the second time we failed -- finish activity
1181 // and give up.
1182 Slog.e(TAG, "Second failure launching "
1183 + r.intent.getComponent().flattenToShortString()
1184 + ", giving up", e);
Craig Mautner4a8dddbf2014-08-13 10:49:26 -07001185 mService.appDiedLocked(app);
Craig Mautner2420ead2013-04-01 17:13:20 -07001186 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1187 "2nd-crash", false);
1188 return false;
1189 }
1190
1191 // This is the first time we failed -- restart process and
1192 // retry.
1193 app.activities.remove(r);
1194 throw e;
1195 }
1196
1197 r.launchFailed = false;
1198 if (stack.updateLRUListLocked(r)) {
1199 Slog.w(TAG, "Activity " + r
1200 + " being launched, but already in LRU list");
1201 }
1202
1203 if (andResume) {
1204 // As part of the process of launching, ActivityThread also performs
1205 // a resume.
1206 stack.minimalResumeActivityLocked(r);
1207 } else {
1208 // This activity is not starting in the resumed state... which
1209 // should look like we asked it to pause+stop (but remain visible),
1210 // and it has done so and reported back the current icicle and
1211 // other state.
1212 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1213 + " (starting in stopped state)");
1214 r.state = ActivityState.STOPPED;
1215 r.stopped = true;
1216 }
1217
1218 // Launch the new version setup screen if needed. We do this -after-
1219 // launching the initial activity (that is, home), so that it can have
1220 // a chance to initialize itself while in the background, making the
1221 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001222 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001223 mService.startSetupActivityLocked();
1224 }
1225
Dianne Hackborn465fa392014-09-14 14:21:18 -07001226 // Update any services we are bound to that might care about whether
1227 // their client may have activities.
1228 mService.mServices.updateServiceConnectionActivitiesLocked(r.app);
1229
Craig Mautner2420ead2013-04-01 17:13:20 -07001230 return true;
1231 }
1232
Craig Mautnere79d42682013-04-01 19:01:53 -07001233 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001234 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001235 // Is this activity's application already running?
1236 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001237 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001238
1239 r.task.stack.setLaunchTime(r);
1240
1241 if (app != null && app.thread != null) {
1242 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001243 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1244 || !"android".equals(r.info.packageName)) {
1245 // Don't add this if it is a platform component that is marked
1246 // to run in multiple processes, because this is actually
1247 // part of the framework so doesn't make sense to track as a
1248 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001249 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1250 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001251 }
George Mount2c92c972014-03-20 09:38:23 -07001252 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001253 return;
1254 } catch (RemoteException e) {
1255 Slog.w(TAG, "Exception when starting activity "
1256 + r.intent.getComponent().flattenToShortString(), e);
1257 }
1258
1259 // If a dead object exception was thrown -- fall through to
1260 // restart the application.
1261 }
1262
1263 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001264 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001265 }
1266
Craig Mautner6170f732013-04-02 13:05:23 -07001267 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001268 Intent intent, String resolvedType, ActivityInfo aInfo,
1269 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1270 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborn95465202014-09-15 16:21:55 -07001271 int callingPid, int callingUid, String callingPackage,
1272 int realCallingPid, int realCallingUid, int startFlags, Bundle options,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001273 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container,
1274 TaskRecord inTask) {
Craig Mautner6170f732013-04-02 13:05:23 -07001275 int err = ActivityManager.START_SUCCESS;
1276
1277 ProcessRecord callerApp = null;
1278 if (caller != null) {
1279 callerApp = mService.getRecordForAppLocked(caller);
1280 if (callerApp != null) {
1281 callingPid = callerApp.pid;
1282 callingUid = callerApp.info.uid;
1283 } else {
1284 Slog.w(TAG, "Unable to find app for caller " + caller
1285 + " (pid=" + callingPid + ") when starting: "
1286 + intent.toString());
1287 err = ActivityManager.START_PERMISSION_DENIED;
1288 }
1289 }
1290
1291 if (err == ActivityManager.START_SUCCESS) {
1292 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1293 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner02a4aa22014-09-03 10:01:24 -07001294 + "} from uid " + callingUid
Craig Mautner9ef471f2014-02-07 13:11:47 -08001295 + " on display " + (container == null ? (mFocusedStack == null ?
1296 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1297 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1298 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001299 }
1300
1301 ActivityRecord sourceRecord = null;
1302 ActivityRecord resultRecord = null;
1303 if (resultTo != null) {
1304 sourceRecord = isInAnyStackLocked(resultTo);
1305 if (DEBUG_RESULTS) Slog.v(
1306 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1307 if (sourceRecord != null) {
1308 if (requestCode >= 0 && !sourceRecord.finishing) {
1309 resultRecord = sourceRecord;
1310 }
1311 }
1312 }
1313 ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1314
Dianne Hackborn91097de2014-04-04 18:02:06 -07001315 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001316
Dianne Hackborn95465202014-09-15 16:21:55 -07001317 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001318 // Transfer the result target from the source activity to the new
1319 // one being started, including any failures.
1320 if (requestCode >= 0) {
1321 ActivityOptions.abort(options);
1322 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1323 }
1324 resultRecord = sourceRecord.resultTo;
1325 resultWho = sourceRecord.resultWho;
1326 requestCode = sourceRecord.requestCode;
1327 sourceRecord.resultTo = null;
1328 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001329 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001330 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001331 if (sourceRecord.launchedFromUid == callingUid) {
1332 // The new activity is being launched from the same uid as the previous
1333 // activity in the flow, and asking to forward its result back to the
1334 // previous. In this case the activity is serving as a trampoline between
1335 // the two, so we also want to update its launchedFromPackage to be the
1336 // same as the previous activity. Note that this is safe, since we know
1337 // these two packages come from the same uid; the caller could just as
1338 // well have supplied that same package name itself. This specifially
1339 // deals with the case of an intent picker/chooser being launched in the app
1340 // flow to redirect to an activity picked by the user, where we want the final
1341 // activity to consider it to have been launched by the previous app activity.
1342 callingPackage = sourceRecord.launchedFromPackage;
1343 }
Craig Mautner6170f732013-04-02 13:05:23 -07001344 }
1345
1346 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1347 // We couldn't find a class that can handle the given Intent.
1348 // That's the end of that!
1349 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1350 }
1351
1352 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1353 // We couldn't find the specific class specified in the Intent.
1354 // Also the end of the line.
1355 err = ActivityManager.START_CLASS_NOT_FOUND;
1356 }
1357
Dianne Hackborn91097de2014-04-04 18:02:06 -07001358 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1359 && sourceRecord.task.voiceSession != null) {
1360 // If this activity is being launched as part of a voice session, we need
1361 // to ensure that it is safe to do so. If the upcoming activity will also
1362 // be part of the voice session, we can only launch it if it has explicitly
1363 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001364 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001365 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1366 try {
Dianne Hackborn95465202014-09-15 16:21:55 -07001367 if (!AppGlobals.getPackageManager().activitySupportsIntent(
1368 intent.getComponent(), intent, resolvedType)) {
Dianne Hackborn91097de2014-04-04 18:02:06 -07001369 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1370 }
1371 } catch (RemoteException e) {
1372 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1373 }
1374 }
1375 }
1376
1377 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1378 // If the caller is starting a new voice session, just make sure the target
1379 // is actually allowing it to run this way.
1380 try {
1381 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1382 intent, resolvedType)) {
1383 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1384 }
1385 } catch (RemoteException e) {
1386 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1387 }
1388 }
1389
Craig Mautner6170f732013-04-02 13:05:23 -07001390 if (err != ActivityManager.START_SUCCESS) {
1391 if (resultRecord != null) {
1392 resultStack.sendActivityResultLocked(-1,
1393 resultRecord, resultWho, requestCode,
1394 Activity.RESULT_CANCELED, null);
1395 }
Craig Mautner6170f732013-04-02 13:05:23 -07001396 ActivityOptions.abort(options);
1397 return err;
1398 }
1399
1400 final int startAnyPerm = mService.checkPermission(
1401 START_ANY_ACTIVITY, callingPid, callingUid);
1402 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1403 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1404 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1405 if (resultRecord != null) {
1406 resultStack.sendActivityResultLocked(-1,
1407 resultRecord, resultWho, requestCode,
1408 Activity.RESULT_CANCELED, null);
1409 }
Craig Mautner6170f732013-04-02 13:05:23 -07001410 String msg;
1411 if (!aInfo.exported) {
1412 msg = "Permission Denial: starting " + intent.toString()
1413 + " from " + callerApp + " (pid=" + callingPid
1414 + ", uid=" + callingUid + ")"
1415 + " not exported from uid " + aInfo.applicationInfo.uid;
1416 } else {
1417 msg = "Permission Denial: starting " + intent.toString()
1418 + " from " + callerApp + " (pid=" + callingPid
1419 + ", uid=" + callingUid + ")"
1420 + " requires " + aInfo.permission;
1421 }
1422 Slog.w(TAG, msg);
1423 throw new SecurityException(msg);
1424 }
1425
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001426 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001427 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001428
Craig Mautner6170f732013-04-02 13:05:23 -07001429 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001430 try {
1431 // The Intent we give to the watcher has the extra data
1432 // stripped off, since it can contain private information.
1433 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001434 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001435 aInfo.applicationInfo.packageName);
1436 } catch (RemoteException e) {
1437 mService.mController = null;
1438 }
Ben Gruver5e207332013-04-03 17:41:37 -07001439 }
Craig Mautner6170f732013-04-02 13:05:23 -07001440
Ben Gruver5e207332013-04-03 17:41:37 -07001441 if (abort) {
1442 if (resultRecord != null) {
1443 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001444 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001445 }
Ben Gruver5e207332013-04-03 17:41:37 -07001446 // We pretend to the caller that it was really started, but
1447 // they will just get a cancel result.
Ben Gruver5e207332013-04-03 17:41:37 -07001448 ActivityOptions.abort(options);
1449 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001450 }
1451
1452 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001453 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001454 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001455 if (outActivity != null) {
1456 outActivity[0] = r;
1457 }
1458
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001459 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001460 if (voiceSession == null && (stack.mResumedActivity == null
1461 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Dianne Hackborn95465202014-09-15 16:21:55 -07001462 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
1463 realCallingPid, realCallingUid, "Activity start")) {
Craig Mautner6170f732013-04-02 13:05:23 -07001464 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001465 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001466 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001467 ActivityOptions.abort(options);
1468 return ActivityManager.START_SWITCHES_CANCELED;
1469 }
1470 }
1471
1472 if (mService.mDidAppSwitch) {
1473 // This is the second allowed switch since we stopped switches,
1474 // so now just generally allow switches. Use case: user presses
1475 // home (switches disabled, switch to home, mDidAppSwitch now true);
1476 // user taps a home icon (coming from home so allowed, we hit here
1477 // and now allow anyone to switch again).
1478 mService.mAppSwitchesAllowedTime = 0;
1479 } else {
1480 mService.mDidAppSwitch = true;
1481 }
1482
Craig Mautneree36c772014-07-16 14:56:05 -07001483 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001484
Dianne Hackborn91097de2014-04-04 18:02:06 -07001485 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001486 startFlags, true, options, inTask);
Craig Mautner10385a12013-09-22 21:08:32 -07001487
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001488 if (err < 0) {
Craig Mautner10385a12013-09-22 21:08:32 -07001489 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001490 // activity start, but we are not actually doing an activity
1491 // switch... just dismiss the keyguard now, because we
1492 // probably want to see whatever is behind it.
Jorim Jaggi8de4311c2014-08-11 22:36:20 +02001493 notifyActivityDrawnForKeyguard();
Craig Mautner6170f732013-04-02 13:05:23 -07001494 }
1495 return err;
1496 }
1497
Craig Mautner1b4bf852014-05-26 15:06:32 -07001498 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001499 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001500
1501 // On leanback only devices we should keep all activities in the same stack.
1502 if (!mLeanbackOnlyDevice &&
1503 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001504 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001505 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001506 if (taskStack.isOnHomeDisplay()) {
1507 if (mFocusedStack != taskStack) {
1508 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1509 "focused stack to r=" + r + " task=" + task);
1510 mFocusedStack = taskStack;
1511 } else {
1512 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1513 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1514 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001515 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001516 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001517 }
1518
Craig Mautnere0a38842013-12-16 16:14:02 -08001519 final ActivityContainer container = r.mInitialActivityContainer;
1520 if (container != null) {
1521 // The first time put it on the desired stack, after this put on task stack.
1522 r.mInitialActivityContainer = null;
1523 return container.mStack;
1524 }
1525
Craig Mautner1b4bf852014-05-26 15:06:32 -07001526 if (mFocusedStack != mHomeStack && (!newTask ||
1527 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001528 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1529 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1530 return mFocusedStack;
1531 }
1532
Craig Mautnere0a38842013-12-16 16:14:02 -08001533 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1534 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1535 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1536 if (!stack.isHomeStack()) {
1537 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1538 "adjustStackFocus: Setting focused stack=" + stack);
1539 mFocusedStack = stack;
1540 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001541 }
1542 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001543
Craig Mautner4a1cb222013-12-04 16:14:06 -08001544 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001545 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001546 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1547 " stackId=" + stackId);
1548 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001549 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001550 }
1551 return mHomeStack;
1552 }
1553
Craig Mautner29219d92013-04-16 20:19:12 -07001554 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001555 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001556 final TaskRecord task = r.task;
1557 boolean isHomeActivity = !r.isApplicationActivity();
1558 if (!isHomeActivity && task != null) {
1559 isHomeActivity = !task.isApplicationTask();
1560 }
1561 if (!isHomeActivity && task != null) {
1562 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1563 isHomeActivity = parent != null && parent.isHomeActivity();
1564 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001565 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001566 }
1567 }
1568
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001569 final int startActivityUncheckedLocked(ActivityRecord r, ActivityRecord sourceRecord,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001570 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001571 boolean doResume, Bundle options, TaskRecord inTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001572 final Intent intent = r.intent;
1573 final int callingUid = r.launchedFromUid;
1574
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001575 // In some flows in to this function, we retrieve the task record and hold on to it
1576 // without a lock before calling back in to here... so the task at this point may
1577 // not actually be in recents. Check for that, and if it isn't in recents just
1578 // consider it invalid.
1579 if (inTask != null && !inTask.inRecents) {
1580 Slog.w(TAG, "Starting activity in task not in recents: " + inTask);
1581 inTask = null;
1582 }
1583
Craig Mautnerad400af2014-08-13 16:41:36 -07001584 final boolean launchSingleTop = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP;
Craig Mautnera228ae92014-07-09 05:44:55 -07001585 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1586 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001587
Craig Mautnera228ae92014-07-09 05:44:55 -07001588 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001589 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001590 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001591 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1592 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1593 "\"singleInstance\" or \"singleTask\"");
1594 launchFlags &=
1595 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1596 } else {
1597 switch (r.info.documentLaunchMode) {
1598 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1599 break;
1600 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1601 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1602 break;
1603 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1604 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1605 break;
1606 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1607 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1608 break;
1609 }
1610 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001611
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001612 final boolean launchTaskBehind = r.mLaunchTaskBehind
1613 && !launchSingleTask && !launchSingleInstance
1614 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001615
1616 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1617 // For whatever reason this activity is being launched into a new
1618 // task... yet the caller has requested a result back. Well, that
1619 // is pretty messed up, so instead immediately send back a cancel
1620 // and let the new task continue launched as normal without a
1621 // dependency on its originator.
1622 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1623 r.resultTo.task.stack.sendActivityResultLocked(-1,
1624 r.resultTo, r.resultWho, r.requestCode,
1625 Activity.RESULT_CANCELED, null);
1626 r.resultTo = null;
1627 }
1628
1629 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1630 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1631 }
1632
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001633 // If we are actually going to launch in to a new task, there are some cases where
1634 // we further want to do multiple task.
1635 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1636 if (launchTaskBehind
1637 || r.info.documentLaunchMode == ActivityInfo.DOCUMENT_LAUNCH_ALWAYS) {
1638 launchFlags |= Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1639 }
1640 }
1641
Craig Mautner8849a5e2013-04-02 16:41:03 -07001642 // We'll invoke onUserLeaving before onPause only if the launching
1643 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001644 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001645 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001646
1647 // If the caller has asked not to resume at this point, we make note
1648 // of this in the record so that we can skip it when trying to find
1649 // the top running activity.
1650 if (!doResume) {
1651 r.delayedResume = true;
1652 }
1653
Craig Mautnera254cd72014-05-25 16:47:39 -07001654 ActivityRecord notTop =
1655 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001656
1657 // If the onlyIfNeeded flag is set, then we can do this if the activity
1658 // being launched is the same as the one making the call... or, as
1659 // a special case, if we do not know the caller then we count the
1660 // current top activity as the caller.
1661 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1662 ActivityRecord checkedCaller = sourceRecord;
1663 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001664 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001665 }
1666 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1667 // Caller is not the same as launcher, so always needed.
1668 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1669 }
1670 }
1671
Craig Mautner8849a5e2013-04-02 16:41:03 -07001672 boolean addingToTask = false;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001673 TaskRecord reuseTask = null;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001674
1675 // If the caller is not coming from another activity, but has given us an
1676 // explicit task into which they would like us to launch the new activity,
1677 // then let's see about doing that.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001678 if (sourceRecord == null && inTask != null && inTask.stack != null) {
1679 final Intent baseIntent = inTask.getBaseIntent();
1680 final ActivityRecord root = inTask.getRootActivity();
1681 if (baseIntent == null) {
1682 ActivityOptions.abort(options);
1683 throw new IllegalArgumentException("Launching into task without base intent: "
1684 + inTask);
1685 }
1686
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001687 // If this task is empty, then we are adding the first activity -- it
1688 // determines the root, and must be launching as a NEW_TASK.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001689 if (launchSingleInstance || launchSingleTask) {
1690 if (!baseIntent.getComponent().equals(r.intent.getComponent())) {
1691 ActivityOptions.abort(options);
1692 throw new IllegalArgumentException("Trying to launch singleInstance/Task "
1693 + r + " into different task " + inTask);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001694 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001695 if (root != null) {
1696 ActivityOptions.abort(options);
1697 throw new IllegalArgumentException("Caller with inTask " + inTask
1698 + " has root " + root + " but target is singleInstance/Task");
1699 }
1700 }
1701
1702 // If task is empty, then adopt the interesting intent launch flags in to the
1703 // activity being started.
1704 if (root == null) {
1705 final int flagsOfInterest = Intent.FLAG_ACTIVITY_NEW_TASK
1706 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
1707 | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
1708 launchFlags = (launchFlags&~flagsOfInterest)
1709 | (baseIntent.getFlags()&flagsOfInterest);
1710 intent.setFlags(launchFlags);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001711 inTask.setIntent(r);
Dianne Hackborn962d5352014-08-29 16:27:40 -07001712 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001713
Dianne Hackborn962d5352014-08-29 16:27:40 -07001714 // If the task is not empty and the caller is asking to start it as the root
1715 // of a new task, then we don't actually want to start this on the task. We
1716 // will bring the task to the front, and possibly give it a new intent.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001717 } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Dianne Hackborn962d5352014-08-29 16:27:40 -07001718 addingToTask = false;
1719
1720 } else {
1721 addingToTask = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001722 }
Dianne Hackborn962d5352014-08-29 16:27:40 -07001723
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001724 reuseTask = inTask;
1725 } else {
1726 inTask = null;
1727 }
1728
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001729 if (inTask == null) {
1730 if (sourceRecord == null) {
1731 // This activity is not being started from another... in this
1732 // case we -always- start a new task.
1733 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && inTask == null) {
1734 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1735 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1736 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1737 }
1738 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1739 // The original activity who is starting us is running as a single
1740 // instance... this new activity it is starting must go on its
1741 // own task.
1742 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1743 } else if (launchSingleInstance || launchSingleTask) {
1744 // The activity being started is a single instance... it always
1745 // gets launched into its own task.
1746 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1747 }
1748 }
1749
1750 ActivityInfo newTaskInfo = null;
1751 Intent newTaskIntent = null;
1752 ActivityStack sourceStack;
1753 if (sourceRecord != null) {
1754 if (sourceRecord.finishing) {
1755 // If the source is finishing, we can't further count it as our source. This
1756 // is because the task it is associated with may now be empty and on its way out,
1757 // so we don't want to blindly throw it in to that task. Instead we will take
1758 // the NEW_TASK flow and try to find a task for it. But save the task information
1759 // so it can be used when creating the new task.
1760 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1761 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1762 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1763 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1764 newTaskInfo = sourceRecord.info;
1765 newTaskIntent = sourceRecord.task.intent;
1766 }
1767 sourceRecord = null;
1768 sourceStack = null;
1769 } else {
1770 sourceStack = sourceRecord.task.stack;
1771 }
1772 } else {
1773 sourceStack = null;
1774 }
1775
1776 boolean movedHome = false;
1777 ActivityStack targetStack;
1778
1779 intent.setFlags(launchFlags);
1780
Dianne Hackborn89ad4562014-08-24 16:45:38 -07001781 // We may want to try to place the new activity in to an existing task. We always
1782 // do this if the target activity is singleTask or singleInstance; we will also do
1783 // this if NEW_TASK has been requested, and there is not an additional qualifier telling
1784 // us to still place it in a new task: multi task, always doc mode, or being asked to
1785 // launch this as a new task behind the current one.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001786 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1787 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001788 || launchSingleInstance || launchSingleTask) {
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001789 // If bring to front is requested, and no result is requested and we have not
1790 // been given an explicit task to launch in to, and
Craig Mautner8849a5e2013-04-02 16:41:03 -07001791 // we can find a task that was started with this same
1792 // component, then instead of launching bring that one to the front.
Dianne Hackbornd7c92892014-08-27 16:44:24 -07001793 if (inTask == null && r.resultTo == null) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001794 // See if there is a task to bring to the front. If this is
1795 // a SINGLE_INSTANCE activity, there can be one and only one
1796 // instance of it in the history, and it is always in its own
1797 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001798 ActivityRecord intentActivity = !launchSingleInstance ?
1799 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001800 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001801 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001802 showLockTaskToast();
Craig Mautner0175b882014-09-07 18:05:31 -07001803 Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
Craig Mautneraea74a52014-03-08 14:23:10 -08001804 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1805 }
Craig Mautner29219d92013-04-16 20:19:12 -07001806 if (r.task == null) {
1807 r.task = intentActivity.task;
1808 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001809 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001810 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001811 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1812 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001813 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001814 if (intentActivity.task.intent == null) {
1815 // This task was started because of movement of
1816 // the activity based on affinity... now that we
1817 // are actually launching it, we can assign the
1818 // base intent.
Winson Chungfee26772014-08-05 12:21:52 -07001819 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001820 }
1821 // If the target task is not in the front, then we need
1822 // to bring it to the front... except... well, with
1823 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1824 // to have the same behavior as if a new instance was
1825 // being started, which means not bringing it to the front
1826 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001827 final ActivityStack lastStack = getLastStack();
1828 ActivityRecord curTop = lastStack == null?
1829 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner7504d7b2013-09-17 10:50:53 -07001830 if (curTop != null && (curTop.task != intentActivity.task ||
1831 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001832 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001833 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1834 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001835 // We really do want to push this one into the
1836 // user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001837 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001838 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1839 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001840 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001841 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001842 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001843 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1844 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001845 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001846 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001847 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001848 options = null;
1849 }
1850 }
1851 // If the caller has requested that the target task be
1852 // reset, then do so.
1853 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1854 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1855 }
1856 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1857 // We don't need to start a new activity, and
1858 // the client said not to do anything if that
1859 // is the case, so this is it! And for paranoia, make
1860 // sure we have correctly resumed the top activity.
1861 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001862 resumeTopActivitiesLocked(targetStack, null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001863 } else {
1864 ActivityOptions.abort(options);
1865 }
1866 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1867 }
1868 if ((launchFlags &
1869 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1870 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1871 // The caller has requested to completely replace any
1872 // existing task with its new activity. Well that should
1873 // not be too hard...
1874 reuseTask = intentActivity.task;
1875 reuseTask.performClearTaskLocked();
Winson Chungfee26772014-08-05 12:21:52 -07001876 reuseTask.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001877 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001878 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001879 // In this situation we want to remove all activities
1880 // from the task up to the one being started. In most
1881 // cases this means we are resetting the task to its
1882 // initial state.
1883 ActivityRecord top =
1884 intentActivity.task.performClearTaskLocked(r, launchFlags);
1885 if (top != null) {
1886 if (top.frontOfTask) {
1887 // Activity aliases may mean we use different
1888 // intents for the top activity, so make sure
1889 // the task now has the identity of the new
1890 // intent.
Winson Chungfee26772014-08-05 12:21:52 -07001891 top.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001892 }
1893 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1894 r, top.task);
1895 top.deliverNewIntentLocked(callingUid, r.intent);
1896 } else {
1897 // A special case: we need to
1898 // start the activity because it is not currently
1899 // running, and the caller has asked to clear the
1900 // current task to have this activity at the top.
1901 addingToTask = true;
1902 // Now pretend like this activity is being started
1903 // by the top of its task, so it is put in the
1904 // right place.
1905 sourceRecord = intentActivity;
1906 }
1907 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1908 // In this case the top activity on the task is the
1909 // same as the one being launched, so we take that
1910 // as a request to bring the task to the foreground.
1911 // If the top activity in the task is the root
1912 // activity, deliver this new intent to it if it
1913 // desires.
Craig Mautnerad400af2014-08-13 16:41:36 -07001914 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0 || launchSingleTop)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001915 && intentActivity.realActivity.equals(r.realActivity)) {
1916 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1917 intentActivity.task);
1918 if (intentActivity.frontOfTask) {
Winson Chungfee26772014-08-05 12:21:52 -07001919 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001920 }
1921 intentActivity.deliverNewIntentLocked(callingUid, r.intent);
1922 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1923 // In this case we are launching the root activity
1924 // of the task, but with a different intent. We
1925 // should start a new instance on top.
1926 addingToTask = true;
1927 sourceRecord = intentActivity;
1928 }
1929 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1930 // In this case an activity is being launched in to an
1931 // existing task, without resetting that task. This
1932 // is typically the situation of launching an activity
1933 // from a notification or shortcut. We want to place
1934 // the new activity on top of the current task.
1935 addingToTask = true;
1936 sourceRecord = intentActivity;
1937 } else if (!intentActivity.task.rootWasReset) {
1938 // In this case we are launching in to an existing task
1939 // that has not yet been started from its front door.
1940 // The current task has been brought to the front.
1941 // Ideally, we'd probably like to place this new task
1942 // at the bottom of its stack, but that's a little hard
1943 // to do with the current organization of the code so
1944 // for now we'll just drop it.
Winson Chungfee26772014-08-05 12:21:52 -07001945 intentActivity.task.setIntent(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001946 }
1947 if (!addingToTask && reuseTask == null) {
1948 // We didn't do anything... but it was needed (a.k.a., client
1949 // don't use that intent!) And for paranoia, make
1950 // sure we have correctly resumed the top activity.
1951 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001952 targetStack.resumeTopActivityLocked(null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001953 } else {
1954 ActivityOptions.abort(options);
1955 }
1956 return ActivityManager.START_TASK_TO_FRONT;
1957 }
1958 }
1959 }
1960 }
1961
1962 //String uri = r.intent.toURI();
1963 //Intent intent2 = new Intent(uri);
1964 //Slog.i(TAG, "Given intent: " + r.intent);
1965 //Slog.i(TAG, "URI is: " + uri);
1966 //Slog.i(TAG, "To intent: " + intent2);
1967
1968 if (r.packageName != null) {
1969 // If the activity being launched is the same as the one currently
1970 // at the top, then we need to check if it should only be launched
1971 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001972 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001973 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001974 if (top != null && r.resultTo == null) {
1975 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
1976 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07001977 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnerad400af2014-08-13 16:41:36 -07001978 || launchSingleTop || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001979 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
1980 top.task);
1981 // For paranoia, make sure we have correctly
1982 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001983 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001984 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001985 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001986 }
1987 ActivityOptions.abort(options);
1988 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1989 // We don't need to start a new activity, and
1990 // the client said not to do anything if that
1991 // is the case, so this is it!
1992 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1993 }
1994 top.deliverNewIntentLocked(callingUid, r.intent);
1995 return ActivityManager.START_DELIVERED_TO_TOP;
1996 }
1997 }
1998 }
1999 }
2000
2001 } else {
2002 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002003 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
2004 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002005 }
2006 ActivityOptions.abort(options);
2007 return ActivityManager.START_CLASS_NOT_FOUND;
2008 }
2009
2010 boolean newTask = false;
2011 boolean keepCurTransition = false;
2012
Craig Mautnerbb742462014-07-07 15:28:55 -07002013 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07002014 sourceRecord.task : null;
2015
Craig Mautner8849a5e2013-04-02 16:41:03 -07002016 // Should this be considered a new task?
Dianne Hackborn962d5352014-08-29 16:27:40 -07002017 if (r.resultTo == null && inTask == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07002018 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08002019 if (isLockTaskModeViolation(reuseTask)) {
2020 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2021 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2022 }
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002023 newTask = true;
2024 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerbb742462014-07-07 15:28:55 -07002025 if (!launchTaskBehind) {
2026 targetStack.moveToFront();
2027 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002028 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08002029 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
2030 newTaskInfo != null ? newTaskInfo : r.info,
2031 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07002032 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
2033 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002034 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
2035 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002036 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07002037 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002038 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002039 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002040 if ((launchFlags &
2041 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
2042 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
2043 // Caller wants to appear on home activity, so before starting
2044 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07002045 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002046 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002047 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002048 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07002049 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08002050 if (isLockTaskModeViolation(sourceTask)) {
2051 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2052 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2053 }
Craig Mautner525f3d92013-05-07 14:01:50 -07002054 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07002055 targetStack.moveToFront();
Craig Mautnerabcc59c2014-05-17 15:13:28 -07002056 mWindowManager.moveTaskToTop(targetStack.topTask().taskId);
Craig Mautnera228ae92014-07-09 05:44:55 -07002057 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07002058 // In this case, we are adding the activity to an existing
2059 // task, but the caller has asked to clear that task if the
2060 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07002061 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002062 keepCurTransition = true;
2063 if (top != null) {
2064 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
2065 top.deliverNewIntentLocked(callingUid, r.intent);
2066 // For paranoia, make sure we have correctly
2067 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07002068 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002069 if (doResume) {
2070 targetStack.resumeTopActivityLocked(null);
2071 }
2072 ActivityOptions.abort(options);
2073 return ActivityManager.START_DELIVERED_TO_TOP;
2074 }
2075 } else if (!addingToTask &&
2076 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
2077 // In this case, we are launching an activity in our own task
2078 // that may already be running somewhere in the history, and
2079 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07002080 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002081 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002082 final TaskRecord task = top.task;
2083 task.moveActivityToFrontLocked(top);
2084 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002085 top.updateOptionsLocked(options);
2086 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner0f922742013-08-06 08:44:42 -07002087 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002088 if (doResume) {
2089 targetStack.resumeTopActivityLocked(null);
2090 }
2091 return ActivityManager.START_DELIVERED_TO_TOP;
2092 }
2093 }
2094 // An existing activity is starting this new activity, so we want
2095 // to keep the new one in the same task as the one that is starting
2096 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07002097 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002098 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002099 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002100
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002101 } else if (inTask != null) {
2102 // The calling is asking that the new activity be started in an explicit
2103 // task it has provided to us.
2104 if (isLockTaskModeViolation(inTask)) {
2105 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
2106 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
2107 }
2108 targetStack = inTask.stack;
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002109 targetStack.moveTaskToFrontLocked(inTask, r, options);
Craig Mautner02a4aa22014-09-03 10:01:24 -07002110 targetStack.moveToFront();
Dianne Hackborn2c924fb2014-08-29 13:02:03 -07002111 mWindowManager.moveTaskToTop(inTask.taskId);
Dianne Hackbornd7c92892014-08-27 16:44:24 -07002112
2113 // Check whether we should actually launch the new activity in to the task,
2114 // or just reuse the current activity on top.
2115 ActivityRecord top = inTask.getTopActivity();
2116 if (top != null && top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
2117 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
2118 || launchSingleTop || launchSingleTask) {
2119 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
2120 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
2121 // We don't need to start a new activity, and
2122 // the client said not to do anything if that
2123 // is the case, so this is it!
2124 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
2125 }
2126 top.deliverNewIntentLocked(callingUid, r.intent);
2127 return ActivityManager.START_DELIVERED_TO_TOP;
2128 }
2129 }
2130
Dianne Hackborn962d5352014-08-29 16:27:40 -07002131 if (!addingToTask) {
2132 // We don't actually want to have this activity added to the task, so just
2133 // stop here but still tell the caller that we consumed the intent.
2134 ActivityOptions.abort(options);
2135 return ActivityManager.START_TASK_TO_FRONT;
2136 }
2137
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002138 r.setTask(inTask, null);
2139 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2140 + " in explicit task " + r.task);
2141
Craig Mautner8849a5e2013-04-02 16:41:03 -07002142 } else {
2143 // This not being started from an existing activity, and not part
2144 // of a new task... just put it in the top task, though these days
2145 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07002146 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerdd221b32014-03-19 19:53:45 -07002147 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07002148 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002149 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002150 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002151 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002152 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2153 + " in new guessed " + r.task);
2154 }
2155
2156 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002157 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002158
Craig Mautner76e2a762014-06-24 09:05:43 -07002159 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2160 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2161 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002162 if (newTask) {
2163 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2164 }
2165 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002166 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002167 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002168 if (!launchTaskBehind) {
2169 // Don't set focus on an activity that's going to the back.
2170 mService.setFocusedActivityLocked(r);
2171 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002172 return ActivityManager.START_SUCCESS;
2173 }
2174
Craig Mautneree36c772014-07-16 14:56:05 -07002175 final void doPendingActivityLaunchesLocked(boolean doResume) {
2176 while (!mPendingActivityLaunches.isEmpty()) {
2177 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002178 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002179 doResume && mPendingActivityLaunches.isEmpty(), null, null);
Craig Mautneree36c772014-07-16 14:56:05 -07002180 }
2181 }
2182
Craig Mautner7f13ed32014-07-28 14:00:35 -07002183 void removePendingActivityLaunchesLocked(ActivityStack stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002184 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2185 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
Craig Mautner7f13ed32014-07-28 14:00:35 -07002186 if (pal.stack == stack) {
Craig Mautneree36c772014-07-16 14:56:05 -07002187 mPendingActivityLaunches.remove(palNdx);
2188 }
2189 }
2190 }
2191
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002192 void acquireLaunchWakelock() {
2193 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2194 throw new IllegalStateException("Calling must be system uid");
2195 }
2196 mLaunchingActivity.acquire();
2197 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2198 // To be safe, don't allow the wake lock to be held for too long.
2199 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2200 }
2201 }
2202
Craig Mautnerf3333272013-04-22 10:55:53 -07002203 // Checked.
2204 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2205 Configuration config) {
2206 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2207
Craig Mautnerf3333272013-04-22 10:55:53 -07002208 ArrayList<ActivityRecord> stops = null;
2209 ArrayList<ActivityRecord> finishes = null;
2210 ArrayList<UserStartedState> startingUsers = null;
2211 int NS = 0;
2212 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002213 boolean booting = false;
2214 boolean enableScreen = false;
2215 boolean activityRemoved = false;
2216
2217 ActivityRecord r = ActivityRecord.forToken(token);
2218 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002219 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2220 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002221 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2222 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002223 if (fromTimeout) {
2224 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002225 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002226
2227 // This is a hack to semi-deal with a race condition
2228 // in the client where it can be constructed with a
2229 // newer configuration from when we asked it to launch.
2230 // We'll update with whatever configuration it now says
2231 // it used to launch.
2232 if (config != null) {
2233 r.configuration = config;
2234 }
2235
2236 // We are now idle. If someone is waiting for a thumbnail from
2237 // us, we can now deliver.
2238 r.idle = true;
2239
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002240 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
2241 if (!mService.mBooted && isFrontStack(r.task.stack)) {
2242 mService.mBooted = true;
2243 enableScreen = true;
2244 }
2245 }
2246
2247 if (allResumedActivitiesIdle()) {
2248 if (r != null) {
2249 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002250 }
2251
2252 if (mLaunchingActivity.isHeld()) {
2253 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2254 if (VALIDATE_WAKE_LOCK_CALLER &&
2255 Binder.getCallingUid() != Process.myUid()) {
2256 throw new IllegalStateException("Calling must be system uid");
2257 }
2258 mLaunchingActivity.release();
2259 }
2260 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002261 }
2262
2263 // Atomically retrieve all of the other things to do.
2264 stops = processStoppingActivitiesLocked(true);
2265 NS = stops != null ? stops.size() : 0;
2266 if ((NF=mFinishingActivities.size()) > 0) {
2267 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2268 mFinishingActivities.clear();
2269 }
2270
Prashant Malani84b08ad2014-07-15 16:28:24 -07002271 booting = mService.mBooting;
2272 mService.mBooting = false;
Craig Mautnerf3333272013-04-22 10:55:53 -07002273
2274 if (mStartingUsers.size() > 0) {
2275 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2276 mStartingUsers.clear();
2277 }
2278
Craig Mautnerf3333272013-04-22 10:55:53 -07002279 // Stop any activities that are scheduled to do so but have been
2280 // waiting for the next one to start.
2281 for (int i = 0; i < NS; i++) {
2282 r = stops.get(i);
2283 final ActivityStack stack = r.task.stack;
2284 if (r.finishing) {
2285 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2286 } else {
2287 stack.stopActivityLocked(r);
2288 }
2289 }
2290
2291 // Finish any activities that are scheduled to do so but have been
2292 // waiting for the next one to start.
2293 for (int i = 0; i < NF; i++) {
2294 r = finishes.get(i);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002295 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07002296 }
2297
2298 if (booting) {
2299 mService.finishBooting();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002300 } else {
2301 // Complete user switch
2302 if (startingUsers != null) {
2303 for (int i = 0; i < startingUsers.size(); i++) {
2304 mService.finishUserSwitch(startingUsers.get(i));
2305 }
2306 }
2307 // Complete starting up of background users
2308 if (mStartingBackgroundUsers.size() > 0) {
2309 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2310 mStartingBackgroundUsers.clear();
2311 for (int i = 0; i < startingUsers.size(); i++) {
2312 mService.finishUserBoot(startingUsers.get(i));
2313 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002314 }
2315 }
2316
2317 mService.trimApplications();
2318 //dump();
2319 //mWindowManager.dump();
2320
2321 if (enableScreen) {
Jeff Brown4b4971b2014-07-25 19:30:05 -07002322 mService.postEnableScreenAfterBootLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002323 }
2324
2325 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002326 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002327 }
2328
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002329 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002330 }
2331
Craig Mautner8e569572013-10-11 17:36:59 -07002332 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002333 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002334 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2335 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002336 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2337 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2338 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002339 }
Craig Mautner19091252013-10-05 00:03:53 -07002340 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002341 }
2342
2343 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002344 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2345 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002346 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2347 stacks.get(stackNdx).closeSystemDialogsLocked();
2348 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002349 }
2350 }
2351
Craig Mautner93529a42013-10-04 15:03:13 -07002352 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002353 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002354 }
2355
Craig Mautner8d341ef2013-03-26 09:03:27 -07002356 /**
2357 * @return true if some activity was finished (or would have finished if doit were true).
2358 */
2359 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2360 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002361 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2362 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002363 final int numStacks = stacks.size();
2364 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2365 final ActivityStack stack = stacks.get(stackNdx);
2366 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2367 didSomething = true;
2368 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002369 }
2370 }
2371 return didSomething;
2372 }
2373
Dianne Hackborna413dc02013-07-12 12:02:55 -07002374 void updatePreviousProcessLocked(ActivityRecord r) {
2375 // Now that this process has stopped, we may want to consider
2376 // it to be the previous app to try to keep around in case
2377 // the user wants to return to it.
2378
2379 // First, found out what is currently the foreground app, so that
2380 // we don't blow away the previous app if this activity is being
2381 // hosted by the process that is actually still the foreground.
2382 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002383 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2384 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002385 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2386 final ActivityStack stack = stacks.get(stackNdx);
2387 if (isFrontStack(stack)) {
2388 if (stack.mResumedActivity != null) {
2389 fgApp = stack.mResumedActivity.app;
2390 } else if (stack.mPausingActivity != null) {
2391 fgApp = stack.mPausingActivity.app;
2392 }
2393 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002394 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002395 }
2396 }
2397
2398 // Now set this one as the previous process, only if that really
2399 // makes sense to.
2400 if (r.app != null && fgApp != null && r.app != fgApp
2401 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002402 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002403 mService.mPreviousProcess = r.app;
2404 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2405 }
2406 }
2407
Craig Mautner05d29032013-05-03 13:40:13 -07002408 boolean resumeTopActivitiesLocked() {
2409 return resumeTopActivitiesLocked(null, null, null);
2410 }
2411
2412 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2413 Bundle targetOptions) {
2414 if (targetStack == null) {
2415 targetStack = getFocusedStack();
2416 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002417 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002418 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002419 if (isFrontStack(targetStack)) {
2420 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2421 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002422 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2423 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002424 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2425 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002426 if (stack == targetStack) {
2427 // Already started above.
2428 continue;
2429 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002430 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002431 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002432 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002433 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002434 }
Craig Mautner05d29032013-05-03 13:40:13 -07002435 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002436 }
2437
2438 void finishTopRunningActivityLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002439 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2440 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002441 final int numStacks = stacks.size();
2442 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2443 final ActivityStack stack = stacks.get(stackNdx);
2444 stack.finishTopRunningActivityLocked(app);
2445 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002446 }
2447 }
2448
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002449 void finishVoiceTask(IVoiceInteractionSession session) {
2450 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2451 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2452 final int numStacks = stacks.size();
2453 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2454 final ActivityStack stack = stacks.get(stackNdx);
2455 stack.finishVoiceTask(session);
2456 }
2457 }
2458 }
2459
Craig Mautneraea74a52014-03-08 14:23:10 -08002460 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2461 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2462 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002463 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002464 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2465 // Caller wants the home activity moved with it. To accomplish this,
2466 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002467 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002468 }
2469 task.stack.moveTaskToFrontLocked(task, null, options);
2470 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2471 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002472 }
2473
Craig Mautner967212c2013-04-13 21:10:58 -07002474 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002475 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2476 if (activityContainer != null) {
2477 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002478 }
2479 return null;
2480 }
2481
Craig Mautner967212c2013-04-13 21:10:58 -07002482 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002483 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002484 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2485 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002486 }
2487 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002488 }
2489
Craig Mautner4a1cb222013-12-04 16:14:06 -08002490 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002491 ActivityRecord homeActivity = getHomeActivity();
2492 if (homeActivity != null) {
2493 return homeActivity.appToken;
2494 }
2495 return null;
2496 }
2497
2498 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002499 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2500 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2501 final TaskRecord task = tasks.get(taskNdx);
2502 if (task.isHomeTask()) {
2503 final ArrayList<ActivityRecord> activities = task.mActivities;
2504 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2505 final ActivityRecord r = activities.get(activityNdx);
2506 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002507 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002508 }
2509 }
2510 }
2511 }
2512 return null;
2513 }
2514
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002515 ActivityContainer createActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002516 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002517 ActivityContainer activityContainer =
2518 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002519 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002520 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002521 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002522 return activityContainer;
2523 }
2524
Craig Mautner34b73df2014-01-12 21:11:08 -08002525 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002526 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2527 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2528 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002529 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2530 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002531 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002532 }
2533 }
2534
Craig Mautner95da1082014-02-24 17:54:35 -08002535 void deleteActivityContainer(IActivityContainer container) {
2536 ActivityContainer activityContainer = (ActivityContainer)container;
2537 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002538 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2539 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002540 final int stackId = activityContainer.mStackId;
2541 mActivityContainers.remove(stackId);
2542 mWindowManager.removeStack(stackId);
2543 }
2544 }
2545
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002546 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002547 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2548 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002549 return -1;
2550 }
2551
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002552 ActivityContainer activityContainer = new ActivityContainer(stackId);
2553 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002554 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002555 return stackId;
2556 }
2557
2558 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002559 while (true) {
2560 if (++mLastStackId <= HOME_STACK_ID) {
2561 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002562 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002563 if (getStack(mLastStackId) == null) {
2564 break;
2565 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002566 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002567 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002568 }
2569
Craig Mautneref73ee12014-04-23 11:45:37 -07002570 void createStackForRestoredTaskHistory(ArrayList<TaskRecord> tasks) {
2571 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
2572 final ActivityStack stack = getStack(stackId);
2573 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2574 final TaskRecord task = tasks.get(taskNdx);
2575 stack.addTask(task, false, false);
2576 final int taskId = task.taskId;
2577 final ArrayList<ActivityRecord> activities = task.mActivities;
2578 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2579 final ActivityRecord r = activities.get(activityNdx);
2580 mWindowManager.addAppToken(0, r.appToken, taskId, stackId,
2581 r.info.screenOrientation, r.fullscreen,
2582 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002583 r.userId, r.info.configChanges, task.voiceSession != null,
2584 r.mLaunchTaskBehind);
Craig Mautneref73ee12014-04-23 11:45:37 -07002585 }
Craig Mautneref73ee12014-04-23 11:45:37 -07002586 }
Craig Mautner84984fa2014-06-19 11:19:20 -07002587 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
Craig Mautneref73ee12014-04-23 11:45:37 -07002588 }
2589
Craig Mautner8d341ef2013-03-26 09:03:27 -07002590 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002591 final TaskRecord task = anyTaskForIdLocked(taskId);
2592 if (task == null) {
2593 return;
2594 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002595 final ActivityStack stack = getStack(stackId);
2596 if (stack == null) {
2597 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2598 return;
2599 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002600 task.stack.removeTask(task);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002601 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002602 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002603 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002604 }
2605
Craig Mautnerac6f8432013-07-17 13:24:59 -07002606 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002607 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002608 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2609 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002610 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2611 final ActivityStack stack = stacks.get(stackNdx);
2612 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002613 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2614 continue;
2615 }
2616 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2617 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2618 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002619 continue;
2620 }
2621 final ActivityRecord ar = stack.findTaskLocked(r);
2622 if (ar != null) {
2623 return ar;
2624 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002625 }
2626 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002627 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002628 return null;
2629 }
2630
2631 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002632 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2633 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002634 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2635 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2636 if (ar != null) {
2637 return ar;
2638 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002639 }
2640 }
2641 return null;
2642 }
2643
Craig Mautner8d341ef2013-03-26 09:03:27 -07002644 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002645 scheduleSleepTimeout();
2646 if (!mGoingToSleep.isHeld()) {
2647 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002648 if (mLaunchingActivity.isHeld()) {
2649 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2650 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002651 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002652 mLaunchingActivity.release();
2653 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002654 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002655 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002656 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002657 }
2658
2659 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002660 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002661
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002662 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002663 final long endTime = System.currentTimeMillis() + timeout;
2664 while (true) {
2665 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002666 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2667 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002668 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2669 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2670 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002671 }
2672 if (cantShutdown) {
2673 long timeRemaining = endTime - System.currentTimeMillis();
2674 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002675 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002676 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002677 } catch (InterruptedException e) {
2678 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002679 } else {
2680 Slog.w(TAG, "Activity manager shutdown timed out");
2681 timedout = true;
2682 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002683 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002684 } else {
2685 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002686 }
2687 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002688
2689 // Force checkReadyForSleep to complete.
2690 mSleepTimeout = true;
2691 checkReadyForSleepLocked();
2692
Craig Mautner8d341ef2013-03-26 09:03:27 -07002693 return timedout;
2694 }
2695
2696 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002697 removeSleepTimeouts();
2698 if (mGoingToSleep.isHeld()) {
2699 mGoingToSleep.release();
2700 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002701 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2702 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002703 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2704 final ActivityStack stack = stacks.get(stackNdx);
2705 stack.awakeFromSleepingLocked();
2706 if (isFrontStack(stack)) {
2707 resumeTopActivitiesLocked();
2708 }
Craig Mautner5314a402013-09-26 12:40:16 -07002709 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002710 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002711 mGoingToSleepActivities.clear();
2712 }
2713
2714 void activitySleptLocked(ActivityRecord r) {
2715 mGoingToSleepActivities.remove(r);
2716 checkReadyForSleepLocked();
2717 }
2718
2719 void checkReadyForSleepLocked() {
2720 if (!mService.isSleepingOrShuttingDown()) {
2721 // Do not care.
2722 return;
2723 }
2724
2725 if (!mSleepTimeout) {
2726 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002727 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2728 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002729 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2730 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2731 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002732 }
2733
2734 if (mStoppingActivities.size() > 0) {
2735 // Still need to tell some activities to stop; can't sleep yet.
2736 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2737 + mStoppingActivities.size() + " activities");
2738 scheduleIdleLocked();
2739 dontSleep = true;
2740 }
2741
2742 if (mGoingToSleepActivities.size() > 0) {
2743 // Still need to tell some activities to sleep; can't sleep yet.
2744 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2745 + mGoingToSleepActivities.size() + " activities");
2746 dontSleep = true;
2747 }
2748
2749 if (dontSleep) {
2750 return;
2751 }
2752 }
2753
Craig Mautnere0a38842013-12-16 16:14:02 -08002754 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2755 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002756 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2757 stacks.get(stackNdx).goToSleep();
2758 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002759 }
2760
2761 removeSleepTimeouts();
2762
2763 if (mGoingToSleep.isHeld()) {
2764 mGoingToSleep.release();
2765 }
2766 if (mService.mShuttingDown) {
2767 mService.notifyAll();
2768 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002769 }
2770
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002771 boolean reportResumedActivityLocked(ActivityRecord r) {
2772 final ActivityStack stack = r.task.stack;
2773 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002774 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002775 }
2776 if (allResumedActivitiesComplete()) {
2777 ensureActivitiesVisibleLocked(null, 0);
2778 mWindowManager.executeAppTransition();
2779 return true;
2780 }
2781 return false;
2782 }
2783
Craig Mautner8d341ef2013-03-26 09:03:27 -07002784 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002785 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2786 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002787 final int numStacks = stacks.size();
2788 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2789 final ActivityStack stack = stacks.get(stackNdx);
2790 stack.handleAppCrashLocked(app);
2791 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002792 }
2793 }
2794
Jose Lima4b6c6692014-08-12 17:41:12 -07002795 boolean requestVisibleBehindLocked(ActivityRecord r, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002796 final ActivityStack stack = r.task.stack;
2797 if (stack == null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002798 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: r=" + r + " visible=" +
2799 visible + " stack is null");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002800 return false;
2801 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002802 final boolean isVisible = stack.hasVisibleBehindActivity();
2803 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind r=" + r + " visible=" +
2804 visible + " isVisible=" + isVisible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002805
2806 final ActivityRecord top = topRunningActivityLocked();
Jose Lima4b6c6692014-08-12 17:41:12 -07002807 if (top == null || top == r || (visible == isVisible)) {
2808 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: quick return");
2809 stack.setVisibleBehindActivity(visible ? r : null);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002810 return true;
2811 }
2812
2813 // A non-top activity is reporting a visibility change.
Craig Mautneraea5ced2014-09-07 17:08:39 -07002814 if (visible && top.fullscreen) {
2815 // Let the caller know that it can't be seen.
Jose Lima4b6c6692014-08-12 17:41:12 -07002816 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning top.fullscreen="
Jose Lima34ff4922014-08-18 15:19:41 -07002817 + top.fullscreen + " top.state=" + top.state + " top.app=" + top.app +
Craig Mautneree2e45a2014-06-27 12:10:03 -07002818 " top.app.thread=" + top.app.thread);
2819 return false;
Jose Lima34ff4922014-08-18 15:19:41 -07002820 } else if (!visible && stack.getVisibleBehindActivity() != r) {
2821 // Only the activity set as currently visible behind should actively reset its
2822 // visible behind state.
2823 if (DEBUG_VISIBLE_BEHIND) Slog.d(TAG, "requestVisibleBehind: returning visible="
2824 + visible + " stack.getVisibleBehindActivity()=" +
2825 stack.getVisibleBehindActivity() + " r=" + r);
2826 return false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002827 }
2828
Jose Lima4b6c6692014-08-12 17:41:12 -07002829 stack.setVisibleBehindActivity(visible ? r : null);
2830 if (!visible) {
Craig Mautnerfa387ad2014-08-05 11:16:41 -07002831 // Make the activity immediately above r opaque.
2832 final ActivityRecord next = stack.findNextTranslucentActivity(r);
2833 if (next != null) {
2834 mService.convertFromTranslucent(next.appToken);
2835 }
2836 }
Craig Mautneraea5ced2014-09-07 17:08:39 -07002837 if (top.app != null && top.app.thread != null) {
2838 // Notify the top app of the change.
2839 try {
2840 top.app.thread.scheduleBackgroundVisibleBehindChanged(top.appToken, visible);
2841 } catch (RemoteException e) {
2842 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07002843 }
2844 return true;
2845 }
2846
Craig Mautnerbb742462014-07-07 15:28:55 -07002847 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2848 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2849 r.mLaunchTaskBehind = false;
2850 final TaskRecord task = r.task;
2851 task.setLastThumbnail(task.stack.screenshotActivities(r));
2852 mService.addRecentTaskLocked(task);
2853 mWindowManager.setAppVisibility(r.appToken, false);
2854 }
2855
2856 void scheduleLaunchTaskBehindComplete(IBinder token) {
2857 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2858 }
2859
Craig Mautnerde4ef022013-04-07 19:01:33 -07002860 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002861 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002862 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2863 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002864 final int topStackNdx = stacks.size() - 1;
2865 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2866 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002867 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002868 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002869 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002870 }
2871
2872 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002873 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2874 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002875 final int numStacks = stacks.size();
2876 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2877 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002878 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002879 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002880 }
2881 }
2882
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002883 void releaseSomeActivitiesLocked(ProcessRecord app, String reason) {
2884 // Examine all activities currently running in the process.
2885 TaskRecord firstTask = null;
2886 // Tasks is non-null only if two or more tasks are found.
2887 ArraySet<TaskRecord> tasks = null;
2888 if (DEBUG_RELEASE) Slog.d(TAG, "Trying to release some activities in " + app);
2889 for (int i=0; i<app.activities.size(); i++) {
2890 ActivityRecord r = app.activities.get(i);
2891 // First, if we find an activity that is in the process of being destroyed,
2892 // then we just aren't going to do anything for now; we want things to settle
2893 // down before we try to prune more activities.
2894 if (r.finishing || r.state == ActivityState.DESTROYING
2895 || r.state == ActivityState.DESTROYED) {
2896 if (DEBUG_RELEASE) Slog.d(TAG, "Abort release; already destroying: " + r);
2897 return;
2898 }
2899 // Don't consider any activies that are currently not in a state where they
2900 // can be destroyed.
2901 if (r.visible || !r.stopped || !r.haveState
2902 || r.state == ActivityState.RESUMED || r.state == ActivityState.PAUSING
2903 || r.state == ActivityState.PAUSED || r.state == ActivityState.STOPPING) {
2904 if (DEBUG_RELEASE) Slog.d(TAG, "Not releasing in-use activity: " + r);
2905 continue;
2906 }
2907 if (r.task != null) {
2908 if (DEBUG_RELEASE) Slog.d(TAG, "Collecting release task " + r.task
2909 + " from " + r);
2910 if (firstTask == null) {
2911 firstTask = r.task;
2912 } else if (firstTask != r.task) {
2913 if (tasks == null) {
2914 tasks = new ArraySet<>();
2915 tasks.add(firstTask);
2916 }
2917 tasks.add(r.task);
2918 }
2919 }
2920 }
2921 if (tasks == null) {
2922 if (DEBUG_RELEASE) Slog.d(TAG, "Didn't find two or more tasks to release");
2923 return;
2924 }
2925 // If we have activities in multiple tasks that are in a position to be destroyed,
2926 // let's iterate through the tasks and release the oldest one.
2927 final int numDisplays = mActivityDisplays.size();
2928 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
2929 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2930 // Step through all stacks starting from behind, to hit the oldest things first.
2931 for (int stackNdx = 0; stackNdx < stacks.size(); stackNdx++) {
2932 final ActivityStack stack = stacks.get(stackNdx);
2933 // Try to release activities in this stack; if we manage to, we are done.
2934 if (stack.releaseSomeActivitiesLocked(app, tasks, reason) > 0) {
2935 return;
2936 }
2937 }
2938 }
2939 }
2940
Craig Mautner8d341ef2013-03-26 09:03:27 -07002941 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002942 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
2943 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002944 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002945
Craig Mautner858d8a62013-04-23 17:08:34 -07002946 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002947 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2948 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002949 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002950 final ActivityStack stack = stacks.get(stackNdx);
2951 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002952 TaskRecord task = stack.topTask();
2953 if (task != null) {
2954 mWindowManager.moveTaskToTop(task.taskId);
2955 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002956 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002957 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002958
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002959 ActivityStack stack = getStack(restoreStackId);
2960 if (stack == null) {
2961 stack = mHomeStack;
2962 }
2963 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002964 if (stack.isOnHomeDisplay()) {
2965 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002966 TaskRecord task = stack.topTask();
2967 if (task != null) {
2968 mWindowManager.moveTaskToTop(task.taskId);
2969 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002970 } else {
2971 // Stack was moved to another display while user was swapped out.
Craig Mautner84984fa2014-06-19 11:19:20 -07002972 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
Craig Mautnere0a38842013-12-16 16:14:02 -08002973 }
Craig Mautner93529a42013-10-04 15:03:13 -07002974 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002975 }
2976
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002977 /**
2978 * Add background users to send boot completed events to.
2979 * @param userId The user being started in the background
2980 * @param uss The state object for the user.
2981 */
2982 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
2983 mStartingBackgroundUsers.add(uss);
2984 }
2985
Craig Mautnerde4ef022013-04-07 19:01:33 -07002986 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
2987 int N = mStoppingActivities.size();
2988 if (N <= 0) return null;
2989
2990 ArrayList<ActivityRecord> stops = null;
2991
2992 final boolean nowVisible = allResumedActivitiesVisible();
2993 for (int i=0; i<N; i++) {
2994 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07002995 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07002996 + nowVisible + " waitingVisible=" + s.waitingVisible
2997 + " finishing=" + s.finishing);
2998 if (s.waitingVisible && nowVisible) {
2999 mWaitingVisibleActivities.remove(s);
3000 s.waitingVisible = false;
3001 if (s.finishing) {
3002 // If this activity is finishing, it is sitting on top of
3003 // everyone else but we now know it is no longer needed...
3004 // so get rid of it. Otherwise, we need to go through the
3005 // normal flow and hide it once we determine that it is
3006 // hidden by the activities in front of it.
3007 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003008 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07003009 }
3010 }
3011 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
3012 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
3013 if (stops == null) {
3014 stops = new ArrayList<ActivityRecord>();
3015 }
3016 stops.add(s);
3017 mStoppingActivities.remove(i);
3018 N--;
3019 i--;
3020 }
3021 }
3022
3023 return stops;
3024 }
3025
Craig Mautnercf910b02013-04-23 11:23:27 -07003026 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003027 // FIXME
3028/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
3029 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07003030 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003031 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07003032 if (isFrontStack(stack)) {
3033 if (r == null) {
3034 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
3035 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003036 final ActivityRecord pausing = stack.mPausingActivity;
3037 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003038 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003039 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003040 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003041 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003042 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003043 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003044 }
3045 }
3046 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003047 final ActivityRecord resumed = stack.mResumedActivity;
3048 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003049 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003050 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003051 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003052 if (r != null && (state == ActivityState.INITIALIZING
3053 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07003054 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07003055 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07003056 }
3057 }
3058 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003059*/
Craig Mautner76ea2242013-05-15 11:40:05 -07003060 }
3061
Craig Mautner27084302013-03-25 08:05:25 -07003062 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003063 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003064 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07003065 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
3066 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
3067 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08003068 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07003069 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003070
Craig Mautner20e72272013-04-01 13:45:53 -07003071 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003072 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07003073 }
3074
Dianne Hackborn390517b2013-05-30 15:03:32 -07003075 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
3076 boolean needSep, String prefix) {
3077 if (activity != null) {
3078 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
3079 if (needSep) {
3080 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07003081 }
3082 pw.print(prefix);
3083 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003084 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003085 }
3086 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003087 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003088 }
3089
Craig Mautner8d341ef2013-03-26 09:03:27 -07003090 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
3091 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003092 boolean printed = false;
3093 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08003094 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3095 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003096 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
3097 pw.println(" (activities from bottom to top):");
Craig Mautnere0a38842013-12-16 16:14:02 -08003098 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003099 final int numStacks = stacks.size();
3100 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
3101 final ActivityStack stack = stacks.get(stackNdx);
3102 StringBuilder stackHeader = new StringBuilder(128);
3103 stackHeader.append(" Stack #");
3104 stackHeader.append(stack.mStackId);
3105 stackHeader.append(":");
3106 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
3107 needSep, stackHeader.toString());
3108 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
3109 !dumpAll, false, dumpPackage, true,
3110 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07003111
Craig Mautner4a1cb222013-12-04 16:14:06 -08003112 needSep = printed;
3113 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
3114 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003115 if (pr) {
3116 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003117 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003118 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003119 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
3120 " mResumedActivity: ");
3121 if (pr) {
3122 printed = true;
3123 needSep = false;
3124 }
3125 if (dumpAll) {
3126 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
3127 " mLastPausedActivity: ");
3128 if (pr) {
3129 printed = true;
3130 needSep = true;
3131 }
3132 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
3133 needSep, " mLastNoHistoryActivity: ");
3134 }
3135 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003136 }
3137 }
3138
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003139 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
3140 false, dumpPackage, true, " Activities waiting to finish:", null);
3141 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
3142 false, dumpPackage, true, " Activities waiting to stop:", null);
3143 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
3144 false, dumpPackage, true, " Activities waiting for another to become visible:",
3145 null);
3146 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3147 false, dumpPackage, true, " Activities waiting to sleep:", null);
3148 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
3149 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07003150
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003151 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003152 }
3153
Dianne Hackborn390517b2013-05-30 15:03:32 -07003154 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07003155 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003156 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07003157 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003158 String innerPrefix = null;
3159 String[] args = null;
3160 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003161 for (int i=list.size()-1; i>=0; i--) {
3162 final ActivityRecord r = list.get(i);
3163 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
3164 continue;
3165 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003166 if (innerPrefix == null) {
3167 innerPrefix = prefix + " ";
3168 args = new String[0];
3169 }
3170 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003171 final boolean full = !brief && (complete || !r.isInHistory());
3172 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07003173 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07003174 needNL = false;
3175 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07003176 if (header1 != null) {
3177 pw.println(header1);
3178 header1 = null;
3179 }
3180 if (header2 != null) {
3181 pw.println(header2);
3182 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07003183 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07003184 if (lastTask != r.task) {
3185 lastTask = r.task;
3186 pw.print(prefix);
3187 pw.print(full ? "* " : " ");
3188 pw.println(lastTask);
3189 if (full) {
3190 lastTask.dump(pw, prefix + " ");
3191 } else if (complete) {
3192 // Complete + brief == give a summary. Isn't that obvious?!?
3193 if (lastTask.intent != null) {
3194 pw.print(prefix); pw.print(" ");
3195 pw.println(lastTask.intent.toInsecureStringWithClip());
3196 }
3197 }
3198 }
3199 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
3200 pw.print(" #"); pw.print(i); pw.print(": ");
3201 pw.println(r);
3202 if (full) {
3203 r.dump(pw, innerPrefix);
3204 } else if (complete) {
3205 // Complete + brief == give a summary. Isn't that obvious?!?
3206 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
3207 if (r.app != null) {
3208 pw.print(innerPrefix); pw.println(r.app);
3209 }
3210 }
3211 if (client && r.app != null && r.app.thread != null) {
3212 // flush anything that is already in the PrintWriter since the thread is going
3213 // to write to the file descriptor directly
3214 pw.flush();
3215 try {
3216 TransferPipe tp = new TransferPipe();
3217 try {
3218 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3219 r.appToken, innerPrefix, args);
3220 // Short timeout, since blocking here can
3221 // deadlock with the application.
3222 tp.go(fd, 2000);
3223 } finally {
3224 tp.kill();
3225 }
3226 } catch (IOException e) {
3227 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3228 } catch (RemoteException e) {
3229 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3230 }
3231 needNL = true;
3232 }
3233 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003234 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003235 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003236
Craig Mautnerf3333272013-04-22 10:55:53 -07003237 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003238 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003239 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3240 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003241 }
3242
3243 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003244 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003245 }
3246
3247 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003248 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003249 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3250 }
3251
Craig Mautner05d29032013-05-03 13:40:13 -07003252 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003253 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3254 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3255 }
Craig Mautner05d29032013-05-03 13:40:13 -07003256 }
3257
Craig Mautner0eea92c2013-05-16 13:35:39 -07003258 void removeSleepTimeouts() {
3259 mSleepTimeout = false;
3260 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3261 }
3262
3263 final void scheduleSleepTimeout() {
3264 removeSleepTimeouts();
3265 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3266 }
3267
Craig Mautner4a1cb222013-12-04 16:14:06 -08003268 @Override
3269 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003270 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003271 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3272 }
3273
3274 @Override
3275 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003276 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003277 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3278 }
3279
3280 @Override
3281 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003282 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003283 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3284 }
3285
3286 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003287 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003288 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003289 newDisplay = mActivityDisplays.get(displayId) == null;
3290 if (newDisplay) {
3291 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
Craig Mautner1a70a162014-09-13 12:09:31 -07003292 if (activityDisplay.mDisplay == null) {
3293 Slog.w(TAG, "Display " + displayId + " gone before initialization complete");
3294 return;
3295 }
Craig Mautner4504de52013-12-20 09:06:56 -08003296 mActivityDisplays.put(displayId, activityDisplay);
3297 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003298 }
Craig Mautner4504de52013-12-20 09:06:56 -08003299 if (newDisplay) {
3300 mWindowManager.onDisplayAdded(displayId);
3301 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003302 }
3303
3304 public void handleDisplayRemovedLocked(int displayId) {
3305 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003306 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3307 if (activityDisplay != null) {
3308 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003309 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003310 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003311 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003312 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003313 }
3314 }
3315 mWindowManager.onDisplayRemoved(displayId);
3316 }
3317
3318 public void handleDisplayChangedLocked(int displayId) {
3319 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003320 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3321 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003322 // TODO: Update the bounds.
3323 }
3324 }
3325 mWindowManager.onDisplayChanged(displayId);
3326 }
3327
3328 StackInfo getStackInfo(ActivityStack stack) {
3329 StackInfo info = new StackInfo();
3330 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3331 info.displayId = Display.DEFAULT_DISPLAY;
3332 info.stackId = stack.mStackId;
3333
3334 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3335 final int numTasks = tasks.size();
3336 int[] taskIds = new int[numTasks];
3337 String[] taskNames = new String[numTasks];
3338 for (int i = 0; i < numTasks; ++i) {
3339 final TaskRecord task = tasks.get(i);
3340 taskIds[i] = task.taskId;
3341 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3342 : task.realActivity != null ? task.realActivity.flattenToString()
3343 : task.getTopActivity() != null ? task.getTopActivity().packageName
3344 : "unknown";
3345 }
3346 info.taskIds = taskIds;
3347 info.taskNames = taskNames;
3348 return info;
3349 }
3350
3351 StackInfo getStackInfoLocked(int stackId) {
3352 ActivityStack stack = getStack(stackId);
3353 if (stack != null) {
3354 return getStackInfo(stack);
3355 }
3356 return null;
3357 }
3358
3359 ArrayList<StackInfo> getAllStackInfosLocked() {
3360 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003361 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3362 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003363 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3364 list.add(getStackInfo(stacks.get(ndx)));
3365 }
3366 }
3367 return list;
3368 }
3369
Jason Monka8f569c2014-07-07 12:15:21 -04003370 void showLockTaskToast() {
3371 mLockTaskNotify.showToast(mLockTaskIsLocked);
3372 }
3373
3374 void setLockTaskModeLocked(TaskRecord task, boolean isLocked) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003375 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003376 // Take out of lock task mode if necessary
3377 if (mLockTaskModeTask != null) {
3378 final Message lockTaskMsg = Message.obtain();
3379 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3380 lockTaskMsg.what = LOCK_TASK_END_MSG;
3381 mLockTaskModeTask = null;
3382 mHandler.sendMessage(lockTaskMsg);
3383 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003384 return;
3385 }
3386 if (isLockTaskModeViolation(task)) {
3387 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3388 return;
3389 }
3390 mLockTaskModeTask = task;
3391 findTaskToMoveToFrontLocked(task, 0, null);
3392 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003393
3394 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003395 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3396 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003397 lockTaskMsg.what = LOCK_TASK_START_MSG;
Jason Monka8f569c2014-07-07 12:15:21 -04003398 lockTaskMsg.arg2 = !isLocked ? 1 : 0;
justinzhang5286d3f2014-05-12 17:06:01 -04003399 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003400 }
3401
3402 boolean isLockTaskModeViolation(TaskRecord task) {
3403 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3404 }
3405
3406 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3407 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
Jason Monk835495c2014-08-20 14:48:46 -04003408 final Message lockTaskMsg = Message.obtain();
3409 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3410 lockTaskMsg.what = LOCK_TASK_END_MSG;
Craig Mautneraea74a52014-03-08 14:23:10 -08003411 mLockTaskModeTask = null;
Jason Monk835495c2014-08-20 14:48:46 -04003412 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003413 }
3414 }
3415
3416 boolean isInLockTaskMode() {
3417 return mLockTaskModeTask != null;
3418 }
3419
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003420 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003421
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003422 public ActivityStackSupervisorHandler(Looper looper) {
3423 super(looper);
3424 }
3425
Craig Mautnerf3333272013-04-22 10:55:53 -07003426 void activityIdleInternal(ActivityRecord r) {
3427 synchronized (mService) {
3428 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3429 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003430 }
3431
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003432 @Override
3433 public void handleMessage(Message msg) {
3434 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003435 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003436 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003437 if (mService.mDidDexOpt) {
3438 mService.mDidDexOpt = false;
3439 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3440 nmsg.obj = msg.obj;
3441 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3442 return;
3443 }
3444 // We don't at this point know if the activity is fullscreen,
3445 // so we need to be conservative and assume it isn't.
3446 activityIdleInternal((ActivityRecord)msg.obj);
3447 } break;
3448 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003449 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003450 activityIdleInternal((ActivityRecord)msg.obj);
3451 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003452 case RESUME_TOP_ACTIVITY_MSG: {
3453 synchronized (mService) {
3454 resumeTopActivitiesLocked();
3455 }
3456 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003457 case SLEEP_TIMEOUT_MSG: {
3458 synchronized (mService) {
3459 if (mService.isSleepingOrShuttingDown()) {
3460 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3461 mSleepTimeout = true;
3462 checkReadyForSleepLocked();
3463 }
3464 }
3465 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003466 case LAUNCH_TIMEOUT_MSG: {
3467 if (mService.mDidDexOpt) {
3468 mService.mDidDexOpt = false;
3469 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3470 return;
3471 }
3472 synchronized (mService) {
3473 if (mLaunchingActivity.isHeld()) {
3474 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3475 if (VALIDATE_WAKE_LOCK_CALLER
3476 && Binder.getCallingUid() != Process.myUid()) {
3477 throw new IllegalStateException("Calling must be system uid");
3478 }
3479 mLaunchingActivity.release();
3480 }
3481 }
3482 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003483 case HANDLE_DISPLAY_ADDED: {
3484 handleDisplayAddedLocked(msg.arg1);
3485 } break;
3486 case HANDLE_DISPLAY_CHANGED: {
3487 handleDisplayChangedLocked(msg.arg1);
3488 } break;
3489 case HANDLE_DISPLAY_REMOVED: {
3490 handleDisplayRemovedLocked(msg.arg1);
3491 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003492 case CONTAINER_CALLBACK_VISIBILITY: {
3493 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003494 final IActivityContainerCallback callback = container.mCallback;
3495 if (callback != null) {
3496 try {
3497 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3498 } catch (RemoteException e) {
3499 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003500 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003501 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003502 case LOCK_TASK_START_MSG: {
3503 // When lock task starts, we disable the status bars.
3504 try {
Jason Monk62515be2014-05-21 16:06:19 -04003505 if (mLockTaskNotify == null) {
3506 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003507 }
Jason Monk62515be2014-05-21 16:06:19 -04003508 mLockTaskNotify.show(true);
Jason Monk2961ce52014-07-23 13:30:27 -04003509 mLockTaskIsLocked = msg.arg2 == 0;
Jason Monk62515be2014-05-21 16:06:19 -04003510 if (getStatusBarService() != null) {
3511 int flags =
3512 StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
Jason Monk2961ce52014-07-23 13:30:27 -04003513 if (!mLockTaskIsLocked) {
Jason Monk62515be2014-05-21 16:06:19 -04003514 flags ^= StatusBarManager.DISABLE_HOME
3515 | StatusBarManager.DISABLE_RECENT;
3516 }
3517 getStatusBarService().disable(flags, mToken,
3518 mService.mContext.getPackageName());
3519 }
3520 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003521 if (getDevicePolicyManager() != null) {
3522 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003523 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003524 }
justinzhang5286d3f2014-05-12 17:06:01 -04003525 } catch (RemoteException ex) {
3526 throw new RuntimeException(ex);
3527 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003528 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003529 case LOCK_TASK_END_MSG: {
3530 // When lock task ends, we enable the status bars.
3531 try {
Jason Monk62515be2014-05-21 16:06:19 -04003532 if (getStatusBarService() != null) {
3533 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3534 mService.mContext.getPackageName());
3535 }
3536 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003537 if (getDevicePolicyManager() != null) {
3538 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3539 msg.arg1);
3540 }
Jason Monk62515be2014-05-21 16:06:19 -04003541 if (mLockTaskNotify == null) {
3542 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3543 }
3544 mLockTaskNotify.show(false);
3545 try {
3546 boolean shouldLockKeyguard = Settings.System.getInt(
3547 mService.mContext.getContentResolver(),
3548 Settings.System.LOCK_TO_APP_EXIT_LOCKED) != 0;
Jason Monk2961ce52014-07-23 13:30:27 -04003549 if (!mLockTaskIsLocked && shouldLockKeyguard) {
Jason Monk62515be2014-05-21 16:06:19 -04003550 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003551 mWindowManager.dismissKeyguard();
Jason Monke0697792014-08-04 16:28:09 -04003552 new LockPatternUtils(mService.mContext)
3553 .requireCredentialEntry(UserHandle.USER_ALL);
Jason Monk62515be2014-05-21 16:06:19 -04003554 }
3555 } catch (SettingNotFoundException e) {
3556 // No setting, don't lock.
3557 }
justinzhang5286d3f2014-05-12 17:06:01 -04003558 } catch (RemoteException ex) {
3559 throw new RuntimeException(ex);
3560 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003561 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003562 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3563 final ActivityContainer container = (ActivityContainer) msg.obj;
3564 final IActivityContainerCallback callback = container.mCallback;
3565 if (callback != null) {
3566 try {
3567 callback.onAllActivitiesComplete(container.asBinder());
3568 } catch (RemoteException e) {
3569 }
3570 }
3571 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003572 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3573 synchronized (mService) {
3574 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3575 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003576 final ActivityContainer container = (ActivityContainer) msg.obj;
3577 container.mStack.finishAllActivitiesLocked(true);
3578 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003579 }
3580 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003581 case LAUNCH_TASK_BEHIND_COMPLETE: {
3582 synchronized (mService) {
3583 ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
3584 if (r != null) {
3585 handleLaunchTaskBehindCompleteLocked(r);
3586 }
3587 }
3588 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003589 }
3590 }
3591 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003592
Ying Wangb081a592014-04-22 15:20:16 -07003593 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003594 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003595 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003596 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003597 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003598 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003599 ActivityRecord mParentActivity = null;
3600 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003601
Craig Mautnere3a00d72014-04-16 08:31:19 -07003602 boolean mVisible = true;
3603
Craig Mautner4a1cb222013-12-04 16:14:06 -08003604 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003605 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003606
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003607 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3608 final static int CONTAINER_STATE_NO_SURFACE = 1;
3609 final static int CONTAINER_STATE_FINISHING = 2;
3610 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3611
3612 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003613 synchronized (mService) {
3614 mStackId = stackId;
3615 mStack = new ActivityStack(this);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003616 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003617 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003618 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003619 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003620
Craig Mautnere0a38842013-12-16 16:14:02 -08003621 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003622 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3623 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003624 mActivityDisplay = activityDisplay;
3625 mStack.mDisplayId = activityDisplay.mDisplayId;
3626 mStack.mStacks = activityDisplay.mStacks;
3627
3628 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003629 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003630 }
3631
3632 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003633 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003634 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003635 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3636 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003637 return;
3638 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003639 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003640 }
3641 }
3642
3643 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003644 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003645 synchronized (mService) {
3646 if (mActivityDisplay != null) {
3647 return mActivityDisplay.mDisplayId;
3648 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003649 }
3650 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003651 }
3652
Jeff Brownca9bc702014-02-11 14:32:56 -08003653 @Override
3654 public boolean injectEvent(InputEvent event) {
3655 final long origId = Binder.clearCallingIdentity();
3656 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003657 synchronized (mService) {
3658 if (mActivityDisplay != null) {
3659 return mInputManagerInternal.injectInputEvent(event,
3660 mActivityDisplay.mDisplayId,
3661 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3662 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003663 }
3664 return false;
3665 } finally {
3666 Binder.restoreCallingIdentity(origId);
3667 }
3668 }
3669
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003670 @Override
3671 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003672 synchronized (mService) {
3673 if (mContainerState == CONTAINER_STATE_FINISHING) {
3674 return;
3675 }
3676 mContainerState = CONTAINER_STATE_FINISHING;
3677
3678 final Message msg =
3679 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003680 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003681
3682 long origId = Binder.clearCallingIdentity();
3683 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003684 mStack.finishAllActivitiesLocked(false);
Craig Mautner7f13ed32014-07-28 14:00:35 -07003685 removePendingActivityLaunchesLocked(mStack);
Craig Mautnerd163e752014-06-13 17:18:47 -07003686 } finally {
3687 Binder.restoreCallingIdentity(origId);
3688 }
3689 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003690 }
3691
Craig Mautner60257702014-09-17 15:02:33 -07003692 protected void detachLocked() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003693 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3694 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003695 if (mActivityDisplay != null) {
3696 mActivityDisplay.detachActivitiesLocked(mStack);
3697 mActivityDisplay = null;
3698 mStack.mDisplayId = -1;
3699 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003700 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003701 }
3702 }
3703
3704 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003705 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003706 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003707 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003708 Binder.getCallingUid(), mCurrentUser, false,
3709 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003710 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003711 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003712 String mimeType = intent.getType();
3713 if (mimeType == null && intent.getData() != null
3714 && "content".equals(intent.getData().getScheme())) {
3715 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3716 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003717 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0,
3718 0, null, null, null, null, userId, this, null);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003719 }
3720
3721 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003722 public final int startActivityIntentSender(IIntentSender intentSender) {
3723 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3724
3725 if (!(intentSender instanceof PendingIntentRecord)) {
3726 throw new IllegalArgumentException("Bad PendingIntent object");
3727 }
3728
3729 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003730 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003731 }
3732
Craig Mautner247ab652014-04-25 10:09:00 -07003733 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3734 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003735 Binder.getCallingUid(), mCurrentUser, false,
3736 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautner247ab652014-04-25 10:09:00 -07003737 if (resolvedType == null) {
3738 resolvedType = intent.getType();
3739 if (resolvedType == null && intent.getData() != null
3740 && "content".equals(intent.getData().getScheme())) {
3741 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3742 }
3743 }
Jeff Hao1b012d32014-08-20 10:35:34 -07003744 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003745 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003746 throw new SecurityException(
3747 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3748 }
3749 }
3750
3751 /** Throw a SecurityException if allowEmbedded is not true */
3752 @Override
3753 public final void checkEmbeddedAllowed(Intent intent) {
3754 checkEmbeddedAllowedInner(intent, null);
3755 }
3756
3757 /** Throw a SecurityException if allowEmbedded is not true */
3758 @Override
3759 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3760 if (!(intentSender instanceof PendingIntentRecord)) {
3761 throw new IllegalArgumentException("Bad PendingIntent object");
3762 }
3763 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3764 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3765 pendingIntent.key.requestResolvedType);
3766 }
3767
Craig Mautnerdf88d732014-01-27 09:21:32 -08003768 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003769 public IBinder asBinder() {
3770 return this;
3771 }
3772
Craig Mautner4504de52013-12-20 09:06:56 -08003773 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003774 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003775 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003776 }
3777
Craig Mautner4a1cb222013-12-04 16:14:06 -08003778 ActivityStackSupervisor getOuter() {
3779 return ActivityStackSupervisor.this;
3780 }
3781
Craig Mautnerd163e752014-06-13 17:18:47 -07003782 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003783 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003784 }
3785
3786 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003787 synchronized (mService) {
3788 if (mActivityDisplay != null) {
3789 mActivityDisplay.getBounds(outBounds);
3790 } else {
3791 outBounds.set(0, 0);
3792 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003793 }
3794 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003795
Craig Mautner6985bad2014-04-21 15:22:06 -07003796 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003797 void setVisible(boolean visible) {
3798 if (mVisible != visible) {
3799 mVisible = visible;
3800 if (mCallback != null) {
3801 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3802 0 /* unused */, this).sendToTarget();
3803 }
3804 }
3805 }
3806
Craig Mautner6985bad2014-04-21 15:22:06 -07003807 void setDrawn() {
3808 }
3809
Craig Mautner1b4bf852014-05-26 15:06:32 -07003810 // You can always start a new task on a regular ActivityStack.
3811 boolean isEligibleForNewTasks() {
3812 return true;
3813 }
3814
Craig Mautnerd163e752014-06-13 17:18:47 -07003815 void onTaskListEmptyLocked() {
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003816 }
3817
Craig Mautner34b73df2014-01-12 21:11:08 -08003818 @Override
3819 public String toString() {
3820 return mIdString + (mActivityDisplay == null ? "N" : "A");
3821 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003822 }
3823
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003824 private class VirtualActivityContainer extends ActivityContainer {
3825 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003826 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003827
3828 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3829 super(getNextStackId());
3830 mParentActivity = parent;
3831 mCallback = callback;
3832 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003833 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003834 }
3835
3836 @Override
3837 public void setSurface(Surface surface, int width, int height, int density) {
3838 super.setSurface(surface, width, height, density);
3839
3840 synchronized (mService) {
3841 final long origId = Binder.clearCallingIdentity();
3842 try {
3843 setSurfaceLocked(surface, width, height, density);
3844 } finally {
3845 Binder.restoreCallingIdentity(origId);
3846 }
3847 }
3848 }
3849
3850 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3851 if (mContainerState == CONTAINER_STATE_FINISHING) {
3852 return;
3853 }
3854 VirtualActivityDisplay virtualActivityDisplay =
3855 (VirtualActivityDisplay) mActivityDisplay;
3856 if (virtualActivityDisplay == null) {
3857 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003858 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003859 mActivityDisplay = virtualActivityDisplay;
3860 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3861 attachToDisplayLocked(virtualActivityDisplay);
3862 }
3863
3864 if (mSurface != null) {
3865 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003866 }
3867
Craig Mautner6985bad2014-04-21 15:22:06 -07003868 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003869 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003870 mStack.resumeTopActivityLocked(null);
3871 } else {
3872 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003873 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003874 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003875 mStack.startPausingLocked(false, true, false, false);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003876 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003877 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003878
Craig Mautnerd163e752014-06-13 17:18:47 -07003879 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003880
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003881 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3882 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003883 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003884
Craig Mautner6985bad2014-04-21 15:22:06 -07003885 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003886 boolean isAttachedLocked() {
3887 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003888 }
3889
3890 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003891 void setDrawn() {
3892 synchronized (mService) {
3893 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003894 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003895 }
3896 }
3897
Craig Mautner1b4bf852014-05-26 15:06:32 -07003898 // Never start a new task on an ActivityView if it isn't explicitly specified.
3899 @Override
3900 boolean isEligibleForNewTasks() {
3901 return false;
3902 }
3903
Craig Mautner60257702014-09-17 15:02:33 -07003904 void onTaskListEmptyLocked() {
3905 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3906 detachLocked();
3907 deleteActivityContainer(this);
3908 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
3909 }
3910
Craig Mautnerd163e752014-06-13 17:18:47 -07003911 private void setSurfaceIfReadyLocked() {
3912 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003913 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3914 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3915 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3916 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3917 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003918 }
3919 }
3920
Craig Mautner4a1cb222013-12-04 16:14:06 -08003921 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3922 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003923 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003924 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003925 int mDisplayId;
3926 Display mDisplay;
3927 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003928
Craig Mautner4a1cb222013-12-04 16:14:06 -08003929 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3930 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08003931 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003932
Jose Lima4b6c6692014-08-12 17:41:12 -07003933 ActivityRecord mVisibleBehindActivity;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003934
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003935 ActivityDisplay() {
3936 }
Craig Mautner4504de52013-12-20 09:06:56 -08003937
Craig Mautner1a70a162014-09-13 12:09:31 -07003938 // After instantiation, check that mDisplay is not null before using this. The alternative
3939 // is for this to throw an exception if mDisplayManager.getDisplay() returns null.
Craig Mautnere0a38842013-12-16 16:14:02 -08003940 ActivityDisplay(int displayId) {
Craig Mautner1a70a162014-09-13 12:09:31 -07003941 final Display display = mDisplayManager.getDisplay(displayId);
3942 if (display == null) {
3943 return;
3944 }
3945 init(display);
Craig Mautner4504de52013-12-20 09:06:56 -08003946 }
3947
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003948 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003949 mDisplay = display;
3950 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003951 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003952 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003953
3954 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003955 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
3956 + mDisplayId);
3957 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003958 }
3959
Craig Mautnere0a38842013-12-16 16:14:02 -08003960 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003961 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003962 + " from displayId=" + mDisplayId);
3963 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003964 }
3965
3966 void getBounds(Point bounds) {
3967 mDisplay.getDisplayInfo(mDisplayInfo);
3968 bounds.x = mDisplayInfo.appWidth;
3969 bounds.y = mDisplayInfo.appHeight;
3970 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003971
Jose Lima4b6c6692014-08-12 17:41:12 -07003972 void setVisibleBehindActivity(ActivityRecord r) {
3973 mVisibleBehindActivity = r;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003974 }
3975
Jose Lima4b6c6692014-08-12 17:41:12 -07003976 boolean hasVisibleBehindActivity() {
3977 return mVisibleBehindActivity != null;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003978 }
3979
Craig Mautner34b73df2014-01-12 21:11:08 -08003980 @Override
3981 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003982 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3983 }
3984 }
3985
3986 class VirtualActivityDisplay extends ActivityDisplay {
3987 VirtualDisplay mVirtualDisplay;
3988
Craig Mautner6985bad2014-04-21 15:22:06 -07003989 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003990 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003991 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3992 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3993 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3994 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003995
3996 init(mVirtualDisplay.getDisplay());
3997
3998 mWindowManager.handleDisplayAdded(mDisplayId);
3999 }
4000
4001 void setSurface(Surface surface) {
4002 if (mVirtualDisplay != null) {
4003 mVirtualDisplay.setSurface(surface);
4004 }
4005 }
4006
4007 @Override
4008 void detachActivitiesLocked(ActivityStack stack) {
4009 super.detachActivitiesLocked(stack);
4010 if (mVirtualDisplay != null) {
4011 mVirtualDisplay.release();
4012 mVirtualDisplay = null;
4013 }
4014 }
4015
4016 @Override
4017 public String toString() {
4018 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08004019 }
Craig Mautnered6649f2013-12-02 14:08:25 -08004020 }
Jose Lima58e66d62014-05-27 20:00:27 -07004021
4022 private boolean isLeanbackOnlyDevice() {
4023 boolean onLeanbackOnly = false;
4024 try {
4025 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
4026 PackageManager.FEATURE_LEANBACK_ONLY);
4027 } catch (RemoteException e) {
4028 // noop
4029 }
4030
4031 return onLeanbackOnly;
4032 }
Craig Mautner27084302013-03-25 08:05:25 -07004033}