blob: d33d6dc5ae2d1f3a048dfe00d69a850eb67a7b23 [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 */
233 final PowerManager.WakeLock mLaunchingActivity;
234
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 */
240 final PowerManager.WakeLock mGoingToSleep;
241
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. */
257 private TaskRecord mLockTaskModeTask;
258
Craig Mautner4a1cb222013-12-04 16:14:06 -0800259 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700260 mService = service;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800261 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700262 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800263 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700264 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
265 throw new IllegalStateException("Calling must be system uid");
266 }
267 mLaunchingActivity =
268 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
269 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700270 }
271
justinzhang5286d3f2014-05-12 17:06:01 -0400272 // This function returns a IStatusBarService. The value is from ServiceManager.
273 // getService and is cached.
274 private IStatusBarService getStatusBarService() {
275 synchronized (mService) {
276 if (mStatusBarService == null) {
277 mStatusBarService = IStatusBarService.Stub.asInterface(
278 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
279 if (mStatusBarService == null) {
280 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
281 }
282 }
283 return mStatusBarService;
284 }
285 }
286
Jason Monk35c62a42014-06-17 10:24:47 -0400287 private IDevicePolicyManager getDevicePolicyManager() {
288 synchronized (mService) {
289 if (mDevicePolicyManager == null) {
290 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
291 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
292 if (mDevicePolicyManager == null) {
293 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
294 }
295 }
296 return mDevicePolicyManager;
297 }
298 }
299
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700300 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800301 synchronized (mService) {
302 mWindowManager = wm;
303
304 mDisplayManager =
305 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
306 mDisplayManager.registerDisplayListener(this, null);
307
308 Display[] displays = mDisplayManager.getDisplays();
309 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
310 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800311 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
312 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800313 }
314
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700315 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800316 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800317
318 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700319
320 // Initialize this here, now that we can get a valid reference to PackageManager.
321 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800322 }
Craig Mautner27084302013-03-25 08:05:25 -0700323 }
324
325 void dismissKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700326 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner27084302013-03-25 08:05:25 -0700327 if (mDismissKeyguardOnNextActivity) {
328 mDismissKeyguardOnNextActivity = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700329 mWindowManager.dismissKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700330 }
331 }
332
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700333 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800334 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700335 }
336
Craig Mautnerde4ef022013-04-07 19:01:33 -0700337 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800338 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700339 }
340
Craig Mautner4a1cb222013-12-04 16:14:06 -0800341 // TODO: Split into two methods isFrontStack for any visible stack and isFrontmostStack for the
342 // top of all visible stacks.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700343 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800344 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
345 if (parent != null) {
346 stack = parent.task.stack;
347 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800348 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800349 if (stacks != null && !stacks.isEmpty()) {
350 return stack == stacks.get(stacks.size() - 1);
351 }
352 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700353 }
354
Craig Mautnerde4ef022013-04-07 19:01:33 -0700355 void moveHomeStack(boolean toFront) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800356 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800357 int topNdx = stacks.size() - 1;
358 if (topNdx <= 0) {
359 return;
360 }
361 ActivityStack topStack = stacks.get(topNdx);
362 final boolean homeInFront = topStack == mHomeStack;
363 if (homeInFront != toFront) {
364 mLastFocusedStack = topStack;
365 stacks.remove(mHomeStack);
366 stacks.add(toFront ? topNdx : 0, mHomeStack);
367 mFocusedStack = stacks.get(topNdx);
368 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
369 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700370 }
371 }
372
Craig Mautner719e6212014-05-29 16:49:41 +0000373 void moveHomeToTop() {
Craig Mautner69ada552013-04-18 13:51:51 -0700374 moveHomeStack(true);
Craig Mautner719e6212014-05-29 16:49:41 +0000375 mHomeStack.moveHomeTaskToTop();
Craig Mautner8e569572013-10-11 17:36:59 -0700376 }
377
Craig Mautner719e6212014-05-29 16:49:41 +0000378 boolean resumeHomeActivity(ActivityRecord prev) {
379 moveHomeToTop();
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700380 if (prev != null) {
Craig Mautner719e6212014-05-29 16:49:41 +0000381 prev.task.mOnTopOfHome = false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700382 }
Craig Mautnera8a90e02013-06-28 15:24:50 -0700383 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner719e6212014-05-29 16:49:41 +0000384 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700385 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700386 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700387 }
388 return mService.startHomeActivityLocked(mCurrentUser);
389 }
390
Craig Mautner27084302013-03-25 08:05:25 -0700391 void setDismissKeyguard(boolean dismiss) {
Craig Mautner5314a402013-09-26 12:40:16 -0700392 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen(" dismiss=" + dismiss);
Craig Mautner27084302013-03-25 08:05:25 -0700393 mDismissKeyguardOnNextActivity = dismiss;
394 }
395
Craig Mautner8d341ef2013-03-26 09:03:27 -0700396 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800397 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800398 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800399 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800400 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
401 ActivityStack stack = stacks.get(stackNdx);
402 TaskRecord task = stack.taskForIdLocked(id);
403 if (task != null) {
404 return task;
405 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700406 }
407 }
408 return null;
409 }
410
Craig Mautner6170f732013-04-02 13:05:23 -0700411 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800412 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800413 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800414 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800415 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
416 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
417 if (r != null) {
418 return r;
419 }
Craig Mautner6170f732013-04-02 13:05:23 -0700420 }
421 }
422 return null;
423 }
424
Craig Mautneref73ee12014-04-23 11:45:37 -0700425 void setNextTaskId(int taskId) {
426 if (taskId > mCurTaskId) {
427 mCurTaskId = taskId;
428 }
429 }
430
Craig Mautner8d341ef2013-03-26 09:03:27 -0700431 int getNextTaskId() {
432 do {
433 mCurTaskId++;
434 if (mCurTaskId <= 0) {
435 mCurTaskId = 1;
436 }
437 } while (anyTaskForIdLocked(mCurTaskId) != null);
438 return mCurTaskId;
439 }
440
Craig Mautnerde4ef022013-04-07 19:01:33 -0700441 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700442 ActivityStack stack = getFocusedStack();
443 if (stack == null) {
444 return null;
445 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700446 ActivityRecord resumedActivity = stack.mResumedActivity;
447 if (resumedActivity == null || resumedActivity.app == null) {
448 resumedActivity = stack.mPausingActivity;
449 if (resumedActivity == null || resumedActivity.app == null) {
450 resumedActivity = stack.topRunningActivityLocked(null);
451 }
452 }
453 return resumedActivity;
454 }
455
Mike Lockwooded8902d2013-11-15 11:01:47 -0800456 boolean attachApplicationLocked(ProcessRecord app) throws Exception {
Craig Mautner20e72272013-04-01 13:45:53 -0700457 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800458 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800459 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
460 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800461 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
462 final ActivityStack stack = stacks.get(stackNdx);
463 if (!isFrontStack(stack)) {
464 continue;
465 }
466 ActivityRecord hr = stack.topRunningActivityLocked(null);
467 if (hr != null) {
468 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
469 && processName.equals(hr.processName)) {
470 try {
George Mount2c92c972014-03-20 09:38:23 -0700471 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800472 didSomething = true;
473 }
474 } catch (Exception e) {
475 Slog.w(TAG, "Exception in new application when starting activity "
476 + hr.intent.getComponent().flattenToShortString(), e);
477 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700478 }
Craig Mautner20e72272013-04-01 13:45:53 -0700479 }
Craig Mautner20e72272013-04-01 13:45:53 -0700480 }
481 }
482 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700483 if (!didSomething) {
484 ensureActivitiesVisibleLocked(null, 0);
485 }
Craig Mautner20e72272013-04-01 13:45:53 -0700486 return didSomething;
487 }
488
489 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800490 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
491 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800492 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
493 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800494 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800495 continue;
496 }
497 final ActivityRecord resumedActivity = stack.mResumedActivity;
498 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800499 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
500 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800501 return false;
502 }
Craig Mautner20e72272013-04-01 13:45:53 -0700503 }
504 }
505 return true;
506 }
507
Craig Mautnerde4ef022013-04-07 19:01:33 -0700508 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800509 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
510 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800511 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
512 final ActivityStack stack = stacks.get(stackNdx);
513 if (isFrontStack(stack)) {
514 final ActivityRecord r = stack.mResumedActivity;
515 if (r != null && r.state != ActivityState.RESUMED) {
516 return false;
517 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700518 }
519 }
520 }
521 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800522 if (DEBUG_STACK) Slog.d(TAG,
523 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
524 mLastFocusedStack + " to=" + mFocusedStack);
525 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700526 return true;
527 }
528
529 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800530 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
531 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800532 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
533 final ActivityStack stack = stacks.get(stackNdx);
534 final ActivityRecord r = stack.mResumedActivity;
535 if (r != null && (!r.nowVisible || r.waitingVisible)) {
536 return false;
537 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700538 }
539 }
540 return true;
541 }
542
Craig Mautner2acc3892013-09-23 10:28:14 -0700543 /**
544 * Pause all activities in either all of the stacks or just the back stacks.
545 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700546 * @return true if any activity was paused as a result of this call.
547 */
Craig Mautner5314a402013-09-26 12:40:16 -0700548 boolean pauseBackStacks(boolean userLeaving) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700549 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800550 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
551 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800552 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
553 final ActivityStack stack = stacks.get(stackNdx);
554 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
555 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
556 " mResumedActivity=" + stack.mResumedActivity);
557 stack.startPausingLocked(userLeaving, false);
558 someActivityPaused = true;
559 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700560 }
561 }
562 return someActivityPaused;
563 }
564
Craig Mautnerde4ef022013-04-07 19:01:33 -0700565 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700566 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800567 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
568 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800569 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
570 final ActivityStack stack = stacks.get(stackNdx);
571 final ActivityRecord r = stack.mPausingActivity;
572 if (r != null && r.state != ActivityState.PAUSED
573 && r.state != ActivityState.STOPPED
574 && r.state != ActivityState.STOPPING) {
575 if (DEBUG_STATES) {
576 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
577 pausing = false;
578 } else {
579 return false;
580 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700581 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700582 }
583 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700584 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700585 }
586
Craig Mautnerdf88d732014-01-27 09:21:32 -0800587 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping) {
John Spurlock8a985d22014-02-25 09:40:05 -0500588 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800589 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
590 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
591 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
592 final ActivityStack stack = stacks.get(stackNdx);
593 if (stack.mResumedActivity != null &&
594 stack.mActivityContainer.mParentActivity == parent) {
595 stack.startPausingLocked(userLeaving, uiSleeping);
596 }
597 }
598 }
599 }
600
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700601 void reportActivityVisibleLocked(ActivityRecord r) {
Craig Mautner858d8a62013-04-23 17:08:34 -0700602 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700603 WaitResult w = mWaitingActivityVisible.get(i);
604 w.timeout = false;
605 if (r != null) {
606 w.who = new ComponentName(r.info.packageName, r.info.name);
607 }
608 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
609 w.thisTime = w.totalTime;
610 }
611 mService.notifyAll();
612 dismissKeyguard();
613 }
614
615 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
616 long thisTime, long totalTime) {
617 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700618 WaitResult w = mWaitingActivityLaunched.remove(i);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700619 w.timeout = timeout;
620 if (r != null) {
621 w.who = new ComponentName(r.info.packageName, r.info.name);
622 }
623 w.thisTime = thisTime;
624 w.totalTime = totalTime;
625 }
626 mService.notifyAll();
627 }
628
Craig Mautner29219d92013-04-16 20:19:12 -0700629 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700630 final ActivityStack focusedStack = getFocusedStack();
631 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
632 if (r != null) {
633 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700634 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700635
Craig Mautner4a1cb222013-12-04 16:14:06 -0800636 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800637 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800638 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
639 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700640 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700641 r = stack.topRunningActivityLocked(null);
642 if (r != null) {
643 return r;
644 }
645 }
646 }
647 return null;
648 }
649
Dianne Hackborn09233282014-04-30 11:33:59 -0700650 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700651 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800652 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
653 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800654 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800655 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800656 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800657 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
658 final ActivityStack stack = stacks.get(stackNdx);
659 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
660 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700661 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700662 }
663 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700664
665 // The lists are already sorted from most recent to oldest. Just pull the most recent off
666 // each list and add it to list. Stop when all lists are empty or maxNum reached.
667 while (maxNum > 0) {
668 long mostRecentActiveTime = Long.MIN_VALUE;
669 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800670 final int numTaskLists = runningTaskLists.size();
671 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
672 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700673 if (!stackTaskList.isEmpty()) {
674 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
675 if (lastActiveTime > mostRecentActiveTime) {
676 mostRecentActiveTime = lastActiveTime;
677 selectedStackList = stackTaskList;
678 }
679 }
680 }
681 if (selectedStackList != null) {
682 list.add(selectedStackList.remove(0));
683 --maxNum;
684 } else {
685 break;
686 }
687 }
Craig Mautner20e72272013-04-01 13:45:53 -0700688 }
689
Craig Mautner23ac33b2013-04-01 16:26:35 -0700690 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
691 String profileFile, ParcelFileDescriptor profileFd, int userId) {
692 // Collect information about the target of the Intent.
693 ActivityInfo aInfo;
694 try {
695 ResolveInfo rInfo =
696 AppGlobals.getPackageManager().resolveIntent(
697 intent, resolvedType,
698 PackageManager.MATCH_DEFAULT_ONLY
699 | ActivityManagerService.STOCK_PM_FLAGS, userId);
700 aInfo = rInfo != null ? rInfo.activityInfo : null;
701 } catch (RemoteException e) {
702 aInfo = null;
703 }
704
705 if (aInfo != null) {
706 // Store the found target back into the intent, because now that
707 // we have it we never want to do this again. For example, if the
708 // user navigates back to this point in the history, we should
709 // always restart the exact same activity.
710 intent.setComponent(new ComponentName(
711 aInfo.applicationInfo.packageName, aInfo.name));
712
713 // Don't debug things in the system process
714 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
715 if (!aInfo.processName.equals("system")) {
716 mService.setDebugApp(aInfo.processName, true, false);
717 }
718 }
719
720 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
721 if (!aInfo.processName.equals("system")) {
722 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
723 }
724 }
725
726 if (profileFile != null) {
727 if (!aInfo.processName.equals("system")) {
728 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName,
729 profileFile, profileFd,
730 (startFlags&ActivityManager.START_FLAG_AUTO_STOP_PROFILER) != 0);
731 }
732 }
733 }
734 return aInfo;
735 }
736
Craig Mautner2219a1b2013-03-25 09:44:30 -0700737 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner719e6212014-05-29 16:49:41 +0000738 moveHomeToTop();
Dianne Hackborn91097de2014-04-04 18:02:06 -0700739 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null, 0,
Craig Mautnere0a38842013-12-16 16:14:02 -0800740 null, false, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700741 }
742
Craig Mautner23ac33b2013-04-01 16:26:35 -0700743 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700744 String callingPackage, Intent intent, String resolvedType,
745 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
746 IBinder resultTo, String resultWho, int requestCode, int startFlags, String profileFile,
Craig Mautner23ac33b2013-04-01 16:26:35 -0700747 ParcelFileDescriptor profileFd, WaitResult outResult, Configuration config,
Craig Mautnere0a38842013-12-16 16:14:02 -0800748 Bundle options, int userId, IActivityContainer iContainer) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700749 // Refuse possible leaked file descriptors
750 if (intent != null && intent.hasFileDescriptors()) {
751 throw new IllegalArgumentException("File descriptors passed in Intent");
752 }
753 boolean componentSpecified = intent.getComponent() != null;
754
755 // Don't modify the client's object!
756 intent = new Intent(intent);
757
758 // Collect information about the target of the Intent.
759 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
760 profileFile, profileFd, userId);
761
Craig Mautnere0a38842013-12-16 16:14:02 -0800762 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700763 synchronized (mService) {
764 int callingPid;
765 if (callingUid >= 0) {
766 callingPid = -1;
767 } else if (caller == null) {
768 callingPid = Binder.getCallingPid();
769 callingUid = Binder.getCallingUid();
770 } else {
771 callingPid = callingUid = -1;
772 }
773
Craig Mautnere0a38842013-12-16 16:14:02 -0800774 final ActivityStack stack;
775 if (container == null || container.mStack.isOnHomeDisplay()) {
776 stack = getFocusedStack();
777 } else {
778 stack = container.mStack;
779 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700780 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700781 && mService.mConfiguration.diff(config) != 0;
782 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700783 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700784
785 final long origId = Binder.clearCallingIdentity();
786
787 if (aInfo != null &&
788 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
789 // This may be a heavy-weight process! Check to see if we already
790 // have another, different heavy-weight process running.
791 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
792 if (mService.mHeavyWeightProcess != null &&
793 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
794 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700795 int realCallingUid = callingUid;
796 if (caller != null) {
797 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
798 if (callerApp != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700799 realCallingUid = callerApp.info.uid;
800 } else {
801 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700802 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700803 + intent.toString());
804 ActivityOptions.abort(options);
805 return ActivityManager.START_PERMISSION_DENIED;
806 }
807 }
808
809 IIntentSender target = mService.getIntentSenderLocked(
810 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
811 realCallingUid, userId, null, null, 0, new Intent[] { intent },
812 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
813 | PendingIntent.FLAG_ONE_SHOT, null);
814
815 Intent newIntent = new Intent();
816 if (requestCode >= 0) {
817 // Caller is requesting a result.
818 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
819 }
820 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
821 new IntentSender(target));
822 if (mService.mHeavyWeightProcess.activities.size() > 0) {
823 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
824 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
825 hist.packageName);
826 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
827 hist.task.taskId);
828 }
829 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
830 aInfo.packageName);
831 newIntent.setFlags(intent.getFlags());
832 newIntent.setClassName("android",
833 HeavyWeightSwitcherActivity.class.getName());
834 intent = newIntent;
835 resolvedType = null;
836 caller = null;
837 callingUid = Binder.getCallingUid();
838 callingPid = Binder.getCallingPid();
839 componentSpecified = true;
840 try {
841 ResolveInfo rInfo =
842 AppGlobals.getPackageManager().resolveIntent(
843 intent, null,
844 PackageManager.MATCH_DEFAULT_ONLY
845 | ActivityManagerService.STOCK_PM_FLAGS, userId);
846 aInfo = rInfo != null ? rInfo.activityInfo : null;
847 aInfo = mService.getActivityInfoForUser(aInfo, userId);
848 } catch (RemoteException e) {
849 aInfo = null;
850 }
851 }
852 }
853 }
854
Dianne Hackborn91097de2014-04-04 18:02:06 -0700855 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
856 voiceSession, voiceInteractor, resultTo, resultWho,
Craig Mautnere0a38842013-12-16 16:14:02 -0800857 requestCode, callingPid, callingUid, callingPackage, startFlags, options,
858 componentSpecified, null, container);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700859
Craig Mautnerde4ef022013-04-07 19:01:33 -0700860 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700861 // If the caller also wants to switch to a new configuration,
862 // do so now. This allows a clean switch, as we are waiting
863 // for the current activity to pause (so we will not destroy
864 // it), and have not yet started the next activity.
865 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
866 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700867 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700868 if (DEBUG_CONFIGURATION) Slog.v(TAG,
869 "Updating to new configuration after starting activity.");
870 mService.updateConfigurationLocked(config, null, false, false);
871 }
872
873 Binder.restoreCallingIdentity(origId);
874
875 if (outResult != null) {
876 outResult.result = res;
877 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700878 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700879 do {
880 try {
881 mService.wait();
882 } catch (InterruptedException e) {
883 }
884 } while (!outResult.timeout && outResult.who == null);
885 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700886 ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700887 if (r.nowVisible) {
888 outResult.timeout = false;
889 outResult.who = new ComponentName(r.info.packageName, r.info.name);
890 outResult.totalTime = 0;
891 outResult.thisTime = 0;
892 } else {
893 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700894 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700895 do {
896 try {
897 mService.wait();
898 } catch (InterruptedException e) {
899 }
900 } while (!outResult.timeout && outResult.who == null);
901 }
902 }
903 }
904
905 return res;
906 }
907 }
908
909 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
910 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
911 Bundle options, int userId) {
912 if (intents == null) {
913 throw new NullPointerException("intents is null");
914 }
915 if (resolvedTypes == null) {
916 throw new NullPointerException("resolvedTypes is null");
917 }
918 if (intents.length != resolvedTypes.length) {
919 throw new IllegalArgumentException("intents are length different than resolvedTypes");
920 }
921
Craig Mautner23ac33b2013-04-01 16:26:35 -0700922
923 int callingPid;
924 if (callingUid >= 0) {
925 callingPid = -1;
926 } else if (caller == null) {
927 callingPid = Binder.getCallingPid();
928 callingUid = Binder.getCallingUid();
929 } else {
930 callingPid = callingUid = -1;
931 }
932 final long origId = Binder.clearCallingIdentity();
933 try {
934 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -0700935 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -0700936 for (int i=0; i<intents.length; i++) {
937 Intent intent = intents[i];
938 if (intent == null) {
939 continue;
940 }
941
942 // Refuse possible leaked file descriptors
943 if (intent != null && intent.hasFileDescriptors()) {
944 throw new IllegalArgumentException("File descriptors passed in Intent");
945 }
946
947 boolean componentSpecified = intent.getComponent() != null;
948
949 // Don't modify the client's object!
950 intent = new Intent(intent);
951
952 // Collect information about the target of the Intent.
953 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i],
954 0, null, null, userId);
955 // TODO: New, check if this is correct
956 aInfo = mService.getActivityInfoForUser(aInfo, userId);
957
958 if (aInfo != null &&
959 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
960 != 0) {
961 throw new IllegalArgumentException(
962 "FLAG_CANT_SAVE_STATE not supported here");
963 }
964
965 Bundle theseOptions;
966 if (options != null && i == intents.length-1) {
967 theseOptions = options;
968 } else {
969 theseOptions = null;
970 }
Craig Mautner6170f732013-04-02 13:05:23 -0700971 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn91097de2014-04-04 18:02:06 -0700972 aInfo, null, null, resultTo, null, -1, callingPid, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -0800973 0, theseOptions, componentSpecified, outActivity, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700974 if (res < 0) {
975 return res;
976 }
977
978 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
979 }
980 }
981 } finally {
982 Binder.restoreCallingIdentity(origId);
983 }
984
985 return ActivityManager.START_SUCCESS;
986 }
987
Craig Mautner2420ead2013-04-01 17:13:20 -0700988 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -0700989 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -0700990 throws RemoteException {
991
992 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -0700993 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700994 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -0700995
996 // schedule launch ticks to collect information about slow apps.
997 r.startLaunchTickingLocked();
998
999 // Have the window manager re-evaluate the orientation of
1000 // the screen based on the new activity order. Note that
1001 // as a result of this, it can call back into the activity
1002 // manager with a new orientation. We don't care about that,
1003 // because the activity is not currently running so we are
1004 // just restarting it anyway.
1005 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001006 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001007 mService.mConfiguration,
1008 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1009 mService.updateConfigurationLocked(config, r, false, false);
1010 }
1011
1012 r.app = app;
1013 app.waitingToKill = null;
1014 r.launchCount++;
1015 r.lastLaunchTime = SystemClock.uptimeMillis();
1016
1017 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1018
1019 int idx = app.activities.indexOf(r);
1020 if (idx < 0) {
1021 app.activities.add(r);
1022 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001023 mService.updateLruProcessLocked(app, true, null);
1024 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001025
1026 final ActivityStack stack = r.task.stack;
1027 try {
1028 if (app.thread == null) {
1029 throw new RemoteException();
1030 }
1031 List<ResultInfo> results = null;
1032 List<Intent> newIntents = null;
1033 if (andResume) {
1034 results = r.results;
1035 newIntents = r.newIntents;
1036 }
1037 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1038 + " icicle=" + r.icicle
1039 + " with results=" + results + " newIntents=" + newIntents
1040 + " andResume=" + andResume);
1041 if (andResume) {
1042 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1043 r.userId, System.identityHashCode(r),
1044 r.task.taskId, r.shortComponentName);
1045 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001046 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001047 // Home process is the root process of the task.
1048 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001049 }
1050 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1051 r.sleeping = false;
1052 r.forceNewConfig = false;
1053 mService.showAskCompatModeDialogLocked(r);
1054 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1055 String profileFile = null;
1056 ParcelFileDescriptor profileFd = null;
1057 boolean profileAutoStop = false;
1058 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1059 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1060 mService.mProfileProc = app;
1061 profileFile = mService.mProfileFile;
1062 profileFd = mService.mProfileFd;
1063 profileAutoStop = mService.mAutoStopProfiler;
1064 }
1065 }
1066 app.hasShownUi = true;
1067 app.pendingUiClean = true;
1068 if (profileFd != null) {
1069 try {
1070 profileFd = profileFd.dup();
1071 } catch (IOException e) {
1072 if (profileFd != null) {
1073 try {
1074 profileFd.close();
1075 } catch (IOException o) {
1076 }
1077 profileFd = null;
1078 }
1079 }
1080 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001081
Dianne Hackborna413dc02013-07-12 12:02:55 -07001082 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001083 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1084 System.identityHashCode(r), r.info,
Craig Mautnera0026042014-04-23 11:45:37 -07001085 new Configuration(mService.mConfiguration), r.compat, r.task.voiceInteractor,
1086 app.repProcState, r.icicle, r.persistentState, results, newIntents, !andResume,
Craig Mautner233ceee2014-05-09 17:05:11 -07001087 mService.isNextTransitionForward(), profileFile, profileFd, profileAutoStop
1088 );
Craig Mautner2420ead2013-04-01 17:13:20 -07001089
1090 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1091 // This may be a heavy-weight process! Note that the package
1092 // manager will ensure that only activity can run in the main
1093 // process of the .apk, which is the only thing that will be
1094 // considered heavy-weight.
1095 if (app.processName.equals(app.info.packageName)) {
1096 if (mService.mHeavyWeightProcess != null
1097 && mService.mHeavyWeightProcess != app) {
1098 Slog.w(TAG, "Starting new heavy weight process " + app
1099 + " when already running "
1100 + mService.mHeavyWeightProcess);
1101 }
1102 mService.mHeavyWeightProcess = app;
1103 Message msg = mService.mHandler.obtainMessage(
1104 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1105 msg.obj = r;
1106 mService.mHandler.sendMessage(msg);
1107 }
1108 }
1109
1110 } catch (RemoteException e) {
1111 if (r.launchFailed) {
1112 // This is the second time we failed -- finish activity
1113 // and give up.
1114 Slog.e(TAG, "Second failure launching "
1115 + r.intent.getComponent().flattenToShortString()
1116 + ", giving up", e);
1117 mService.appDiedLocked(app, app.pid, app.thread);
1118 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1119 "2nd-crash", false);
1120 return false;
1121 }
1122
1123 // This is the first time we failed -- restart process and
1124 // retry.
1125 app.activities.remove(r);
1126 throw e;
1127 }
1128
1129 r.launchFailed = false;
1130 if (stack.updateLRUListLocked(r)) {
1131 Slog.w(TAG, "Activity " + r
1132 + " being launched, but already in LRU list");
1133 }
1134
1135 if (andResume) {
1136 // As part of the process of launching, ActivityThread also performs
1137 // a resume.
1138 stack.minimalResumeActivityLocked(r);
1139 } else {
1140 // This activity is not starting in the resumed state... which
1141 // should look like we asked it to pause+stop (but remain visible),
1142 // and it has done so and reported back the current icicle and
1143 // other state.
1144 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1145 + " (starting in stopped state)");
1146 r.state = ActivityState.STOPPED;
1147 r.stopped = true;
1148 }
1149
1150 // Launch the new version setup screen if needed. We do this -after-
1151 // launching the initial activity (that is, home), so that it can have
1152 // a chance to initialize itself while in the background, making the
1153 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001154 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001155 mService.startSetupActivityLocked();
1156 }
1157
1158 return true;
1159 }
1160
Craig Mautnere79d42682013-04-01 19:01:53 -07001161 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001162 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001163 // Is this activity's application already running?
1164 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001165 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001166
1167 r.task.stack.setLaunchTime(r);
1168
1169 if (app != null && app.thread != null) {
1170 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001171 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1172 || !"android".equals(r.info.packageName)) {
1173 // Don't add this if it is a platform component that is marked
1174 // to run in multiple processes, because this is actually
1175 // part of the framework so doesn't make sense to track as a
1176 // separate apk in the process.
1177 app.addPackage(r.info.packageName, mService.mProcessStats);
1178 }
George Mount2c92c972014-03-20 09:38:23 -07001179 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001180 return;
1181 } catch (RemoteException e) {
1182 Slog.w(TAG, "Exception when starting activity "
1183 + r.intent.getComponent().flattenToShortString(), e);
1184 }
1185
1186 // If a dead object exception was thrown -- fall through to
1187 // restart the application.
1188 }
1189
1190 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001191 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001192 }
1193
Craig Mautner6170f732013-04-02 13:05:23 -07001194 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001195 Intent intent, String resolvedType, ActivityInfo aInfo,
1196 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1197 IBinder resultTo, String resultWho, int requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001198 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
Craig Mautnere0a38842013-12-16 16:14:02 -08001199 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container) {
Craig Mautner6170f732013-04-02 13:05:23 -07001200 int err = ActivityManager.START_SUCCESS;
1201
1202 ProcessRecord callerApp = null;
1203 if (caller != null) {
1204 callerApp = mService.getRecordForAppLocked(caller);
1205 if (callerApp != null) {
1206 callingPid = callerApp.pid;
1207 callingUid = callerApp.info.uid;
1208 } else {
1209 Slog.w(TAG, "Unable to find app for caller " + caller
1210 + " (pid=" + callingPid + ") when starting: "
1211 + intent.toString());
1212 err = ActivityManager.START_PERMISSION_DENIED;
1213 }
1214 }
1215
1216 if (err == ActivityManager.START_SUCCESS) {
1217 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1218 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner9ef471f2014-02-07 13:11:47 -08001219 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid)
1220 + " on display " + (container == null ? (mFocusedStack == null ?
1221 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1222 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1223 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001224 }
1225
1226 ActivityRecord sourceRecord = null;
1227 ActivityRecord resultRecord = null;
1228 if (resultTo != null) {
1229 sourceRecord = isInAnyStackLocked(resultTo);
1230 if (DEBUG_RESULTS) Slog.v(
1231 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1232 if (sourceRecord != null) {
1233 if (requestCode >= 0 && !sourceRecord.finishing) {
1234 resultRecord = sourceRecord;
1235 }
1236 }
1237 }
1238 ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1239
Dianne Hackborn91097de2014-04-04 18:02:06 -07001240 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001241
1242 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
1243 && sourceRecord != null) {
1244 // Transfer the result target from the source activity to the new
1245 // one being started, including any failures.
1246 if (requestCode >= 0) {
1247 ActivityOptions.abort(options);
1248 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1249 }
1250 resultRecord = sourceRecord.resultTo;
1251 resultWho = sourceRecord.resultWho;
1252 requestCode = sourceRecord.requestCode;
1253 sourceRecord.resultTo = null;
1254 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001255 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001256 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001257 if (sourceRecord.launchedFromUid == callingUid) {
1258 // The new activity is being launched from the same uid as the previous
1259 // activity in the flow, and asking to forward its result back to the
1260 // previous. In this case the activity is serving as a trampoline between
1261 // the two, so we also want to update its launchedFromPackage to be the
1262 // same as the previous activity. Note that this is safe, since we know
1263 // these two packages come from the same uid; the caller could just as
1264 // well have supplied that same package name itself. This specifially
1265 // deals with the case of an intent picker/chooser being launched in the app
1266 // flow to redirect to an activity picked by the user, where we want the final
1267 // activity to consider it to have been launched by the previous app activity.
1268 callingPackage = sourceRecord.launchedFromPackage;
1269 }
Craig Mautner6170f732013-04-02 13:05:23 -07001270 }
1271
1272 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1273 // We couldn't find a class that can handle the given Intent.
1274 // That's the end of that!
1275 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1276 }
1277
1278 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1279 // We couldn't find the specific class specified in the Intent.
1280 // Also the end of the line.
1281 err = ActivityManager.START_CLASS_NOT_FOUND;
1282 }
1283
Dianne Hackborn91097de2014-04-04 18:02:06 -07001284 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1285 && sourceRecord.task.voiceSession != null) {
1286 // If this activity is being launched as part of a voice session, we need
1287 // to ensure that it is safe to do so. If the upcoming activity will also
1288 // be part of the voice session, we can only launch it if it has explicitly
1289 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001290 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001291 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1292 try {
1293 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1294 intent, resolvedType)) {
1295 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1296 }
1297 } catch (RemoteException e) {
1298 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1299 }
1300 }
1301 }
1302
1303 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1304 // If the caller is starting a new voice session, just make sure the target
1305 // is actually allowing it to run this way.
1306 try {
1307 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1308 intent, resolvedType)) {
1309 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1310 }
1311 } catch (RemoteException e) {
1312 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1313 }
1314 }
1315
Craig Mautner6170f732013-04-02 13:05:23 -07001316 if (err != ActivityManager.START_SUCCESS) {
1317 if (resultRecord != null) {
1318 resultStack.sendActivityResultLocked(-1,
1319 resultRecord, resultWho, requestCode,
1320 Activity.RESULT_CANCELED, null);
1321 }
1322 setDismissKeyguard(false);
1323 ActivityOptions.abort(options);
1324 return err;
1325 }
1326
1327 final int startAnyPerm = mService.checkPermission(
1328 START_ANY_ACTIVITY, callingPid, callingUid);
1329 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1330 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1331 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1332 if (resultRecord != null) {
1333 resultStack.sendActivityResultLocked(-1,
1334 resultRecord, resultWho, requestCode,
1335 Activity.RESULT_CANCELED, null);
1336 }
1337 setDismissKeyguard(false);
1338 String msg;
1339 if (!aInfo.exported) {
1340 msg = "Permission Denial: starting " + intent.toString()
1341 + " from " + callerApp + " (pid=" + callingPid
1342 + ", uid=" + callingUid + ")"
1343 + " not exported from uid " + aInfo.applicationInfo.uid;
1344 } else {
1345 msg = "Permission Denial: starting " + intent.toString()
1346 + " from " + callerApp + " (pid=" + callingPid
1347 + ", uid=" + callingUid + ")"
1348 + " requires " + aInfo.permission;
1349 }
1350 Slog.w(TAG, msg);
1351 throw new SecurityException(msg);
1352 }
1353
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001354 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001355 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001356
Craig Mautner6170f732013-04-02 13:05:23 -07001357 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001358 try {
1359 // The Intent we give to the watcher has the extra data
1360 // stripped off, since it can contain private information.
1361 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001362 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001363 aInfo.applicationInfo.packageName);
1364 } catch (RemoteException e) {
1365 mService.mController = null;
1366 }
Ben Gruver5e207332013-04-03 17:41:37 -07001367 }
Craig Mautner6170f732013-04-02 13:05:23 -07001368
Ben Gruver5e207332013-04-03 17:41:37 -07001369 if (abort) {
1370 if (resultRecord != null) {
1371 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001372 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001373 }
Ben Gruver5e207332013-04-03 17:41:37 -07001374 // We pretend to the caller that it was really started, but
1375 // they will just get a cancel result.
1376 setDismissKeyguard(false);
1377 ActivityOptions.abort(options);
1378 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001379 }
1380
1381 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001382 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001383 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001384 if (outActivity != null) {
1385 outActivity[0] = r;
1386 }
1387
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001388 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001389 if (voiceSession == null && (stack.mResumedActivity == null
1390 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Craig Mautner6170f732013-04-02 13:05:23 -07001391 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
1392 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001393 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautner6170f732013-04-02 13:05:23 -07001394 mService.mPendingActivityLaunches.add(pal);
1395 setDismissKeyguard(false);
1396 ActivityOptions.abort(options);
1397 return ActivityManager.START_SWITCHES_CANCELED;
1398 }
1399 }
1400
1401 if (mService.mDidAppSwitch) {
1402 // This is the second allowed switch since we stopped switches,
1403 // so now just generally allow switches. Use case: user presses
1404 // home (switches disabled, switch to home, mDidAppSwitch now true);
1405 // user taps a home icon (coming from home so allowed, we hit here
1406 // and now allow anyone to switch again).
1407 mService.mAppSwitchesAllowedTime = 0;
1408 } else {
1409 mService.mDidAppSwitch = true;
1410 }
1411
1412 mService.doPendingActivityLaunchesLocked(false);
1413
Dianne Hackborn91097de2014-04-04 18:02:06 -07001414 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
1415 startFlags, true, options);
Craig Mautner10385a12013-09-22 21:08:32 -07001416
1417 if (allPausedActivitiesComplete()) {
1418 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001419 // activity start, but we are not actually doing an activity
1420 // switch... just dismiss the keyguard now, because we
1421 // probably want to see whatever is behind it.
1422 dismissKeyguard();
1423 }
1424 return err;
1425 }
1426
Craig Mautner1b4bf852014-05-26 15:06:32 -07001427 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001428 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001429
1430 // On leanback only devices we should keep all activities in the same stack.
1431 if (!mLeanbackOnlyDevice &&
1432 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001433 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001434 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001435 if (taskStack.isOnHomeDisplay()) {
1436 if (mFocusedStack != taskStack) {
1437 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1438 "focused stack to r=" + r + " task=" + task);
1439 mFocusedStack = taskStack;
1440 } else {
1441 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1442 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1443 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001444 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001445 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001446 }
1447
Craig Mautnere0a38842013-12-16 16:14:02 -08001448 final ActivityContainer container = r.mInitialActivityContainer;
1449 if (container != null) {
1450 // The first time put it on the desired stack, after this put on task stack.
1451 r.mInitialActivityContainer = null;
1452 return container.mStack;
1453 }
1454
Craig Mautner1b4bf852014-05-26 15:06:32 -07001455 if (mFocusedStack != mHomeStack && (!newTask ||
1456 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001457 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1458 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1459 return mFocusedStack;
1460 }
1461
Craig Mautnere0a38842013-12-16 16:14:02 -08001462 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1463 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1464 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1465 if (!stack.isHomeStack()) {
1466 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1467 "adjustStackFocus: Setting focused stack=" + stack);
1468 mFocusedStack = stack;
1469 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001470 }
1471 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001472
Craig Mautner4a1cb222013-12-04 16:14:06 -08001473 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001474 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001475 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1476 " stackId=" + stackId);
1477 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001478 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001479 }
1480 return mHomeStack;
1481 }
1482
Craig Mautner29219d92013-04-16 20:19:12 -07001483 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001484 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001485 final TaskRecord task = r.task;
1486 boolean isHomeActivity = !r.isApplicationActivity();
1487 if (!isHomeActivity && task != null) {
1488 isHomeActivity = !task.isApplicationTask();
1489 }
1490 if (!isHomeActivity && task != null) {
1491 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1492 isHomeActivity = parent != null && parent.isHomeActivity();
1493 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001494 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001495 }
1496 }
1497
Craig Mautner8849a5e2013-04-02 16:41:03 -07001498 final int startActivityUncheckedLocked(ActivityRecord r,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001499 ActivityRecord sourceRecord,
1500 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
1501 boolean doResume, Bundle options) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001502 final Intent intent = r.intent;
1503 final int callingUid = r.launchedFromUid;
1504
1505 int launchFlags = intent.getFlags();
1506
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001507 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
1508 (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
1509 r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK)) {
1510 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1511 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1512 "\"singleInstance\" or \"singleTask\"");
1513 launchFlags &=
1514 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1515 } else {
1516 switch (r.info.documentLaunchMode) {
1517 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1518 break;
1519 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1520 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1521 break;
1522 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1523 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1524 break;
1525 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1526 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1527 break;
1528 }
1529 }
1530
1531 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1532 // For whatever reason this activity is being launched into a new
1533 // task... yet the caller has requested a result back. Well, that
1534 // is pretty messed up, so instead immediately send back a cancel
1535 // and let the new task continue launched as normal without a
1536 // dependency on its originator.
1537 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1538 r.resultTo.task.stack.sendActivityResultLocked(-1,
1539 r.resultTo, r.resultWho, r.requestCode,
1540 Activity.RESULT_CANCELED, null);
1541 r.resultTo = null;
1542 }
1543
1544 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1545 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1546 }
1547
Craig Mautner8849a5e2013-04-02 16:41:03 -07001548 // We'll invoke onUserLeaving before onPause only if the launching
1549 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001550 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001551 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001552
1553 // If the caller has asked not to resume at this point, we make note
1554 // of this in the record so that we can skip it when trying to find
1555 // the top running activity.
1556 if (!doResume) {
1557 r.delayedResume = true;
1558 }
1559
Craig Mautnera254cd72014-05-25 16:47:39 -07001560 ActivityRecord notTop =
1561 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001562
1563 // If the onlyIfNeeded flag is set, then we can do this if the activity
1564 // being launched is the same as the one making the call... or, as
1565 // a special case, if we do not know the caller then we count the
1566 // current top activity as the caller.
1567 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1568 ActivityRecord checkedCaller = sourceRecord;
1569 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001570 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001571 }
1572 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1573 // Caller is not the same as launcher, so always needed.
1574 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1575 }
1576 }
1577
1578 if (sourceRecord == null) {
1579 // This activity is not being started from another... in this
1580 // case we -always- start a new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001581 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Craig Mautner29219d92013-04-16 20:19:12 -07001582 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1583 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001584 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1585 }
1586 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1587 // The original activity who is starting us is running as a single
1588 // instance... this new activity it is starting must go on its
1589 // own task.
1590 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1591 } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
1592 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1593 // The activity being started is a single instance... it always
1594 // gets launched into its own task.
1595 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1596 }
1597
Craig Mautner88629292013-11-10 20:39:05 -08001598 ActivityInfo newTaskInfo = null;
1599 Intent newTaskIntent = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001600 final ActivityStack sourceStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001601 if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001602 if (sourceRecord.finishing) {
1603 // If the source is finishing, we can't further count it as our source. This
1604 // is because the task it is associated with may now be empty and on its way out,
1605 // so we don't want to blindly throw it in to that task. Instead we will take
Craig Mautner88629292013-11-10 20:39:05 -08001606 // the NEW_TASK flow and try to find a task for it. But save the task information
1607 // so it can be used when creating the new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001608 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001609 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1610 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1611 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
Craig Mautner88629292013-11-10 20:39:05 -08001612 newTaskInfo = sourceRecord.info;
1613 newTaskIntent = sourceRecord.task.intent;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001614 }
1615 sourceRecord = null;
1616 sourceStack = null;
1617 } else {
1618 sourceStack = sourceRecord.task.stack;
1619 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001620 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001621 sourceStack = null;
1622 }
1623
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001624 intent.setFlags(launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001625
1626 boolean addingToTask = false;
1627 boolean movedHome = false;
1628 TaskRecord reuseTask = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001629 ActivityStack targetStack;
Craig Mautnerd00f4742014-03-12 14:17:26 -07001630 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1631 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001632 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1633 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1634 // If bring to front is requested, and no result is requested, and
1635 // we can find a task that was started with this same
1636 // component, then instead of launching bring that one to the front.
1637 if (r.resultTo == null) {
1638 // See if there is a task to bring to the front. If this is
1639 // a SINGLE_INSTANCE activity, there can be one and only one
1640 // instance of it in the history, and it is always in its own
1641 // unique task, so we do a special search.
1642 ActivityRecord intentActivity = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
Craig Mautnerac6f8432013-07-17 13:24:59 -07001643 ? findTaskLocked(r)
Craig Mautner8849a5e2013-04-02 16:41:03 -07001644 : findActivityLocked(intent, r.info);
1645 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001646 if (isLockTaskModeViolation(intentActivity.task)) {
1647 Slog.e(TAG, "moveTaskToFront: Attempt to violate Lock Task Mode");
1648 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1649 }
Craig Mautner29219d92013-04-16 20:19:12 -07001650 if (r.task == null) {
1651 r.task = intentActivity.task;
1652 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001653 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001654 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001655 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1656 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001657 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001658 if (intentActivity.task.intent == null) {
1659 // This task was started because of movement of
1660 // the activity based on affinity... now that we
1661 // are actually launching it, we can assign the
1662 // base intent.
1663 intentActivity.task.setIntent(intent, r.info);
1664 }
1665 // If the target task is not in the front, then we need
1666 // to bring it to the front... except... well, with
1667 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1668 // to have the same behavior as if a new instance was
1669 // being started, which means not bringing it to the front
1670 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001671 final ActivityStack lastStack = getLastStack();
1672 ActivityRecord curTop = lastStack == null?
1673 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner7504d7b2013-09-17 10:50:53 -07001674 if (curTop != null && (curTop.task != intentActivity.task ||
1675 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001676 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001677 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1678 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001679 // We really do want to push this one into the
1680 // user's face, right now.
1681 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001682 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001683 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001684 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1685 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001686 // Caller wants to appear on home activity.
Craig Mautner719e6212014-05-29 16:49:41 +00001687 intentActivity.task.mOnTopOfHome = true;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001688 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001689 options = null;
1690 }
1691 }
1692 // If the caller has requested that the target task be
1693 // reset, then do so.
1694 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1695 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1696 }
1697 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1698 // We don't need to start a new activity, and
1699 // the client said not to do anything if that
1700 // is the case, so this is it! And for paranoia, make
1701 // sure we have correctly resumed the top activity.
1702 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001703 resumeTopActivitiesLocked(targetStack, null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001704 } else {
1705 ActivityOptions.abort(options);
1706 }
1707 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1708 }
1709 if ((launchFlags &
1710 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1711 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1712 // The caller has requested to completely replace any
1713 // existing task with its new activity. Well that should
1714 // not be too hard...
1715 reuseTask = intentActivity.task;
1716 reuseTask.performClearTaskLocked();
1717 reuseTask.setIntent(r.intent, r.info);
1718 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
1719 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
1720 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1721 // In this situation we want to remove all activities
1722 // from the task up to the one being started. In most
1723 // cases this means we are resetting the task to its
1724 // initial state.
1725 ActivityRecord top =
1726 intentActivity.task.performClearTaskLocked(r, launchFlags);
1727 if (top != null) {
1728 if (top.frontOfTask) {
1729 // Activity aliases may mean we use different
1730 // intents for the top activity, so make sure
1731 // the task now has the identity of the new
1732 // intent.
1733 top.task.setIntent(r.intent, r.info);
1734 }
1735 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1736 r, top.task);
1737 top.deliverNewIntentLocked(callingUid, r.intent);
1738 } else {
1739 // A special case: we need to
1740 // start the activity because it is not currently
1741 // running, and the caller has asked to clear the
1742 // current task to have this activity at the top.
1743 addingToTask = true;
1744 // Now pretend like this activity is being started
1745 // by the top of its task, so it is put in the
1746 // right place.
1747 sourceRecord = intentActivity;
1748 }
1749 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1750 // In this case the top activity on the task is the
1751 // same as the one being launched, so we take that
1752 // as a request to bring the task to the foreground.
1753 // If the top activity in the task is the root
1754 // activity, deliver this new intent to it if it
1755 // desires.
1756 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
1757 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
1758 && intentActivity.realActivity.equals(r.realActivity)) {
1759 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1760 intentActivity.task);
1761 if (intentActivity.frontOfTask) {
1762 intentActivity.task.setIntent(r.intent, r.info);
1763 }
1764 intentActivity.deliverNewIntentLocked(callingUid, r.intent);
1765 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1766 // In this case we are launching the root activity
1767 // of the task, but with a different intent. We
1768 // should start a new instance on top.
1769 addingToTask = true;
1770 sourceRecord = intentActivity;
1771 }
1772 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1773 // In this case an activity is being launched in to an
1774 // existing task, without resetting that task. This
1775 // is typically the situation of launching an activity
1776 // from a notification or shortcut. We want to place
1777 // the new activity on top of the current task.
1778 addingToTask = true;
1779 sourceRecord = intentActivity;
1780 } else if (!intentActivity.task.rootWasReset) {
1781 // In this case we are launching in to an existing task
1782 // that has not yet been started from its front door.
1783 // The current task has been brought to the front.
1784 // Ideally, we'd probably like to place this new task
1785 // at the bottom of its stack, but that's a little hard
1786 // to do with the current organization of the code so
1787 // for now we'll just drop it.
1788 intentActivity.task.setIntent(r.intent, r.info);
1789 }
1790 if (!addingToTask && reuseTask == null) {
1791 // We didn't do anything... but it was needed (a.k.a., client
1792 // don't use that intent!) And for paranoia, make
1793 // sure we have correctly resumed the top activity.
1794 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001795 targetStack.resumeTopActivityLocked(null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001796 } else {
1797 ActivityOptions.abort(options);
1798 }
1799 return ActivityManager.START_TASK_TO_FRONT;
1800 }
1801 }
1802 }
1803 }
1804
1805 //String uri = r.intent.toURI();
1806 //Intent intent2 = new Intent(uri);
1807 //Slog.i(TAG, "Given intent: " + r.intent);
1808 //Slog.i(TAG, "URI is: " + uri);
1809 //Slog.i(TAG, "To intent: " + intent2);
1810
1811 if (r.packageName != null) {
1812 // If the activity being launched is the same as the one currently
1813 // at the top, then we need to check if it should only be launched
1814 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001815 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001816 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001817 if (top != null && r.resultTo == null) {
1818 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
1819 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07001820 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautner8849a5e2013-04-02 16:41:03 -07001821 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP
1822 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
1823 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
1824 top.task);
1825 // For paranoia, make sure we have correctly
1826 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001827 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001828 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001829 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001830 }
1831 ActivityOptions.abort(options);
1832 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1833 // We don't need to start a new activity, and
1834 // the client said not to do anything if that
1835 // is the case, so this is it!
1836 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1837 }
1838 top.deliverNewIntentLocked(callingUid, r.intent);
1839 return ActivityManager.START_DELIVERED_TO_TOP;
1840 }
1841 }
1842 }
1843 }
1844
1845 } else {
1846 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001847 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
1848 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001849 }
1850 ActivityOptions.abort(options);
1851 return ActivityManager.START_CLASS_NOT_FOUND;
1852 }
1853
1854 boolean newTask = false;
1855 boolean keepCurTransition = false;
1856
1857 // Should this be considered a new task?
1858 if (r.resultTo == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001859 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001860 if (isLockTaskModeViolation(reuseTask)) {
1861 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1862 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1863 }
Craig Mautner1b4bf852014-05-26 15:06:32 -07001864 newTask = true;
1865 targetStack = adjustStackFocus(r, newTask);
Craig Mautnere0a38842013-12-16 16:14:02 -08001866 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001867 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08001868 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
1869 newTaskInfo != null ? newTaskInfo : r.info,
1870 newTaskIntent != null ? newTaskIntent : intent,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001871 voiceSession, voiceInteractor, true), null, true);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001872 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
1873 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001874 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001875 r.setTask(reuseTask, reuseTask, true);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001876 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001877 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001878 if ((launchFlags &
1879 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
1880 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
1881 // Caller wants to appear on home activity, so before starting
1882 // their own activity we will bring home to the front.
Craig Mautner719e6212014-05-29 16:49:41 +00001883 r.task.mOnTopOfHome = r.task.stack.isOnHomeDisplay();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001884 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001885 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001886 } else if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001887 TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08001888 if (isLockTaskModeViolation(sourceTask)) {
1889 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1890 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1891 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001892 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07001893 targetStack.moveToFront();
Craig Mautnerabcc59c2014-05-17 15:13:28 -07001894 mWindowManager.moveTaskToTop(targetStack.topTask().taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001895 if (!addingToTask &&
1896 (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
1897 // In this case, we are adding the activity to an existing
1898 // task, but the caller has asked to clear that task if the
1899 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07001900 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001901 keepCurTransition = true;
1902 if (top != null) {
1903 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
1904 top.deliverNewIntentLocked(callingUid, r.intent);
1905 // For paranoia, make sure we have correctly
1906 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001907 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001908 if (doResume) {
1909 targetStack.resumeTopActivityLocked(null);
1910 }
1911 ActivityOptions.abort(options);
1912 return ActivityManager.START_DELIVERED_TO_TOP;
1913 }
1914 } else if (!addingToTask &&
1915 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
1916 // In this case, we are launching an activity in our own task
1917 // that may already be running somewhere in the history, and
1918 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07001919 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001920 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001921 final TaskRecord task = top.task;
1922 task.moveActivityToFrontLocked(top);
1923 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001924 top.updateOptionsLocked(options);
1925 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner0f922742013-08-06 08:44:42 -07001926 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001927 if (doResume) {
1928 targetStack.resumeTopActivityLocked(null);
1929 }
1930 return ActivityManager.START_DELIVERED_TO_TOP;
1931 }
1932 }
1933 // An existing activity is starting this new activity, so we want
1934 // to keep the new one in the same task as the one that is starting
1935 // it.
Craig Mautner525f3d92013-05-07 14:01:50 -07001936 r.setTask(sourceTask, sourceRecord.thumbHolder, false);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001937 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001938 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001939
1940 } else {
1941 // This not being started from an existing activity, and not part
1942 // of a new task... just put it in the top task, though these days
1943 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07001944 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerdd221b32014-03-19 19:53:45 -07001945 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07001946 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001947 r.setTask(prev != null ? prev.task
Dianne Hackborn91097de2014-04-04 18:02:06 -07001948 : targetStack.createTaskRecord(getNextTaskId(), r.info, intent, null, null, true),
Craig Mautnerde4ef022013-04-07 19:01:33 -07001949 null, true);
Craig Mautner95e9daa2014-03-17 15:57:20 -07001950 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001951 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
1952 + " in new guessed " + r.task);
1953 }
1954
1955 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001956 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001957
1958 if (newTask) {
1959 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
1960 }
1961 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07001962 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001963 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautner1d001b62013-06-18 16:52:43 -07001964 mService.setFocusedActivityLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001965 return ActivityManager.START_SUCCESS;
1966 }
1967
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001968 void acquireLaunchWakelock() {
1969 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
1970 throw new IllegalStateException("Calling must be system uid");
1971 }
1972 mLaunchingActivity.acquire();
1973 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
1974 // To be safe, don't allow the wake lock to be held for too long.
1975 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
1976 }
1977 }
1978
Craig Mautnerf3333272013-04-22 10:55:53 -07001979 // Checked.
1980 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
1981 Configuration config) {
1982 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
1983
Craig Mautnerf3333272013-04-22 10:55:53 -07001984 ArrayList<ActivityRecord> stops = null;
1985 ArrayList<ActivityRecord> finishes = null;
1986 ArrayList<UserStartedState> startingUsers = null;
1987 int NS = 0;
1988 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07001989 boolean booting = false;
1990 boolean enableScreen = false;
1991 boolean activityRemoved = false;
1992
1993 ActivityRecord r = ActivityRecord.forToken(token);
1994 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07001995 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
1996 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07001997 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
1998 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07001999 if (fromTimeout) {
2000 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002001 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002002
2003 // This is a hack to semi-deal with a race condition
2004 // in the client where it can be constructed with a
2005 // newer configuration from when we asked it to launch.
2006 // We'll update with whatever configuration it now says
2007 // it used to launch.
2008 if (config != null) {
2009 r.configuration = config;
2010 }
2011
2012 // We are now idle. If someone is waiting for a thumbnail from
2013 // us, we can now deliver.
2014 r.idle = true;
2015
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002016 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
2017 if (!mService.mBooted && isFrontStack(r.task.stack)) {
2018 mService.mBooted = true;
2019 enableScreen = true;
2020 }
2021 }
2022
2023 if (allResumedActivitiesIdle()) {
2024 if (r != null) {
2025 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002026 }
2027
2028 if (mLaunchingActivity.isHeld()) {
2029 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2030 if (VALIDATE_WAKE_LOCK_CALLER &&
2031 Binder.getCallingUid() != Process.myUid()) {
2032 throw new IllegalStateException("Calling must be system uid");
2033 }
2034 mLaunchingActivity.release();
2035 }
2036 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002037 }
2038
2039 // Atomically retrieve all of the other things to do.
2040 stops = processStoppingActivitiesLocked(true);
2041 NS = stops != null ? stops.size() : 0;
2042 if ((NF=mFinishingActivities.size()) > 0) {
2043 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2044 mFinishingActivities.clear();
2045 }
2046
Craig Mautnerf3333272013-04-22 10:55:53 -07002047 if (isFrontStack(mHomeStack)) {
2048 booting = mService.mBooting;
2049 mService.mBooting = false;
2050 }
2051
2052 if (mStartingUsers.size() > 0) {
2053 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2054 mStartingUsers.clear();
2055 }
2056
Craig Mautnerf3333272013-04-22 10:55:53 -07002057 // Stop any activities that are scheduled to do so but have been
2058 // waiting for the next one to start.
2059 for (int i = 0; i < NS; i++) {
2060 r = stops.get(i);
2061 final ActivityStack stack = r.task.stack;
2062 if (r.finishing) {
2063 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2064 } else {
2065 stack.stopActivityLocked(r);
2066 }
2067 }
2068
2069 // Finish any activities that are scheduled to do so but have been
2070 // waiting for the next one to start.
2071 for (int i = 0; i < NF; i++) {
2072 r = finishes.get(i);
2073 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, false, "finish-idle");
2074 }
2075
2076 if (booting) {
2077 mService.finishBooting();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002078 } else {
2079 // Complete user switch
2080 if (startingUsers != null) {
2081 for (int i = 0; i < startingUsers.size(); i++) {
2082 mService.finishUserSwitch(startingUsers.get(i));
2083 }
2084 }
2085 // Complete starting up of background users
2086 if (mStartingBackgroundUsers.size() > 0) {
2087 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2088 mStartingBackgroundUsers.clear();
2089 for (int i = 0; i < startingUsers.size(); i++) {
2090 mService.finishUserBoot(startingUsers.get(i));
2091 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002092 }
2093 }
2094
2095 mService.trimApplications();
2096 //dump();
2097 //mWindowManager.dump();
2098
2099 if (enableScreen) {
2100 mService.enableScreenAfterBoot();
2101 }
2102
2103 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002104 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002105 }
2106
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002107 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002108 }
2109
Craig Mautner8e569572013-10-11 17:36:59 -07002110 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002111 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002112 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2113 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002114 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2115 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2116 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002117 }
Craig Mautner19091252013-10-05 00:03:53 -07002118 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002119 }
2120
2121 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002122 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2123 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002124 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2125 stacks.get(stackNdx).closeSystemDialogsLocked();
2126 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002127 }
2128 }
2129
Craig Mautner93529a42013-10-04 15:03:13 -07002130 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002131 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002132 }
2133
Craig Mautner8d341ef2013-03-26 09:03:27 -07002134 /**
2135 * @return true if some activity was finished (or would have finished if doit were true).
2136 */
2137 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2138 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002139 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2140 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002141 final int numStacks = stacks.size();
2142 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2143 final ActivityStack stack = stacks.get(stackNdx);
2144 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2145 didSomething = true;
2146 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002147 }
2148 }
2149 return didSomething;
2150 }
2151
Dianne Hackborna413dc02013-07-12 12:02:55 -07002152 void updatePreviousProcessLocked(ActivityRecord r) {
2153 // Now that this process has stopped, we may want to consider
2154 // it to be the previous app to try to keep around in case
2155 // the user wants to return to it.
2156
2157 // First, found out what is currently the foreground app, so that
2158 // we don't blow away the previous app if this activity is being
2159 // hosted by the process that is actually still the foreground.
2160 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002161 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2162 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002163 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2164 final ActivityStack stack = stacks.get(stackNdx);
2165 if (isFrontStack(stack)) {
2166 if (stack.mResumedActivity != null) {
2167 fgApp = stack.mResumedActivity.app;
2168 } else if (stack.mPausingActivity != null) {
2169 fgApp = stack.mPausingActivity.app;
2170 }
2171 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002172 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002173 }
2174 }
2175
2176 // Now set this one as the previous process, only if that really
2177 // makes sense to.
2178 if (r.app != null && fgApp != null && r.app != fgApp
2179 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002180 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002181 mService.mPreviousProcess = r.app;
2182 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2183 }
2184 }
2185
Craig Mautner05d29032013-05-03 13:40:13 -07002186 boolean resumeTopActivitiesLocked() {
2187 return resumeTopActivitiesLocked(null, null, null);
2188 }
2189
2190 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2191 Bundle targetOptions) {
2192 if (targetStack == null) {
2193 targetStack = getFocusedStack();
2194 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002195 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002196 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002197 if (isFrontStack(targetStack)) {
2198 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2199 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002200 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2201 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002202 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2203 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002204 if (stack == targetStack) {
2205 // Already started above.
2206 continue;
2207 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002208 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002209 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002210 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002211 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002212 }
Craig Mautner05d29032013-05-03 13:40:13 -07002213 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002214 }
2215
2216 void finishTopRunningActivityLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002217 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2218 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002219 final int numStacks = stacks.size();
2220 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2221 final ActivityStack stack = stacks.get(stackNdx);
2222 stack.finishTopRunningActivityLocked(app);
2223 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002224 }
2225 }
2226
Craig Mautneraea74a52014-03-08 14:23:10 -08002227 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2228 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2229 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002230 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002231 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2232 // Caller wants the home activity moved with it. To accomplish this,
2233 // we'll just indicate that this task returns to the home task.
Craig Mautner719e6212014-05-29 16:49:41 +00002234 task.mOnTopOfHome = true;
Craig Mautneraea74a52014-03-08 14:23:10 -08002235 }
2236 task.stack.moveTaskToFrontLocked(task, null, options);
2237 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2238 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002239 }
2240
Craig Mautner967212c2013-04-13 21:10:58 -07002241 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002242 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2243 if (activityContainer != null) {
2244 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002245 }
2246 return null;
2247 }
2248
Craig Mautner967212c2013-04-13 21:10:58 -07002249 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002250 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002251 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2252 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002253 }
2254 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002255 }
2256
Craig Mautner4a1cb222013-12-04 16:14:06 -08002257 IBinder getHomeActivityToken() {
2258 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2259 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2260 final TaskRecord task = tasks.get(taskNdx);
2261 if (task.isHomeTask()) {
2262 final ArrayList<ActivityRecord> activities = task.mActivities;
2263 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2264 final ActivityRecord r = activities.get(activityNdx);
2265 if (r.isHomeActivity()) {
2266 return r.appToken;
2267 }
2268 }
2269 }
2270 }
2271 return null;
2272 }
2273
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002274 ActivityContainer createActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002275 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002276 ActivityContainer activityContainer =
2277 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002278 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002279 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002280 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002281 return activityContainer;
2282 }
2283
Craig Mautner34b73df2014-01-12 21:11:08 -08002284 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002285 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2286 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2287 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002288 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2289 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002290 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002291 }
2292 }
2293
Craig Mautner95da1082014-02-24 17:54:35 -08002294 void deleteActivityContainer(IActivityContainer container) {
2295 ActivityContainer activityContainer = (ActivityContainer)container;
2296 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002297 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2298 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002299 final int stackId = activityContainer.mStackId;
2300 mActivityContainers.remove(stackId);
2301 mWindowManager.removeStack(stackId);
2302 }
2303 }
2304
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002305 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002306 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2307 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002308 return -1;
2309 }
2310
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002311 ActivityContainer activityContainer = new ActivityContainer(stackId);
2312 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002313 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002314 return stackId;
2315 }
2316
2317 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002318 while (true) {
2319 if (++mLastStackId <= HOME_STACK_ID) {
2320 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002321 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002322 if (getStack(mLastStackId) == null) {
2323 break;
2324 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002325 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002326 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002327 }
2328
Craig Mautneref73ee12014-04-23 11:45:37 -07002329 void createStackForRestoredTaskHistory(ArrayList<TaskRecord> tasks) {
2330 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
2331 final ActivityStack stack = getStack(stackId);
2332 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2333 final TaskRecord task = tasks.get(taskNdx);
2334 stack.addTask(task, false, false);
2335 final int taskId = task.taskId;
2336 final ArrayList<ActivityRecord> activities = task.mActivities;
2337 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2338 final ActivityRecord r = activities.get(activityNdx);
2339 mWindowManager.addAppToken(0, r.appToken, taskId, stackId,
2340 r.info.screenOrientation, r.fullscreen,
2341 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Dianne Hackborne30e02f2014-05-27 18:24:45 -07002342 r.userId, r.info.configChanges, task.voiceSession != null);
Craig Mautneref73ee12014-04-23 11:45:37 -07002343 }
2344 mWindowManager.addTask(taskId, stackId, false);
2345 }
Craig Mautner719e6212014-05-29 16:49:41 +00002346 resumeHomeActivity(null);
Craig Mautneref73ee12014-04-23 11:45:37 -07002347 }
2348
Craig Mautner8d341ef2013-03-26 09:03:27 -07002349 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002350 final TaskRecord task = anyTaskForIdLocked(taskId);
2351 if (task == null) {
2352 return;
2353 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002354 final ActivityStack stack = getStack(stackId);
2355 if (stack == null) {
2356 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2357 return;
2358 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002359 task.stack.removeTask(task);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002360 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002361 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002362 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002363 }
2364
Craig Mautnerac6f8432013-07-17 13:24:59 -07002365 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002366 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002367 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2368 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002369 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2370 final ActivityStack stack = stacks.get(stackNdx);
2371 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002372 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2373 continue;
2374 }
2375 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2376 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2377 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002378 continue;
2379 }
2380 final ActivityRecord ar = stack.findTaskLocked(r);
2381 if (ar != null) {
2382 return ar;
2383 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002384 }
2385 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002386 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002387 return null;
2388 }
2389
2390 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002391 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2392 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002393 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2394 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2395 if (ar != null) {
2396 return ar;
2397 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002398 }
2399 }
2400 return null;
2401 }
2402
Craig Mautner8d341ef2013-03-26 09:03:27 -07002403 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002404 scheduleSleepTimeout();
2405 if (!mGoingToSleep.isHeld()) {
2406 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002407 if (mLaunchingActivity.isHeld()) {
2408 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2409 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002410 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002411 mLaunchingActivity.release();
2412 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002413 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002414 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002415 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002416 }
2417
2418 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002419 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002420
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002421 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002422 final long endTime = System.currentTimeMillis() + timeout;
2423 while (true) {
2424 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002425 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2426 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002427 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2428 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2429 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002430 }
2431 if (cantShutdown) {
2432 long timeRemaining = endTime - System.currentTimeMillis();
2433 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002434 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002435 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002436 } catch (InterruptedException e) {
2437 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002438 } else {
2439 Slog.w(TAG, "Activity manager shutdown timed out");
2440 timedout = true;
2441 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002442 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002443 } else {
2444 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002445 }
2446 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002447
2448 // Force checkReadyForSleep to complete.
2449 mSleepTimeout = true;
2450 checkReadyForSleepLocked();
2451
Craig Mautner8d341ef2013-03-26 09:03:27 -07002452 return timedout;
2453 }
2454
2455 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002456 removeSleepTimeouts();
2457 if (mGoingToSleep.isHeld()) {
2458 mGoingToSleep.release();
2459 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002460 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2461 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002462 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2463 final ActivityStack stack = stacks.get(stackNdx);
2464 stack.awakeFromSleepingLocked();
2465 if (isFrontStack(stack)) {
2466 resumeTopActivitiesLocked();
2467 }
Craig Mautner5314a402013-09-26 12:40:16 -07002468 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002469 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002470 mGoingToSleepActivities.clear();
2471 }
2472
2473 void activitySleptLocked(ActivityRecord r) {
2474 mGoingToSleepActivities.remove(r);
2475 checkReadyForSleepLocked();
2476 }
2477
2478 void checkReadyForSleepLocked() {
2479 if (!mService.isSleepingOrShuttingDown()) {
2480 // Do not care.
2481 return;
2482 }
2483
2484 if (!mSleepTimeout) {
2485 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002486 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2487 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002488 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2489 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2490 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002491 }
2492
2493 if (mStoppingActivities.size() > 0) {
2494 // Still need to tell some activities to stop; can't sleep yet.
2495 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2496 + mStoppingActivities.size() + " activities");
2497 scheduleIdleLocked();
2498 dontSleep = true;
2499 }
2500
2501 if (mGoingToSleepActivities.size() > 0) {
2502 // Still need to tell some activities to sleep; can't sleep yet.
2503 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2504 + mGoingToSleepActivities.size() + " activities");
2505 dontSleep = true;
2506 }
2507
2508 if (dontSleep) {
2509 return;
2510 }
2511 }
2512
Craig Mautnere0a38842013-12-16 16:14:02 -08002513 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2514 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002515 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2516 stacks.get(stackNdx).goToSleep();
2517 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002518 }
2519
2520 removeSleepTimeouts();
2521
2522 if (mGoingToSleep.isHeld()) {
2523 mGoingToSleep.release();
2524 }
2525 if (mService.mShuttingDown) {
2526 mService.notifyAll();
2527 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002528 }
2529
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002530 boolean reportResumedActivityLocked(ActivityRecord r) {
2531 final ActivityStack stack = r.task.stack;
2532 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002533 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002534 }
2535 if (allResumedActivitiesComplete()) {
2536 ensureActivitiesVisibleLocked(null, 0);
2537 mWindowManager.executeAppTransition();
2538 return true;
2539 }
2540 return false;
2541 }
2542
Craig Mautner8d341ef2013-03-26 09:03:27 -07002543 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002544 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2545 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002546 final int numStacks = stacks.size();
2547 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2548 final ActivityStack stack = stacks.get(stackNdx);
2549 stack.handleAppCrashLocked(app);
2550 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002551 }
2552 }
2553
Craig Mautnerde4ef022013-04-07 19:01:33 -07002554 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002555 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002556 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2557 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002558 final int topStackNdx = stacks.size() - 1;
2559 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2560 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002561 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002562 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002563 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002564 }
2565
2566 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002567 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2568 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002569 final int numStacks = stacks.size();
2570 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2571 final ActivityStack stack = stacks.get(stackNdx);
2572 stack.scheduleDestroyActivities(app, false, reason);
2573 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002574 }
2575 }
2576
2577 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002578 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
2579 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002580 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002581
Craig Mautner858d8a62013-04-23 17:08:34 -07002582 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002583 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2584 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002585 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002586 final ActivityStack stack = stacks.get(stackNdx);
2587 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002588 TaskRecord task = stack.topTask();
2589 if (task != null) {
2590 mWindowManager.moveTaskToTop(task.taskId);
2591 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002592 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002593 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002594
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002595 ActivityStack stack = getStack(restoreStackId);
2596 if (stack == null) {
2597 stack = mHomeStack;
2598 }
2599 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002600 if (stack.isOnHomeDisplay()) {
2601 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002602 TaskRecord task = stack.topTask();
2603 if (task != null) {
2604 mWindowManager.moveTaskToTop(task.taskId);
2605 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002606 } else {
2607 // Stack was moved to another display while user was swapped out.
Craig Mautner719e6212014-05-29 16:49:41 +00002608 resumeHomeActivity(null);
Craig Mautnere0a38842013-12-16 16:14:02 -08002609 }
Craig Mautner93529a42013-10-04 15:03:13 -07002610 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002611 }
2612
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002613 /**
2614 * Add background users to send boot completed events to.
2615 * @param userId The user being started in the background
2616 * @param uss The state object for the user.
2617 */
2618 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
2619 mStartingBackgroundUsers.add(uss);
2620 }
2621
Craig Mautnerde4ef022013-04-07 19:01:33 -07002622 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
2623 int N = mStoppingActivities.size();
2624 if (N <= 0) return null;
2625
2626 ArrayList<ActivityRecord> stops = null;
2627
2628 final boolean nowVisible = allResumedActivitiesVisible();
2629 for (int i=0; i<N; i++) {
2630 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07002631 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07002632 + nowVisible + " waitingVisible=" + s.waitingVisible
2633 + " finishing=" + s.finishing);
2634 if (s.waitingVisible && nowVisible) {
2635 mWaitingVisibleActivities.remove(s);
2636 s.waitingVisible = false;
2637 if (s.finishing) {
2638 // If this activity is finishing, it is sitting on top of
2639 // everyone else but we now know it is no longer needed...
2640 // so get rid of it. Otherwise, we need to go through the
2641 // normal flow and hide it once we determine that it is
2642 // hidden by the activities in front of it.
2643 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002644 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002645 }
2646 }
2647 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
2648 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
2649 if (stops == null) {
2650 stops = new ArrayList<ActivityRecord>();
2651 }
2652 stops.add(s);
2653 mStoppingActivities.remove(i);
2654 N--;
2655 i--;
2656 }
2657 }
2658
2659 return stops;
2660 }
2661
Craig Mautnercf910b02013-04-23 11:23:27 -07002662 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002663 // FIXME
2664/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2665 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07002666 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002667 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07002668 if (isFrontStack(stack)) {
2669 if (r == null) {
2670 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
2671 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002672 final ActivityRecord pausing = stack.mPausingActivity;
2673 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002674 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002675 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002676 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002677 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002678 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002679 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002680 }
2681 }
2682 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002683 final ActivityRecord resumed = stack.mResumedActivity;
2684 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002685 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002686 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002687 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002688 if (r != null && (state == ActivityState.INITIALIZING
2689 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002690 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002691 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002692 }
2693 }
2694 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002695*/
Craig Mautner76ea2242013-05-15 11:40:05 -07002696 }
2697
Craig Mautner27084302013-03-25 08:05:25 -07002698 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002699 pw.print(prefix); pw.print("mDismissKeyguardOnNextActivity=");
Craig Mautner27084302013-03-25 08:05:25 -07002700 pw.println(mDismissKeyguardOnNextActivity);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002701 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002702 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002703 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2704 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2705 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002706 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07002707 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002708
Craig Mautner20e72272013-04-01 13:45:53 -07002709 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002710 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002711 }
2712
Dianne Hackborn390517b2013-05-30 15:03:32 -07002713 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2714 boolean needSep, String prefix) {
2715 if (activity != null) {
2716 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2717 if (needSep) {
2718 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002719 }
2720 pw.print(prefix);
2721 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002722 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002723 }
2724 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002725 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002726 }
2727
Craig Mautner8d341ef2013-03-26 09:03:27 -07002728 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2729 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002730 boolean printed = false;
2731 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002732 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2733 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
2734 pw.print("Display #"); pw.println(activityDisplay.mDisplayId);
2735 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002736 final int numStacks = stacks.size();
2737 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2738 final ActivityStack stack = stacks.get(stackNdx);
2739 StringBuilder stackHeader = new StringBuilder(128);
2740 stackHeader.append(" Stack #");
2741 stackHeader.append(stack.mStackId);
2742 stackHeader.append(":");
2743 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2744 needSep, stackHeader.toString());
2745 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2746 !dumpAll, false, dumpPackage, true,
2747 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002748
Craig Mautner4a1cb222013-12-04 16:14:06 -08002749 needSep = printed;
2750 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2751 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002752 if (pr) {
2753 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002754 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002755 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002756 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2757 " mResumedActivity: ");
2758 if (pr) {
2759 printed = true;
2760 needSep = false;
2761 }
2762 if (dumpAll) {
2763 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2764 " mLastPausedActivity: ");
2765 if (pr) {
2766 printed = true;
2767 needSep = true;
2768 }
2769 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2770 needSep, " mLastNoHistoryActivity: ");
2771 }
2772 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002773 }
2774 }
2775
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002776 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2777 false, dumpPackage, true, " Activities waiting to finish:", null);
2778 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2779 false, dumpPackage, true, " Activities waiting to stop:", null);
2780 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2781 false, dumpPackage, true, " Activities waiting for another to become visible:",
2782 null);
2783 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2784 false, dumpPackage, true, " Activities waiting to sleep:", null);
2785 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2786 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002787
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002788 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002789 }
2790
Dianne Hackborn390517b2013-05-30 15:03:32 -07002791 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002792 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002793 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002794 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002795 String innerPrefix = null;
2796 String[] args = null;
2797 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002798 for (int i=list.size()-1; i>=0; i--) {
2799 final ActivityRecord r = list.get(i);
2800 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2801 continue;
2802 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002803 if (innerPrefix == null) {
2804 innerPrefix = prefix + " ";
2805 args = new String[0];
2806 }
2807 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002808 final boolean full = !brief && (complete || !r.isInHistory());
2809 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002810 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002811 needNL = false;
2812 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002813 if (header1 != null) {
2814 pw.println(header1);
2815 header1 = null;
2816 }
2817 if (header2 != null) {
2818 pw.println(header2);
2819 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002820 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002821 if (lastTask != r.task) {
2822 lastTask = r.task;
2823 pw.print(prefix);
2824 pw.print(full ? "* " : " ");
2825 pw.println(lastTask);
2826 if (full) {
2827 lastTask.dump(pw, prefix + " ");
2828 } else if (complete) {
2829 // Complete + brief == give a summary. Isn't that obvious?!?
2830 if (lastTask.intent != null) {
2831 pw.print(prefix); pw.print(" ");
2832 pw.println(lastTask.intent.toInsecureStringWithClip());
2833 }
2834 }
2835 }
2836 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2837 pw.print(" #"); pw.print(i); pw.print(": ");
2838 pw.println(r);
2839 if (full) {
2840 r.dump(pw, innerPrefix);
2841 } else if (complete) {
2842 // Complete + brief == give a summary. Isn't that obvious?!?
2843 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2844 if (r.app != null) {
2845 pw.print(innerPrefix); pw.println(r.app);
2846 }
2847 }
2848 if (client && r.app != null && r.app.thread != null) {
2849 // flush anything that is already in the PrintWriter since the thread is going
2850 // to write to the file descriptor directly
2851 pw.flush();
2852 try {
2853 TransferPipe tp = new TransferPipe();
2854 try {
2855 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2856 r.appToken, innerPrefix, args);
2857 // Short timeout, since blocking here can
2858 // deadlock with the application.
2859 tp.go(fd, 2000);
2860 } finally {
2861 tp.kill();
2862 }
2863 } catch (IOException e) {
2864 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2865 } catch (RemoteException e) {
2866 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2867 }
2868 needNL = true;
2869 }
2870 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002871 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002872 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002873
Craig Mautnerf3333272013-04-22 10:55:53 -07002874 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002875 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002876 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2877 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002878 }
2879
2880 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002881 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002882 }
2883
2884 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002885 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002886 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2887 }
2888
Craig Mautner05d29032013-05-03 13:40:13 -07002889 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08002890 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
2891 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2892 }
Craig Mautner05d29032013-05-03 13:40:13 -07002893 }
2894
Craig Mautner0eea92c2013-05-16 13:35:39 -07002895 void removeSleepTimeouts() {
2896 mSleepTimeout = false;
2897 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2898 }
2899
2900 final void scheduleSleepTimeout() {
2901 removeSleepTimeouts();
2902 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2903 }
2904
Craig Mautner4a1cb222013-12-04 16:14:06 -08002905 @Override
2906 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002907 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002908 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
2909 }
2910
2911 @Override
2912 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002913 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002914 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
2915 }
2916
2917 @Override
2918 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002919 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002920 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
2921 }
2922
2923 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08002924 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002925 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08002926 newDisplay = mActivityDisplays.get(displayId) == null;
2927 if (newDisplay) {
2928 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
2929 mActivityDisplays.put(displayId, activityDisplay);
2930 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002931 }
Craig Mautner4504de52013-12-20 09:06:56 -08002932 if (newDisplay) {
2933 mWindowManager.onDisplayAdded(displayId);
2934 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002935 }
2936
2937 public void handleDisplayRemovedLocked(int displayId) {
2938 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002939 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2940 if (activityDisplay != null) {
2941 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002942 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08002943 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08002944 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002945 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002946 }
2947 }
2948 mWindowManager.onDisplayRemoved(displayId);
2949 }
2950
2951 public void handleDisplayChangedLocked(int displayId) {
2952 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002953 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2954 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002955 // TODO: Update the bounds.
2956 }
2957 }
2958 mWindowManager.onDisplayChanged(displayId);
2959 }
2960
2961 StackInfo getStackInfo(ActivityStack stack) {
2962 StackInfo info = new StackInfo();
2963 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
2964 info.displayId = Display.DEFAULT_DISPLAY;
2965 info.stackId = stack.mStackId;
2966
2967 ArrayList<TaskRecord> tasks = stack.getAllTasks();
2968 final int numTasks = tasks.size();
2969 int[] taskIds = new int[numTasks];
2970 String[] taskNames = new String[numTasks];
2971 for (int i = 0; i < numTasks; ++i) {
2972 final TaskRecord task = tasks.get(i);
2973 taskIds[i] = task.taskId;
2974 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
2975 : task.realActivity != null ? task.realActivity.flattenToString()
2976 : task.getTopActivity() != null ? task.getTopActivity().packageName
2977 : "unknown";
2978 }
2979 info.taskIds = taskIds;
2980 info.taskNames = taskNames;
2981 return info;
2982 }
2983
2984 StackInfo getStackInfoLocked(int stackId) {
2985 ActivityStack stack = getStack(stackId);
2986 if (stack != null) {
2987 return getStackInfo(stack);
2988 }
2989 return null;
2990 }
2991
2992 ArrayList<StackInfo> getAllStackInfosLocked() {
2993 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002994 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2995 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002996 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
2997 list.add(getStackInfo(stacks.get(ndx)));
2998 }
2999 }
3000 return list;
3001 }
3002
Craig Mautneraea74a52014-03-08 14:23:10 -08003003 void setLockTaskModeLocked(TaskRecord task) {
justinzhang5286d3f2014-05-12 17:06:01 -04003004 final Message lockTaskMsg = Message.obtain();
Craig Mautneraea74a52014-03-08 14:23:10 -08003005 if (task == null) {
3006 // Take out of lock task mode.
Jason Monk35c62a42014-06-17 10:24:47 -04003007 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003008 lockTaskMsg.what = LOCK_TASK_END_MSG;
Jason Monk35c62a42014-06-17 10:24:47 -04003009 mLockTaskModeTask = null;
justinzhang5286d3f2014-05-12 17:06:01 -04003010 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003011 return;
3012 }
3013 if (isLockTaskModeViolation(task)) {
3014 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3015 return;
3016 }
3017 mLockTaskModeTask = task;
3018 findTaskToMoveToFrontLocked(task, 0, null);
3019 resumeTopActivitiesLocked();
Jason Monk35c62a42014-06-17 10:24:47 -04003020 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3021 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003022 lockTaskMsg.what = LOCK_TASK_START_MSG;
3023 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003024 }
3025
3026 boolean isLockTaskModeViolation(TaskRecord task) {
3027 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3028 }
3029
3030 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3031 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
3032 mLockTaskModeTask = null;
3033 }
3034 }
3035
3036 boolean isInLockTaskMode() {
3037 return mLockTaskModeTask != null;
3038 }
3039
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003040 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003041
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003042 public ActivityStackSupervisorHandler(Looper looper) {
3043 super(looper);
3044 }
3045
Craig Mautnerf3333272013-04-22 10:55:53 -07003046 void activityIdleInternal(ActivityRecord r) {
3047 synchronized (mService) {
3048 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3049 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003050 }
3051
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003052 @Override
3053 public void handleMessage(Message msg) {
3054 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003055 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003056 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003057 if (mService.mDidDexOpt) {
3058 mService.mDidDexOpt = false;
3059 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3060 nmsg.obj = msg.obj;
3061 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3062 return;
3063 }
3064 // We don't at this point know if the activity is fullscreen,
3065 // so we need to be conservative and assume it isn't.
3066 activityIdleInternal((ActivityRecord)msg.obj);
3067 } break;
3068 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003069 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003070 activityIdleInternal((ActivityRecord)msg.obj);
3071 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003072 case RESUME_TOP_ACTIVITY_MSG: {
3073 synchronized (mService) {
3074 resumeTopActivitiesLocked();
3075 }
3076 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003077 case SLEEP_TIMEOUT_MSG: {
3078 synchronized (mService) {
3079 if (mService.isSleepingOrShuttingDown()) {
3080 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3081 mSleepTimeout = true;
3082 checkReadyForSleepLocked();
3083 }
3084 }
3085 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003086 case LAUNCH_TIMEOUT_MSG: {
3087 if (mService.mDidDexOpt) {
3088 mService.mDidDexOpt = false;
3089 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3090 return;
3091 }
3092 synchronized (mService) {
3093 if (mLaunchingActivity.isHeld()) {
3094 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3095 if (VALIDATE_WAKE_LOCK_CALLER
3096 && Binder.getCallingUid() != Process.myUid()) {
3097 throw new IllegalStateException("Calling must be system uid");
3098 }
3099 mLaunchingActivity.release();
3100 }
3101 }
3102 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003103 case HANDLE_DISPLAY_ADDED: {
3104 handleDisplayAddedLocked(msg.arg1);
3105 } break;
3106 case HANDLE_DISPLAY_CHANGED: {
3107 handleDisplayChangedLocked(msg.arg1);
3108 } break;
3109 case HANDLE_DISPLAY_REMOVED: {
3110 handleDisplayRemovedLocked(msg.arg1);
3111 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003112 case CONTAINER_CALLBACK_VISIBILITY: {
3113 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003114 final IActivityContainerCallback callback = container.mCallback;
3115 if (callback != null) {
3116 try {
3117 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3118 } catch (RemoteException e) {
3119 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003120 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003121 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003122 case LOCK_TASK_START_MSG: {
3123 // When lock task starts, we disable the status bars.
3124 try {
3125 if (getStatusBarService() != null) {
3126 getStatusBarService().disable
3127 (StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK,
3128 mToken, mService.mContext.getPackageName());
3129 }
Jason Monk35c62a42014-06-17 10:24:47 -04003130 if (getDevicePolicyManager() != null) {
3131 getDevicePolicyManager().notifyLockTaskModeChanged(true,
3132 (String)msg.obj,
3133 msg.arg1);
3134 }
justinzhang5286d3f2014-05-12 17:06:01 -04003135 } catch (RemoteException ex) {
3136 throw new RuntimeException(ex);
3137 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003138 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003139 case LOCK_TASK_END_MSG: {
3140 // When lock task ends, we enable the status bars.
3141 try {
3142 if (getStatusBarService() != null) {
3143 getStatusBarService().disable
3144 (StatusBarManager.DISABLE_NONE,
3145 mToken, mService.mContext.getPackageName());
3146 }
Jason Monk35c62a42014-06-17 10:24:47 -04003147 if (getDevicePolicyManager() != null) {
3148 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3149 msg.arg1);
3150 }
justinzhang5286d3f2014-05-12 17:06:01 -04003151 } catch (RemoteException ex) {
3152 throw new RuntimeException(ex);
3153 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003154 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003155 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3156 final ActivityContainer container = (ActivityContainer) msg.obj;
3157 final IActivityContainerCallback callback = container.mCallback;
3158 if (callback != null) {
3159 try {
3160 callback.onAllActivitiesComplete(container.asBinder());
3161 } catch (RemoteException e) {
3162 }
3163 }
3164 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003165 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3166 synchronized (mService) {
3167 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3168 msg.obj);
3169 ((ActivityContainer) msg.obj).onTaskListEmptyLocked();
3170 }
3171 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003172 }
3173 }
3174 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003175
Ying Wangb081a592014-04-22 15:20:16 -07003176 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003177 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003178 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003179 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003180 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003181 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003182 ActivityRecord mParentActivity = null;
3183 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003184
Craig Mautnere3a00d72014-04-16 08:31:19 -07003185 boolean mVisible = true;
3186
Craig Mautner4a1cb222013-12-04 16:14:06 -08003187 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003188 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003189
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003190 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3191 final static int CONTAINER_STATE_NO_SURFACE = 1;
3192 final static int CONTAINER_STATE_FINISHING = 2;
3193 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3194
3195 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003196 synchronized (mService) {
3197 mStackId = stackId;
3198 mStack = new ActivityStack(this);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003199 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003200 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003201 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003202 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003203
Craig Mautnere0a38842013-12-16 16:14:02 -08003204 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003205 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3206 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003207 mActivityDisplay = activityDisplay;
3208 mStack.mDisplayId = activityDisplay.mDisplayId;
3209 mStack.mStacks = activityDisplay.mStacks;
3210
3211 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003212 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003213 }
3214
3215 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003216 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003217 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003218 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3219 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003220 return;
3221 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003222 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003223 }
3224 }
3225
3226 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003227 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003228 synchronized (mService) {
3229 if (mActivityDisplay != null) {
3230 return mActivityDisplay.mDisplayId;
3231 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003232 }
3233 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003234 }
3235
Jeff Brownca9bc702014-02-11 14:32:56 -08003236 @Override
3237 public boolean injectEvent(InputEvent event) {
3238 final long origId = Binder.clearCallingIdentity();
3239 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003240 synchronized (mService) {
3241 if (mActivityDisplay != null) {
3242 return mInputManagerInternal.injectInputEvent(event,
3243 mActivityDisplay.mDisplayId,
3244 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3245 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003246 }
3247 return false;
3248 } finally {
3249 Binder.restoreCallingIdentity(origId);
3250 }
3251 }
3252
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003253 @Override
3254 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003255 synchronized (mService) {
3256 if (mContainerState == CONTAINER_STATE_FINISHING) {
3257 return;
3258 }
3259 mContainerState = CONTAINER_STATE_FINISHING;
3260
3261 final Message msg =
3262 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3263 mHandler.sendMessageDelayed(msg, 1000);
3264
3265 long origId = Binder.clearCallingIdentity();
3266 try {
3267 mStack.finishAllActivitiesLocked();
3268 } finally {
3269 Binder.restoreCallingIdentity(origId);
3270 }
3271 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003272 }
3273
Craig Mautner34b73df2014-01-12 21:11:08 -08003274 private void detachLocked() {
3275 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3276 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003277 if (mActivityDisplay != null) {
3278 mActivityDisplay.detachActivitiesLocked(mStack);
3279 mActivityDisplay = null;
3280 mStack.mDisplayId = -1;
3281 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003282 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003283 }
3284 }
3285
3286 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003287 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003288 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003289 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
3290 Binder.getCallingUid(), mCurrentUser, false, true, "ActivityContainer", null);
3291 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003292 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003293 String mimeType = intent.getType();
3294 if (mimeType == null && intent.getData() != null
3295 && "content".equals(intent.getData().getScheme())) {
3296 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3297 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003298 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0, 0, null,
Craig Mautnere0a38842013-12-16 16:14:02 -08003299 null, null, null, null, userId, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003300 }
3301
3302 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003303 public final int startActivityIntentSender(IIntentSender intentSender) {
3304 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3305
3306 if (!(intentSender instanceof PendingIntentRecord)) {
3307 throw new IllegalArgumentException("Bad PendingIntent object");
3308 }
3309
3310 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003311 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003312 }
3313
Craig Mautner247ab652014-04-25 10:09:00 -07003314 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3315 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
3316 Binder.getCallingUid(), mCurrentUser, false, true, "ActivityContainer", null);
3317 if (resolvedType == null) {
3318 resolvedType = intent.getType();
3319 if (resolvedType == null && intent.getData() != null
3320 && "content".equals(intent.getData().getScheme())) {
3321 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3322 }
3323 }
3324 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003325 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003326 throw new SecurityException(
3327 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3328 }
3329 }
3330
3331 /** Throw a SecurityException if allowEmbedded is not true */
3332 @Override
3333 public final void checkEmbeddedAllowed(Intent intent) {
3334 checkEmbeddedAllowedInner(intent, null);
3335 }
3336
3337 /** Throw a SecurityException if allowEmbedded is not true */
3338 @Override
3339 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3340 if (!(intentSender instanceof PendingIntentRecord)) {
3341 throw new IllegalArgumentException("Bad PendingIntent object");
3342 }
3343 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3344 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3345 pendingIntent.key.requestResolvedType);
3346 }
3347
Craig Mautnerdf88d732014-01-27 09:21:32 -08003348 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003349 public IBinder asBinder() {
3350 return this;
3351 }
3352
Craig Mautner4504de52013-12-20 09:06:56 -08003353 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003354 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003355 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003356 }
3357
Craig Mautner4a1cb222013-12-04 16:14:06 -08003358 ActivityStackSupervisor getOuter() {
3359 return ActivityStackSupervisor.this;
3360 }
3361
Craig Mautnerd163e752014-06-13 17:18:47 -07003362 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003363 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003364 }
3365
3366 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003367 synchronized (mService) {
3368 if (mActivityDisplay != null) {
3369 mActivityDisplay.getBounds(outBounds);
3370 } else {
3371 outBounds.set(0, 0);
3372 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003373 }
3374 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003375
Craig Mautner6985bad2014-04-21 15:22:06 -07003376 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003377 void setVisible(boolean visible) {
3378 if (mVisible != visible) {
3379 mVisible = visible;
3380 if (mCallback != null) {
3381 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3382 0 /* unused */, this).sendToTarget();
3383 }
3384 }
3385 }
3386
Craig Mautner6985bad2014-04-21 15:22:06 -07003387 void setDrawn() {
3388 }
3389
Craig Mautner1b4bf852014-05-26 15:06:32 -07003390 // You can always start a new task on a regular ActivityStack.
3391 boolean isEligibleForNewTasks() {
3392 return true;
3393 }
3394
Craig Mautnerd163e752014-06-13 17:18:47 -07003395 void onTaskListEmptyLocked() {
3396 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3397 if (!mStack.isHomeStack()) {
3398 detachLocked();
3399 deleteActivityContainer(this);
3400 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003401 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
3402 }
3403
Craig Mautner34b73df2014-01-12 21:11:08 -08003404 @Override
3405 public String toString() {
3406 return mIdString + (mActivityDisplay == null ? "N" : "A");
3407 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003408 }
3409
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003410 private class VirtualActivityContainer extends ActivityContainer {
3411 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003412 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003413
3414 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3415 super(getNextStackId());
3416 mParentActivity = parent;
3417 mCallback = callback;
3418 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003419 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003420 }
3421
3422 @Override
3423 public void setSurface(Surface surface, int width, int height, int density) {
3424 super.setSurface(surface, width, height, density);
3425
3426 synchronized (mService) {
3427 final long origId = Binder.clearCallingIdentity();
3428 try {
3429 setSurfaceLocked(surface, width, height, density);
3430 } finally {
3431 Binder.restoreCallingIdentity(origId);
3432 }
3433 }
3434 }
3435
3436 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3437 if (mContainerState == CONTAINER_STATE_FINISHING) {
3438 return;
3439 }
3440 VirtualActivityDisplay virtualActivityDisplay =
3441 (VirtualActivityDisplay) mActivityDisplay;
3442 if (virtualActivityDisplay == null) {
3443 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003444 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003445 mActivityDisplay = virtualActivityDisplay;
3446 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3447 attachToDisplayLocked(virtualActivityDisplay);
3448 }
3449
3450 if (mSurface != null) {
3451 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003452 }
3453
Craig Mautner6985bad2014-04-21 15:22:06 -07003454 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003455 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003456 mStack.resumeTopActivityLocked(null);
3457 } else {
3458 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003459 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003460 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
3461 mStack.startPausingLocked(false, true);
3462 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003463 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003464
Craig Mautnerd163e752014-06-13 17:18:47 -07003465 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003466
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003467 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3468 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003469 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003470
Craig Mautner6985bad2014-04-21 15:22:06 -07003471 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003472 boolean isAttachedLocked() {
3473 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003474 }
3475
3476 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003477 void setDrawn() {
3478 synchronized (mService) {
3479 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003480 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003481 }
3482 }
3483
Craig Mautner1b4bf852014-05-26 15:06:32 -07003484 // Never start a new task on an ActivityView if it isn't explicitly specified.
3485 @Override
3486 boolean isEligibleForNewTasks() {
3487 return false;
3488 }
3489
Craig Mautnerd163e752014-06-13 17:18:47 -07003490 private void setSurfaceIfReadyLocked() {
3491 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003492 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3493 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3494 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3495 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3496 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003497 }
3498 }
3499
Craig Mautner4a1cb222013-12-04 16:14:06 -08003500 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3501 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003502 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003503 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003504 int mDisplayId;
3505 Display mDisplay;
3506 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003507
Craig Mautner4a1cb222013-12-04 16:14:06 -08003508 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3509 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08003510 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003511
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003512 ActivityDisplay() {
3513 }
Craig Mautner4504de52013-12-20 09:06:56 -08003514
Craig Mautnere0a38842013-12-16 16:14:02 -08003515 ActivityDisplay(int displayId) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003516 init(mDisplayManager.getDisplay(displayId));
Craig Mautner4504de52013-12-20 09:06:56 -08003517 }
3518
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003519 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003520 mDisplay = display;
3521 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003522 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003523 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003524
3525 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003526 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
3527 + mDisplayId);
3528 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003529 }
3530
Craig Mautnere0a38842013-12-16 16:14:02 -08003531 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003532 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003533 + " from displayId=" + mDisplayId);
3534 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003535 }
3536
3537 void getBounds(Point bounds) {
3538 mDisplay.getDisplayInfo(mDisplayInfo);
3539 bounds.x = mDisplayInfo.appWidth;
3540 bounds.y = mDisplayInfo.appHeight;
3541 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003542
3543 @Override
3544 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003545 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3546 }
3547 }
3548
3549 class VirtualActivityDisplay extends ActivityDisplay {
3550 VirtualDisplay mVirtualDisplay;
3551
Craig Mautner6985bad2014-04-21 15:22:06 -07003552 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003553 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
3554 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, VIRTUAL_DISPLAY_BASE_NAME,
Craig Mautner6985bad2014-04-21 15:22:06 -07003555 width, height, density, null, DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003556 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY);
3557
3558 init(mVirtualDisplay.getDisplay());
3559
3560 mWindowManager.handleDisplayAdded(mDisplayId);
3561 }
3562
3563 void setSurface(Surface surface) {
3564 if (mVirtualDisplay != null) {
3565 mVirtualDisplay.setSurface(surface);
3566 }
3567 }
3568
3569 @Override
3570 void detachActivitiesLocked(ActivityStack stack) {
3571 super.detachActivitiesLocked(stack);
3572 if (mVirtualDisplay != null) {
3573 mVirtualDisplay.release();
3574 mVirtualDisplay = null;
3575 }
3576 }
3577
3578 @Override
3579 public String toString() {
3580 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003581 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003582 }
Jose Lima58e66d62014-05-27 20:00:27 -07003583
3584 private boolean isLeanbackOnlyDevice() {
3585 boolean onLeanbackOnly = false;
3586 try {
3587 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
3588 PackageManager.FEATURE_LEANBACK_ONLY);
3589 } catch (RemoteException e) {
3590 // noop
3591 }
3592
3593 return onLeanbackOnly;
3594 }
Craig Mautner27084302013-03-25 08:05:25 -07003595}