blob: 9264186c6329ab29b47fb5fe5e04675a9c8f065c [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;
34
Craig Mautner2420ead2013-04-01 17:13:20 -070035import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070036import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080037import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070038import android.app.ActivityOptions;
39import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080040import android.app.IActivityContainer;
41import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070042import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070043import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070044import android.app.PendingIntent;
Craig Mautner20e72272013-04-01 13:45:53 -070045import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070047import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040048import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040049import android.app.admin.DevicePolicyManager;
50import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070051import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070052import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070053import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070054import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070055import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070056import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.content.pm.ApplicationInfo;
58import android.content.pm.PackageManager;
59import android.content.pm.ResolveInfo;
60import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080061import android.graphics.Point;
62import android.hardware.display.DisplayManager;
63import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080064import android.hardware.display.DisplayManagerGlobal;
65import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080066import android.hardware.input.InputManager;
67import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070068import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070069import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070070import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070071import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070072import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070073import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070074import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070075import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070076import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070077import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070078import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040079import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070080import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070081import android.os.UserHandle;
Dianne Hackborn91097de2014-04-04 18:02:06 -070082import android.service.voice.IVoiceInteractionSession;
Craig Mautner2420ead2013-04-01 17:13:20 -070083import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070084import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080085import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070086
Craig Mautner4a1cb222013-12-04 16:14:06 -080087import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080088import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080089import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080090import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080091import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -070092import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -070093import com.android.internal.app.IVoiceInteractor;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070094import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -040095import com.android.internal.statusbar.IStatusBarService;
Jeff Brownca9bc702014-02-11 14:32:56 -080096import com.android.server.LocalServices;
Craig Mautner6170f732013-04-02 13:05:23 -070097import com.android.server.am.ActivityManagerService.PendingActivityLaunch;
Craig Mautner2420ead2013-04-01 17:13:20 -070098import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070099import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700100
justinzhang5286d3f2014-05-12 17:06:01 -0400101
Craig Mautner8d341ef2013-03-26 09:03:27 -0700102import java.io.FileDescriptor;
103import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700104import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700105import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700106import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700107
Craig Mautner4a1cb222013-12-04 16:14:06 -0800108public final class ActivityStackSupervisor implements DisplayListener {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700109 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
110 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
111 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700112 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700113 static final boolean DEBUG_IDLE = DEBUG || false;
114 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
115 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
116 static final boolean DEBUG_STATES = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700117
Craig Mautner2219a1b2013-03-25 09:44:30 -0700118 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700119
Craig Mautnerf3333272013-04-22 10:55:53 -0700120 /** How long we wait until giving up on the last activity telling us it is idle. */
121 static final int IDLE_TIMEOUT = 10*1000;
122
Craig Mautner0eea92c2013-05-16 13:35:39 -0700123 /** How long we can hold the sleep wake lock before giving up. */
124 static final int SLEEP_TIMEOUT = 5*1000;
125
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700126 // How long we can hold the launch wake lock before giving up.
127 static final int LAUNCH_TIMEOUT = 10*1000;
128
Craig Mautner05d29032013-05-03 13:40:13 -0700129 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
130 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
131 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700132 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700133 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800134 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
135 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
136 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700137 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400138 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
139 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700140 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4c07d022014-06-11 17:12:59 -0700141 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800142
Craig Mautner4504de52013-12-20 09:06:56 -0800143 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700144
justinzhang5286d3f2014-05-12 17:06:01 -0400145 /** Status Bar Service **/
146 private IBinder mToken = new Binder();
147 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400148 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400149
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700150 // For debugging to make sure the caller when acquiring/releasing our
151 // wake lock is the system process.
152 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700153
Craig Mautner27084302013-03-25 08:05:25 -0700154 final ActivityManagerService mService;
155
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700156 final ActivityStackSupervisorHandler mHandler;
157
158 /** Short cut */
159 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800160 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700161
Craig Mautner27084302013-03-25 08:05:25 -0700162 /** Dismiss the keyguard after the next activity is displayed? */
Craig Mautner5314a402013-09-26 12:40:16 -0700163 boolean mDismissKeyguardOnNextActivity = false;
Craig Mautner27084302013-03-25 08:05:25 -0700164
Craig Mautner8d341ef2013-03-26 09:03:27 -0700165 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700166 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700167
168 /** Task identifier that activities are currently being started in. Incremented each time a
169 * new task is created. */
170 private int mCurTaskId = 0;
171
Craig Mautner2420ead2013-04-01 17:13:20 -0700172 /** The current user */
173 private int mCurrentUser;
174
Craig Mautnere0a38842013-12-16 16:14:02 -0800175 /** The stack containing the launcher app. Assumed to always be attached to
176 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700177 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700178
Craig Mautnere0a38842013-12-16 16:14:02 -0800179 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700180 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700181
Craig Mautner4a1cb222013-12-04 16:14:06 -0800182 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
183 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800184 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800185 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700186
187 /** List of activities that are waiting for a new activity to become visible before completing
188 * whatever operation they are supposed to do. */
189 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();
190
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700191 /** List of processes waiting to find out about the next visible activity. */
192 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
193 new ArrayList<IActivityManager.WaitResult>();
194
195 /** List of processes waiting to find out about the next launched activity. */
196 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
197 new ArrayList<IActivityManager.WaitResult>();
198
Craig Mautnerde4ef022013-04-07 19:01:33 -0700199 /** List of activities that are ready to be stopped, but waiting for the next activity to
200 * settle down before doing so. */
201 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
202
Craig Mautnerf3333272013-04-22 10:55:53 -0700203 /** List of activities that are ready to be finished, but waiting for the previous activity to
204 * settle down before doing so. It contains ActivityRecord objects. */
205 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
206
Craig Mautner0eea92c2013-05-16 13:35:39 -0700207 /** List of activities that are in the process of going to sleep. */
208 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
209
Craig Mautnerf3333272013-04-22 10:55:53 -0700210 /** Used on user changes */
211 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<UserStartedState>();
212
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700213 /** Used to queue up any background users being started */
214 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<UserStartedState>();
215
Craig Mautnerde4ef022013-04-07 19:01:33 -0700216 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
217 * is being brought in front of us. */
218 boolean mUserLeaving = false;
219
Craig Mautner0eea92c2013-05-16 13:35:39 -0700220 /** Set when we have taken too long waiting to go to sleep. */
221 boolean mSleepTimeout = false;
222
Jose Lima58e66d62014-05-27 20:00:27 -0700223 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
224 * setWindowManager is called. **/
225 private boolean mLeanbackOnlyDevice;
226
Craig Mautner0eea92c2013-05-16 13:35:39 -0700227 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700228 * We don't want to allow the device to go to sleep while in the process
229 * of launching an activity. This is primarily to allow alarm intent
230 * receivers to launch an activity and get that to run before the device
231 * goes back to sleep.
232 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700233 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700234
235 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700236 * Set when the system is going to sleep, until we have
237 * successfully paused the current activity and released our wake lock.
238 * At that point the system is allowed to actually sleep.
239 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700240 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700241
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700242 /** Stack id of the front stack when user switched, indexed by userId. */
243 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700244
Craig Mautner4504de52013-12-20 09:06:56 -0800245 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800246 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700247 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800248
249 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700250 private final SparseArray<ActivityDisplay> mActivityDisplays =
251 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800252
Jeff Brownca9bc702014-02-11 14:32:56 -0800253 InputManagerInternal mInputManagerInternal;
254
Craig Mautneraea74a52014-03-08 14:23:10 -0800255 /** If non-null then the task specified remains in front and no other tasks may be started
256 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400257 TaskRecord mLockTaskModeTask;
Craig Mautneraea74a52014-03-08 14:23:10 -0800258
Craig Mautner4a1cb222013-12-04 16:14:06 -0800259 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700260 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700261 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
262 }
263
264 /**
265 * At the time when the constructor runs, the power manager has not yet been
266 * initialized. So we initialize our wakelocks afterwards.
267 */
268 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800269 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700270 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700271 mLaunchingActivity =
272 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
273 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700274 }
275
justinzhang5286d3f2014-05-12 17:06:01 -0400276 // This function returns a IStatusBarService. The value is from ServiceManager.
277 // getService and is cached.
278 private IStatusBarService getStatusBarService() {
279 synchronized (mService) {
280 if (mStatusBarService == null) {
281 mStatusBarService = IStatusBarService.Stub.asInterface(
282 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
283 if (mStatusBarService == null) {
284 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
285 }
286 }
287 return mStatusBarService;
288 }
289 }
290
Jason Monk35c62a42014-06-17 10:24:47 -0400291 private IDevicePolicyManager getDevicePolicyManager() {
292 synchronized (mService) {
293 if (mDevicePolicyManager == null) {
294 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
295 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
296 if (mDevicePolicyManager == null) {
297 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
298 }
299 }
300 return mDevicePolicyManager;
301 }
302 }
303
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700304 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800305 synchronized (mService) {
306 mWindowManager = wm;
307
308 mDisplayManager =
309 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
310 mDisplayManager.registerDisplayListener(this, null);
311
312 Display[] displays = mDisplayManager.getDisplays();
313 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
314 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800315 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
316 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800317 }
318
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700319 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800320 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800321
322 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700323
324 // Initialize this here, now that we can get a valid reference to PackageManager.
325 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800326 }
Craig Mautner27084302013-03-25 08:05:25 -0700327 }
328
329 void dismissKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700330 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner27084302013-03-25 08:05:25 -0700331 if (mDismissKeyguardOnNextActivity) {
332 mDismissKeyguardOnNextActivity = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700333 mWindowManager.dismissKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700334 }
335 }
336
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700337 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800338 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700339 }
340
Craig Mautnerde4ef022013-04-07 19:01:33 -0700341 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800342 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700343 }
344
Craig Mautner4a1cb222013-12-04 16:14:06 -0800345 // TODO: Split into two methods isFrontStack for any visible stack and isFrontmostStack for the
346 // top of all visible stacks.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700347 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800348 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
349 if (parent != null) {
350 stack = parent.task.stack;
351 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800352 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800353 if (stacks != null && !stacks.isEmpty()) {
354 return stack == stacks.get(stacks.size() - 1);
355 }
356 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700357 }
358
Craig Mautnerde4ef022013-04-07 19:01:33 -0700359 void moveHomeStack(boolean toFront) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800360 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800361 int topNdx = stacks.size() - 1;
362 if (topNdx <= 0) {
363 return;
364 }
365 ActivityStack topStack = stacks.get(topNdx);
366 final boolean homeInFront = topStack == mHomeStack;
367 if (homeInFront != toFront) {
368 mLastFocusedStack = topStack;
369 stacks.remove(mHomeStack);
370 stacks.add(toFront ? topNdx : 0, mHomeStack);
371 mFocusedStack = stacks.get(topNdx);
372 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
373 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700374 }
375 }
376
Craig Mautner719e6212014-05-29 16:49:41 +0000377 void moveHomeToTop() {
Craig Mautner69ada552013-04-18 13:51:51 -0700378 moveHomeStack(true);
Craig Mautner719e6212014-05-29 16:49:41 +0000379 mHomeStack.moveHomeTaskToTop();
Craig Mautner8e569572013-10-11 17:36:59 -0700380 }
381
Craig Mautner719e6212014-05-29 16:49:41 +0000382 boolean resumeHomeActivity(ActivityRecord prev) {
383 moveHomeToTop();
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700384 if (prev != null) {
Craig Mautner719e6212014-05-29 16:49:41 +0000385 prev.task.mOnTopOfHome = false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700386 }
Craig Mautnera8a90e02013-06-28 15:24:50 -0700387 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner719e6212014-05-29 16:49:41 +0000388 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700389 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700390 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700391 }
392 return mService.startHomeActivityLocked(mCurrentUser);
393 }
394
Craig Mautner27084302013-03-25 08:05:25 -0700395 void setDismissKeyguard(boolean dismiss) {
Craig Mautner5314a402013-09-26 12:40:16 -0700396 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen(" dismiss=" + dismiss);
Craig Mautner27084302013-03-25 08:05:25 -0700397 mDismissKeyguardOnNextActivity = dismiss;
398 }
399
Craig Mautner8d341ef2013-03-26 09:03:27 -0700400 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800401 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800402 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800403 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800404 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
405 ActivityStack stack = stacks.get(stackNdx);
406 TaskRecord task = stack.taskForIdLocked(id);
407 if (task != null) {
408 return task;
409 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700410 }
411 }
412 return null;
413 }
414
Craig Mautner6170f732013-04-02 13:05:23 -0700415 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800416 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800417 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800418 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800419 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
420 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
421 if (r != null) {
422 return r;
423 }
Craig Mautner6170f732013-04-02 13:05:23 -0700424 }
425 }
426 return null;
427 }
428
Craig Mautneref73ee12014-04-23 11:45:37 -0700429 void setNextTaskId(int taskId) {
430 if (taskId > mCurTaskId) {
431 mCurTaskId = taskId;
432 }
433 }
434
Craig Mautner8d341ef2013-03-26 09:03:27 -0700435 int getNextTaskId() {
436 do {
437 mCurTaskId++;
438 if (mCurTaskId <= 0) {
439 mCurTaskId = 1;
440 }
441 } while (anyTaskForIdLocked(mCurTaskId) != null);
442 return mCurTaskId;
443 }
444
Craig Mautnerde4ef022013-04-07 19:01:33 -0700445 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700446 ActivityStack stack = getFocusedStack();
447 if (stack == null) {
448 return null;
449 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700450 ActivityRecord resumedActivity = stack.mResumedActivity;
451 if (resumedActivity == null || resumedActivity.app == null) {
452 resumedActivity = stack.mPausingActivity;
453 if (resumedActivity == null || resumedActivity.app == null) {
454 resumedActivity = stack.topRunningActivityLocked(null);
455 }
456 }
457 return resumedActivity;
458 }
459
Mike Lockwooded8902d2013-11-15 11:01:47 -0800460 boolean attachApplicationLocked(ProcessRecord app) throws Exception {
Craig Mautner20e72272013-04-01 13:45:53 -0700461 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800462 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800463 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
464 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 ActivityStack stack = stacks.get(stackNdx);
467 if (!isFrontStack(stack)) {
468 continue;
469 }
470 ActivityRecord hr = stack.topRunningActivityLocked(null);
471 if (hr != null) {
472 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
473 && processName.equals(hr.processName)) {
474 try {
George Mount2c92c972014-03-20 09:38:23 -0700475 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800476 didSomething = true;
477 }
478 } catch (Exception e) {
479 Slog.w(TAG, "Exception in new application when starting activity "
480 + hr.intent.getComponent().flattenToShortString(), e);
481 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700482 }
Craig Mautner20e72272013-04-01 13:45:53 -0700483 }
Craig Mautner20e72272013-04-01 13:45:53 -0700484 }
485 }
486 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700487 if (!didSomething) {
488 ensureActivitiesVisibleLocked(null, 0);
489 }
Craig Mautner20e72272013-04-01 13:45:53 -0700490 return didSomething;
491 }
492
493 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800494 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
495 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800496 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
497 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800498 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800499 continue;
500 }
501 final ActivityRecord resumedActivity = stack.mResumedActivity;
502 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800503 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
504 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800505 return false;
506 }
Craig Mautner20e72272013-04-01 13:45:53 -0700507 }
508 }
509 return true;
510 }
511
Craig Mautnerde4ef022013-04-07 19:01:33 -0700512 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800513 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
514 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800515 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
516 final ActivityStack stack = stacks.get(stackNdx);
517 if (isFrontStack(stack)) {
518 final ActivityRecord r = stack.mResumedActivity;
519 if (r != null && r.state != ActivityState.RESUMED) {
520 return false;
521 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700522 }
523 }
524 }
525 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800526 if (DEBUG_STACK) Slog.d(TAG,
527 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
528 mLastFocusedStack + " to=" + mFocusedStack);
529 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700530 return true;
531 }
532
533 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800534 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
535 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800536 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
537 final ActivityStack stack = stacks.get(stackNdx);
538 final ActivityRecord r = stack.mResumedActivity;
539 if (r != null && (!r.nowVisible || r.waitingVisible)) {
540 return false;
541 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700542 }
543 }
544 return true;
545 }
546
Craig Mautner2acc3892013-09-23 10:28:14 -0700547 /**
548 * Pause all activities in either all of the stacks or just the back stacks.
549 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700550 * @return true if any activity was paused as a result of this call.
551 */
Craig Mautner5314a402013-09-26 12:40:16 -0700552 boolean pauseBackStacks(boolean userLeaving) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700553 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800554 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
555 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800556 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
557 final ActivityStack stack = stacks.get(stackNdx);
558 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
559 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
560 " mResumedActivity=" + stack.mResumedActivity);
561 stack.startPausingLocked(userLeaving, false);
562 someActivityPaused = true;
563 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700564 }
565 }
566 return someActivityPaused;
567 }
568
Craig Mautnerde4ef022013-04-07 19:01:33 -0700569 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700570 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800571 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
572 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800573 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
574 final ActivityStack stack = stacks.get(stackNdx);
575 final ActivityRecord r = stack.mPausingActivity;
576 if (r != null && r.state != ActivityState.PAUSED
577 && r.state != ActivityState.STOPPED
578 && r.state != ActivityState.STOPPING) {
579 if (DEBUG_STATES) {
580 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
581 pausing = false;
582 } else {
583 return false;
584 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700585 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700586 }
587 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700588 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700589 }
590
Craig Mautnerdf88d732014-01-27 09:21:32 -0800591 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping) {
John Spurlock8a985d22014-02-25 09:40:05 -0500592 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800593 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
594 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
595 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
596 final ActivityStack stack = stacks.get(stackNdx);
597 if (stack.mResumedActivity != null &&
598 stack.mActivityContainer.mParentActivity == parent) {
599 stack.startPausingLocked(userLeaving, uiSleeping);
600 }
601 }
602 }
603 }
604
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700605 void reportActivityVisibleLocked(ActivityRecord r) {
Craig Mautner858d8a62013-04-23 17:08:34 -0700606 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700607 WaitResult w = mWaitingActivityVisible.get(i);
608 w.timeout = false;
609 if (r != null) {
610 w.who = new ComponentName(r.info.packageName, r.info.name);
611 }
612 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
613 w.thisTime = w.totalTime;
614 }
615 mService.notifyAll();
616 dismissKeyguard();
617 }
618
619 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
620 long thisTime, long totalTime) {
621 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700622 WaitResult w = mWaitingActivityLaunched.remove(i);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700623 w.timeout = timeout;
624 if (r != null) {
625 w.who = new ComponentName(r.info.packageName, r.info.name);
626 }
627 w.thisTime = thisTime;
628 w.totalTime = totalTime;
629 }
630 mService.notifyAll();
631 }
632
Craig Mautner29219d92013-04-16 20:19:12 -0700633 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700634 final ActivityStack focusedStack = getFocusedStack();
635 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
636 if (r != null) {
637 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700638 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700639
Craig Mautner4a1cb222013-12-04 16:14:06 -0800640 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800641 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800642 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
643 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700644 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700645 r = stack.topRunningActivityLocked(null);
646 if (r != null) {
647 return r;
648 }
649 }
650 }
651 return null;
652 }
653
Dianne Hackborn09233282014-04-30 11:33:59 -0700654 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700655 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800656 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
657 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800658 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800659 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800660 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800661 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
662 final ActivityStack stack = stacks.get(stackNdx);
663 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
664 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700665 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700666 }
667 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700668
669 // The lists are already sorted from most recent to oldest. Just pull the most recent off
670 // each list and add it to list. Stop when all lists are empty or maxNum reached.
671 while (maxNum > 0) {
672 long mostRecentActiveTime = Long.MIN_VALUE;
673 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800674 final int numTaskLists = runningTaskLists.size();
675 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
676 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700677 if (!stackTaskList.isEmpty()) {
678 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
679 if (lastActiveTime > mostRecentActiveTime) {
680 mostRecentActiveTime = lastActiveTime;
681 selectedStackList = stackTaskList;
682 }
683 }
684 }
685 if (selectedStackList != null) {
686 list.add(selectedStackList.remove(0));
687 --maxNum;
688 } else {
689 break;
690 }
691 }
Craig Mautner20e72272013-04-01 13:45:53 -0700692 }
693
Craig Mautner23ac33b2013-04-01 16:26:35 -0700694 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
695 String profileFile, ParcelFileDescriptor profileFd, int userId) {
696 // Collect information about the target of the Intent.
697 ActivityInfo aInfo;
698 try {
699 ResolveInfo rInfo =
700 AppGlobals.getPackageManager().resolveIntent(
701 intent, resolvedType,
702 PackageManager.MATCH_DEFAULT_ONLY
703 | ActivityManagerService.STOCK_PM_FLAGS, userId);
704 aInfo = rInfo != null ? rInfo.activityInfo : null;
705 } catch (RemoteException e) {
706 aInfo = null;
707 }
708
709 if (aInfo != null) {
710 // Store the found target back into the intent, because now that
711 // we have it we never want to do this again. For example, if the
712 // user navigates back to this point in the history, we should
713 // always restart the exact same activity.
714 intent.setComponent(new ComponentName(
715 aInfo.applicationInfo.packageName, aInfo.name));
716
717 // Don't debug things in the system process
718 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
719 if (!aInfo.processName.equals("system")) {
720 mService.setDebugApp(aInfo.processName, true, false);
721 }
722 }
723
724 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
725 if (!aInfo.processName.equals("system")) {
726 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
727 }
728 }
729
730 if (profileFile != null) {
731 if (!aInfo.processName.equals("system")) {
732 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName,
733 profileFile, profileFd,
734 (startFlags&ActivityManager.START_FLAG_AUTO_STOP_PROFILER) != 0);
735 }
736 }
737 }
738 return aInfo;
739 }
740
Craig Mautner2219a1b2013-03-25 09:44:30 -0700741 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner719e6212014-05-29 16:49:41 +0000742 moveHomeToTop();
Dianne Hackborn91097de2014-04-04 18:02:06 -0700743 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null, 0,
Craig Mautnere0a38842013-12-16 16:14:02 -0800744 null, false, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700745 }
746
Craig Mautner23ac33b2013-04-01 16:26:35 -0700747 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700748 String callingPackage, Intent intent, String resolvedType,
749 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
750 IBinder resultTo, String resultWho, int requestCode, int startFlags, String profileFile,
Craig Mautner23ac33b2013-04-01 16:26:35 -0700751 ParcelFileDescriptor profileFd, WaitResult outResult, Configuration config,
Craig Mautnere0a38842013-12-16 16:14:02 -0800752 Bundle options, int userId, IActivityContainer iContainer) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700753 // Refuse possible leaked file descriptors
754 if (intent != null && intent.hasFileDescriptors()) {
755 throw new IllegalArgumentException("File descriptors passed in Intent");
756 }
757 boolean componentSpecified = intent.getComponent() != null;
758
759 // Don't modify the client's object!
760 intent = new Intent(intent);
761
762 // Collect information about the target of the Intent.
763 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
764 profileFile, profileFd, userId);
765
Craig Mautnere0a38842013-12-16 16:14:02 -0800766 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700767 synchronized (mService) {
768 int callingPid;
769 if (callingUid >= 0) {
770 callingPid = -1;
771 } else if (caller == null) {
772 callingPid = Binder.getCallingPid();
773 callingUid = Binder.getCallingUid();
774 } else {
775 callingPid = callingUid = -1;
776 }
777
Craig Mautnere0a38842013-12-16 16:14:02 -0800778 final ActivityStack stack;
779 if (container == null || container.mStack.isOnHomeDisplay()) {
780 stack = getFocusedStack();
781 } else {
782 stack = container.mStack;
783 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700784 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700785 && mService.mConfiguration.diff(config) != 0;
786 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700787 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700788
789 final long origId = Binder.clearCallingIdentity();
790
791 if (aInfo != null &&
792 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
793 // This may be a heavy-weight process! Check to see if we already
794 // have another, different heavy-weight process running.
795 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
796 if (mService.mHeavyWeightProcess != null &&
797 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
798 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700799 int realCallingUid = callingUid;
800 if (caller != null) {
801 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
802 if (callerApp != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700803 realCallingUid = callerApp.info.uid;
804 } else {
805 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700806 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700807 + intent.toString());
808 ActivityOptions.abort(options);
809 return ActivityManager.START_PERMISSION_DENIED;
810 }
811 }
812
813 IIntentSender target = mService.getIntentSenderLocked(
814 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
815 realCallingUid, userId, null, null, 0, new Intent[] { intent },
816 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
817 | PendingIntent.FLAG_ONE_SHOT, null);
818
819 Intent newIntent = new Intent();
820 if (requestCode >= 0) {
821 // Caller is requesting a result.
822 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
823 }
824 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
825 new IntentSender(target));
826 if (mService.mHeavyWeightProcess.activities.size() > 0) {
827 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
828 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
829 hist.packageName);
830 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
831 hist.task.taskId);
832 }
833 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
834 aInfo.packageName);
835 newIntent.setFlags(intent.getFlags());
836 newIntent.setClassName("android",
837 HeavyWeightSwitcherActivity.class.getName());
838 intent = newIntent;
839 resolvedType = null;
840 caller = null;
841 callingUid = Binder.getCallingUid();
842 callingPid = Binder.getCallingPid();
843 componentSpecified = true;
844 try {
845 ResolveInfo rInfo =
846 AppGlobals.getPackageManager().resolveIntent(
847 intent, null,
848 PackageManager.MATCH_DEFAULT_ONLY
849 | ActivityManagerService.STOCK_PM_FLAGS, userId);
850 aInfo = rInfo != null ? rInfo.activityInfo : null;
851 aInfo = mService.getActivityInfoForUser(aInfo, userId);
852 } catch (RemoteException e) {
853 aInfo = null;
854 }
855 }
856 }
857 }
858
Dianne Hackborn91097de2014-04-04 18:02:06 -0700859 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
860 voiceSession, voiceInteractor, resultTo, resultWho,
Craig Mautnere0a38842013-12-16 16:14:02 -0800861 requestCode, callingPid, callingUid, callingPackage, startFlags, options,
862 componentSpecified, null, container);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700863
Craig Mautnerde4ef022013-04-07 19:01:33 -0700864 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700865 // If the caller also wants to switch to a new configuration,
866 // do so now. This allows a clean switch, as we are waiting
867 // for the current activity to pause (so we will not destroy
868 // it), and have not yet started the next activity.
869 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
870 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700871 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700872 if (DEBUG_CONFIGURATION) Slog.v(TAG,
873 "Updating to new configuration after starting activity.");
874 mService.updateConfigurationLocked(config, null, false, false);
875 }
876
877 Binder.restoreCallingIdentity(origId);
878
879 if (outResult != null) {
880 outResult.result = res;
881 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700882 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700883 do {
884 try {
885 mService.wait();
886 } catch (InterruptedException e) {
887 }
888 } while (!outResult.timeout && outResult.who == null);
889 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700890 ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700891 if (r.nowVisible) {
892 outResult.timeout = false;
893 outResult.who = new ComponentName(r.info.packageName, r.info.name);
894 outResult.totalTime = 0;
895 outResult.thisTime = 0;
896 } else {
897 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700898 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700899 do {
900 try {
901 mService.wait();
902 } catch (InterruptedException e) {
903 }
904 } while (!outResult.timeout && outResult.who == null);
905 }
906 }
907 }
908
909 return res;
910 }
911 }
912
913 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
914 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
915 Bundle options, int userId) {
916 if (intents == null) {
917 throw new NullPointerException("intents is null");
918 }
919 if (resolvedTypes == null) {
920 throw new NullPointerException("resolvedTypes is null");
921 }
922 if (intents.length != resolvedTypes.length) {
923 throw new IllegalArgumentException("intents are length different than resolvedTypes");
924 }
925
Craig Mautner23ac33b2013-04-01 16:26:35 -0700926
927 int callingPid;
928 if (callingUid >= 0) {
929 callingPid = -1;
930 } else if (caller == null) {
931 callingPid = Binder.getCallingPid();
932 callingUid = Binder.getCallingUid();
933 } else {
934 callingPid = callingUid = -1;
935 }
936 final long origId = Binder.clearCallingIdentity();
937 try {
938 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -0700939 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -0700940 for (int i=0; i<intents.length; i++) {
941 Intent intent = intents[i];
942 if (intent == null) {
943 continue;
944 }
945
946 // Refuse possible leaked file descriptors
947 if (intent != null && intent.hasFileDescriptors()) {
948 throw new IllegalArgumentException("File descriptors passed in Intent");
949 }
950
951 boolean componentSpecified = intent.getComponent() != null;
952
953 // Don't modify the client's object!
954 intent = new Intent(intent);
955
956 // Collect information about the target of the Intent.
957 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i],
958 0, null, null, userId);
959 // TODO: New, check if this is correct
960 aInfo = mService.getActivityInfoForUser(aInfo, userId);
961
962 if (aInfo != null &&
963 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
964 != 0) {
965 throw new IllegalArgumentException(
966 "FLAG_CANT_SAVE_STATE not supported here");
967 }
968
969 Bundle theseOptions;
970 if (options != null && i == intents.length-1) {
971 theseOptions = options;
972 } else {
973 theseOptions = null;
974 }
Craig Mautner6170f732013-04-02 13:05:23 -0700975 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn91097de2014-04-04 18:02:06 -0700976 aInfo, null, null, resultTo, null, -1, callingPid, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -0800977 0, theseOptions, componentSpecified, outActivity, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700978 if (res < 0) {
979 return res;
980 }
981
982 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
983 }
984 }
985 } finally {
986 Binder.restoreCallingIdentity(origId);
987 }
988
989 return ActivityManager.START_SUCCESS;
990 }
991
Craig Mautner2420ead2013-04-01 17:13:20 -0700992 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -0700993 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -0700994 throws RemoteException {
995
996 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -0700997 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700998 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -0700999
1000 // schedule launch ticks to collect information about slow apps.
1001 r.startLaunchTickingLocked();
1002
1003 // Have the window manager re-evaluate the orientation of
1004 // the screen based on the new activity order. Note that
1005 // as a result of this, it can call back into the activity
1006 // manager with a new orientation. We don't care about that,
1007 // because the activity is not currently running so we are
1008 // just restarting it anyway.
1009 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001010 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001011 mService.mConfiguration,
1012 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1013 mService.updateConfigurationLocked(config, r, false, false);
1014 }
1015
1016 r.app = app;
1017 app.waitingToKill = null;
1018 r.launchCount++;
1019 r.lastLaunchTime = SystemClock.uptimeMillis();
1020
1021 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1022
1023 int idx = app.activities.indexOf(r);
1024 if (idx < 0) {
1025 app.activities.add(r);
1026 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001027 mService.updateLruProcessLocked(app, true, null);
1028 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001029
1030 final ActivityStack stack = r.task.stack;
1031 try {
1032 if (app.thread == null) {
1033 throw new RemoteException();
1034 }
1035 List<ResultInfo> results = null;
1036 List<Intent> newIntents = null;
1037 if (andResume) {
1038 results = r.results;
1039 newIntents = r.newIntents;
1040 }
1041 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1042 + " icicle=" + r.icicle
1043 + " with results=" + results + " newIntents=" + newIntents
1044 + " andResume=" + andResume);
1045 if (andResume) {
1046 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1047 r.userId, System.identityHashCode(r),
1048 r.task.taskId, r.shortComponentName);
1049 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001050 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001051 // Home process is the root process of the task.
1052 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001053 }
1054 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1055 r.sleeping = false;
1056 r.forceNewConfig = false;
1057 mService.showAskCompatModeDialogLocked(r);
1058 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1059 String profileFile = null;
1060 ParcelFileDescriptor profileFd = null;
1061 boolean profileAutoStop = false;
1062 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1063 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1064 mService.mProfileProc = app;
1065 profileFile = mService.mProfileFile;
1066 profileFd = mService.mProfileFd;
1067 profileAutoStop = mService.mAutoStopProfiler;
1068 }
1069 }
1070 app.hasShownUi = true;
1071 app.pendingUiClean = true;
1072 if (profileFd != null) {
1073 try {
1074 profileFd = profileFd.dup();
1075 } catch (IOException e) {
1076 if (profileFd != null) {
1077 try {
1078 profileFd.close();
1079 } catch (IOException o) {
1080 }
1081 profileFd = null;
1082 }
1083 }
1084 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001085
Dianne Hackborna413dc02013-07-12 12:02:55 -07001086 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001087 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1088 System.identityHashCode(r), r.info,
Craig Mautnera0026042014-04-23 11:45:37 -07001089 new Configuration(mService.mConfiguration), r.compat, r.task.voiceInteractor,
1090 app.repProcState, r.icicle, r.persistentState, results, newIntents, !andResume,
Craig Mautner233ceee2014-05-09 17:05:11 -07001091 mService.isNextTransitionForward(), profileFile, profileFd, profileAutoStop
1092 );
Craig Mautner2420ead2013-04-01 17:13:20 -07001093
1094 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1095 // This may be a heavy-weight process! Note that the package
1096 // manager will ensure that only activity can run in the main
1097 // process of the .apk, which is the only thing that will be
1098 // considered heavy-weight.
1099 if (app.processName.equals(app.info.packageName)) {
1100 if (mService.mHeavyWeightProcess != null
1101 && mService.mHeavyWeightProcess != app) {
1102 Slog.w(TAG, "Starting new heavy weight process " + app
1103 + " when already running "
1104 + mService.mHeavyWeightProcess);
1105 }
1106 mService.mHeavyWeightProcess = app;
1107 Message msg = mService.mHandler.obtainMessage(
1108 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1109 msg.obj = r;
1110 mService.mHandler.sendMessage(msg);
1111 }
1112 }
1113
1114 } catch (RemoteException e) {
1115 if (r.launchFailed) {
1116 // This is the second time we failed -- finish activity
1117 // and give up.
1118 Slog.e(TAG, "Second failure launching "
1119 + r.intent.getComponent().flattenToShortString()
1120 + ", giving up", e);
1121 mService.appDiedLocked(app, app.pid, app.thread);
1122 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1123 "2nd-crash", false);
1124 return false;
1125 }
1126
1127 // This is the first time we failed -- restart process and
1128 // retry.
1129 app.activities.remove(r);
1130 throw e;
1131 }
1132
1133 r.launchFailed = false;
1134 if (stack.updateLRUListLocked(r)) {
1135 Slog.w(TAG, "Activity " + r
1136 + " being launched, but already in LRU list");
1137 }
1138
1139 if (andResume) {
1140 // As part of the process of launching, ActivityThread also performs
1141 // a resume.
1142 stack.minimalResumeActivityLocked(r);
1143 } else {
1144 // This activity is not starting in the resumed state... which
1145 // should look like we asked it to pause+stop (but remain visible),
1146 // and it has done so and reported back the current icicle and
1147 // other state.
1148 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1149 + " (starting in stopped state)");
1150 r.state = ActivityState.STOPPED;
1151 r.stopped = true;
1152 }
1153
1154 // Launch the new version setup screen if needed. We do this -after-
1155 // launching the initial activity (that is, home), so that it can have
1156 // a chance to initialize itself while in the background, making the
1157 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001158 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001159 mService.startSetupActivityLocked();
1160 }
1161
1162 return true;
1163 }
1164
Craig Mautnere79d42682013-04-01 19:01:53 -07001165 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001166 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001167 // Is this activity's application already running?
1168 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001169 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001170
1171 r.task.stack.setLaunchTime(r);
1172
1173 if (app != null && app.thread != null) {
1174 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001175 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1176 || !"android".equals(r.info.packageName)) {
1177 // Don't add this if it is a platform component that is marked
1178 // to run in multiple processes, because this is actually
1179 // part of the framework so doesn't make sense to track as a
1180 // separate apk in the process.
1181 app.addPackage(r.info.packageName, mService.mProcessStats);
1182 }
George Mount2c92c972014-03-20 09:38:23 -07001183 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001184 return;
1185 } catch (RemoteException e) {
1186 Slog.w(TAG, "Exception when starting activity "
1187 + r.intent.getComponent().flattenToShortString(), e);
1188 }
1189
1190 // If a dead object exception was thrown -- fall through to
1191 // restart the application.
1192 }
1193
1194 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001195 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001196 }
1197
Craig Mautner6170f732013-04-02 13:05:23 -07001198 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001199 Intent intent, String resolvedType, ActivityInfo aInfo,
1200 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1201 IBinder resultTo, String resultWho, int requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001202 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
Craig Mautnere0a38842013-12-16 16:14:02 -08001203 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container) {
Craig Mautner6170f732013-04-02 13:05:23 -07001204 int err = ActivityManager.START_SUCCESS;
1205
1206 ProcessRecord callerApp = null;
1207 if (caller != null) {
1208 callerApp = mService.getRecordForAppLocked(caller);
1209 if (callerApp != null) {
1210 callingPid = callerApp.pid;
1211 callingUid = callerApp.info.uid;
1212 } else {
1213 Slog.w(TAG, "Unable to find app for caller " + caller
1214 + " (pid=" + callingPid + ") when starting: "
1215 + intent.toString());
1216 err = ActivityManager.START_PERMISSION_DENIED;
1217 }
1218 }
1219
1220 if (err == ActivityManager.START_SUCCESS) {
1221 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1222 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner9ef471f2014-02-07 13:11:47 -08001223 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid)
1224 + " on display " + (container == null ? (mFocusedStack == null ?
1225 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1226 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1227 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001228 }
1229
1230 ActivityRecord sourceRecord = null;
1231 ActivityRecord resultRecord = null;
1232 if (resultTo != null) {
1233 sourceRecord = isInAnyStackLocked(resultTo);
1234 if (DEBUG_RESULTS) Slog.v(
1235 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1236 if (sourceRecord != null) {
1237 if (requestCode >= 0 && !sourceRecord.finishing) {
1238 resultRecord = sourceRecord;
1239 }
1240 }
1241 }
1242 ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1243
Dianne Hackborn91097de2014-04-04 18:02:06 -07001244 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001245
1246 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
1247 && sourceRecord != null) {
1248 // Transfer the result target from the source activity to the new
1249 // one being started, including any failures.
1250 if (requestCode >= 0) {
1251 ActivityOptions.abort(options);
1252 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1253 }
1254 resultRecord = sourceRecord.resultTo;
1255 resultWho = sourceRecord.resultWho;
1256 requestCode = sourceRecord.requestCode;
1257 sourceRecord.resultTo = null;
1258 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001259 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001260 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001261 if (sourceRecord.launchedFromUid == callingUid) {
1262 // The new activity is being launched from the same uid as the previous
1263 // activity in the flow, and asking to forward its result back to the
1264 // previous. In this case the activity is serving as a trampoline between
1265 // the two, so we also want to update its launchedFromPackage to be the
1266 // same as the previous activity. Note that this is safe, since we know
1267 // these two packages come from the same uid; the caller could just as
1268 // well have supplied that same package name itself. This specifially
1269 // deals with the case of an intent picker/chooser being launched in the app
1270 // flow to redirect to an activity picked by the user, where we want the final
1271 // activity to consider it to have been launched by the previous app activity.
1272 callingPackage = sourceRecord.launchedFromPackage;
1273 }
Craig Mautner6170f732013-04-02 13:05:23 -07001274 }
1275
1276 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1277 // We couldn't find a class that can handle the given Intent.
1278 // That's the end of that!
1279 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1280 }
1281
1282 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1283 // We couldn't find the specific class specified in the Intent.
1284 // Also the end of the line.
1285 err = ActivityManager.START_CLASS_NOT_FOUND;
1286 }
1287
Dianne Hackborn91097de2014-04-04 18:02:06 -07001288 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1289 && sourceRecord.task.voiceSession != null) {
1290 // If this activity is being launched as part of a voice session, we need
1291 // to ensure that it is safe to do so. If the upcoming activity will also
1292 // be part of the voice session, we can only launch it if it has explicitly
1293 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001294 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001295 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1296 try {
1297 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1298 intent, resolvedType)) {
1299 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1300 }
1301 } catch (RemoteException e) {
1302 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1303 }
1304 }
1305 }
1306
1307 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1308 // If the caller is starting a new voice session, just make sure the target
1309 // is actually allowing it to run this way.
1310 try {
1311 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1312 intent, resolvedType)) {
1313 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1314 }
1315 } catch (RemoteException e) {
1316 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1317 }
1318 }
1319
Craig Mautner6170f732013-04-02 13:05:23 -07001320 if (err != ActivityManager.START_SUCCESS) {
1321 if (resultRecord != null) {
1322 resultStack.sendActivityResultLocked(-1,
1323 resultRecord, resultWho, requestCode,
1324 Activity.RESULT_CANCELED, null);
1325 }
1326 setDismissKeyguard(false);
1327 ActivityOptions.abort(options);
1328 return err;
1329 }
1330
1331 final int startAnyPerm = mService.checkPermission(
1332 START_ANY_ACTIVITY, callingPid, callingUid);
1333 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1334 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1335 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1336 if (resultRecord != null) {
1337 resultStack.sendActivityResultLocked(-1,
1338 resultRecord, resultWho, requestCode,
1339 Activity.RESULT_CANCELED, null);
1340 }
1341 setDismissKeyguard(false);
1342 String msg;
1343 if (!aInfo.exported) {
1344 msg = "Permission Denial: starting " + intent.toString()
1345 + " from " + callerApp + " (pid=" + callingPid
1346 + ", uid=" + callingUid + ")"
1347 + " not exported from uid " + aInfo.applicationInfo.uid;
1348 } else {
1349 msg = "Permission Denial: starting " + intent.toString()
1350 + " from " + callerApp + " (pid=" + callingPid
1351 + ", uid=" + callingUid + ")"
1352 + " requires " + aInfo.permission;
1353 }
1354 Slog.w(TAG, msg);
1355 throw new SecurityException(msg);
1356 }
1357
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001358 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001359 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001360
Craig Mautner6170f732013-04-02 13:05:23 -07001361 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001362 try {
1363 // The Intent we give to the watcher has the extra data
1364 // stripped off, since it can contain private information.
1365 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001366 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001367 aInfo.applicationInfo.packageName);
1368 } catch (RemoteException e) {
1369 mService.mController = null;
1370 }
Ben Gruver5e207332013-04-03 17:41:37 -07001371 }
Craig Mautner6170f732013-04-02 13:05:23 -07001372
Ben Gruver5e207332013-04-03 17:41:37 -07001373 if (abort) {
1374 if (resultRecord != null) {
1375 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001376 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001377 }
Ben Gruver5e207332013-04-03 17:41:37 -07001378 // We pretend to the caller that it was really started, but
1379 // they will just get a cancel result.
1380 setDismissKeyguard(false);
1381 ActivityOptions.abort(options);
1382 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001383 }
1384
1385 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001386 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001387 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001388 if (outActivity != null) {
1389 outActivity[0] = r;
1390 }
1391
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001392 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001393 if (voiceSession == null && (stack.mResumedActivity == null
1394 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Craig Mautner6170f732013-04-02 13:05:23 -07001395 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
1396 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001397 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautner6170f732013-04-02 13:05:23 -07001398 mService.mPendingActivityLaunches.add(pal);
1399 setDismissKeyguard(false);
1400 ActivityOptions.abort(options);
1401 return ActivityManager.START_SWITCHES_CANCELED;
1402 }
1403 }
1404
1405 if (mService.mDidAppSwitch) {
1406 // This is the second allowed switch since we stopped switches,
1407 // so now just generally allow switches. Use case: user presses
1408 // home (switches disabled, switch to home, mDidAppSwitch now true);
1409 // user taps a home icon (coming from home so allowed, we hit here
1410 // and now allow anyone to switch again).
1411 mService.mAppSwitchesAllowedTime = 0;
1412 } else {
1413 mService.mDidAppSwitch = true;
1414 }
1415
1416 mService.doPendingActivityLaunchesLocked(false);
1417
Dianne Hackborn91097de2014-04-04 18:02:06 -07001418 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
1419 startFlags, true, options);
Craig Mautner10385a12013-09-22 21:08:32 -07001420
1421 if (allPausedActivitiesComplete()) {
1422 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001423 // activity start, but we are not actually doing an activity
1424 // switch... just dismiss the keyguard now, because we
1425 // probably want to see whatever is behind it.
1426 dismissKeyguard();
1427 }
1428 return err;
1429 }
1430
Craig Mautner1b4bf852014-05-26 15:06:32 -07001431 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001432 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001433
1434 // On leanback only devices we should keep all activities in the same stack.
1435 if (!mLeanbackOnlyDevice &&
1436 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001437 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001438 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001439 if (taskStack.isOnHomeDisplay()) {
1440 if (mFocusedStack != taskStack) {
1441 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1442 "focused stack to r=" + r + " task=" + task);
1443 mFocusedStack = taskStack;
1444 } else {
1445 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1446 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1447 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001448 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001449 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001450 }
1451
Craig Mautnere0a38842013-12-16 16:14:02 -08001452 final ActivityContainer container = r.mInitialActivityContainer;
1453 if (container != null) {
1454 // The first time put it on the desired stack, after this put on task stack.
1455 r.mInitialActivityContainer = null;
1456 return container.mStack;
1457 }
1458
Craig Mautner1b4bf852014-05-26 15:06:32 -07001459 if (mFocusedStack != mHomeStack && (!newTask ||
1460 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001461 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1462 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1463 return mFocusedStack;
1464 }
1465
Craig Mautnere0a38842013-12-16 16:14:02 -08001466 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1467 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1468 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1469 if (!stack.isHomeStack()) {
1470 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1471 "adjustStackFocus: Setting focused stack=" + stack);
1472 mFocusedStack = stack;
1473 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001474 }
1475 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001476
Craig Mautner4a1cb222013-12-04 16:14:06 -08001477 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001478 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001479 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1480 " stackId=" + stackId);
1481 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001482 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001483 }
1484 return mHomeStack;
1485 }
1486
Craig Mautner29219d92013-04-16 20:19:12 -07001487 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001488 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001489 final TaskRecord task = r.task;
1490 boolean isHomeActivity = !r.isApplicationActivity();
1491 if (!isHomeActivity && task != null) {
1492 isHomeActivity = !task.isApplicationTask();
1493 }
1494 if (!isHomeActivity && task != null) {
1495 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1496 isHomeActivity = parent != null && parent.isHomeActivity();
1497 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001498 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001499 }
1500 }
1501
Craig Mautner8849a5e2013-04-02 16:41:03 -07001502 final int startActivityUncheckedLocked(ActivityRecord r,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001503 ActivityRecord sourceRecord,
1504 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
1505 boolean doResume, Bundle options) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001506 final Intent intent = r.intent;
1507 final int callingUid = r.launchedFromUid;
1508
1509 int launchFlags = intent.getFlags();
1510
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001511 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
1512 (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
1513 r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK)) {
1514 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1515 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1516 "\"singleInstance\" or \"singleTask\"");
1517 launchFlags &=
1518 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1519 } else {
1520 switch (r.info.documentLaunchMode) {
1521 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1522 break;
1523 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1524 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1525 break;
1526 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1527 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1528 break;
1529 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1530 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1531 break;
1532 }
1533 }
1534
1535 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1536 // For whatever reason this activity is being launched into a new
1537 // task... yet the caller has requested a result back. Well, that
1538 // is pretty messed up, so instead immediately send back a cancel
1539 // and let the new task continue launched as normal without a
1540 // dependency on its originator.
1541 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1542 r.resultTo.task.stack.sendActivityResultLocked(-1,
1543 r.resultTo, r.resultWho, r.requestCode,
1544 Activity.RESULT_CANCELED, null);
1545 r.resultTo = null;
1546 }
1547
1548 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1549 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1550 }
1551
Craig Mautner8849a5e2013-04-02 16:41:03 -07001552 // We'll invoke onUserLeaving before onPause only if the launching
1553 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001554 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001555 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001556
1557 // If the caller has asked not to resume at this point, we make note
1558 // of this in the record so that we can skip it when trying to find
1559 // the top running activity.
1560 if (!doResume) {
1561 r.delayedResume = true;
1562 }
1563
Craig Mautnera254cd72014-05-25 16:47:39 -07001564 ActivityRecord notTop =
1565 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001566
1567 // If the onlyIfNeeded flag is set, then we can do this if the activity
1568 // being launched is the same as the one making the call... or, as
1569 // a special case, if we do not know the caller then we count the
1570 // current top activity as the caller.
1571 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1572 ActivityRecord checkedCaller = sourceRecord;
1573 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001574 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001575 }
1576 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1577 // Caller is not the same as launcher, so always needed.
1578 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1579 }
1580 }
1581
1582 if (sourceRecord == null) {
1583 // This activity is not being started from another... in this
1584 // case we -always- start a new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001585 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Craig Mautner29219d92013-04-16 20:19:12 -07001586 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1587 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001588 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1589 }
1590 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1591 // The original activity who is starting us is running as a single
1592 // instance... this new activity it is starting must go on its
1593 // own task.
1594 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1595 } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
1596 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1597 // The activity being started is a single instance... it always
1598 // gets launched into its own task.
1599 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1600 }
1601
Craig Mautner88629292013-11-10 20:39:05 -08001602 ActivityInfo newTaskInfo = null;
1603 Intent newTaskIntent = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001604 final ActivityStack sourceStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001605 if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001606 if (sourceRecord.finishing) {
1607 // If the source is finishing, we can't further count it as our source. This
1608 // is because the task it is associated with may now be empty and on its way out,
1609 // so we don't want to blindly throw it in to that task. Instead we will take
Craig Mautner88629292013-11-10 20:39:05 -08001610 // the NEW_TASK flow and try to find a task for it. But save the task information
1611 // so it can be used when creating the new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001612 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001613 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1614 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1615 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
Craig Mautner88629292013-11-10 20:39:05 -08001616 newTaskInfo = sourceRecord.info;
1617 newTaskIntent = sourceRecord.task.intent;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001618 }
1619 sourceRecord = null;
1620 sourceStack = null;
1621 } else {
1622 sourceStack = sourceRecord.task.stack;
1623 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001624 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001625 sourceStack = null;
1626 }
1627
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001628 intent.setFlags(launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001629
1630 boolean addingToTask = false;
1631 boolean movedHome = false;
1632 TaskRecord reuseTask = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001633 ActivityStack targetStack;
Craig Mautnerd00f4742014-03-12 14:17:26 -07001634 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1635 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001636 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1637 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1638 // If bring to front is requested, and no result is requested, and
1639 // we can find a task that was started with this same
1640 // component, then instead of launching bring that one to the front.
1641 if (r.resultTo == null) {
1642 // See if there is a task to bring to the front. If this is
1643 // a SINGLE_INSTANCE activity, there can be one and only one
1644 // instance of it in the history, and it is always in its own
1645 // unique task, so we do a special search.
1646 ActivityRecord intentActivity = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
Craig Mautnerac6f8432013-07-17 13:24:59 -07001647 ? findTaskLocked(r)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001648 : findActivityLocked(intent, r.info);
1649 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001650 if (isLockTaskModeViolation(intentActivity.task)) {
1651 Slog.e(TAG, "moveTaskToFront: Attempt to violate Lock Task Mode");
1652 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1653 }
Craig Mautner29219d92013-04-16 20:19:12 -07001654 if (r.task == null) {
1655 r.task = intentActivity.task;
1656 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001657 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001658 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001659 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1660 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001661 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001662 if (intentActivity.task.intent == null) {
1663 // This task was started because of movement of
1664 // the activity based on affinity... now that we
1665 // are actually launching it, we can assign the
1666 // base intent.
1667 intentActivity.task.setIntent(intent, r.info);
1668 }
1669 // If the target task is not in the front, then we need
1670 // to bring it to the front... except... well, with
1671 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1672 // to have the same behavior as if a new instance was
1673 // being started, which means not bringing it to the front
1674 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001675 final ActivityStack lastStack = getLastStack();
1676 ActivityRecord curTop = lastStack == null?
1677 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner7504d7b2013-09-17 10:50:53 -07001678 if (curTop != null && (curTop.task != intentActivity.task ||
1679 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001680 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001681 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1682 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001683 // We really do want to push this one into the
1684 // user's face, right now.
1685 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001686 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001687 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001688 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1689 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001690 // Caller wants to appear on home activity.
Craig Mautner719e6212014-05-29 16:49:41 +00001691 intentActivity.task.mOnTopOfHome = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001692 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001693 options = null;
1694 }
1695 }
1696 // If the caller has requested that the target task be
1697 // reset, then do so.
1698 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1699 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1700 }
1701 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1702 // We don't need to start a new activity, and
1703 // the client said not to do anything if that
1704 // is the case, so this is it! And for paranoia, make
1705 // sure we have correctly resumed the top activity.
1706 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001707 resumeTopActivitiesLocked(targetStack, null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001708 } else {
1709 ActivityOptions.abort(options);
1710 }
1711 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1712 }
1713 if ((launchFlags &
1714 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1715 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1716 // The caller has requested to completely replace any
1717 // existing task with its new activity. Well that should
1718 // not be too hard...
1719 reuseTask = intentActivity.task;
1720 reuseTask.performClearTaskLocked();
1721 reuseTask.setIntent(r.intent, r.info);
1722 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
1723 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1724 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1725 // In this situation we want to remove all activities
1726 // from the task up to the one being started. In most
1727 // cases this means we are resetting the task to its
1728 // initial state.
1729 ActivityRecord top =
1730 intentActivity.task.performClearTaskLocked(r, launchFlags);
1731 if (top != null) {
1732 if (top.frontOfTask) {
1733 // Activity aliases may mean we use different
1734 // intents for the top activity, so make sure
1735 // the task now has the identity of the new
1736 // intent.
1737 top.task.setIntent(r.intent, r.info);
1738 }
1739 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1740 r, top.task);
1741 top.deliverNewIntentLocked(callingUid, r.intent);
1742 } else {
1743 // A special case: we need to
1744 // start the activity because it is not currently
1745 // running, and the caller has asked to clear the
1746 // current task to have this activity at the top.
1747 addingToTask = true;
1748 // Now pretend like this activity is being started
1749 // by the top of its task, so it is put in the
1750 // right place.
1751 sourceRecord = intentActivity;
1752 }
1753 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1754 // In this case the top activity on the task is the
1755 // same as the one being launched, so we take that
1756 // as a request to bring the task to the foreground.
1757 // If the top activity in the task is the root
1758 // activity, deliver this new intent to it if it
1759 // desires.
1760 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
1761 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
1762 && intentActivity.realActivity.equals(r.realActivity)) {
1763 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1764 intentActivity.task);
1765 if (intentActivity.frontOfTask) {
1766 intentActivity.task.setIntent(r.intent, r.info);
1767 }
1768 intentActivity.deliverNewIntentLocked(callingUid, r.intent);
1769 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1770 // In this case we are launching the root activity
1771 // of the task, but with a different intent. We
1772 // should start a new instance on top.
1773 addingToTask = true;
1774 sourceRecord = intentActivity;
1775 }
1776 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1777 // In this case an activity is being launched in to an
1778 // existing task, without resetting that task. This
1779 // is typically the situation of launching an activity
1780 // from a notification or shortcut. We want to place
1781 // the new activity on top of the current task.
1782 addingToTask = true;
1783 sourceRecord = intentActivity;
1784 } else if (!intentActivity.task.rootWasReset) {
1785 // In this case we are launching in to an existing task
1786 // that has not yet been started from its front door.
1787 // The current task has been brought to the front.
1788 // Ideally, we'd probably like to place this new task
1789 // at the bottom of its stack, but that's a little hard
1790 // to do with the current organization of the code so
1791 // for now we'll just drop it.
1792 intentActivity.task.setIntent(r.intent, r.info);
1793 }
1794 if (!addingToTask && reuseTask == null) {
1795 // We didn't do anything... but it was needed (a.k.a., client
1796 // don't use that intent!) And for paranoia, make
1797 // sure we have correctly resumed the top activity.
1798 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001799 targetStack.resumeTopActivityLocked(null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001800 } else {
1801 ActivityOptions.abort(options);
1802 }
1803 return ActivityManager.START_TASK_TO_FRONT;
1804 }
1805 }
1806 }
1807 }
1808
1809 //String uri = r.intent.toURI();
1810 //Intent intent2 = new Intent(uri);
1811 //Slog.i(TAG, "Given intent: " + r.intent);
1812 //Slog.i(TAG, "URI is: " + uri);
1813 //Slog.i(TAG, "To intent: " + intent2);
1814
1815 if (r.packageName != null) {
1816 // If the activity being launched is the same as the one currently
1817 // at the top, then we need to check if it should only be launched
1818 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001819 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001820 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001821 if (top != null && r.resultTo == null) {
1822 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
1823 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07001824 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautner8849a5e2013-04-02 16:41:03 -07001825 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP
1826 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1827 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
1828 top.task);
1829 // For paranoia, make sure we have correctly
1830 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001831 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001832 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001833 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001834 }
1835 ActivityOptions.abort(options);
1836 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1837 // We don't need to start a new activity, and
1838 // the client said not to do anything if that
1839 // is the case, so this is it!
1840 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1841 }
1842 top.deliverNewIntentLocked(callingUid, r.intent);
1843 return ActivityManager.START_DELIVERED_TO_TOP;
1844 }
1845 }
1846 }
1847 }
1848
1849 } else {
1850 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001851 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
1852 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001853 }
1854 ActivityOptions.abort(options);
1855 return ActivityManager.START_CLASS_NOT_FOUND;
1856 }
1857
1858 boolean newTask = false;
1859 boolean keepCurTransition = false;
1860
1861 // Should this be considered a new task?
1862 if (r.resultTo == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001863 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001864 if (isLockTaskModeViolation(reuseTask)) {
1865 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1866 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1867 }
Craig Mautner1b4bf852014-05-26 15:06:32 -07001868 newTask = true;
1869 targetStack = adjustStackFocus(r, newTask);
Craig Mautnere0a38842013-12-16 16:14:02 -08001870 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001871 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08001872 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
1873 newTaskInfo != null ? newTaskInfo : r.info,
1874 newTaskIntent != null ? newTaskIntent : intent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001875 voiceSession, voiceInteractor, true), null, true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001876 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
1877 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001878 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001879 r.setTask(reuseTask, reuseTask, true);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001880 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001881 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001882 if ((launchFlags &
1883 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
1884 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
1885 // Caller wants to appear on home activity, so before starting
1886 // their own activity we will bring home to the front.
Craig Mautner719e6212014-05-29 16:49:41 +00001887 r.task.mOnTopOfHome = r.task.stack.isOnHomeDisplay();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001888 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001889 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001890 } else if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001891 TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08001892 if (isLockTaskModeViolation(sourceTask)) {
1893 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1894 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1895 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001896 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07001897 targetStack.moveToFront();
Craig Mautnerabcc59c2014-05-17 15:13:28 -07001898 mWindowManager.moveTaskToTop(targetStack.topTask().taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001899 if (!addingToTask &&
1900 (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
1901 // In this case, we are adding the activity to an existing
1902 // task, but the caller has asked to clear that task if the
1903 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07001904 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001905 keepCurTransition = true;
1906 if (top != null) {
1907 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
1908 top.deliverNewIntentLocked(callingUid, r.intent);
1909 // For paranoia, make sure we have correctly
1910 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001911 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001912 if (doResume) {
1913 targetStack.resumeTopActivityLocked(null);
1914 }
1915 ActivityOptions.abort(options);
1916 return ActivityManager.START_DELIVERED_TO_TOP;
1917 }
1918 } else if (!addingToTask &&
1919 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
1920 // In this case, we are launching an activity in our own task
1921 // that may already be running somewhere in the history, and
1922 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07001923 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001924 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001925 final TaskRecord task = top.task;
1926 task.moveActivityToFrontLocked(top);
1927 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001928 top.updateOptionsLocked(options);
1929 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner0f922742013-08-06 08:44:42 -07001930 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001931 if (doResume) {
1932 targetStack.resumeTopActivityLocked(null);
1933 }
1934 return ActivityManager.START_DELIVERED_TO_TOP;
1935 }
1936 }
1937 // An existing activity is starting this new activity, so we want
1938 // to keep the new one in the same task as the one that is starting
1939 // it.
Craig Mautner525f3d92013-05-07 14:01:50 -07001940 r.setTask(sourceTask, sourceRecord.thumbHolder, false);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001941 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001942 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001943
1944 } else {
1945 // This not being started from an existing activity, and not part
1946 // of a new task... just put it in the top task, though these days
1947 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07001948 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerdd221b32014-03-19 19:53:45 -07001949 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07001950 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001951 r.setTask(prev != null ? prev.task
Dianne Hackborn91097de2014-04-04 18:02:06 -07001952 : targetStack.createTaskRecord(getNextTaskId(), r.info, intent, null, null, true),
Craig Mautnerde4ef022013-04-07 19:01:33 -07001953 null, true);
Craig Mautner95e9daa2014-03-17 15:57:20 -07001954 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001955 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
1956 + " in new guessed " + r.task);
1957 }
1958
1959 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001960 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001961
1962 if (newTask) {
1963 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
1964 }
1965 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07001966 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001967 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautner1d001b62013-06-18 16:52:43 -07001968 mService.setFocusedActivityLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001969 return ActivityManager.START_SUCCESS;
1970 }
1971
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001972 void acquireLaunchWakelock() {
1973 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1974 throw new IllegalStateException("Calling must be system uid");
1975 }
1976 mLaunchingActivity.acquire();
1977 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1978 // To be safe, don't allow the wake lock to be held for too long.
1979 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1980 }
1981 }
1982
Craig Mautnerf3333272013-04-22 10:55:53 -07001983 // Checked.
1984 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1985 Configuration config) {
1986 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
1987
Craig Mautnerf3333272013-04-22 10:55:53 -07001988 ArrayList<ActivityRecord> stops = null;
1989 ArrayList<ActivityRecord> finishes = null;
1990 ArrayList<UserStartedState> startingUsers = null;
1991 int NS = 0;
1992 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001993 boolean booting = false;
1994 boolean enableScreen = false;
1995 boolean activityRemoved = false;
1996
1997 ActivityRecord r = ActivityRecord.forToken(token);
1998 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001999 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2000 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002001 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2002 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002003 if (fromTimeout) {
2004 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002005 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002006
2007 // This is a hack to semi-deal with a race condition
2008 // in the client where it can be constructed with a
2009 // newer configuration from when we asked it to launch.
2010 // We'll update with whatever configuration it now says
2011 // it used to launch.
2012 if (config != null) {
2013 r.configuration = config;
2014 }
2015
2016 // We are now idle. If someone is waiting for a thumbnail from
2017 // us, we can now deliver.
2018 r.idle = true;
2019
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002020 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
2021 if (!mService.mBooted && isFrontStack(r.task.stack)) {
2022 mService.mBooted = true;
2023 enableScreen = true;
2024 }
2025 }
2026
2027 if (allResumedActivitiesIdle()) {
2028 if (r != null) {
2029 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002030 }
2031
2032 if (mLaunchingActivity.isHeld()) {
2033 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2034 if (VALIDATE_WAKE_LOCK_CALLER &&
2035 Binder.getCallingUid() != Process.myUid()) {
2036 throw new IllegalStateException("Calling must be system uid");
2037 }
2038 mLaunchingActivity.release();
2039 }
2040 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002041 }
2042
2043 // Atomically retrieve all of the other things to do.
2044 stops = processStoppingActivitiesLocked(true);
2045 NS = stops != null ? stops.size() : 0;
2046 if ((NF=mFinishingActivities.size()) > 0) {
2047 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2048 mFinishingActivities.clear();
2049 }
2050
Craig Mautnerf3333272013-04-22 10:55:53 -07002051 if (isFrontStack(mHomeStack)) {
2052 booting = mService.mBooting;
2053 mService.mBooting = false;
2054 }
2055
2056 if (mStartingUsers.size() > 0) {
2057 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2058 mStartingUsers.clear();
2059 }
2060
Craig Mautnerf3333272013-04-22 10:55:53 -07002061 // Stop any activities that are scheduled to do so but have been
2062 // waiting for the next one to start.
2063 for (int i = 0; i < NS; i++) {
2064 r = stops.get(i);
2065 final ActivityStack stack = r.task.stack;
2066 if (r.finishing) {
2067 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2068 } else {
2069 stack.stopActivityLocked(r);
2070 }
2071 }
2072
2073 // Finish any activities that are scheduled to do so but have been
2074 // waiting for the next one to start.
2075 for (int i = 0; i < NF; i++) {
2076 r = finishes.get(i);
2077 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, false, "finish-idle");
2078 }
2079
2080 if (booting) {
2081 mService.finishBooting();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002082 } else {
2083 // Complete user switch
2084 if (startingUsers != null) {
2085 for (int i = 0; i < startingUsers.size(); i++) {
2086 mService.finishUserSwitch(startingUsers.get(i));
2087 }
2088 }
2089 // Complete starting up of background users
2090 if (mStartingBackgroundUsers.size() > 0) {
2091 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2092 mStartingBackgroundUsers.clear();
2093 for (int i = 0; i < startingUsers.size(); i++) {
2094 mService.finishUserBoot(startingUsers.get(i));
2095 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002096 }
2097 }
2098
2099 mService.trimApplications();
2100 //dump();
2101 //mWindowManager.dump();
2102
2103 if (enableScreen) {
2104 mService.enableScreenAfterBoot();
2105 }
2106
2107 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002108 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002109 }
2110
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002111 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002112 }
2113
Craig Mautner8e569572013-10-11 17:36:59 -07002114 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002115 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002116 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2117 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002118 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2119 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2120 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002121 }
Craig Mautner19091252013-10-05 00:03:53 -07002122 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002123 }
2124
2125 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002126 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2127 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002128 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2129 stacks.get(stackNdx).closeSystemDialogsLocked();
2130 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002131 }
2132 }
2133
Craig Mautner93529a42013-10-04 15:03:13 -07002134 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002135 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002136 }
2137
Craig Mautner8d341ef2013-03-26 09:03:27 -07002138 /**
2139 * @return true if some activity was finished (or would have finished if doit were true).
2140 */
2141 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2142 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002143 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2144 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002145 final int numStacks = stacks.size();
2146 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2147 final ActivityStack stack = stacks.get(stackNdx);
2148 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2149 didSomething = true;
2150 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002151 }
2152 }
2153 return didSomething;
2154 }
2155
Dianne Hackborna413dc02013-07-12 12:02:55 -07002156 void updatePreviousProcessLocked(ActivityRecord r) {
2157 // Now that this process has stopped, we may want to consider
2158 // it to be the previous app to try to keep around in case
2159 // the user wants to return to it.
2160
2161 // First, found out what is currently the foreground app, so that
2162 // we don't blow away the previous app if this activity is being
2163 // hosted by the process that is actually still the foreground.
2164 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002165 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2166 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002167 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2168 final ActivityStack stack = stacks.get(stackNdx);
2169 if (isFrontStack(stack)) {
2170 if (stack.mResumedActivity != null) {
2171 fgApp = stack.mResumedActivity.app;
2172 } else if (stack.mPausingActivity != null) {
2173 fgApp = stack.mPausingActivity.app;
2174 }
2175 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002176 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002177 }
2178 }
2179
2180 // Now set this one as the previous process, only if that really
2181 // makes sense to.
2182 if (r.app != null && fgApp != null && r.app != fgApp
2183 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002184 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002185 mService.mPreviousProcess = r.app;
2186 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2187 }
2188 }
2189
Craig Mautner05d29032013-05-03 13:40:13 -07002190 boolean resumeTopActivitiesLocked() {
2191 return resumeTopActivitiesLocked(null, null, null);
2192 }
2193
2194 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2195 Bundle targetOptions) {
2196 if (targetStack == null) {
2197 targetStack = getFocusedStack();
2198 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002199 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002200 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002201 if (isFrontStack(targetStack)) {
2202 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2203 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002204 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2205 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002206 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2207 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002208 if (stack == targetStack) {
2209 // Already started above.
2210 continue;
2211 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002212 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002213 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002214 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002215 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002216 }
Craig Mautner05d29032013-05-03 13:40:13 -07002217 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002218 }
2219
2220 void finishTopRunningActivityLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002221 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2222 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002223 final int numStacks = stacks.size();
2224 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2225 final ActivityStack stack = stacks.get(stackNdx);
2226 stack.finishTopRunningActivityLocked(app);
2227 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002228 }
2229 }
2230
Craig Mautneraea74a52014-03-08 14:23:10 -08002231 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2232 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2233 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002234 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002235 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2236 // Caller wants the home activity moved with it. To accomplish this,
2237 // we'll just indicate that this task returns to the home task.
Craig Mautner719e6212014-05-29 16:49:41 +00002238 task.mOnTopOfHome = true;
Craig Mautneraea74a52014-03-08 14:23:10 -08002239 }
2240 task.stack.moveTaskToFrontLocked(task, null, options);
2241 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2242 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002243 }
2244
Craig Mautner967212c2013-04-13 21:10:58 -07002245 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002246 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2247 if (activityContainer != null) {
2248 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002249 }
2250 return null;
2251 }
2252
Craig Mautner967212c2013-04-13 21:10:58 -07002253 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002254 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002255 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2256 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002257 }
2258 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002259 }
2260
Craig Mautner4a1cb222013-12-04 16:14:06 -08002261 IBinder getHomeActivityToken() {
2262 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2263 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2264 final TaskRecord task = tasks.get(taskNdx);
2265 if (task.isHomeTask()) {
2266 final ArrayList<ActivityRecord> activities = task.mActivities;
2267 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2268 final ActivityRecord r = activities.get(activityNdx);
2269 if (r.isHomeActivity()) {
2270 return r.appToken;
2271 }
2272 }
2273 }
2274 }
2275 return null;
2276 }
2277
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002278 ActivityContainer createActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002279 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002280 ActivityContainer activityContainer =
2281 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002282 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002283 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002284 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002285 return activityContainer;
2286 }
2287
Craig Mautner34b73df2014-01-12 21:11:08 -08002288 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002289 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2290 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2291 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002292 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2293 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002294 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002295 }
2296 }
2297
Craig Mautner95da1082014-02-24 17:54:35 -08002298 void deleteActivityContainer(IActivityContainer container) {
2299 ActivityContainer activityContainer = (ActivityContainer)container;
2300 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002301 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2302 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002303 final int stackId = activityContainer.mStackId;
2304 mActivityContainers.remove(stackId);
2305 mWindowManager.removeStack(stackId);
2306 }
2307 }
2308
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002309 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002310 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2311 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002312 return -1;
2313 }
2314
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002315 ActivityContainer activityContainer = new ActivityContainer(stackId);
2316 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002317 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002318 return stackId;
2319 }
2320
2321 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002322 while (true) {
2323 if (++mLastStackId <= HOME_STACK_ID) {
2324 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002325 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002326 if (getStack(mLastStackId) == null) {
2327 break;
2328 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002329 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002330 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002331 }
2332
Craig Mautneref73ee12014-04-23 11:45:37 -07002333 void createStackForRestoredTaskHistory(ArrayList<TaskRecord> tasks) {
2334 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
2335 final ActivityStack stack = getStack(stackId);
2336 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2337 final TaskRecord task = tasks.get(taskNdx);
2338 stack.addTask(task, false, false);
2339 final int taskId = task.taskId;
2340 final ArrayList<ActivityRecord> activities = task.mActivities;
2341 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2342 final ActivityRecord r = activities.get(activityNdx);
2343 mWindowManager.addAppToken(0, r.appToken, taskId, stackId,
2344 r.info.screenOrientation, r.fullscreen,
2345 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Dianne Hackborne30e02f2014-05-27 18:24:45 -07002346 r.userId, r.info.configChanges, task.voiceSession != null);
Craig Mautneref73ee12014-04-23 11:45:37 -07002347 }
2348 mWindowManager.addTask(taskId, stackId, false);
2349 }
Craig Mautner719e6212014-05-29 16:49:41 +00002350 resumeHomeActivity(null);
Craig Mautneref73ee12014-04-23 11:45:37 -07002351 }
2352
Craig Mautner8d341ef2013-03-26 09:03:27 -07002353 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002354 final TaskRecord task = anyTaskForIdLocked(taskId);
2355 if (task == null) {
2356 return;
2357 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002358 final ActivityStack stack = getStack(stackId);
2359 if (stack == null) {
2360 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2361 return;
2362 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002363 task.stack.removeTask(task);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002364 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002365 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002366 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002367 }
2368
Craig Mautnerac6f8432013-07-17 13:24:59 -07002369 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002370 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002371 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2372 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002373 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2374 final ActivityStack stack = stacks.get(stackNdx);
2375 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002376 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2377 continue;
2378 }
2379 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2380 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2381 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002382 continue;
2383 }
2384 final ActivityRecord ar = stack.findTaskLocked(r);
2385 if (ar != null) {
2386 return ar;
2387 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002388 }
2389 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002390 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002391 return null;
2392 }
2393
2394 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002395 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2396 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002397 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2398 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2399 if (ar != null) {
2400 return ar;
2401 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002402 }
2403 }
2404 return null;
2405 }
2406
Craig Mautner8d341ef2013-03-26 09:03:27 -07002407 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002408 scheduleSleepTimeout();
2409 if (!mGoingToSleep.isHeld()) {
2410 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002411 if (mLaunchingActivity.isHeld()) {
2412 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2413 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002414 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002415 mLaunchingActivity.release();
2416 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002417 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002418 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002419 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002420 }
2421
2422 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002423 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002424
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002425 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002426 final long endTime = System.currentTimeMillis() + timeout;
2427 while (true) {
2428 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002429 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2430 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002431 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2432 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2433 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002434 }
2435 if (cantShutdown) {
2436 long timeRemaining = endTime - System.currentTimeMillis();
2437 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002438 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002439 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002440 } catch (InterruptedException e) {
2441 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002442 } else {
2443 Slog.w(TAG, "Activity manager shutdown timed out");
2444 timedout = true;
2445 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002446 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002447 } else {
2448 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002449 }
2450 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002451
2452 // Force checkReadyForSleep to complete.
2453 mSleepTimeout = true;
2454 checkReadyForSleepLocked();
2455
Craig Mautner8d341ef2013-03-26 09:03:27 -07002456 return timedout;
2457 }
2458
2459 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002460 removeSleepTimeouts();
2461 if (mGoingToSleep.isHeld()) {
2462 mGoingToSleep.release();
2463 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002464 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2465 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002466 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2467 final ActivityStack stack = stacks.get(stackNdx);
2468 stack.awakeFromSleepingLocked();
2469 if (isFrontStack(stack)) {
2470 resumeTopActivitiesLocked();
2471 }
Craig Mautner5314a402013-09-26 12:40:16 -07002472 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002473 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002474 mGoingToSleepActivities.clear();
2475 }
2476
2477 void activitySleptLocked(ActivityRecord r) {
2478 mGoingToSleepActivities.remove(r);
2479 checkReadyForSleepLocked();
2480 }
2481
2482 void checkReadyForSleepLocked() {
2483 if (!mService.isSleepingOrShuttingDown()) {
2484 // Do not care.
2485 return;
2486 }
2487
2488 if (!mSleepTimeout) {
2489 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002490 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2491 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002492 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2493 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2494 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002495 }
2496
2497 if (mStoppingActivities.size() > 0) {
2498 // Still need to tell some activities to stop; can't sleep yet.
2499 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2500 + mStoppingActivities.size() + " activities");
2501 scheduleIdleLocked();
2502 dontSleep = true;
2503 }
2504
2505 if (mGoingToSleepActivities.size() > 0) {
2506 // Still need to tell some activities to sleep; can't sleep yet.
2507 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2508 + mGoingToSleepActivities.size() + " activities");
2509 dontSleep = true;
2510 }
2511
2512 if (dontSleep) {
2513 return;
2514 }
2515 }
2516
Craig Mautnere0a38842013-12-16 16:14:02 -08002517 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2518 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002519 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2520 stacks.get(stackNdx).goToSleep();
2521 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002522 }
2523
2524 removeSleepTimeouts();
2525
2526 if (mGoingToSleep.isHeld()) {
2527 mGoingToSleep.release();
2528 }
2529 if (mService.mShuttingDown) {
2530 mService.notifyAll();
2531 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002532 }
2533
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002534 boolean reportResumedActivityLocked(ActivityRecord r) {
2535 final ActivityStack stack = r.task.stack;
2536 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002537 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002538 }
2539 if (allResumedActivitiesComplete()) {
2540 ensureActivitiesVisibleLocked(null, 0);
2541 mWindowManager.executeAppTransition();
2542 return true;
2543 }
2544 return false;
2545 }
2546
Craig Mautner8d341ef2013-03-26 09:03:27 -07002547 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002548 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2549 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002550 final int numStacks = stacks.size();
2551 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2552 final ActivityStack stack = stacks.get(stackNdx);
2553 stack.handleAppCrashLocked(app);
2554 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002555 }
2556 }
2557
Craig Mautnerde4ef022013-04-07 19:01:33 -07002558 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002559 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002560 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2561 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002562 final int topStackNdx = stacks.size() - 1;
2563 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2564 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002565 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002566 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002567 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002568 }
2569
2570 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002571 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2572 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002573 final int numStacks = stacks.size();
2574 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2575 final ActivityStack stack = stacks.get(stackNdx);
2576 stack.scheduleDestroyActivities(app, false, reason);
2577 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002578 }
2579 }
2580
2581 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002582 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
2583 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002584 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002585
Craig Mautner858d8a62013-04-23 17:08:34 -07002586 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002587 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2588 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002589 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002590 final ActivityStack stack = stacks.get(stackNdx);
2591 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002592 TaskRecord task = stack.topTask();
2593 if (task != null) {
2594 mWindowManager.moveTaskToTop(task.taskId);
2595 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002596 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002597 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002598
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002599 ActivityStack stack = getStack(restoreStackId);
2600 if (stack == null) {
2601 stack = mHomeStack;
2602 }
2603 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002604 if (stack.isOnHomeDisplay()) {
2605 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002606 TaskRecord task = stack.topTask();
2607 if (task != null) {
2608 mWindowManager.moveTaskToTop(task.taskId);
2609 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002610 } else {
2611 // Stack was moved to another display while user was swapped out.
Craig Mautner719e6212014-05-29 16:49:41 +00002612 resumeHomeActivity(null);
Craig Mautnere0a38842013-12-16 16:14:02 -08002613 }
Craig Mautner93529a42013-10-04 15:03:13 -07002614 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002615 }
2616
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002617 /**
2618 * Add background users to send boot completed events to.
2619 * @param userId The user being started in the background
2620 * @param uss The state object for the user.
2621 */
2622 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
2623 mStartingBackgroundUsers.add(uss);
2624 }
2625
Craig Mautnerde4ef022013-04-07 19:01:33 -07002626 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
2627 int N = mStoppingActivities.size();
2628 if (N <= 0) return null;
2629
2630 ArrayList<ActivityRecord> stops = null;
2631
2632 final boolean nowVisible = allResumedActivitiesVisible();
2633 for (int i=0; i<N; i++) {
2634 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07002635 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07002636 + nowVisible + " waitingVisible=" + s.waitingVisible
2637 + " finishing=" + s.finishing);
2638 if (s.waitingVisible && nowVisible) {
2639 mWaitingVisibleActivities.remove(s);
2640 s.waitingVisible = false;
2641 if (s.finishing) {
2642 // If this activity is finishing, it is sitting on top of
2643 // everyone else but we now know it is no longer needed...
2644 // so get rid of it. Otherwise, we need to go through the
2645 // normal flow and hide it once we determine that it is
2646 // hidden by the activities in front of it.
2647 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002648 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002649 }
2650 }
2651 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
2652 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
2653 if (stops == null) {
2654 stops = new ArrayList<ActivityRecord>();
2655 }
2656 stops.add(s);
2657 mStoppingActivities.remove(i);
2658 N--;
2659 i--;
2660 }
2661 }
2662
2663 return stops;
2664 }
2665
Craig Mautnercf910b02013-04-23 11:23:27 -07002666 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002667 // FIXME
2668/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2669 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07002670 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002671 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07002672 if (isFrontStack(stack)) {
2673 if (r == null) {
2674 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
2675 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002676 final ActivityRecord pausing = stack.mPausingActivity;
2677 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002678 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002679 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002680 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002681 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002682 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002683 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002684 }
2685 }
2686 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002687 final ActivityRecord resumed = stack.mResumedActivity;
2688 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002689 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002690 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002691 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002692 if (r != null && (state == ActivityState.INITIALIZING
2693 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002694 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002695 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002696 }
2697 }
2698 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002699*/
Craig Mautner76ea2242013-05-15 11:40:05 -07002700 }
2701
Craig Mautner27084302013-03-25 08:05:25 -07002702 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002703 pw.print(prefix); pw.print("mDismissKeyguardOnNextActivity=");
Craig Mautner27084302013-03-25 08:05:25 -07002704 pw.println(mDismissKeyguardOnNextActivity);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002705 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002706 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002707 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2708 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2709 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002710 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07002711 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002712
Craig Mautner20e72272013-04-01 13:45:53 -07002713 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002714 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002715 }
2716
Dianne Hackborn390517b2013-05-30 15:03:32 -07002717 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2718 boolean needSep, String prefix) {
2719 if (activity != null) {
2720 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2721 if (needSep) {
2722 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002723 }
2724 pw.print(prefix);
2725 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002726 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002727 }
2728 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002729 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002730 }
2731
Craig Mautner8d341ef2013-03-26 09:03:27 -07002732 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2733 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002734 boolean printed = false;
2735 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002736 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2737 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
2738 pw.print("Display #"); pw.println(activityDisplay.mDisplayId);
2739 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002740 final int numStacks = stacks.size();
2741 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2742 final ActivityStack stack = stacks.get(stackNdx);
2743 StringBuilder stackHeader = new StringBuilder(128);
2744 stackHeader.append(" Stack #");
2745 stackHeader.append(stack.mStackId);
2746 stackHeader.append(":");
2747 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2748 needSep, stackHeader.toString());
2749 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2750 !dumpAll, false, dumpPackage, true,
2751 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002752
Craig Mautner4a1cb222013-12-04 16:14:06 -08002753 needSep = printed;
2754 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2755 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002756 if (pr) {
2757 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002758 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002759 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002760 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2761 " mResumedActivity: ");
2762 if (pr) {
2763 printed = true;
2764 needSep = false;
2765 }
2766 if (dumpAll) {
2767 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2768 " mLastPausedActivity: ");
2769 if (pr) {
2770 printed = true;
2771 needSep = true;
2772 }
2773 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2774 needSep, " mLastNoHistoryActivity: ");
2775 }
2776 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002777 }
2778 }
2779
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002780 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2781 false, dumpPackage, true, " Activities waiting to finish:", null);
2782 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2783 false, dumpPackage, true, " Activities waiting to stop:", null);
2784 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2785 false, dumpPackage, true, " Activities waiting for another to become visible:",
2786 null);
2787 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2788 false, dumpPackage, true, " Activities waiting to sleep:", null);
2789 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2790 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002791
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002792 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002793 }
2794
Dianne Hackborn390517b2013-05-30 15:03:32 -07002795 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002796 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002797 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002798 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002799 String innerPrefix = null;
2800 String[] args = null;
2801 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002802 for (int i=list.size()-1; i>=0; i--) {
2803 final ActivityRecord r = list.get(i);
2804 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2805 continue;
2806 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002807 if (innerPrefix == null) {
2808 innerPrefix = prefix + " ";
2809 args = new String[0];
2810 }
2811 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002812 final boolean full = !brief && (complete || !r.isInHistory());
2813 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002814 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002815 needNL = false;
2816 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002817 if (header1 != null) {
2818 pw.println(header1);
2819 header1 = null;
2820 }
2821 if (header2 != null) {
2822 pw.println(header2);
2823 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002824 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002825 if (lastTask != r.task) {
2826 lastTask = r.task;
2827 pw.print(prefix);
2828 pw.print(full ? "* " : " ");
2829 pw.println(lastTask);
2830 if (full) {
2831 lastTask.dump(pw, prefix + " ");
2832 } else if (complete) {
2833 // Complete + brief == give a summary. Isn't that obvious?!?
2834 if (lastTask.intent != null) {
2835 pw.print(prefix); pw.print(" ");
2836 pw.println(lastTask.intent.toInsecureStringWithClip());
2837 }
2838 }
2839 }
2840 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2841 pw.print(" #"); pw.print(i); pw.print(": ");
2842 pw.println(r);
2843 if (full) {
2844 r.dump(pw, innerPrefix);
2845 } else if (complete) {
2846 // Complete + brief == give a summary. Isn't that obvious?!?
2847 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2848 if (r.app != null) {
2849 pw.print(innerPrefix); pw.println(r.app);
2850 }
2851 }
2852 if (client && r.app != null && r.app.thread != null) {
2853 // flush anything that is already in the PrintWriter since the thread is going
2854 // to write to the file descriptor directly
2855 pw.flush();
2856 try {
2857 TransferPipe tp = new TransferPipe();
2858 try {
2859 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2860 r.appToken, innerPrefix, args);
2861 // Short timeout, since blocking here can
2862 // deadlock with the application.
2863 tp.go(fd, 2000);
2864 } finally {
2865 tp.kill();
2866 }
2867 } catch (IOException e) {
2868 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2869 } catch (RemoteException e) {
2870 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2871 }
2872 needNL = true;
2873 }
2874 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002875 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002876 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002877
Craig Mautnerf3333272013-04-22 10:55:53 -07002878 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002879 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002880 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2881 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002882 }
2883
2884 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002885 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002886 }
2887
2888 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002889 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002890 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2891 }
2892
Craig Mautner05d29032013-05-03 13:40:13 -07002893 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08002894 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
2895 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2896 }
Craig Mautner05d29032013-05-03 13:40:13 -07002897 }
2898
Craig Mautner0eea92c2013-05-16 13:35:39 -07002899 void removeSleepTimeouts() {
2900 mSleepTimeout = false;
2901 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2902 }
2903
2904 final void scheduleSleepTimeout() {
2905 removeSleepTimeouts();
2906 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2907 }
2908
Craig Mautner4a1cb222013-12-04 16:14:06 -08002909 @Override
2910 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002911 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002912 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
2913 }
2914
2915 @Override
2916 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002917 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002918 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
2919 }
2920
2921 @Override
2922 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002923 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002924 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
2925 }
2926
2927 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08002928 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002929 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08002930 newDisplay = mActivityDisplays.get(displayId) == null;
2931 if (newDisplay) {
2932 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
2933 mActivityDisplays.put(displayId, activityDisplay);
2934 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002935 }
Craig Mautner4504de52013-12-20 09:06:56 -08002936 if (newDisplay) {
2937 mWindowManager.onDisplayAdded(displayId);
2938 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002939 }
2940
2941 public void handleDisplayRemovedLocked(int displayId) {
2942 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002943 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2944 if (activityDisplay != null) {
2945 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002946 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002947 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002948 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002949 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002950 }
2951 }
2952 mWindowManager.onDisplayRemoved(displayId);
2953 }
2954
2955 public void handleDisplayChangedLocked(int displayId) {
2956 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002957 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2958 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002959 // TODO: Update the bounds.
2960 }
2961 }
2962 mWindowManager.onDisplayChanged(displayId);
2963 }
2964
2965 StackInfo getStackInfo(ActivityStack stack) {
2966 StackInfo info = new StackInfo();
2967 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
2968 info.displayId = Display.DEFAULT_DISPLAY;
2969 info.stackId = stack.mStackId;
2970
2971 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2972 final int numTasks = tasks.size();
2973 int[] taskIds = new int[numTasks];
2974 String[] taskNames = new String[numTasks];
2975 for (int i = 0; i < numTasks; ++i) {
2976 final TaskRecord task = tasks.get(i);
2977 taskIds[i] = task.taskId;
2978 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
2979 : task.realActivity != null ? task.realActivity.flattenToString()
2980 : task.getTopActivity() != null ? task.getTopActivity().packageName
2981 : "unknown";
2982 }
2983 info.taskIds = taskIds;
2984 info.taskNames = taskNames;
2985 return info;
2986 }
2987
2988 StackInfo getStackInfoLocked(int stackId) {
2989 ActivityStack stack = getStack(stackId);
2990 if (stack != null) {
2991 return getStackInfo(stack);
2992 }
2993 return null;
2994 }
2995
2996 ArrayList<StackInfo> getAllStackInfosLocked() {
2997 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002998 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2999 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003000 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3001 list.add(getStackInfo(stacks.get(ndx)));
3002 }
3003 }
3004 return list;
3005 }
3006
Craig Mautneraea74a52014-03-08 14:23:10 -08003007 void setLockTaskModeLocked(TaskRecord task) {
3008 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003009 // Take out of lock task mode if necessary
3010 if (mLockTaskModeTask != null) {
3011 final Message lockTaskMsg = Message.obtain();
3012 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3013 lockTaskMsg.what = LOCK_TASK_END_MSG;
3014 mLockTaskModeTask = null;
3015 mHandler.sendMessage(lockTaskMsg);
3016 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003017 return;
3018 }
3019 if (isLockTaskModeViolation(task)) {
3020 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3021 return;
3022 }
3023 mLockTaskModeTask = task;
3024 findTaskToMoveToFrontLocked(task, 0, null);
3025 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003026
3027 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003028 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3029 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003030 lockTaskMsg.what = LOCK_TASK_START_MSG;
3031 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003032 }
3033
3034 boolean isLockTaskModeViolation(TaskRecord task) {
3035 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3036 }
3037
3038 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3039 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
3040 mLockTaskModeTask = null;
3041 }
3042 }
3043
3044 boolean isInLockTaskMode() {
3045 return mLockTaskModeTask != null;
3046 }
3047
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003048 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003049
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003050 public ActivityStackSupervisorHandler(Looper looper) {
3051 super(looper);
3052 }
3053
Craig Mautnerf3333272013-04-22 10:55:53 -07003054 void activityIdleInternal(ActivityRecord r) {
3055 synchronized (mService) {
3056 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3057 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003058 }
3059
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003060 @Override
3061 public void handleMessage(Message msg) {
3062 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003063 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003064 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003065 if (mService.mDidDexOpt) {
3066 mService.mDidDexOpt = false;
3067 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3068 nmsg.obj = msg.obj;
3069 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3070 return;
3071 }
3072 // We don't at this point know if the activity is fullscreen,
3073 // so we need to be conservative and assume it isn't.
3074 activityIdleInternal((ActivityRecord)msg.obj);
3075 } break;
3076 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003077 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003078 activityIdleInternal((ActivityRecord)msg.obj);
3079 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003080 case RESUME_TOP_ACTIVITY_MSG: {
3081 synchronized (mService) {
3082 resumeTopActivitiesLocked();
3083 }
3084 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003085 case SLEEP_TIMEOUT_MSG: {
3086 synchronized (mService) {
3087 if (mService.isSleepingOrShuttingDown()) {
3088 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3089 mSleepTimeout = true;
3090 checkReadyForSleepLocked();
3091 }
3092 }
3093 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003094 case LAUNCH_TIMEOUT_MSG: {
3095 if (mService.mDidDexOpt) {
3096 mService.mDidDexOpt = false;
3097 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3098 return;
3099 }
3100 synchronized (mService) {
3101 if (mLaunchingActivity.isHeld()) {
3102 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3103 if (VALIDATE_WAKE_LOCK_CALLER
3104 && Binder.getCallingUid() != Process.myUid()) {
3105 throw new IllegalStateException("Calling must be system uid");
3106 }
3107 mLaunchingActivity.release();
3108 }
3109 }
3110 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003111 case HANDLE_DISPLAY_ADDED: {
3112 handleDisplayAddedLocked(msg.arg1);
3113 } break;
3114 case HANDLE_DISPLAY_CHANGED: {
3115 handleDisplayChangedLocked(msg.arg1);
3116 } break;
3117 case HANDLE_DISPLAY_REMOVED: {
3118 handleDisplayRemovedLocked(msg.arg1);
3119 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003120 case CONTAINER_CALLBACK_VISIBILITY: {
3121 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003122 final IActivityContainerCallback callback = container.mCallback;
3123 if (callback != null) {
3124 try {
3125 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3126 } catch (RemoteException e) {
3127 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003128 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003129 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003130 case LOCK_TASK_START_MSG: {
3131 // When lock task starts, we disable the status bars.
3132 try {
3133 if (getStatusBarService() != null) {
3134 getStatusBarService().disable
3135 (StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK,
3136 mToken, mService.mContext.getPackageName());
3137 }
Jason Monk35c62a42014-06-17 10:24:47 -04003138 if (getDevicePolicyManager() != null) {
3139 getDevicePolicyManager().notifyLockTaskModeChanged(true,
3140 (String)msg.obj,
3141 msg.arg1);
3142 }
justinzhang5286d3f2014-05-12 17:06:01 -04003143 } catch (RemoteException ex) {
3144 throw new RuntimeException(ex);
3145 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003146 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003147 case LOCK_TASK_END_MSG: {
3148 // When lock task ends, we enable the status bars.
3149 try {
3150 if (getStatusBarService() != null) {
3151 getStatusBarService().disable
3152 (StatusBarManager.DISABLE_NONE,
3153 mToken, mService.mContext.getPackageName());
3154 }
Jason Monk35c62a42014-06-17 10:24:47 -04003155 if (getDevicePolicyManager() != null) {
3156 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3157 msg.arg1);
3158 }
justinzhang5286d3f2014-05-12 17:06:01 -04003159 } catch (RemoteException ex) {
3160 throw new RuntimeException(ex);
3161 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003162 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003163 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3164 final ActivityContainer container = (ActivityContainer) msg.obj;
3165 final IActivityContainerCallback callback = container.mCallback;
3166 if (callback != null) {
3167 try {
3168 callback.onAllActivitiesComplete(container.asBinder());
3169 } catch (RemoteException e) {
3170 }
3171 }
3172 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003173 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3174 synchronized (mService) {
3175 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3176 msg.obj);
3177 ((ActivityContainer) msg.obj).onTaskListEmptyLocked();
3178 }
3179 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003180 }
3181 }
3182 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003183
Ying Wangb081a592014-04-22 15:20:16 -07003184 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003185 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003186 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003187 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003188 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003189 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003190 ActivityRecord mParentActivity = null;
3191 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003192
Craig Mautnere3a00d72014-04-16 08:31:19 -07003193 boolean mVisible = true;
3194
Craig Mautner4a1cb222013-12-04 16:14:06 -08003195 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003196 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003197
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003198 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3199 final static int CONTAINER_STATE_NO_SURFACE = 1;
3200 final static int CONTAINER_STATE_FINISHING = 2;
3201 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3202
3203 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003204 synchronized (mService) {
3205 mStackId = stackId;
3206 mStack = new ActivityStack(this);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003207 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003208 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003209 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003210 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003211
Craig Mautnere0a38842013-12-16 16:14:02 -08003212 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003213 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3214 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003215 mActivityDisplay = activityDisplay;
3216 mStack.mDisplayId = activityDisplay.mDisplayId;
3217 mStack.mStacks = activityDisplay.mStacks;
3218
3219 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003220 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003221 }
3222
3223 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003224 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003225 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003226 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3227 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003228 return;
3229 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003230 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003231 }
3232 }
3233
3234 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003235 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003236 synchronized (mService) {
3237 if (mActivityDisplay != null) {
3238 return mActivityDisplay.mDisplayId;
3239 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003240 }
3241 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003242 }
3243
Jeff Brownca9bc702014-02-11 14:32:56 -08003244 @Override
3245 public boolean injectEvent(InputEvent event) {
3246 final long origId = Binder.clearCallingIdentity();
3247 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003248 synchronized (mService) {
3249 if (mActivityDisplay != null) {
3250 return mInputManagerInternal.injectInputEvent(event,
3251 mActivityDisplay.mDisplayId,
3252 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3253 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003254 }
3255 return false;
3256 } finally {
3257 Binder.restoreCallingIdentity(origId);
3258 }
3259 }
3260
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003261 @Override
3262 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003263 synchronized (mService) {
3264 if (mContainerState == CONTAINER_STATE_FINISHING) {
3265 return;
3266 }
3267 mContainerState = CONTAINER_STATE_FINISHING;
3268
3269 final Message msg =
3270 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3271 mHandler.sendMessageDelayed(msg, 1000);
3272
3273 long origId = Binder.clearCallingIdentity();
3274 try {
3275 mStack.finishAllActivitiesLocked();
3276 } finally {
3277 Binder.restoreCallingIdentity(origId);
3278 }
3279 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003280 }
3281
Craig Mautner34b73df2014-01-12 21:11:08 -08003282 private void detachLocked() {
3283 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3284 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003285 if (mActivityDisplay != null) {
3286 mActivityDisplay.detachActivitiesLocked(mStack);
3287 mActivityDisplay = null;
3288 mStack.mDisplayId = -1;
3289 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003290 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003291 }
3292 }
3293
3294 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003295 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003296 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003297 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
3298 Binder.getCallingUid(), mCurrentUser, false, true, "ActivityContainer", null);
3299 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003300 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003301 String mimeType = intent.getType();
3302 if (mimeType == null && intent.getData() != null
3303 && "content".equals(intent.getData().getScheme())) {
3304 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3305 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003306 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0, 0, null,
Craig Mautnere0a38842013-12-16 16:14:02 -08003307 null, null, null, null, userId, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003308 }
3309
3310 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003311 public final int startActivityIntentSender(IIntentSender intentSender) {
3312 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3313
3314 if (!(intentSender instanceof PendingIntentRecord)) {
3315 throw new IllegalArgumentException("Bad PendingIntent object");
3316 }
3317
3318 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003319 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003320 }
3321
Craig Mautner247ab652014-04-25 10:09:00 -07003322 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3323 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
3324 Binder.getCallingUid(), mCurrentUser, false, true, "ActivityContainer", null);
3325 if (resolvedType == null) {
3326 resolvedType = intent.getType();
3327 if (resolvedType == null && intent.getData() != null
3328 && "content".equals(intent.getData().getScheme())) {
3329 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3330 }
3331 }
3332 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003333 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003334 throw new SecurityException(
3335 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3336 }
3337 }
3338
3339 /** Throw a SecurityException if allowEmbedded is not true */
3340 @Override
3341 public final void checkEmbeddedAllowed(Intent intent) {
3342 checkEmbeddedAllowedInner(intent, null);
3343 }
3344
3345 /** Throw a SecurityException if allowEmbedded is not true */
3346 @Override
3347 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3348 if (!(intentSender instanceof PendingIntentRecord)) {
3349 throw new IllegalArgumentException("Bad PendingIntent object");
3350 }
3351 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3352 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3353 pendingIntent.key.requestResolvedType);
3354 }
3355
Craig Mautnerdf88d732014-01-27 09:21:32 -08003356 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003357 public IBinder asBinder() {
3358 return this;
3359 }
3360
Craig Mautner4504de52013-12-20 09:06:56 -08003361 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003362 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003363 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003364 }
3365
Craig Mautner4a1cb222013-12-04 16:14:06 -08003366 ActivityStackSupervisor getOuter() {
3367 return ActivityStackSupervisor.this;
3368 }
3369
Craig Mautnerd163e752014-06-13 17:18:47 -07003370 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003371 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003372 }
3373
3374 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003375 synchronized (mService) {
3376 if (mActivityDisplay != null) {
3377 mActivityDisplay.getBounds(outBounds);
3378 } else {
3379 outBounds.set(0, 0);
3380 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003381 }
3382 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003383
Craig Mautner6985bad2014-04-21 15:22:06 -07003384 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003385 void setVisible(boolean visible) {
3386 if (mVisible != visible) {
3387 mVisible = visible;
3388 if (mCallback != null) {
3389 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3390 0 /* unused */, this).sendToTarget();
3391 }
3392 }
3393 }
3394
Craig Mautner6985bad2014-04-21 15:22:06 -07003395 void setDrawn() {
3396 }
3397
Craig Mautner1b4bf852014-05-26 15:06:32 -07003398 // You can always start a new task on a regular ActivityStack.
3399 boolean isEligibleForNewTasks() {
3400 return true;
3401 }
3402
Craig Mautnerd163e752014-06-13 17:18:47 -07003403 void onTaskListEmptyLocked() {
3404 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3405 if (!mStack.isHomeStack()) {
3406 detachLocked();
3407 deleteActivityContainer(this);
3408 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003409 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
3410 }
3411
Craig Mautner34b73df2014-01-12 21:11:08 -08003412 @Override
3413 public String toString() {
3414 return mIdString + (mActivityDisplay == null ? "N" : "A");
3415 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003416 }
3417
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003418 private class VirtualActivityContainer extends ActivityContainer {
3419 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003420 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003421
3422 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3423 super(getNextStackId());
3424 mParentActivity = parent;
3425 mCallback = callback;
3426 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003427 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003428 }
3429
3430 @Override
3431 public void setSurface(Surface surface, int width, int height, int density) {
3432 super.setSurface(surface, width, height, density);
3433
3434 synchronized (mService) {
3435 final long origId = Binder.clearCallingIdentity();
3436 try {
3437 setSurfaceLocked(surface, width, height, density);
3438 } finally {
3439 Binder.restoreCallingIdentity(origId);
3440 }
3441 }
3442 }
3443
3444 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3445 if (mContainerState == CONTAINER_STATE_FINISHING) {
3446 return;
3447 }
3448 VirtualActivityDisplay virtualActivityDisplay =
3449 (VirtualActivityDisplay) mActivityDisplay;
3450 if (virtualActivityDisplay == null) {
3451 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003452 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003453 mActivityDisplay = virtualActivityDisplay;
3454 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3455 attachToDisplayLocked(virtualActivityDisplay);
3456 }
3457
3458 if (mSurface != null) {
3459 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003460 }
3461
Craig Mautner6985bad2014-04-21 15:22:06 -07003462 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003463 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003464 mStack.resumeTopActivityLocked(null);
3465 } else {
3466 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003467 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003468 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
3469 mStack.startPausingLocked(false, true);
3470 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003471 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003472
Craig Mautnerd163e752014-06-13 17:18:47 -07003473 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003474
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003475 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3476 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003477 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003478
Craig Mautner6985bad2014-04-21 15:22:06 -07003479 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003480 boolean isAttachedLocked() {
3481 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003482 }
3483
3484 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003485 void setDrawn() {
3486 synchronized (mService) {
3487 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003488 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003489 }
3490 }
3491
Craig Mautner1b4bf852014-05-26 15:06:32 -07003492 // Never start a new task on an ActivityView if it isn't explicitly specified.
3493 @Override
3494 boolean isEligibleForNewTasks() {
3495 return false;
3496 }
3497
Craig Mautnerd163e752014-06-13 17:18:47 -07003498 private void setSurfaceIfReadyLocked() {
3499 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003500 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3501 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3502 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3503 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3504 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003505 }
3506 }
3507
Craig Mautner4a1cb222013-12-04 16:14:06 -08003508 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3509 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003510 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003511 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003512 int mDisplayId;
3513 Display mDisplay;
3514 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003515
Craig Mautner4a1cb222013-12-04 16:14:06 -08003516 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3517 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08003518 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003519
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003520 ActivityDisplay() {
3521 }
Craig Mautner4504de52013-12-20 09:06:56 -08003522
Craig Mautnere0a38842013-12-16 16:14:02 -08003523 ActivityDisplay(int displayId) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003524 init(mDisplayManager.getDisplay(displayId));
Craig Mautner4504de52013-12-20 09:06:56 -08003525 }
3526
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003527 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003528 mDisplay = display;
3529 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003530 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003531 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003532
3533 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003534 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
3535 + mDisplayId);
3536 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003537 }
3538
Craig Mautnere0a38842013-12-16 16:14:02 -08003539 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003540 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003541 + " from displayId=" + mDisplayId);
3542 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003543 }
3544
3545 void getBounds(Point bounds) {
3546 mDisplay.getDisplayInfo(mDisplayInfo);
3547 bounds.x = mDisplayInfo.appWidth;
3548 bounds.y = mDisplayInfo.appHeight;
3549 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003550
3551 @Override
3552 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003553 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3554 }
3555 }
3556
3557 class VirtualActivityDisplay extends ActivityDisplay {
3558 VirtualDisplay mVirtualDisplay;
3559
Craig Mautner6985bad2014-04-21 15:22:06 -07003560 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003561 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
3562 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, VIRTUAL_DISPLAY_BASE_NAME,
Craig Mautner6985bad2014-04-21 15:22:06 -07003563 width, height, density, null, DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003564 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY);
3565
3566 init(mVirtualDisplay.getDisplay());
3567
3568 mWindowManager.handleDisplayAdded(mDisplayId);
3569 }
3570
3571 void setSurface(Surface surface) {
3572 if (mVirtualDisplay != null) {
3573 mVirtualDisplay.setSurface(surface);
3574 }
3575 }
3576
3577 @Override
3578 void detachActivitiesLocked(ActivityStack stack) {
3579 super.detachActivitiesLocked(stack);
3580 if (mVirtualDisplay != null) {
3581 mVirtualDisplay.release();
3582 mVirtualDisplay = null;
3583 }
3584 }
3585
3586 @Override
3587 public String toString() {
3588 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003589 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003590 }
Jose Lima58e66d62014-05-27 20:00:27 -07003591
3592 private boolean isLeanbackOnlyDevice() {
3593 boolean onLeanbackOnly = false;
3594 try {
3595 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
3596 PackageManager.FEATURE_LEANBACK_ONLY);
3597 } catch (RemoteException e) {
3598 // noop
3599 }
3600
3601 return onLeanbackOnly;
3602 }
Craig Mautner27084302013-03-25 08:05:25 -07003603}