blob: bc184c6a2b257c7b90625992c3f6179f2d4def21 [file] [log] [blame]
Craig Mautner27084302013-03-25 08:05:25 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
Craig Mautner6170f732013-04-02 13:05:23 -070019import static android.Manifest.permission.START_ANY_ACTIVITY;
Craig Mautner29219d92013-04-16 20:19:12 -070020import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
21import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
Craig Mautner6170f732013-04-02 13:05:23 -070022import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Craig Mautner2420ead2013-04-01 17:13:20 -070023import static com.android.server.am.ActivityManagerService.localLOGV;
Craig Mautner23ac33b2013-04-01 16:26:35 -070024import static com.android.server.am.ActivityManagerService.DEBUG_CONFIGURATION;
Craig Mautnere7c58b62013-06-12 20:19:00 -070025import static com.android.server.am.ActivityManagerService.DEBUG_FOCUS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070026import static com.android.server.am.ActivityManagerService.DEBUG_PAUSE;
Craig Mautner6170f732013-04-02 13:05:23 -070027import static com.android.server.am.ActivityManagerService.DEBUG_RESULTS;
Craig Mautner0eea92c2013-05-16 13:35:39 -070028import static com.android.server.am.ActivityManagerService.DEBUG_STACK;
Craig Mautner2420ead2013-04-01 17:13:20 -070029import static com.android.server.am.ActivityManagerService.DEBUG_SWITCH;
Craig Mautner8849a5e2013-04-02 16:41:03 -070030import static com.android.server.am.ActivityManagerService.DEBUG_TASKS;
31import static com.android.server.am.ActivityManagerService.DEBUG_USER_LEAVING;
Craig Mautner05d29032013-05-03 13:40:13 -070032import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
Craig Mautner8d341ef2013-03-26 09:03:27 -070033import static com.android.server.am.ActivityManagerService.TAG;
Craig Mautner84984fa2014-06-19 11:19:20 -070034import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
35import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
36import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
Craig Mautner8d341ef2013-03-26 09:03:27 -070037
Craig Mautner2420ead2013-04-01 17:13:20 -070038import android.app.Activity;
Craig Mautner23ac33b2013-04-01 16:26:35 -070039import android.app.ActivityManager;
Craig Mautnered6649f2013-12-02 14:08:25 -080040import android.app.ActivityManager.StackInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070041import android.app.ActivityOptions;
42import android.app.AppGlobals;
Craig Mautner4a1cb222013-12-04 16:14:06 -080043import android.app.IActivityContainer;
44import android.app.IActivityContainerCallback;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070045import android.app.IActivityManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070046import android.app.IApplicationThread;
Craig Mautner23ac33b2013-04-01 16:26:35 -070047import android.app.PendingIntent;
Craig Mautner20e72272013-04-01 13:45:53 -070048import android.app.ActivityManager.RunningTaskInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070049import android.app.IActivityManager.WaitResult;
Craig Mautner2420ead2013-04-01 17:13:20 -070050import android.app.ResultInfo;
justinzhang5286d3f2014-05-12 17:06:01 -040051import android.app.StatusBarManager;
Jason Monk35c62a42014-06-17 10:24:47 -040052import android.app.admin.IDevicePolicyManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070053import android.content.ComponentName;
Craig Mautner2219a1b2013-03-25 09:44:30 -070054import android.content.Context;
Craig Mautner23ac33b2013-04-01 16:26:35 -070055import android.content.IIntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070056import android.content.Intent;
Craig Mautner23ac33b2013-04-01 16:26:35 -070057import android.content.IntentSender;
Craig Mautner2219a1b2013-03-25 09:44:30 -070058import android.content.pm.ActivityInfo;
Craig Mautner23ac33b2013-04-01 16:26:35 -070059import android.content.pm.ApplicationInfo;
60import android.content.pm.PackageManager;
61import android.content.pm.ResolveInfo;
62import android.content.res.Configuration;
Craig Mautner4a1cb222013-12-04 16:14:06 -080063import android.graphics.Point;
64import android.hardware.display.DisplayManager;
65import android.hardware.display.DisplayManager.DisplayListener;
Craig Mautner4504de52013-12-20 09:06:56 -080066import android.hardware.display.DisplayManagerGlobal;
67import android.hardware.display.VirtualDisplay;
Jeff Brownca9bc702014-02-11 14:32:56 -080068import android.hardware.input.InputManager;
69import android.hardware.input.InputManagerInternal;
Craig Mautner23ac33b2013-04-01 16:26:35 -070070import android.os.Binder;
Craig Mautner8d341ef2013-03-26 09:03:27 -070071import android.os.Bundle;
Craig Mautnerb59dcfd2013-05-06 13:12:58 -070072import android.os.Debug;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -070073import android.os.Handler;
Craig Mautner23ac33b2013-04-01 16:26:35 -070074import android.os.IBinder;
Craig Mautner2219a1b2013-03-25 09:44:30 -070075import android.os.Looper;
Craig Mautner2420ead2013-04-01 17:13:20 -070076import android.os.Message;
Craig Mautner23ac33b2013-04-01 16:26:35 -070077import android.os.ParcelFileDescriptor;
Craig Mautner0eea92c2013-05-16 13:35:39 -070078import android.os.PowerManager;
Craig Mautner7ea5bd42013-07-05 15:27:08 -070079import android.os.Process;
Craig Mautner8d341ef2013-03-26 09:03:27 -070080import android.os.RemoteException;
justinzhang5286d3f2014-05-12 17:06:01 -040081import android.os.ServiceManager;
Craig Mautner23ac33b2013-04-01 16:26:35 -070082import android.os.SystemClock;
Craig Mautner6170f732013-04-02 13:05:23 -070083import android.os.UserHandle;
Jason Monk62515be2014-05-21 16:06:19 -040084import android.provider.Settings;
85import android.provider.Settings.SettingNotFoundException;
Dianne Hackborn91097de2014-04-04 18:02:06 -070086import android.service.voice.IVoiceInteractionSession;
Craig Mautner2420ead2013-04-01 17:13:20 -070087import android.util.EventLog;
Craig Mautner8d341ef2013-03-26 09:03:27 -070088import android.util.Slog;
Craig Mautner4a1cb222013-12-04 16:14:06 -080089import android.util.SparseArray;
Craig Mautner2219a1b2013-03-25 09:44:30 -070090
Craig Mautner4a1cb222013-12-04 16:14:06 -080091import android.util.SparseIntArray;
Craig Mautnered6649f2013-12-02 14:08:25 -080092import android.view.Display;
Craig Mautner4a1cb222013-12-04 16:14:06 -080093import android.view.DisplayInfo;
Jeff Brownca9bc702014-02-11 14:32:56 -080094import android.view.InputEvent;
Craig Mautner4504de52013-12-20 09:06:56 -080095import android.view.Surface;
Craig Mautner23ac33b2013-04-01 16:26:35 -070096import com.android.internal.app.HeavyWeightSwitcherActivity;
Dianne Hackborn91097de2014-04-04 18:02:06 -070097import com.android.internal.app.IVoiceInteractor;
Dianne Hackborncbfd23e2013-06-11 14:26:53 -070098import com.android.internal.os.TransferPipe;
justinzhang5286d3f2014-05-12 17:06:01 -040099import com.android.internal.statusbar.IStatusBarService;
Jeff Brownca9bc702014-02-11 14:32:56 -0800100import com.android.server.LocalServices;
Craig Mautner6170f732013-04-02 13:05:23 -0700101import com.android.server.am.ActivityManagerService.PendingActivityLaunch;
Craig Mautner2420ead2013-04-01 17:13:20 -0700102import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700103import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700104
justinzhang5286d3f2014-05-12 17:06:01 -0400105
Craig Mautner8d341ef2013-03-26 09:03:27 -0700106import java.io.FileDescriptor;
107import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700108import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700109import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700110import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700111
Craig Mautner4a1cb222013-12-04 16:14:06 -0800112public final class ActivityStackSupervisor implements DisplayListener {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700113 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
114 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
115 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700116 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700117 static final boolean DEBUG_IDLE = DEBUG || false;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700118 static final boolean DEBUG_MEDIA_VISIBILITY = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700119 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
120 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
121 static final boolean DEBUG_STATES = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700122
Craig Mautner2219a1b2013-03-25 09:44:30 -0700123 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700124
Craig Mautnerf3333272013-04-22 10:55:53 -0700125 /** How long we wait until giving up on the last activity telling us it is idle. */
126 static final int IDLE_TIMEOUT = 10*1000;
127
Craig Mautner0eea92c2013-05-16 13:35:39 -0700128 /** How long we can hold the sleep wake lock before giving up. */
129 static final int SLEEP_TIMEOUT = 5*1000;
130
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700131 // How long we can hold the launch wake lock before giving up.
132 static final int LAUNCH_TIMEOUT = 10*1000;
133
Craig Mautner05d29032013-05-03 13:40:13 -0700134 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
135 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
136 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700137 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700138 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800139 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
140 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
141 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700142 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400143 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
144 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700145 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4c07d022014-06-11 17:12:59 -0700146 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800147
Craig Mautner4504de52013-12-20 09:06:56 -0800148 private final static String VIRTUAL_DISPLAY_BASE_NAME = "ActivityViewVirtualDisplay";
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700149
Jason Monk62515be2014-05-21 16:06:19 -0400150 private static final String LOCK_TASK_TAG = "Lock-to-App";
151
justinzhang5286d3f2014-05-12 17:06:01 -0400152 /** Status Bar Service **/
153 private IBinder mToken = new Binder();
154 private IStatusBarService mStatusBarService;
Jason Monk35c62a42014-06-17 10:24:47 -0400155 private IDevicePolicyManager mDevicePolicyManager;
justinzhang5286d3f2014-05-12 17:06:01 -0400156
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700157 // For debugging to make sure the caller when acquiring/releasing our
158 // wake lock is the system process.
159 static final boolean VALIDATE_WAKE_LOCK_CALLER = false;
Craig Mautnerf3333272013-04-22 10:55:53 -0700160
Craig Mautner27084302013-03-25 08:05:25 -0700161 final ActivityManagerService mService;
162
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700163 final ActivityStackSupervisorHandler mHandler;
164
165 /** Short cut */
166 WindowManagerService mWindowManager;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800167 DisplayManager mDisplayManager;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700168
Craig Mautner27084302013-03-25 08:05:25 -0700169 /** Dismiss the keyguard after the next activity is displayed? */
Craig Mautner5314a402013-09-26 12:40:16 -0700170 boolean mDismissKeyguardOnNextActivity = false;
Craig Mautner27084302013-03-25 08:05:25 -0700171
Craig Mautner8d341ef2013-03-26 09:03:27 -0700172 /** Identifier counter for all ActivityStacks */
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700173 private int mLastStackId = HOME_STACK_ID;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700174
175 /** Task identifier that activities are currently being started in. Incremented each time a
176 * new task is created. */
177 private int mCurTaskId = 0;
178
Craig Mautner2420ead2013-04-01 17:13:20 -0700179 /** The current user */
180 private int mCurrentUser;
181
Craig Mautnere0a38842013-12-16 16:14:02 -0800182 /** The stack containing the launcher app. Assumed to always be attached to
183 * Display.DEFAULT_DISPLAY. */
Craig Mautner2219a1b2013-03-25 09:44:30 -0700184 private ActivityStack mHomeStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700185
Craig Mautnere0a38842013-12-16 16:14:02 -0800186 /** The stack currently receiving input or launching the next activity. */
Craig Mautner29219d92013-04-16 20:19:12 -0700187 private ActivityStack mFocusedStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700188
Craig Mautner4a1cb222013-12-04 16:14:06 -0800189 /** If this is the same as mFocusedStack then the activity on the top of the focused stack has
190 * been resumed. If stacks are changing position this will hold the old stack until the new
Craig Mautnere0a38842013-12-16 16:14:02 -0800191 * stack becomes resumed after which it will be set to mFocusedStack. */
Craig Mautner4a1cb222013-12-04 16:14:06 -0800192 private ActivityStack mLastFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700193
194 /** List of activities that are waiting for a new activity to become visible before completing
195 * whatever operation they are supposed to do. */
196 final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();
197
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700198 /** List of processes waiting to find out about the next visible activity. */
199 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
200 new ArrayList<IActivityManager.WaitResult>();
201
202 /** List of processes waiting to find out about the next launched activity. */
203 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
204 new ArrayList<IActivityManager.WaitResult>();
205
Craig Mautnerde4ef022013-04-07 19:01:33 -0700206 /** List of activities that are ready to be stopped, but waiting for the next activity to
207 * settle down before doing so. */
208 final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
209
Craig Mautnerf3333272013-04-22 10:55:53 -0700210 /** List of activities that are ready to be finished, but waiting for the previous activity to
211 * settle down before doing so. It contains ActivityRecord objects. */
212 final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
213
Craig Mautner0eea92c2013-05-16 13:35:39 -0700214 /** List of activities that are in the process of going to sleep. */
215 final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
216
Craig Mautnerf3333272013-04-22 10:55:53 -0700217 /** Used on user changes */
218 final ArrayList<UserStartedState> mStartingUsers = new ArrayList<UserStartedState>();
219
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -0700220 /** Used to queue up any background users being started */
221 final ArrayList<UserStartedState> mStartingBackgroundUsers = new ArrayList<UserStartedState>();
222
Craig Mautnerde4ef022013-04-07 19:01:33 -0700223 /** Set to indicate whether to issue an onUserLeaving callback when a newly launched activity
224 * is being brought in front of us. */
225 boolean mUserLeaving = false;
226
Craig Mautner0eea92c2013-05-16 13:35:39 -0700227 /** Set when we have taken too long waiting to go to sleep. */
228 boolean mSleepTimeout = false;
229
Jose Lima58e66d62014-05-27 20:00:27 -0700230 /** Indicates if we are running on a Leanback-only (TV) device. Only initialized after
231 * setWindowManager is called. **/
232 private boolean mLeanbackOnlyDevice;
233
Craig Mautner0eea92c2013-05-16 13:35:39 -0700234 /**
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700235 * We don't want to allow the device to go to sleep while in the process
236 * of launching an activity. This is primarily to allow alarm intent
237 * receivers to launch an activity and get that to run before the device
238 * goes back to sleep.
239 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700240 PowerManager.WakeLock mLaunchingActivity;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700241
242 /**
Craig Mautner0eea92c2013-05-16 13:35:39 -0700243 * Set when the system is going to sleep, until we have
244 * successfully paused the current activity and released our wake lock.
245 * At that point the system is allowed to actually sleep.
246 */
Jeff Brown2c43c332014-06-12 22:38:59 -0700247 PowerManager.WakeLock mGoingToSleep;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700248
Craig Mautner4f1df4f2013-10-15 15:44:14 -0700249 /** Stack id of the front stack when user switched, indexed by userId. */
250 SparseIntArray mUserStackInFront = new SparseIntArray(2);
Craig Mautner93529a42013-10-04 15:03:13 -0700251
Craig Mautner4504de52013-12-20 09:06:56 -0800252 // TODO: Add listener for removal of references.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800253 /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
Craig Mautnerd163e752014-06-13 17:18:47 -0700254 private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800255
256 /** Mapping from displayId to display current state */
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700257 private final SparseArray<ActivityDisplay> mActivityDisplays =
258 new SparseArray<ActivityDisplay>();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800259
Jeff Brownca9bc702014-02-11 14:32:56 -0800260 InputManagerInternal mInputManagerInternal;
261
Craig Mautneraea74a52014-03-08 14:23:10 -0800262 /** If non-null then the task specified remains in front and no other tasks may be started
263 * until the task exits or #stopLockTaskMode() is called. */
Jason Monkd7b86212014-06-16 13:15:38 -0400264 TaskRecord mLockTaskModeTask;
Jason Monka8f569c2014-07-07 12:15:21 -0400265 /** Whether lock task has been entered by an authorized app and cannot
266 * be exited. */
267 private boolean mLockTaskIsLocked;
Jason Monk62515be2014-05-21 16:06:19 -0400268 /**
269 * Notifies the user when entering/exiting lock-task.
270 */
271 private LockTaskNotify mLockTaskNotify;
Craig Mautneraea74a52014-03-08 14:23:10 -0800272
Craig Mautner4a1cb222013-12-04 16:14:06 -0800273 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700274 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700275 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
276 }
277
278 /**
279 * At the time when the constructor runs, the power manager has not yet been
280 * initialized. So we initialize our wakelocks afterwards.
281 */
282 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800283 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700284 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700285 mLaunchingActivity =
286 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
287 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700288 }
289
justinzhang5286d3f2014-05-12 17:06:01 -0400290 // This function returns a IStatusBarService. The value is from ServiceManager.
291 // getService and is cached.
292 private IStatusBarService getStatusBarService() {
293 synchronized (mService) {
294 if (mStatusBarService == null) {
295 mStatusBarService = IStatusBarService.Stub.asInterface(
296 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
297 if (mStatusBarService == null) {
298 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
299 }
300 }
301 return mStatusBarService;
302 }
303 }
304
Jason Monk35c62a42014-06-17 10:24:47 -0400305 private IDevicePolicyManager getDevicePolicyManager() {
306 synchronized (mService) {
307 if (mDevicePolicyManager == null) {
308 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
309 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
310 if (mDevicePolicyManager == null) {
311 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
312 }
313 }
314 return mDevicePolicyManager;
315 }
316 }
317
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700318 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800319 synchronized (mService) {
320 mWindowManager = wm;
321
322 mDisplayManager =
323 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
324 mDisplayManager.registerDisplayListener(this, null);
325
326 Display[] displays = mDisplayManager.getDisplays();
327 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
328 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800329 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
330 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800331 }
332
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700333 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800334 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800335
336 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700337
338 // Initialize this here, now that we can get a valid reference to PackageManager.
339 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800340 }
Craig Mautner27084302013-03-25 08:05:25 -0700341 }
342
343 void dismissKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700344 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner27084302013-03-25 08:05:25 -0700345 if (mDismissKeyguardOnNextActivity) {
346 mDismissKeyguardOnNextActivity = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700347 mWindowManager.dismissKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700348 }
349 }
350
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700351 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800352 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700353 }
354
Craig Mautnerde4ef022013-04-07 19:01:33 -0700355 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800356 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700357 }
358
Craig Mautner4a1cb222013-12-04 16:14:06 -0800359 // TODO: Split into two methods isFrontStack for any visible stack and isFrontmostStack for the
360 // top of all visible stacks.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700361 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800362 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
363 if (parent != null) {
364 stack = parent.task.stack;
365 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800366 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800367 if (stacks != null && !stacks.isEmpty()) {
368 return stack == stacks.get(stacks.size() - 1);
369 }
370 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700371 }
372
Craig Mautnerde4ef022013-04-07 19:01:33 -0700373 void moveHomeStack(boolean toFront) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800374 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800375 int topNdx = stacks.size() - 1;
376 if (topNdx <= 0) {
377 return;
378 }
379 ActivityStack topStack = stacks.get(topNdx);
380 final boolean homeInFront = topStack == mHomeStack;
381 if (homeInFront != toFront) {
382 mLastFocusedStack = topStack;
383 stacks.remove(mHomeStack);
384 stacks.add(toFront ? topNdx : 0, mHomeStack);
385 mFocusedStack = stacks.get(topNdx);
386 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
387 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700388 }
389 }
390
Craig Mautner84984fa2014-06-19 11:19:20 -0700391 void moveHomeStackTaskToTop(int homeStackTaskType) {
392 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
393 mWindowManager.showRecentApps();
394 return;
395 }
Craig Mautner69ada552013-04-18 13:51:51 -0700396 moveHomeStack(true);
Craig Mautner84984fa2014-06-19 11:19:20 -0700397 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Craig Mautner8e569572013-10-11 17:36:59 -0700398 }
399
Craig Mautner84984fa2014-06-19 11:19:20 -0700400 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev) {
401 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
402 mWindowManager.showRecentApps();
403 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700404 }
Craig Mautner84984fa2014-06-19 11:19:20 -0700405 moveHomeStackTaskToTop(homeStackTaskType);
406 if (prev != null) {
407 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
408 }
409
Craig Mautnera8a90e02013-06-28 15:24:50 -0700410 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700411 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000412 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700413 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700414 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700415 }
416 return mService.startHomeActivityLocked(mCurrentUser);
417 }
418
Craig Mautner27084302013-03-25 08:05:25 -0700419 void setDismissKeyguard(boolean dismiss) {
Craig Mautner5314a402013-09-26 12:40:16 -0700420 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen(" dismiss=" + dismiss);
Craig Mautner27084302013-03-25 08:05:25 -0700421 mDismissKeyguardOnNextActivity = dismiss;
422 }
423
Craig Mautner8d341ef2013-03-26 09:03:27 -0700424 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800425 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800426 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800427 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800428 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
429 ActivityStack stack = stacks.get(stackNdx);
430 TaskRecord task = stack.taskForIdLocked(id);
431 if (task != null) {
432 return task;
433 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700434 }
435 }
436 return null;
437 }
438
Craig Mautner6170f732013-04-02 13:05:23 -0700439 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800440 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800441 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800442 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800443 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
444 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
445 if (r != null) {
446 return r;
447 }
Craig Mautner6170f732013-04-02 13:05:23 -0700448 }
449 }
450 return null;
451 }
452
Craig Mautneref73ee12014-04-23 11:45:37 -0700453 void setNextTaskId(int taskId) {
454 if (taskId > mCurTaskId) {
455 mCurTaskId = taskId;
456 }
457 }
458
Craig Mautner8d341ef2013-03-26 09:03:27 -0700459 int getNextTaskId() {
460 do {
461 mCurTaskId++;
462 if (mCurTaskId <= 0) {
463 mCurTaskId = 1;
464 }
465 } while (anyTaskForIdLocked(mCurTaskId) != null);
466 return mCurTaskId;
467 }
468
Craig Mautnerde4ef022013-04-07 19:01:33 -0700469 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700470 ActivityStack stack = getFocusedStack();
471 if (stack == null) {
472 return null;
473 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700474 ActivityRecord resumedActivity = stack.mResumedActivity;
475 if (resumedActivity == null || resumedActivity.app == null) {
476 resumedActivity = stack.mPausingActivity;
477 if (resumedActivity == null || resumedActivity.app == null) {
478 resumedActivity = stack.topRunningActivityLocked(null);
479 }
480 }
481 return resumedActivity;
482 }
483
Mike Lockwooded8902d2013-11-15 11:01:47 -0800484 boolean attachApplicationLocked(ProcessRecord app) throws Exception {
Craig Mautner20e72272013-04-01 13:45:53 -0700485 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800486 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800487 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
488 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800489 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
490 final ActivityStack stack = stacks.get(stackNdx);
491 if (!isFrontStack(stack)) {
492 continue;
493 }
494 ActivityRecord hr = stack.topRunningActivityLocked(null);
495 if (hr != null) {
496 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
497 && processName.equals(hr.processName)) {
498 try {
George Mount2c92c972014-03-20 09:38:23 -0700499 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800500 didSomething = true;
501 }
502 } catch (Exception e) {
503 Slog.w(TAG, "Exception in new application when starting activity "
504 + hr.intent.getComponent().flattenToShortString(), e);
505 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700506 }
Craig Mautner20e72272013-04-01 13:45:53 -0700507 }
Craig Mautner20e72272013-04-01 13:45:53 -0700508 }
509 }
510 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700511 if (!didSomething) {
512 ensureActivitiesVisibleLocked(null, 0);
513 }
Craig Mautner20e72272013-04-01 13:45:53 -0700514 return didSomething;
515 }
516
517 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800518 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
519 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800520 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
521 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800522 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800523 continue;
524 }
525 final ActivityRecord resumedActivity = stack.mResumedActivity;
526 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800527 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
528 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800529 return false;
530 }
Craig Mautner20e72272013-04-01 13:45:53 -0700531 }
532 }
533 return true;
534 }
535
Craig Mautnerde4ef022013-04-07 19:01:33 -0700536 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800537 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
538 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800539 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
540 final ActivityStack stack = stacks.get(stackNdx);
541 if (isFrontStack(stack)) {
542 final ActivityRecord r = stack.mResumedActivity;
543 if (r != null && r.state != ActivityState.RESUMED) {
544 return false;
545 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700546 }
547 }
548 }
549 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800550 if (DEBUG_STACK) Slog.d(TAG,
551 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
552 mLastFocusedStack + " to=" + mFocusedStack);
553 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700554 return true;
555 }
556
557 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800558 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
559 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800560 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
561 final ActivityStack stack = stacks.get(stackNdx);
562 final ActivityRecord r = stack.mResumedActivity;
563 if (r != null && (!r.nowVisible || r.waitingVisible)) {
564 return false;
565 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700566 }
567 }
568 return true;
569 }
570
Craig Mautner2acc3892013-09-23 10:28:14 -0700571 /**
572 * Pause all activities in either all of the stacks or just the back stacks.
573 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700574 * @return true if any activity was paused as a result of this call.
575 */
Craig Mautner5314a402013-09-26 12:40:16 -0700576 boolean pauseBackStacks(boolean userLeaving) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700577 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800578 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
579 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800580 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
581 final ActivityStack stack = stacks.get(stackNdx);
582 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
583 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
584 " mResumedActivity=" + stack.mResumedActivity);
585 stack.startPausingLocked(userLeaving, false);
586 someActivityPaused = true;
587 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700588 }
589 }
590 return someActivityPaused;
591 }
592
Craig Mautnerde4ef022013-04-07 19:01:33 -0700593 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700594 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800595 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
596 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800597 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
598 final ActivityStack stack = stacks.get(stackNdx);
599 final ActivityRecord r = stack.mPausingActivity;
600 if (r != null && r.state != ActivityState.PAUSED
601 && r.state != ActivityState.STOPPED
602 && r.state != ActivityState.STOPPING) {
603 if (DEBUG_STATES) {
604 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
605 pausing = false;
606 } else {
607 return false;
608 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700609 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700610 }
611 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700612 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700613 }
614
Craig Mautnerdf88d732014-01-27 09:21:32 -0800615 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping) {
John Spurlock8a985d22014-02-25 09:40:05 -0500616 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800617 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
618 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
619 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
620 final ActivityStack stack = stacks.get(stackNdx);
621 if (stack.mResumedActivity != null &&
622 stack.mActivityContainer.mParentActivity == parent) {
623 stack.startPausingLocked(userLeaving, uiSleeping);
624 }
625 }
626 }
627 }
628
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700629 void reportActivityVisibleLocked(ActivityRecord r) {
Craig Mautner858d8a62013-04-23 17:08:34 -0700630 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700631 WaitResult w = mWaitingActivityVisible.get(i);
632 w.timeout = false;
633 if (r != null) {
634 w.who = new ComponentName(r.info.packageName, r.info.name);
635 }
636 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
637 w.thisTime = w.totalTime;
638 }
639 mService.notifyAll();
640 dismissKeyguard();
641 }
642
643 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
644 long thisTime, long totalTime) {
645 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700646 WaitResult w = mWaitingActivityLaunched.remove(i);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700647 w.timeout = timeout;
648 if (r != null) {
649 w.who = new ComponentName(r.info.packageName, r.info.name);
650 }
651 w.thisTime = thisTime;
652 w.totalTime = totalTime;
653 }
654 mService.notifyAll();
655 }
656
Craig Mautner29219d92013-04-16 20:19:12 -0700657 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700658 final ActivityStack focusedStack = getFocusedStack();
659 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
660 if (r != null) {
661 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700662 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700663
Craig Mautner4a1cb222013-12-04 16:14:06 -0800664 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800665 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800666 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
667 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700668 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700669 r = stack.topRunningActivityLocked(null);
670 if (r != null) {
671 return r;
672 }
673 }
674 }
675 return null;
676 }
677
Dianne Hackborn09233282014-04-30 11:33:59 -0700678 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700679 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800680 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
681 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800682 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800683 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800684 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800685 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
686 final ActivityStack stack = stacks.get(stackNdx);
687 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
688 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700689 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700690 }
691 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700692
693 // The lists are already sorted from most recent to oldest. Just pull the most recent off
694 // each list and add it to list. Stop when all lists are empty or maxNum reached.
695 while (maxNum > 0) {
696 long mostRecentActiveTime = Long.MIN_VALUE;
697 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800698 final int numTaskLists = runningTaskLists.size();
699 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
700 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700701 if (!stackTaskList.isEmpty()) {
702 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
703 if (lastActiveTime > mostRecentActiveTime) {
704 mostRecentActiveTime = lastActiveTime;
705 selectedStackList = stackTaskList;
706 }
707 }
708 }
709 if (selectedStackList != null) {
710 list.add(selectedStackList.remove(0));
711 --maxNum;
712 } else {
713 break;
714 }
715 }
Craig Mautner20e72272013-04-01 13:45:53 -0700716 }
717
Craig Mautner23ac33b2013-04-01 16:26:35 -0700718 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
719 String profileFile, ParcelFileDescriptor profileFd, int userId) {
720 // Collect information about the target of the Intent.
721 ActivityInfo aInfo;
722 try {
723 ResolveInfo rInfo =
724 AppGlobals.getPackageManager().resolveIntent(
725 intent, resolvedType,
726 PackageManager.MATCH_DEFAULT_ONLY
727 | ActivityManagerService.STOCK_PM_FLAGS, userId);
728 aInfo = rInfo != null ? rInfo.activityInfo : null;
729 } catch (RemoteException e) {
730 aInfo = null;
731 }
732
733 if (aInfo != null) {
734 // Store the found target back into the intent, because now that
735 // we have it we never want to do this again. For example, if the
736 // user navigates back to this point in the history, we should
737 // always restart the exact same activity.
738 intent.setComponent(new ComponentName(
739 aInfo.applicationInfo.packageName, aInfo.name));
740
741 // Don't debug things in the system process
742 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
743 if (!aInfo.processName.equals("system")) {
744 mService.setDebugApp(aInfo.processName, true, false);
745 }
746 }
747
748 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
749 if (!aInfo.processName.equals("system")) {
750 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
751 }
752 }
753
754 if (profileFile != null) {
755 if (!aInfo.processName.equals("system")) {
756 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName,
757 profileFile, profileFd,
758 (startFlags&ActivityManager.START_FLAG_AUTO_STOP_PROFILER) != 0);
759 }
760 }
761 }
762 return aInfo;
763 }
764
Craig Mautner2219a1b2013-03-25 09:44:30 -0700765 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700766 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700767 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null, 0,
Craig Mautnere0a38842013-12-16 16:14:02 -0800768 null, false, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700769 }
770
Craig Mautner23ac33b2013-04-01 16:26:35 -0700771 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700772 String callingPackage, Intent intent, String resolvedType,
773 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
774 IBinder resultTo, String resultWho, int requestCode, int startFlags, String profileFile,
Craig Mautner23ac33b2013-04-01 16:26:35 -0700775 ParcelFileDescriptor profileFd, WaitResult outResult, Configuration config,
Craig Mautnere0a38842013-12-16 16:14:02 -0800776 Bundle options, int userId, IActivityContainer iContainer) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700777 // Refuse possible leaked file descriptors
778 if (intent != null && intent.hasFileDescriptors()) {
779 throw new IllegalArgumentException("File descriptors passed in Intent");
780 }
781 boolean componentSpecified = intent.getComponent() != null;
782
783 // Don't modify the client's object!
784 intent = new Intent(intent);
785
786 // Collect information about the target of the Intent.
787 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
788 profileFile, profileFd, userId);
789
Craig Mautnere0a38842013-12-16 16:14:02 -0800790 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700791 synchronized (mService) {
792 int callingPid;
793 if (callingUid >= 0) {
794 callingPid = -1;
795 } else if (caller == null) {
796 callingPid = Binder.getCallingPid();
797 callingUid = Binder.getCallingUid();
798 } else {
799 callingPid = callingUid = -1;
800 }
801
Craig Mautnere0a38842013-12-16 16:14:02 -0800802 final ActivityStack stack;
803 if (container == null || container.mStack.isOnHomeDisplay()) {
804 stack = getFocusedStack();
805 } else {
806 stack = container.mStack;
807 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700808 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700809 && mService.mConfiguration.diff(config) != 0;
810 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700811 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700812
813 final long origId = Binder.clearCallingIdentity();
814
815 if (aInfo != null &&
816 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
817 // This may be a heavy-weight process! Check to see if we already
818 // have another, different heavy-weight process running.
819 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
820 if (mService.mHeavyWeightProcess != null &&
821 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
822 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700823 int realCallingUid = callingUid;
824 if (caller != null) {
825 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
826 if (callerApp != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700827 realCallingUid = callerApp.info.uid;
828 } else {
829 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700830 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700831 + intent.toString());
832 ActivityOptions.abort(options);
833 return ActivityManager.START_PERMISSION_DENIED;
834 }
835 }
836
837 IIntentSender target = mService.getIntentSenderLocked(
838 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
839 realCallingUid, userId, null, null, 0, new Intent[] { intent },
840 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
841 | PendingIntent.FLAG_ONE_SHOT, null);
842
843 Intent newIntent = new Intent();
844 if (requestCode >= 0) {
845 // Caller is requesting a result.
846 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
847 }
848 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
849 new IntentSender(target));
850 if (mService.mHeavyWeightProcess.activities.size() > 0) {
851 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
852 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
853 hist.packageName);
854 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
855 hist.task.taskId);
856 }
857 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
858 aInfo.packageName);
859 newIntent.setFlags(intent.getFlags());
860 newIntent.setClassName("android",
861 HeavyWeightSwitcherActivity.class.getName());
862 intent = newIntent;
863 resolvedType = null;
864 caller = null;
865 callingUid = Binder.getCallingUid();
866 callingPid = Binder.getCallingPid();
867 componentSpecified = true;
868 try {
869 ResolveInfo rInfo =
870 AppGlobals.getPackageManager().resolveIntent(
871 intent, null,
872 PackageManager.MATCH_DEFAULT_ONLY
873 | ActivityManagerService.STOCK_PM_FLAGS, userId);
874 aInfo = rInfo != null ? rInfo.activityInfo : null;
875 aInfo = mService.getActivityInfoForUser(aInfo, userId);
876 } catch (RemoteException e) {
877 aInfo = null;
878 }
879 }
880 }
881 }
882
Dianne Hackborn91097de2014-04-04 18:02:06 -0700883 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
884 voiceSession, voiceInteractor, resultTo, resultWho,
Craig Mautnere0a38842013-12-16 16:14:02 -0800885 requestCode, callingPid, callingUid, callingPackage, startFlags, options,
886 componentSpecified, null, container);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700887
Craig Mautnerde4ef022013-04-07 19:01:33 -0700888 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700889 // If the caller also wants to switch to a new configuration,
890 // do so now. This allows a clean switch, as we are waiting
891 // for the current activity to pause (so we will not destroy
892 // it), and have not yet started the next activity.
893 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
894 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700895 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700896 if (DEBUG_CONFIGURATION) Slog.v(TAG,
897 "Updating to new configuration after starting activity.");
898 mService.updateConfigurationLocked(config, null, false, false);
899 }
900
901 Binder.restoreCallingIdentity(origId);
902
903 if (outResult != null) {
904 outResult.result = res;
905 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700906 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700907 do {
908 try {
909 mService.wait();
910 } catch (InterruptedException e) {
911 }
912 } while (!outResult.timeout && outResult.who == null);
913 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700914 ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700915 if (r.nowVisible) {
916 outResult.timeout = false;
917 outResult.who = new ComponentName(r.info.packageName, r.info.name);
918 outResult.totalTime = 0;
919 outResult.thisTime = 0;
920 } else {
921 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700922 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700923 do {
924 try {
925 mService.wait();
926 } catch (InterruptedException e) {
927 }
928 } while (!outResult.timeout && outResult.who == null);
929 }
930 }
931 }
932
933 return res;
934 }
935 }
936
937 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
938 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
939 Bundle options, int userId) {
940 if (intents == null) {
941 throw new NullPointerException("intents is null");
942 }
943 if (resolvedTypes == null) {
944 throw new NullPointerException("resolvedTypes is null");
945 }
946 if (intents.length != resolvedTypes.length) {
947 throw new IllegalArgumentException("intents are length different than resolvedTypes");
948 }
949
Craig Mautner23ac33b2013-04-01 16:26:35 -0700950
951 int callingPid;
952 if (callingUid >= 0) {
953 callingPid = -1;
954 } else if (caller == null) {
955 callingPid = Binder.getCallingPid();
956 callingUid = Binder.getCallingUid();
957 } else {
958 callingPid = callingUid = -1;
959 }
960 final long origId = Binder.clearCallingIdentity();
961 try {
962 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -0700963 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -0700964 for (int i=0; i<intents.length; i++) {
965 Intent intent = intents[i];
966 if (intent == null) {
967 continue;
968 }
969
970 // Refuse possible leaked file descriptors
971 if (intent != null && intent.hasFileDescriptors()) {
972 throw new IllegalArgumentException("File descriptors passed in Intent");
973 }
974
975 boolean componentSpecified = intent.getComponent() != null;
976
977 // Don't modify the client's object!
978 intent = new Intent(intent);
979
980 // Collect information about the target of the Intent.
981 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i],
982 0, null, null, userId);
983 // TODO: New, check if this is correct
984 aInfo = mService.getActivityInfoForUser(aInfo, userId);
985
986 if (aInfo != null &&
987 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
988 != 0) {
989 throw new IllegalArgumentException(
990 "FLAG_CANT_SAVE_STATE not supported here");
991 }
992
993 Bundle theseOptions;
994 if (options != null && i == intents.length-1) {
995 theseOptions = options;
996 } else {
997 theseOptions = null;
998 }
Craig Mautner6170f732013-04-02 13:05:23 -0700999 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn91097de2014-04-04 18:02:06 -07001000 aInfo, null, null, resultTo, null, -1, callingPid, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001001 0, theseOptions, componentSpecified, outActivity, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001002 if (res < 0) {
1003 return res;
1004 }
1005
1006 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1007 }
1008 }
1009 } finally {
1010 Binder.restoreCallingIdentity(origId);
1011 }
1012
1013 return ActivityManager.START_SUCCESS;
1014 }
1015
Craig Mautner2420ead2013-04-01 17:13:20 -07001016 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001017 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001018 throws RemoteException {
1019
1020 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001021 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001022 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001023
1024 // schedule launch ticks to collect information about slow apps.
1025 r.startLaunchTickingLocked();
1026
1027 // Have the window manager re-evaluate the orientation of
1028 // the screen based on the new activity order. Note that
1029 // as a result of this, it can call back into the activity
1030 // manager with a new orientation. We don't care about that,
1031 // because the activity is not currently running so we are
1032 // just restarting it anyway.
1033 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001034 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001035 mService.mConfiguration,
1036 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1037 mService.updateConfigurationLocked(config, r, false, false);
1038 }
1039
1040 r.app = app;
1041 app.waitingToKill = null;
1042 r.launchCount++;
1043 r.lastLaunchTime = SystemClock.uptimeMillis();
1044
1045 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1046
1047 int idx = app.activities.indexOf(r);
1048 if (idx < 0) {
1049 app.activities.add(r);
1050 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001051 mService.updateLruProcessLocked(app, true, null);
1052 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001053
1054 final ActivityStack stack = r.task.stack;
1055 try {
1056 if (app.thread == null) {
1057 throw new RemoteException();
1058 }
1059 List<ResultInfo> results = null;
1060 List<Intent> newIntents = null;
1061 if (andResume) {
1062 results = r.results;
1063 newIntents = r.newIntents;
1064 }
1065 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1066 + " icicle=" + r.icicle
1067 + " with results=" + results + " newIntents=" + newIntents
1068 + " andResume=" + andResume);
1069 if (andResume) {
1070 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1071 r.userId, System.identityHashCode(r),
1072 r.task.taskId, r.shortComponentName);
1073 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001074 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001075 // Home process is the root process of the task.
1076 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001077 }
1078 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1079 r.sleeping = false;
1080 r.forceNewConfig = false;
1081 mService.showAskCompatModeDialogLocked(r);
1082 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1083 String profileFile = null;
1084 ParcelFileDescriptor profileFd = null;
1085 boolean profileAutoStop = false;
1086 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1087 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1088 mService.mProfileProc = app;
1089 profileFile = mService.mProfileFile;
1090 profileFd = mService.mProfileFd;
1091 profileAutoStop = mService.mAutoStopProfiler;
1092 }
1093 }
1094 app.hasShownUi = true;
1095 app.pendingUiClean = true;
1096 if (profileFd != null) {
1097 try {
1098 profileFd = profileFd.dup();
1099 } catch (IOException e) {
1100 if (profileFd != null) {
1101 try {
1102 profileFd.close();
1103 } catch (IOException o) {
1104 }
1105 profileFd = null;
1106 }
1107 }
1108 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001109
Dianne Hackborna413dc02013-07-12 12:02:55 -07001110 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001111 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1112 System.identityHashCode(r), r.info,
Craig Mautnera0026042014-04-23 11:45:37 -07001113 new Configuration(mService.mConfiguration), r.compat, r.task.voiceInteractor,
1114 app.repProcState, r.icicle, r.persistentState, results, newIntents, !andResume,
Craig Mautner233ceee2014-05-09 17:05:11 -07001115 mService.isNextTransitionForward(), profileFile, profileFd, profileAutoStop
1116 );
Craig Mautner2420ead2013-04-01 17:13:20 -07001117
1118 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1119 // This may be a heavy-weight process! Note that the package
1120 // manager will ensure that only activity can run in the main
1121 // process of the .apk, which is the only thing that will be
1122 // considered heavy-weight.
1123 if (app.processName.equals(app.info.packageName)) {
1124 if (mService.mHeavyWeightProcess != null
1125 && mService.mHeavyWeightProcess != app) {
1126 Slog.w(TAG, "Starting new heavy weight process " + app
1127 + " when already running "
1128 + mService.mHeavyWeightProcess);
1129 }
1130 mService.mHeavyWeightProcess = app;
1131 Message msg = mService.mHandler.obtainMessage(
1132 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1133 msg.obj = r;
1134 mService.mHandler.sendMessage(msg);
1135 }
1136 }
1137
1138 } catch (RemoteException e) {
1139 if (r.launchFailed) {
1140 // This is the second time we failed -- finish activity
1141 // and give up.
1142 Slog.e(TAG, "Second failure launching "
1143 + r.intent.getComponent().flattenToShortString()
1144 + ", giving up", e);
1145 mService.appDiedLocked(app, app.pid, app.thread);
1146 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1147 "2nd-crash", false);
1148 return false;
1149 }
1150
1151 // This is the first time we failed -- restart process and
1152 // retry.
1153 app.activities.remove(r);
1154 throw e;
1155 }
1156
1157 r.launchFailed = false;
1158 if (stack.updateLRUListLocked(r)) {
1159 Slog.w(TAG, "Activity " + r
1160 + " being launched, but already in LRU list");
1161 }
1162
1163 if (andResume) {
1164 // As part of the process of launching, ActivityThread also performs
1165 // a resume.
1166 stack.minimalResumeActivityLocked(r);
1167 } else {
1168 // This activity is not starting in the resumed state... which
1169 // should look like we asked it to pause+stop (but remain visible),
1170 // and it has done so and reported back the current icicle and
1171 // other state.
1172 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1173 + " (starting in stopped state)");
1174 r.state = ActivityState.STOPPED;
1175 r.stopped = true;
1176 }
1177
1178 // Launch the new version setup screen if needed. We do this -after-
1179 // launching the initial activity (that is, home), so that it can have
1180 // a chance to initialize itself while in the background, making the
1181 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001182 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001183 mService.startSetupActivityLocked();
1184 }
1185
1186 return true;
1187 }
1188
Craig Mautnere79d42682013-04-01 19:01:53 -07001189 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001190 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001191 // Is this activity's application already running?
1192 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001193 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001194
1195 r.task.stack.setLaunchTime(r);
1196
1197 if (app != null && app.thread != null) {
1198 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001199 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1200 || !"android".equals(r.info.packageName)) {
1201 // Don't add this if it is a platform component that is marked
1202 // to run in multiple processes, because this is actually
1203 // part of the framework so doesn't make sense to track as a
1204 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001205 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1206 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001207 }
George Mount2c92c972014-03-20 09:38:23 -07001208 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001209 return;
1210 } catch (RemoteException e) {
1211 Slog.w(TAG, "Exception when starting activity "
1212 + r.intent.getComponent().flattenToShortString(), e);
1213 }
1214
1215 // If a dead object exception was thrown -- fall through to
1216 // restart the application.
1217 }
1218
1219 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001220 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001221 }
1222
Craig Mautner6170f732013-04-02 13:05:23 -07001223 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001224 Intent intent, String resolvedType, ActivityInfo aInfo,
1225 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1226 IBinder resultTo, String resultWho, int requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001227 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
Craig Mautnere0a38842013-12-16 16:14:02 -08001228 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container) {
Craig Mautner6170f732013-04-02 13:05:23 -07001229 int err = ActivityManager.START_SUCCESS;
1230
1231 ProcessRecord callerApp = null;
1232 if (caller != null) {
1233 callerApp = mService.getRecordForAppLocked(caller);
1234 if (callerApp != null) {
1235 callingPid = callerApp.pid;
1236 callingUid = callerApp.info.uid;
1237 } else {
1238 Slog.w(TAG, "Unable to find app for caller " + caller
1239 + " (pid=" + callingPid + ") when starting: "
1240 + intent.toString());
1241 err = ActivityManager.START_PERMISSION_DENIED;
1242 }
1243 }
1244
1245 if (err == ActivityManager.START_SUCCESS) {
1246 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1247 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner9ef471f2014-02-07 13:11:47 -08001248 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid)
1249 + " on display " + (container == null ? (mFocusedStack == null ?
1250 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1251 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1252 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001253 }
1254
1255 ActivityRecord sourceRecord = null;
1256 ActivityRecord resultRecord = null;
1257 if (resultTo != null) {
1258 sourceRecord = isInAnyStackLocked(resultTo);
1259 if (DEBUG_RESULTS) Slog.v(
1260 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1261 if (sourceRecord != null) {
1262 if (requestCode >= 0 && !sourceRecord.finishing) {
1263 resultRecord = sourceRecord;
1264 }
1265 }
1266 }
1267 ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1268
Dianne Hackborn91097de2014-04-04 18:02:06 -07001269 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001270
1271 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
1272 && sourceRecord != null) {
1273 // Transfer the result target from the source activity to the new
1274 // one being started, including any failures.
1275 if (requestCode >= 0) {
1276 ActivityOptions.abort(options);
1277 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1278 }
1279 resultRecord = sourceRecord.resultTo;
1280 resultWho = sourceRecord.resultWho;
1281 requestCode = sourceRecord.requestCode;
1282 sourceRecord.resultTo = null;
1283 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001284 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001285 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001286 if (sourceRecord.launchedFromUid == callingUid) {
1287 // The new activity is being launched from the same uid as the previous
1288 // activity in the flow, and asking to forward its result back to the
1289 // previous. In this case the activity is serving as a trampoline between
1290 // the two, so we also want to update its launchedFromPackage to be the
1291 // same as the previous activity. Note that this is safe, since we know
1292 // these two packages come from the same uid; the caller could just as
1293 // well have supplied that same package name itself. This specifially
1294 // deals with the case of an intent picker/chooser being launched in the app
1295 // flow to redirect to an activity picked by the user, where we want the final
1296 // activity to consider it to have been launched by the previous app activity.
1297 callingPackage = sourceRecord.launchedFromPackage;
1298 }
Craig Mautner6170f732013-04-02 13:05:23 -07001299 }
1300
1301 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1302 // We couldn't find a class that can handle the given Intent.
1303 // That's the end of that!
1304 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1305 }
1306
1307 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1308 // We couldn't find the specific class specified in the Intent.
1309 // Also the end of the line.
1310 err = ActivityManager.START_CLASS_NOT_FOUND;
1311 }
1312
Dianne Hackborn91097de2014-04-04 18:02:06 -07001313 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1314 && sourceRecord.task.voiceSession != null) {
1315 // If this activity is being launched as part of a voice session, we need
1316 // to ensure that it is safe to do so. If the upcoming activity will also
1317 // be part of the voice session, we can only launch it if it has explicitly
1318 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001319 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001320 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1321 try {
1322 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1323 intent, resolvedType)) {
1324 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1325 }
1326 } catch (RemoteException e) {
1327 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1328 }
1329 }
1330 }
1331
1332 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1333 // If the caller is starting a new voice session, just make sure the target
1334 // is actually allowing it to run this way.
1335 try {
1336 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1337 intent, resolvedType)) {
1338 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1339 }
1340 } catch (RemoteException e) {
1341 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1342 }
1343 }
1344
Craig Mautner6170f732013-04-02 13:05:23 -07001345 if (err != ActivityManager.START_SUCCESS) {
1346 if (resultRecord != null) {
1347 resultStack.sendActivityResultLocked(-1,
1348 resultRecord, resultWho, requestCode,
1349 Activity.RESULT_CANCELED, null);
1350 }
1351 setDismissKeyguard(false);
1352 ActivityOptions.abort(options);
1353 return err;
1354 }
1355
1356 final int startAnyPerm = mService.checkPermission(
1357 START_ANY_ACTIVITY, callingPid, callingUid);
1358 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1359 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1360 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1361 if (resultRecord != null) {
1362 resultStack.sendActivityResultLocked(-1,
1363 resultRecord, resultWho, requestCode,
1364 Activity.RESULT_CANCELED, null);
1365 }
1366 setDismissKeyguard(false);
1367 String msg;
1368 if (!aInfo.exported) {
1369 msg = "Permission Denial: starting " + intent.toString()
1370 + " from " + callerApp + " (pid=" + callingPid
1371 + ", uid=" + callingUid + ")"
1372 + " not exported from uid " + aInfo.applicationInfo.uid;
1373 } else {
1374 msg = "Permission Denial: starting " + intent.toString()
1375 + " from " + callerApp + " (pid=" + callingPid
1376 + ", uid=" + callingUid + ")"
1377 + " requires " + aInfo.permission;
1378 }
1379 Slog.w(TAG, msg);
1380 throw new SecurityException(msg);
1381 }
1382
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001383 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001384 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001385
Craig Mautner6170f732013-04-02 13:05:23 -07001386 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001387 try {
1388 // The Intent we give to the watcher has the extra data
1389 // stripped off, since it can contain private information.
1390 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001391 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001392 aInfo.applicationInfo.packageName);
1393 } catch (RemoteException e) {
1394 mService.mController = null;
1395 }
Ben Gruver5e207332013-04-03 17:41:37 -07001396 }
Craig Mautner6170f732013-04-02 13:05:23 -07001397
Ben Gruver5e207332013-04-03 17:41:37 -07001398 if (abort) {
1399 if (resultRecord != null) {
1400 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001401 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001402 }
Ben Gruver5e207332013-04-03 17:41:37 -07001403 // We pretend to the caller that it was really started, but
1404 // they will just get a cancel result.
1405 setDismissKeyguard(false);
1406 ActivityOptions.abort(options);
1407 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001408 }
1409
1410 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001411 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001412 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001413 if (outActivity != null) {
1414 outActivity[0] = r;
1415 }
1416
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001417 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001418 if (voiceSession == null && (stack.mResumedActivity == null
1419 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Craig Mautner6170f732013-04-02 13:05:23 -07001420 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
1421 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001422 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautner6170f732013-04-02 13:05:23 -07001423 mService.mPendingActivityLaunches.add(pal);
1424 setDismissKeyguard(false);
1425 ActivityOptions.abort(options);
1426 return ActivityManager.START_SWITCHES_CANCELED;
1427 }
1428 }
1429
1430 if (mService.mDidAppSwitch) {
1431 // This is the second allowed switch since we stopped switches,
1432 // so now just generally allow switches. Use case: user presses
1433 // home (switches disabled, switch to home, mDidAppSwitch now true);
1434 // user taps a home icon (coming from home so allowed, we hit here
1435 // and now allow anyone to switch again).
1436 mService.mAppSwitchesAllowedTime = 0;
1437 } else {
1438 mService.mDidAppSwitch = true;
1439 }
1440
1441 mService.doPendingActivityLaunchesLocked(false);
1442
Dianne Hackborn91097de2014-04-04 18:02:06 -07001443 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
1444 startFlags, true, options);
Craig Mautner10385a12013-09-22 21:08:32 -07001445
1446 if (allPausedActivitiesComplete()) {
1447 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001448 // activity start, but we are not actually doing an activity
1449 // switch... just dismiss the keyguard now, because we
1450 // probably want to see whatever is behind it.
1451 dismissKeyguard();
1452 }
1453 return err;
1454 }
1455
Craig Mautner1b4bf852014-05-26 15:06:32 -07001456 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001457 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001458
1459 // On leanback only devices we should keep all activities in the same stack.
1460 if (!mLeanbackOnlyDevice &&
1461 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001462 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001463 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001464 if (taskStack.isOnHomeDisplay()) {
1465 if (mFocusedStack != taskStack) {
1466 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1467 "focused stack to r=" + r + " task=" + task);
1468 mFocusedStack = taskStack;
1469 } else {
1470 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1471 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1472 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001473 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001474 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001475 }
1476
Craig Mautnere0a38842013-12-16 16:14:02 -08001477 final ActivityContainer container = r.mInitialActivityContainer;
1478 if (container != null) {
1479 // The first time put it on the desired stack, after this put on task stack.
1480 r.mInitialActivityContainer = null;
1481 return container.mStack;
1482 }
1483
Craig Mautner1b4bf852014-05-26 15:06:32 -07001484 if (mFocusedStack != mHomeStack && (!newTask ||
1485 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001486 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1487 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1488 return mFocusedStack;
1489 }
1490
Craig Mautnere0a38842013-12-16 16:14:02 -08001491 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1492 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1493 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1494 if (!stack.isHomeStack()) {
1495 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1496 "adjustStackFocus: Setting focused stack=" + stack);
1497 mFocusedStack = stack;
1498 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001499 }
1500 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001501
Craig Mautner4a1cb222013-12-04 16:14:06 -08001502 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001503 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001504 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1505 " stackId=" + stackId);
1506 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001507 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001508 }
1509 return mHomeStack;
1510 }
1511
Craig Mautner29219d92013-04-16 20:19:12 -07001512 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001513 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001514 final TaskRecord task = r.task;
1515 boolean isHomeActivity = !r.isApplicationActivity();
1516 if (!isHomeActivity && task != null) {
1517 isHomeActivity = !task.isApplicationTask();
1518 }
1519 if (!isHomeActivity && task != null) {
1520 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1521 isHomeActivity = parent != null && parent.isHomeActivity();
1522 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001523 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001524 }
1525 }
1526
Craig Mautner8849a5e2013-04-02 16:41:03 -07001527 final int startActivityUncheckedLocked(ActivityRecord r,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001528 ActivityRecord sourceRecord,
1529 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
1530 boolean doResume, Bundle options) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001531 final Intent intent = r.intent;
1532 final int callingUid = r.launchedFromUid;
1533
Craig Mautnera228ae92014-07-09 05:44:55 -07001534 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1535 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001536
Craig Mautnera228ae92014-07-09 05:44:55 -07001537 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001538 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001539 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001540 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1541 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1542 "\"singleInstance\" or \"singleTask\"");
1543 launchFlags &=
1544 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1545 } else {
1546 switch (r.info.documentLaunchMode) {
1547 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1548 break;
1549 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1550 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1551 break;
1552 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1553 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1554 break;
1555 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1556 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1557 break;
1558 }
1559 }
Craig Mautnera228ae92014-07-09 05:44:55 -07001560 final int launchBehindFlags = Intent.FLAG_ACTIVITY_LAUNCH_BEHIND |
1561 Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1562 final boolean affiliateTask = (launchFlags & launchBehindFlags) == launchBehindFlags;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001563
1564 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1565 // For whatever reason this activity is being launched into a new
1566 // task... yet the caller has requested a result back. Well, that
1567 // is pretty messed up, so instead immediately send back a cancel
1568 // and let the new task continue launched as normal without a
1569 // dependency on its originator.
1570 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1571 r.resultTo.task.stack.sendActivityResultLocked(-1,
1572 r.resultTo, r.resultWho, r.requestCode,
1573 Activity.RESULT_CANCELED, null);
1574 r.resultTo = null;
1575 }
1576
1577 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1578 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1579 }
1580
Craig Mautner8849a5e2013-04-02 16:41:03 -07001581 // We'll invoke onUserLeaving before onPause only if the launching
1582 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001583 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001584 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001585
1586 // If the caller has asked not to resume at this point, we make note
1587 // of this in the record so that we can skip it when trying to find
1588 // the top running activity.
1589 if (!doResume) {
1590 r.delayedResume = true;
1591 }
1592
Craig Mautnera254cd72014-05-25 16:47:39 -07001593 ActivityRecord notTop =
1594 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001595
1596 // If the onlyIfNeeded flag is set, then we can do this if the activity
1597 // being launched is the same as the one making the call... or, as
1598 // a special case, if we do not know the caller then we count the
1599 // current top activity as the caller.
1600 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1601 ActivityRecord checkedCaller = sourceRecord;
1602 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001603 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001604 }
1605 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1606 // Caller is not the same as launcher, so always needed.
1607 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1608 }
1609 }
1610
1611 if (sourceRecord == null) {
1612 // This activity is not being started from another... in this
1613 // case we -always- start a new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001614 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Craig Mautner29219d92013-04-16 20:19:12 -07001615 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1616 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001617 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1618 }
1619 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1620 // The original activity who is starting us is running as a single
1621 // instance... this new activity it is starting must go on its
1622 // own task.
1623 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
Craig Mautnera228ae92014-07-09 05:44:55 -07001624 } else if (launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001625 // The activity being started is a single instance... it always
1626 // gets launched into its own task.
1627 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1628 }
1629
Craig Mautner88629292013-11-10 20:39:05 -08001630 ActivityInfo newTaskInfo = null;
1631 Intent newTaskIntent = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001632 final ActivityStack sourceStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001633 if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001634 if (sourceRecord.finishing) {
1635 // If the source is finishing, we can't further count it as our source. This
1636 // is because the task it is associated with may now be empty and on its way out,
1637 // so we don't want to blindly throw it in to that task. Instead we will take
Craig Mautner88629292013-11-10 20:39:05 -08001638 // the NEW_TASK flow and try to find a task for it. But save the task information
1639 // so it can be used when creating the new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001640 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001641 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1642 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1643 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
Craig Mautner88629292013-11-10 20:39:05 -08001644 newTaskInfo = sourceRecord.info;
1645 newTaskIntent = sourceRecord.task.intent;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001646 }
1647 sourceRecord = null;
1648 sourceStack = null;
1649 } else {
1650 sourceStack = sourceRecord.task.stack;
1651 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001652 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001653 sourceStack = null;
1654 }
1655
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001656 intent.setFlags(launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001657
1658 boolean addingToTask = false;
1659 boolean movedHome = false;
1660 TaskRecord reuseTask = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001661 ActivityStack targetStack;
Craig Mautnerd00f4742014-03-12 14:17:26 -07001662 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1663 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001664 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001665 // If bring to front is requested, and no result is requested, and
1666 // we can find a task that was started with this same
1667 // component, then instead of launching bring that one to the front.
1668 if (r.resultTo == null) {
1669 // See if there is a task to bring to the front. If this is
1670 // a SINGLE_INSTANCE activity, there can be one and only one
1671 // instance of it in the history, and it is always in its own
1672 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001673 ActivityRecord intentActivity = !launchSingleInstance ?
1674 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001675 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001676 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001677 showLockTaskToast();
Craig Mautneraea74a52014-03-08 14:23:10 -08001678 Slog.e(TAG, "moveTaskToFront: Attempt to violate Lock Task Mode");
1679 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1680 }
Craig Mautner29219d92013-04-16 20:19:12 -07001681 if (r.task == null) {
1682 r.task = intentActivity.task;
1683 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001684 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001685 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001686 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1687 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001688 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001689 if (intentActivity.task.intent == null) {
1690 // This task was started because of movement of
1691 // the activity based on affinity... now that we
1692 // are actually launching it, we can assign the
1693 // base intent.
1694 intentActivity.task.setIntent(intent, r.info);
1695 }
1696 // If the target task is not in the front, then we need
1697 // to bring it to the front... except... well, with
1698 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1699 // to have the same behavior as if a new instance was
1700 // being started, which means not bringing it to the front
1701 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001702 final ActivityStack lastStack = getLastStack();
1703 ActivityRecord curTop = lastStack == null?
1704 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner7504d7b2013-09-17 10:50:53 -07001705 if (curTop != null && (curTop.task != intentActivity.task ||
1706 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001707 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001708 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1709 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001710 // We really do want to push this one into the
1711 // user's face, right now.
Craig Mautnera228ae92014-07-09 05:44:55 -07001712 if (affiliateTask && sourceRecord != null) {
1713 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1714 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001715 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001716 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001717 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001718 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1719 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001720 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001721 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001722 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001723 options = null;
1724 }
1725 }
1726 // If the caller has requested that the target task be
1727 // reset, then do so.
1728 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1729 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1730 }
1731 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1732 // We don't need to start a new activity, and
1733 // the client said not to do anything if that
1734 // is the case, so this is it! And for paranoia, make
1735 // sure we have correctly resumed the top activity.
1736 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001737 resumeTopActivitiesLocked(targetStack, null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001738 } else {
1739 ActivityOptions.abort(options);
1740 }
1741 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1742 }
1743 if ((launchFlags &
1744 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1745 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1746 // The caller has requested to completely replace any
1747 // existing task with its new activity. Well that should
1748 // not be too hard...
1749 reuseTask = intentActivity.task;
1750 reuseTask.performClearTaskLocked();
1751 reuseTask.setIntent(r.intent, r.info);
1752 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001753 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001754 // In this situation we want to remove all activities
1755 // from the task up to the one being started. In most
1756 // cases this means we are resetting the task to its
1757 // initial state.
1758 ActivityRecord top =
1759 intentActivity.task.performClearTaskLocked(r, launchFlags);
1760 if (top != null) {
1761 if (top.frontOfTask) {
1762 // Activity aliases may mean we use different
1763 // intents for the top activity, so make sure
1764 // the task now has the identity of the new
1765 // intent.
1766 top.task.setIntent(r.intent, r.info);
1767 }
1768 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1769 r, top.task);
1770 top.deliverNewIntentLocked(callingUid, r.intent);
1771 } else {
1772 // A special case: we need to
1773 // start the activity because it is not currently
1774 // running, and the caller has asked to clear the
1775 // current task to have this activity at the top.
1776 addingToTask = true;
1777 // Now pretend like this activity is being started
1778 // by the top of its task, so it is put in the
1779 // right place.
1780 sourceRecord = intentActivity;
1781 }
1782 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1783 // In this case the top activity on the task is the
1784 // same as the one being launched, so we take that
1785 // as a request to bring the task to the foreground.
1786 // If the top activity in the task is the root
1787 // activity, deliver this new intent to it if it
1788 // desires.
1789 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
1790 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
1791 && intentActivity.realActivity.equals(r.realActivity)) {
1792 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1793 intentActivity.task);
1794 if (intentActivity.frontOfTask) {
1795 intentActivity.task.setIntent(r.intent, r.info);
1796 }
1797 intentActivity.deliverNewIntentLocked(callingUid, r.intent);
1798 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1799 // In this case we are launching the root activity
1800 // of the task, but with a different intent. We
1801 // should start a new instance on top.
1802 addingToTask = true;
1803 sourceRecord = intentActivity;
1804 }
1805 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1806 // In this case an activity is being launched in to an
1807 // existing task, without resetting that task. This
1808 // is typically the situation of launching an activity
1809 // from a notification or shortcut. We want to place
1810 // the new activity on top of the current task.
1811 addingToTask = true;
1812 sourceRecord = intentActivity;
1813 } else if (!intentActivity.task.rootWasReset) {
1814 // In this case we are launching in to an existing task
1815 // that has not yet been started from its front door.
1816 // The current task has been brought to the front.
1817 // Ideally, we'd probably like to place this new task
1818 // at the bottom of its stack, but that's a little hard
1819 // to do with the current organization of the code so
1820 // for now we'll just drop it.
1821 intentActivity.task.setIntent(r.intent, r.info);
1822 }
1823 if (!addingToTask && reuseTask == null) {
1824 // We didn't do anything... but it was needed (a.k.a., client
1825 // don't use that intent!) And for paranoia, make
1826 // sure we have correctly resumed the top activity.
1827 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001828 targetStack.resumeTopActivityLocked(null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001829 } else {
1830 ActivityOptions.abort(options);
1831 }
1832 return ActivityManager.START_TASK_TO_FRONT;
1833 }
1834 }
1835 }
1836 }
1837
1838 //String uri = r.intent.toURI();
1839 //Intent intent2 = new Intent(uri);
1840 //Slog.i(TAG, "Given intent: " + r.intent);
1841 //Slog.i(TAG, "URI is: " + uri);
1842 //Slog.i(TAG, "To intent: " + intent2);
1843
1844 if (r.packageName != null) {
1845 // If the activity being launched is the same as the one currently
1846 // at the top, then we need to check if it should only be launched
1847 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001848 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001849 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001850 if (top != null && r.resultTo == null) {
1851 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
1852 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07001853 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001854 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001855 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
1856 top.task);
1857 // For paranoia, make sure we have correctly
1858 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001859 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001860 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001861 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001862 }
1863 ActivityOptions.abort(options);
1864 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1865 // We don't need to start a new activity, and
1866 // the client said not to do anything if that
1867 // is the case, so this is it!
1868 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1869 }
1870 top.deliverNewIntentLocked(callingUid, r.intent);
1871 return ActivityManager.START_DELIVERED_TO_TOP;
1872 }
1873 }
1874 }
1875 }
1876
1877 } else {
1878 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001879 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
1880 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001881 }
1882 ActivityOptions.abort(options);
1883 return ActivityManager.START_CLASS_NOT_FOUND;
1884 }
1885
1886 boolean newTask = false;
1887 boolean keepCurTransition = false;
1888
Craig Mautnera228ae92014-07-09 05:44:55 -07001889 TaskRecord taskToAffiliate = affiliateTask && sourceRecord != null ?
1890 sourceRecord.task : null;
1891
Craig Mautner8849a5e2013-04-02 16:41:03 -07001892 // Should this be considered a new task?
1893 if (r.resultTo == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001894 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001895 if (isLockTaskModeViolation(reuseTask)) {
1896 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1897 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1898 }
Craig Mautner1b4bf852014-05-26 15:06:32 -07001899 newTask = true;
1900 targetStack = adjustStackFocus(r, newTask);
Craig Mautnere0a38842013-12-16 16:14:02 -08001901 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001902 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08001903 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
1904 newTaskInfo != null ? newTaskInfo : r.info,
1905 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnera228ae92014-07-09 05:44:55 -07001906 voiceSession, voiceInteractor, true), taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001907 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
1908 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001909 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07001910 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001911 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001912 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001913 if ((launchFlags &
1914 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
1915 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
1916 // Caller wants to appear on home activity, so before starting
1917 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07001918 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001919 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001920 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001921 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001922 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08001923 if (isLockTaskModeViolation(sourceTask)) {
1924 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1925 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1926 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001927 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07001928 targetStack.moveToFront();
Craig Mautnerabcc59c2014-05-17 15:13:28 -07001929 mWindowManager.moveTaskToTop(targetStack.topTask().taskId);
Craig Mautnera228ae92014-07-09 05:44:55 -07001930 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001931 // In this case, we are adding the activity to an existing
1932 // task, but the caller has asked to clear that task if the
1933 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07001934 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001935 keepCurTransition = true;
1936 if (top != null) {
1937 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
1938 top.deliverNewIntentLocked(callingUid, r.intent);
1939 // For paranoia, make sure we have correctly
1940 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001941 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001942 if (doResume) {
1943 targetStack.resumeTopActivityLocked(null);
1944 }
1945 ActivityOptions.abort(options);
1946 return ActivityManager.START_DELIVERED_TO_TOP;
1947 }
1948 } else if (!addingToTask &&
1949 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
1950 // In this case, we are launching an activity in our own task
1951 // that may already be running somewhere in the history, and
1952 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07001953 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001954 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001955 final TaskRecord task = top.task;
1956 task.moveActivityToFrontLocked(top);
1957 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001958 top.updateOptionsLocked(options);
1959 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner0f922742013-08-06 08:44:42 -07001960 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001961 if (doResume) {
1962 targetStack.resumeTopActivityLocked(null);
1963 }
1964 return ActivityManager.START_DELIVERED_TO_TOP;
1965 }
1966 }
1967 // An existing activity is starting this new activity, so we want
1968 // to keep the new one in the same task as the one that is starting
1969 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07001970 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001971 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001972 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001973
1974 } else {
1975 // This not being started from an existing activity, and not part
1976 // of a new task... just put it in the top task, though these days
1977 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07001978 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerdd221b32014-03-19 19:53:45 -07001979 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07001980 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07001981 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07001982 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07001983 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001984 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
1985 + " in new guessed " + r.task);
1986 }
1987
1988 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01001989 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001990
Craig Mautner76e2a762014-06-24 09:05:43 -07001991 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
1992 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
1993 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001994 if (newTask) {
1995 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
1996 }
1997 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07001998 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001999 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautner1d001b62013-06-18 16:52:43 -07002000 mService.setFocusedActivityLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002001 return ActivityManager.START_SUCCESS;
2002 }
2003
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002004 void acquireLaunchWakelock() {
2005 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2006 throw new IllegalStateException("Calling must be system uid");
2007 }
2008 mLaunchingActivity.acquire();
2009 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2010 // To be safe, don't allow the wake lock to be held for too long.
2011 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2012 }
2013 }
2014
Craig Mautnerf3333272013-04-22 10:55:53 -07002015 // Checked.
2016 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2017 Configuration config) {
2018 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2019
Craig Mautnerf3333272013-04-22 10:55:53 -07002020 ArrayList<ActivityRecord> stops = null;
2021 ArrayList<ActivityRecord> finishes = null;
2022 ArrayList<UserStartedState> startingUsers = null;
2023 int NS = 0;
2024 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002025 boolean booting = false;
2026 boolean enableScreen = false;
2027 boolean activityRemoved = false;
2028
2029 ActivityRecord r = ActivityRecord.forToken(token);
2030 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002031 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2032 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002033 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2034 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002035 if (fromTimeout) {
2036 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002037 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002038
2039 // This is a hack to semi-deal with a race condition
2040 // in the client where it can be constructed with a
2041 // newer configuration from when we asked it to launch.
2042 // We'll update with whatever configuration it now says
2043 // it used to launch.
2044 if (config != null) {
2045 r.configuration = config;
2046 }
2047
2048 // We are now idle. If someone is waiting for a thumbnail from
2049 // us, we can now deliver.
2050 r.idle = true;
2051
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002052 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
2053 if (!mService.mBooted && isFrontStack(r.task.stack)) {
2054 mService.mBooted = true;
2055 enableScreen = true;
2056 }
2057 }
2058
2059 if (allResumedActivitiesIdle()) {
2060 if (r != null) {
2061 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002062 }
2063
2064 if (mLaunchingActivity.isHeld()) {
2065 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2066 if (VALIDATE_WAKE_LOCK_CALLER &&
2067 Binder.getCallingUid() != Process.myUid()) {
2068 throw new IllegalStateException("Calling must be system uid");
2069 }
2070 mLaunchingActivity.release();
2071 }
2072 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002073 }
2074
2075 // Atomically retrieve all of the other things to do.
2076 stops = processStoppingActivitiesLocked(true);
2077 NS = stops != null ? stops.size() : 0;
2078 if ((NF=mFinishingActivities.size()) > 0) {
2079 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2080 mFinishingActivities.clear();
2081 }
2082
Craig Mautnerf3333272013-04-22 10:55:53 -07002083 if (isFrontStack(mHomeStack)) {
2084 booting = mService.mBooting;
2085 mService.mBooting = false;
2086 }
2087
2088 if (mStartingUsers.size() > 0) {
2089 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2090 mStartingUsers.clear();
2091 }
2092
Craig Mautnerf3333272013-04-22 10:55:53 -07002093 // Stop any activities that are scheduled to do so but have been
2094 // waiting for the next one to start.
2095 for (int i = 0; i < NS; i++) {
2096 r = stops.get(i);
2097 final ActivityStack stack = r.task.stack;
2098 if (r.finishing) {
2099 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2100 } else {
2101 stack.stopActivityLocked(r);
2102 }
2103 }
2104
2105 // Finish any activities that are scheduled to do so but have been
2106 // waiting for the next one to start.
2107 for (int i = 0; i < NF; i++) {
2108 r = finishes.get(i);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002109 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07002110 }
2111
2112 if (booting) {
2113 mService.finishBooting();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002114 } else {
2115 // Complete user switch
2116 if (startingUsers != null) {
2117 for (int i = 0; i < startingUsers.size(); i++) {
2118 mService.finishUserSwitch(startingUsers.get(i));
2119 }
2120 }
2121 // Complete starting up of background users
2122 if (mStartingBackgroundUsers.size() > 0) {
2123 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2124 mStartingBackgroundUsers.clear();
2125 for (int i = 0; i < startingUsers.size(); i++) {
2126 mService.finishUserBoot(startingUsers.get(i));
2127 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002128 }
2129 }
2130
2131 mService.trimApplications();
2132 //dump();
2133 //mWindowManager.dump();
2134
2135 if (enableScreen) {
2136 mService.enableScreenAfterBoot();
2137 }
2138
2139 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002140 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002141 }
2142
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002143 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002144 }
2145
Craig Mautner8e569572013-10-11 17:36:59 -07002146 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002147 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002148 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2149 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002150 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2151 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2152 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002153 }
Craig Mautner19091252013-10-05 00:03:53 -07002154 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002155 }
2156
2157 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002158 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2159 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002160 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2161 stacks.get(stackNdx).closeSystemDialogsLocked();
2162 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002163 }
2164 }
2165
Craig Mautner93529a42013-10-04 15:03:13 -07002166 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002167 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002168 }
2169
Craig Mautner8d341ef2013-03-26 09:03:27 -07002170 /**
2171 * @return true if some activity was finished (or would have finished if doit were true).
2172 */
2173 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2174 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002175 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2176 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002177 final int numStacks = stacks.size();
2178 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2179 final ActivityStack stack = stacks.get(stackNdx);
2180 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2181 didSomething = true;
2182 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002183 }
2184 }
2185 return didSomething;
2186 }
2187
Dianne Hackborna413dc02013-07-12 12:02:55 -07002188 void updatePreviousProcessLocked(ActivityRecord r) {
2189 // Now that this process has stopped, we may want to consider
2190 // it to be the previous app to try to keep around in case
2191 // the user wants to return to it.
2192
2193 // First, found out what is currently the foreground app, so that
2194 // we don't blow away the previous app if this activity is being
2195 // hosted by the process that is actually still the foreground.
2196 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002197 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2198 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002199 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2200 final ActivityStack stack = stacks.get(stackNdx);
2201 if (isFrontStack(stack)) {
2202 if (stack.mResumedActivity != null) {
2203 fgApp = stack.mResumedActivity.app;
2204 } else if (stack.mPausingActivity != null) {
2205 fgApp = stack.mPausingActivity.app;
2206 }
2207 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002208 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002209 }
2210 }
2211
2212 // Now set this one as the previous process, only if that really
2213 // makes sense to.
2214 if (r.app != null && fgApp != null && r.app != fgApp
2215 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002216 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002217 mService.mPreviousProcess = r.app;
2218 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2219 }
2220 }
2221
Craig Mautner05d29032013-05-03 13:40:13 -07002222 boolean resumeTopActivitiesLocked() {
2223 return resumeTopActivitiesLocked(null, null, null);
2224 }
2225
2226 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2227 Bundle targetOptions) {
2228 if (targetStack == null) {
2229 targetStack = getFocusedStack();
2230 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002231 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002232 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002233 if (isFrontStack(targetStack)) {
2234 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2235 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002236 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2237 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002238 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2239 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002240 if (stack == targetStack) {
2241 // Already started above.
2242 continue;
2243 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002244 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002245 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002246 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002247 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002248 }
Craig Mautner05d29032013-05-03 13:40:13 -07002249 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002250 }
2251
2252 void finishTopRunningActivityLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002253 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2254 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002255 final int numStacks = stacks.size();
2256 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2257 final ActivityStack stack = stacks.get(stackNdx);
2258 stack.finishTopRunningActivityLocked(app);
2259 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002260 }
2261 }
2262
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002263 void finishVoiceTask(IVoiceInteractionSession session) {
2264 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2265 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2266 final int numStacks = stacks.size();
2267 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2268 final ActivityStack stack = stacks.get(stackNdx);
2269 stack.finishVoiceTask(session);
2270 }
2271 }
2272 }
2273
Craig Mautneraea74a52014-03-08 14:23:10 -08002274 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2275 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2276 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002277 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002278 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2279 // Caller wants the home activity moved with it. To accomplish this,
2280 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002281 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002282 }
2283 task.stack.moveTaskToFrontLocked(task, null, options);
2284 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2285 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002286 }
2287
Craig Mautner967212c2013-04-13 21:10:58 -07002288 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002289 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2290 if (activityContainer != null) {
2291 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002292 }
2293 return null;
2294 }
2295
Craig Mautner967212c2013-04-13 21:10:58 -07002296 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002297 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002298 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2299 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002300 }
2301 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002302 }
2303
Craig Mautner4a1cb222013-12-04 16:14:06 -08002304 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002305 ActivityRecord homeActivity = getHomeActivity();
2306 if (homeActivity != null) {
2307 return homeActivity.appToken;
2308 }
2309 return null;
2310 }
2311
2312 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002313 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2314 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2315 final TaskRecord task = tasks.get(taskNdx);
2316 if (task.isHomeTask()) {
2317 final ArrayList<ActivityRecord> activities = task.mActivities;
2318 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2319 final ActivityRecord r = activities.get(activityNdx);
2320 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002321 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002322 }
2323 }
2324 }
2325 }
2326 return null;
2327 }
2328
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002329 ActivityContainer createActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002330 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002331 ActivityContainer activityContainer =
2332 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002333 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002334 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002335 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002336 return activityContainer;
2337 }
2338
Craig Mautner34b73df2014-01-12 21:11:08 -08002339 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002340 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2341 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2342 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002343 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2344 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002345 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002346 }
2347 }
2348
Craig Mautner95da1082014-02-24 17:54:35 -08002349 void deleteActivityContainer(IActivityContainer container) {
2350 ActivityContainer activityContainer = (ActivityContainer)container;
2351 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002352 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2353 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002354 final int stackId = activityContainer.mStackId;
2355 mActivityContainers.remove(stackId);
2356 mWindowManager.removeStack(stackId);
2357 }
2358 }
2359
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002360 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002361 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2362 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002363 return -1;
2364 }
2365
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002366 ActivityContainer activityContainer = new ActivityContainer(stackId);
2367 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002368 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002369 return stackId;
2370 }
2371
2372 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002373 while (true) {
2374 if (++mLastStackId <= HOME_STACK_ID) {
2375 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002376 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002377 if (getStack(mLastStackId) == null) {
2378 break;
2379 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002380 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002381 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002382 }
2383
Craig Mautneref73ee12014-04-23 11:45:37 -07002384 void createStackForRestoredTaskHistory(ArrayList<TaskRecord> tasks) {
2385 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
2386 final ActivityStack stack = getStack(stackId);
2387 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2388 final TaskRecord task = tasks.get(taskNdx);
2389 stack.addTask(task, false, false);
2390 final int taskId = task.taskId;
2391 final ArrayList<ActivityRecord> activities = task.mActivities;
2392 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2393 final ActivityRecord r = activities.get(activityNdx);
2394 mWindowManager.addAppToken(0, r.appToken, taskId, stackId,
2395 r.info.screenOrientation, r.fullscreen,
2396 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Dianne Hackborne30e02f2014-05-27 18:24:45 -07002397 r.userId, r.info.configChanges, task.voiceSession != null);
Craig Mautneref73ee12014-04-23 11:45:37 -07002398 }
2399 mWindowManager.addTask(taskId, stackId, false);
2400 }
Craig Mautner84984fa2014-06-19 11:19:20 -07002401 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
Craig Mautneref73ee12014-04-23 11:45:37 -07002402 }
2403
Craig Mautner8d341ef2013-03-26 09:03:27 -07002404 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002405 final TaskRecord task = anyTaskForIdLocked(taskId);
2406 if (task == null) {
2407 return;
2408 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002409 final ActivityStack stack = getStack(stackId);
2410 if (stack == null) {
2411 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2412 return;
2413 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002414 task.stack.removeTask(task);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002415 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002416 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002417 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002418 }
2419
Craig Mautnerac6f8432013-07-17 13:24:59 -07002420 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002421 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002422 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2423 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002424 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2425 final ActivityStack stack = stacks.get(stackNdx);
2426 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002427 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2428 continue;
2429 }
2430 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2431 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2432 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002433 continue;
2434 }
2435 final ActivityRecord ar = stack.findTaskLocked(r);
2436 if (ar != null) {
2437 return ar;
2438 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002439 }
2440 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002441 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002442 return null;
2443 }
2444
2445 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002446 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2447 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002448 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2449 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2450 if (ar != null) {
2451 return ar;
2452 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002453 }
2454 }
2455 return null;
2456 }
2457
Craig Mautner8d341ef2013-03-26 09:03:27 -07002458 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002459 scheduleSleepTimeout();
2460 if (!mGoingToSleep.isHeld()) {
2461 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002462 if (mLaunchingActivity.isHeld()) {
2463 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2464 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002465 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002466 mLaunchingActivity.release();
2467 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002468 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002469 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002470 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002471 }
2472
2473 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002474 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002475
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002476 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002477 final long endTime = System.currentTimeMillis() + timeout;
2478 while (true) {
2479 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002480 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2481 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002482 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2483 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2484 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002485 }
2486 if (cantShutdown) {
2487 long timeRemaining = endTime - System.currentTimeMillis();
2488 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002489 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002490 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002491 } catch (InterruptedException e) {
2492 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002493 } else {
2494 Slog.w(TAG, "Activity manager shutdown timed out");
2495 timedout = true;
2496 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002497 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002498 } else {
2499 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002500 }
2501 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002502
2503 // Force checkReadyForSleep to complete.
2504 mSleepTimeout = true;
2505 checkReadyForSleepLocked();
2506
Craig Mautner8d341ef2013-03-26 09:03:27 -07002507 return timedout;
2508 }
2509
2510 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002511 removeSleepTimeouts();
2512 if (mGoingToSleep.isHeld()) {
2513 mGoingToSleep.release();
2514 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002515 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2516 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002517 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2518 final ActivityStack stack = stacks.get(stackNdx);
2519 stack.awakeFromSleepingLocked();
2520 if (isFrontStack(stack)) {
2521 resumeTopActivitiesLocked();
2522 }
Craig Mautner5314a402013-09-26 12:40:16 -07002523 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002524 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002525 mGoingToSleepActivities.clear();
2526 }
2527
2528 void activitySleptLocked(ActivityRecord r) {
2529 mGoingToSleepActivities.remove(r);
2530 checkReadyForSleepLocked();
2531 }
2532
2533 void checkReadyForSleepLocked() {
2534 if (!mService.isSleepingOrShuttingDown()) {
2535 // Do not care.
2536 return;
2537 }
2538
2539 if (!mSleepTimeout) {
2540 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002541 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2542 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002543 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2544 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2545 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002546 }
2547
2548 if (mStoppingActivities.size() > 0) {
2549 // Still need to tell some activities to stop; can't sleep yet.
2550 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2551 + mStoppingActivities.size() + " activities");
2552 scheduleIdleLocked();
2553 dontSleep = true;
2554 }
2555
2556 if (mGoingToSleepActivities.size() > 0) {
2557 // Still need to tell some activities to sleep; can't sleep yet.
2558 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2559 + mGoingToSleepActivities.size() + " activities");
2560 dontSleep = true;
2561 }
2562
2563 if (dontSleep) {
2564 return;
2565 }
2566 }
2567
Craig Mautnere0a38842013-12-16 16:14:02 -08002568 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2569 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002570 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2571 stacks.get(stackNdx).goToSleep();
2572 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002573 }
2574
2575 removeSleepTimeouts();
2576
2577 if (mGoingToSleep.isHeld()) {
2578 mGoingToSleep.release();
2579 }
2580 if (mService.mShuttingDown) {
2581 mService.notifyAll();
2582 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002583 }
2584
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002585 boolean reportResumedActivityLocked(ActivityRecord r) {
2586 final ActivityStack stack = r.task.stack;
2587 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002588 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002589 }
2590 if (allResumedActivitiesComplete()) {
2591 ensureActivitiesVisibleLocked(null, 0);
2592 mWindowManager.executeAppTransition();
2593 return true;
2594 }
2595 return false;
2596 }
2597
Craig Mautner8d341ef2013-03-26 09:03:27 -07002598 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002599 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2600 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002601 final int numStacks = stacks.size();
2602 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2603 final ActivityStack stack = stacks.get(stackNdx);
2604 stack.handleAppCrashLocked(app);
2605 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002606 }
2607 }
2608
Craig Mautneree2e45a2014-06-27 12:10:03 -07002609 boolean setMediaPlayingLocked(ActivityRecord r, boolean playing) {
2610 final ActivityStack stack = r.task.stack;
2611 if (stack == null) {
2612 if (DEBUG_MEDIA_VISIBILITY) Slog.d(TAG, "setMediaPlaying: r=" + r + " playing=" +
2613 playing + " stack is null");
2614 return false;
2615 }
2616 final boolean isPlaying = stack.isMediaPlaying();
2617 if (DEBUG_MEDIA_VISIBILITY) Slog.d(TAG, "setMediaPlayer: r=" + r + " playing=" +
2618 playing + " isPlaying=" + isPlaying);
2619
2620 final ActivityRecord top = topRunningActivityLocked();
2621 if (top == null || top == r || (playing == isPlaying)) {
2622 if (DEBUG_MEDIA_VISIBILITY) Slog.d(TAG, "setMediaPlaying: quick return");
2623 stack.setMediaPlayer(playing ? r : null);
2624 return true;
2625 }
2626
2627 // A non-top activity is reporting a visibility change.
2628 if (top.fullscreen || top.state != ActivityState.RESUMED || top.app == null ||
2629 top.app.thread == null) {
2630 // Can't carry out this request.
2631 if (DEBUG_MEDIA_VISIBILITY) Slog.d(TAG, "setMediaPlaying: returning top.fullscreen=" +
2632 top.fullscreen+ " top.state=" + top.state + " top.app=" + top.app +
2633 " top.app.thread=" + top.app.thread);
2634 return false;
2635 }
2636
2637 stack.setMediaPlayer(playing ? r : null);
2638 try {
2639 top.app.thread.scheduleBackgroundMediaPlayingChanged(top.appToken, playing);
2640 } catch (RemoteException e) {
2641 }
2642 return true;
2643 }
2644
Craig Mautnerde4ef022013-04-07 19:01:33 -07002645 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002646 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002647 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2648 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002649 final int topStackNdx = stacks.size() - 1;
2650 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2651 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002652 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002653 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002654 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002655 }
2656
2657 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002658 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2659 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002660 final int numStacks = stacks.size();
2661 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2662 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002663 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002664 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002665 }
2666 }
2667
2668 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002669 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
2670 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002671 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002672
Craig Mautner858d8a62013-04-23 17:08:34 -07002673 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002674 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2675 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002676 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002677 final ActivityStack stack = stacks.get(stackNdx);
2678 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002679 TaskRecord task = stack.topTask();
2680 if (task != null) {
2681 mWindowManager.moveTaskToTop(task.taskId);
2682 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002683 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002684 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002685
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002686 ActivityStack stack = getStack(restoreStackId);
2687 if (stack == null) {
2688 stack = mHomeStack;
2689 }
2690 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002691 if (stack.isOnHomeDisplay()) {
2692 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002693 TaskRecord task = stack.topTask();
2694 if (task != null) {
2695 mWindowManager.moveTaskToTop(task.taskId);
2696 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002697 } else {
2698 // Stack was moved to another display while user was swapped out.
Craig Mautner84984fa2014-06-19 11:19:20 -07002699 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
Craig Mautnere0a38842013-12-16 16:14:02 -08002700 }
Craig Mautner93529a42013-10-04 15:03:13 -07002701 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002702 }
2703
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002704 /**
2705 * Add background users to send boot completed events to.
2706 * @param userId The user being started in the background
2707 * @param uss The state object for the user.
2708 */
2709 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
2710 mStartingBackgroundUsers.add(uss);
2711 }
2712
Craig Mautnerde4ef022013-04-07 19:01:33 -07002713 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
2714 int N = mStoppingActivities.size();
2715 if (N <= 0) return null;
2716
2717 ArrayList<ActivityRecord> stops = null;
2718
2719 final boolean nowVisible = allResumedActivitiesVisible();
2720 for (int i=0; i<N; i++) {
2721 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07002722 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07002723 + nowVisible + " waitingVisible=" + s.waitingVisible
2724 + " finishing=" + s.finishing);
2725 if (s.waitingVisible && nowVisible) {
2726 mWaitingVisibleActivities.remove(s);
2727 s.waitingVisible = false;
2728 if (s.finishing) {
2729 // If this activity is finishing, it is sitting on top of
2730 // everyone else but we now know it is no longer needed...
2731 // so get rid of it. Otherwise, we need to go through the
2732 // normal flow and hide it once we determine that it is
2733 // hidden by the activities in front of it.
2734 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002735 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002736 }
2737 }
2738 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
2739 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
2740 if (stops == null) {
2741 stops = new ArrayList<ActivityRecord>();
2742 }
2743 stops.add(s);
2744 mStoppingActivities.remove(i);
2745 N--;
2746 i--;
2747 }
2748 }
2749
2750 return stops;
2751 }
2752
Craig Mautnercf910b02013-04-23 11:23:27 -07002753 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002754 // FIXME
2755/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2756 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07002757 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002758 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07002759 if (isFrontStack(stack)) {
2760 if (r == null) {
2761 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
2762 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002763 final ActivityRecord pausing = stack.mPausingActivity;
2764 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002765 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002766 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002767 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002768 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002769 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002770 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002771 }
2772 }
2773 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002774 final ActivityRecord resumed = stack.mResumedActivity;
2775 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002776 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002777 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002778 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002779 if (r != null && (state == ActivityState.INITIALIZING
2780 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002781 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002782 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002783 }
2784 }
2785 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002786*/
Craig Mautner76ea2242013-05-15 11:40:05 -07002787 }
2788
Craig Mautner27084302013-03-25 08:05:25 -07002789 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002790 pw.print(prefix); pw.print("mDismissKeyguardOnNextActivity=");
Craig Mautner27084302013-03-25 08:05:25 -07002791 pw.println(mDismissKeyguardOnNextActivity);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002792 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002793 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002794 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2795 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2796 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002797 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07002798 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002799
Craig Mautner20e72272013-04-01 13:45:53 -07002800 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002801 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002802 }
2803
Dianne Hackborn390517b2013-05-30 15:03:32 -07002804 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2805 boolean needSep, String prefix) {
2806 if (activity != null) {
2807 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2808 if (needSep) {
2809 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002810 }
2811 pw.print(prefix);
2812 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002813 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002814 }
2815 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002816 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002817 }
2818
Craig Mautner8d341ef2013-03-26 09:03:27 -07002819 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2820 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002821 boolean printed = false;
2822 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002823 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2824 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002825 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
2826 pw.println(" (activities from bottom to top):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002827 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002828 final int numStacks = stacks.size();
2829 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2830 final ActivityStack stack = stacks.get(stackNdx);
2831 StringBuilder stackHeader = new StringBuilder(128);
2832 stackHeader.append(" Stack #");
2833 stackHeader.append(stack.mStackId);
2834 stackHeader.append(":");
2835 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2836 needSep, stackHeader.toString());
2837 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2838 !dumpAll, false, dumpPackage, true,
2839 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002840
Craig Mautner4a1cb222013-12-04 16:14:06 -08002841 needSep = printed;
2842 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2843 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002844 if (pr) {
2845 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002846 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002847 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002848 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2849 " mResumedActivity: ");
2850 if (pr) {
2851 printed = true;
2852 needSep = false;
2853 }
2854 if (dumpAll) {
2855 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2856 " mLastPausedActivity: ");
2857 if (pr) {
2858 printed = true;
2859 needSep = true;
2860 }
2861 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2862 needSep, " mLastNoHistoryActivity: ");
2863 }
2864 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002865 }
2866 }
2867
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002868 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2869 false, dumpPackage, true, " Activities waiting to finish:", null);
2870 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2871 false, dumpPackage, true, " Activities waiting to stop:", null);
2872 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2873 false, dumpPackage, true, " Activities waiting for another to become visible:",
2874 null);
2875 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2876 false, dumpPackage, true, " Activities waiting to sleep:", null);
2877 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2878 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002879
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002880 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002881 }
2882
Dianne Hackborn390517b2013-05-30 15:03:32 -07002883 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002884 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002885 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002886 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002887 String innerPrefix = null;
2888 String[] args = null;
2889 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002890 for (int i=list.size()-1; i>=0; i--) {
2891 final ActivityRecord r = list.get(i);
2892 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2893 continue;
2894 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002895 if (innerPrefix == null) {
2896 innerPrefix = prefix + " ";
2897 args = new String[0];
2898 }
2899 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002900 final boolean full = !brief && (complete || !r.isInHistory());
2901 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002902 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002903 needNL = false;
2904 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002905 if (header1 != null) {
2906 pw.println(header1);
2907 header1 = null;
2908 }
2909 if (header2 != null) {
2910 pw.println(header2);
2911 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002912 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002913 if (lastTask != r.task) {
2914 lastTask = r.task;
2915 pw.print(prefix);
2916 pw.print(full ? "* " : " ");
2917 pw.println(lastTask);
2918 if (full) {
2919 lastTask.dump(pw, prefix + " ");
2920 } else if (complete) {
2921 // Complete + brief == give a summary. Isn't that obvious?!?
2922 if (lastTask.intent != null) {
2923 pw.print(prefix); pw.print(" ");
2924 pw.println(lastTask.intent.toInsecureStringWithClip());
2925 }
2926 }
2927 }
2928 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2929 pw.print(" #"); pw.print(i); pw.print(": ");
2930 pw.println(r);
2931 if (full) {
2932 r.dump(pw, innerPrefix);
2933 } else if (complete) {
2934 // Complete + brief == give a summary. Isn't that obvious?!?
2935 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2936 if (r.app != null) {
2937 pw.print(innerPrefix); pw.println(r.app);
2938 }
2939 }
2940 if (client && r.app != null && r.app.thread != null) {
2941 // flush anything that is already in the PrintWriter since the thread is going
2942 // to write to the file descriptor directly
2943 pw.flush();
2944 try {
2945 TransferPipe tp = new TransferPipe();
2946 try {
2947 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
2948 r.appToken, innerPrefix, args);
2949 // Short timeout, since blocking here can
2950 // deadlock with the application.
2951 tp.go(fd, 2000);
2952 } finally {
2953 tp.kill();
2954 }
2955 } catch (IOException e) {
2956 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
2957 } catch (RemoteException e) {
2958 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
2959 }
2960 needNL = true;
2961 }
2962 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002963 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002964 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002965
Craig Mautnerf3333272013-04-22 10:55:53 -07002966 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002967 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07002968 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
2969 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07002970 }
2971
2972 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07002973 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07002974 }
2975
2976 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002977 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002978 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2979 }
2980
Craig Mautner05d29032013-05-03 13:40:13 -07002981 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08002982 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
2983 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2984 }
Craig Mautner05d29032013-05-03 13:40:13 -07002985 }
2986
Craig Mautner0eea92c2013-05-16 13:35:39 -07002987 void removeSleepTimeouts() {
2988 mSleepTimeout = false;
2989 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
2990 }
2991
2992 final void scheduleSleepTimeout() {
2993 removeSleepTimeouts();
2994 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
2995 }
2996
Craig Mautner4a1cb222013-12-04 16:14:06 -08002997 @Override
2998 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002999 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003000 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3001 }
3002
3003 @Override
3004 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003005 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003006 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3007 }
3008
3009 @Override
3010 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003011 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003012 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3013 }
3014
3015 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003016 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003017 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003018 newDisplay = mActivityDisplays.get(displayId) == null;
3019 if (newDisplay) {
3020 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
3021 mActivityDisplays.put(displayId, activityDisplay);
3022 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003023 }
Craig Mautner4504de52013-12-20 09:06:56 -08003024 if (newDisplay) {
3025 mWindowManager.onDisplayAdded(displayId);
3026 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003027 }
3028
3029 public void handleDisplayRemovedLocked(int displayId) {
3030 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003031 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3032 if (activityDisplay != null) {
3033 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003034 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003035 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003036 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003037 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003038 }
3039 }
3040 mWindowManager.onDisplayRemoved(displayId);
3041 }
3042
3043 public void handleDisplayChangedLocked(int displayId) {
3044 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003045 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3046 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003047 // TODO: Update the bounds.
3048 }
3049 }
3050 mWindowManager.onDisplayChanged(displayId);
3051 }
3052
3053 StackInfo getStackInfo(ActivityStack stack) {
3054 StackInfo info = new StackInfo();
3055 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3056 info.displayId = Display.DEFAULT_DISPLAY;
3057 info.stackId = stack.mStackId;
3058
3059 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3060 final int numTasks = tasks.size();
3061 int[] taskIds = new int[numTasks];
3062 String[] taskNames = new String[numTasks];
3063 for (int i = 0; i < numTasks; ++i) {
3064 final TaskRecord task = tasks.get(i);
3065 taskIds[i] = task.taskId;
3066 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3067 : task.realActivity != null ? task.realActivity.flattenToString()
3068 : task.getTopActivity() != null ? task.getTopActivity().packageName
3069 : "unknown";
3070 }
3071 info.taskIds = taskIds;
3072 info.taskNames = taskNames;
3073 return info;
3074 }
3075
3076 StackInfo getStackInfoLocked(int stackId) {
3077 ActivityStack stack = getStack(stackId);
3078 if (stack != null) {
3079 return getStackInfo(stack);
3080 }
3081 return null;
3082 }
3083
3084 ArrayList<StackInfo> getAllStackInfosLocked() {
3085 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003086 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3087 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003088 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3089 list.add(getStackInfo(stacks.get(ndx)));
3090 }
3091 }
3092 return list;
3093 }
3094
Jason Monka8f569c2014-07-07 12:15:21 -04003095 void showLockTaskToast() {
3096 mLockTaskNotify.showToast(mLockTaskIsLocked);
3097 }
3098
3099 void setLockTaskModeLocked(TaskRecord task, boolean isLocked) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003100 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003101 // Take out of lock task mode if necessary
3102 if (mLockTaskModeTask != null) {
3103 final Message lockTaskMsg = Message.obtain();
3104 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3105 lockTaskMsg.what = LOCK_TASK_END_MSG;
3106 mLockTaskModeTask = null;
3107 mHandler.sendMessage(lockTaskMsg);
3108 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003109 return;
3110 }
3111 if (isLockTaskModeViolation(task)) {
3112 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3113 return;
3114 }
3115 mLockTaskModeTask = task;
3116 findTaskToMoveToFrontLocked(task, 0, null);
3117 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003118
3119 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003120 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3121 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003122 lockTaskMsg.what = LOCK_TASK_START_MSG;
Jason Monka8f569c2014-07-07 12:15:21 -04003123 lockTaskMsg.arg2 = !isLocked ? 1 : 0;
justinzhang5286d3f2014-05-12 17:06:01 -04003124 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003125 }
3126
3127 boolean isLockTaskModeViolation(TaskRecord task) {
3128 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3129 }
3130
3131 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3132 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
3133 mLockTaskModeTask = null;
3134 }
3135 }
3136
3137 boolean isInLockTaskMode() {
3138 return mLockTaskModeTask != null;
3139 }
3140
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003141 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003142
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003143 public ActivityStackSupervisorHandler(Looper looper) {
3144 super(looper);
3145 }
3146
Craig Mautnerf3333272013-04-22 10:55:53 -07003147 void activityIdleInternal(ActivityRecord r) {
3148 synchronized (mService) {
3149 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3150 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003151 }
3152
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003153 @Override
3154 public void handleMessage(Message msg) {
3155 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003156 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003157 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003158 if (mService.mDidDexOpt) {
3159 mService.mDidDexOpt = false;
3160 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3161 nmsg.obj = msg.obj;
3162 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3163 return;
3164 }
3165 // We don't at this point know if the activity is fullscreen,
3166 // so we need to be conservative and assume it isn't.
3167 activityIdleInternal((ActivityRecord)msg.obj);
3168 } break;
3169 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003170 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003171 activityIdleInternal((ActivityRecord)msg.obj);
3172 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003173 case RESUME_TOP_ACTIVITY_MSG: {
3174 synchronized (mService) {
3175 resumeTopActivitiesLocked();
3176 }
3177 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003178 case SLEEP_TIMEOUT_MSG: {
3179 synchronized (mService) {
3180 if (mService.isSleepingOrShuttingDown()) {
3181 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3182 mSleepTimeout = true;
3183 checkReadyForSleepLocked();
3184 }
3185 }
3186 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003187 case LAUNCH_TIMEOUT_MSG: {
3188 if (mService.mDidDexOpt) {
3189 mService.mDidDexOpt = false;
3190 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3191 return;
3192 }
3193 synchronized (mService) {
3194 if (mLaunchingActivity.isHeld()) {
3195 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3196 if (VALIDATE_WAKE_LOCK_CALLER
3197 && Binder.getCallingUid() != Process.myUid()) {
3198 throw new IllegalStateException("Calling must be system uid");
3199 }
3200 mLaunchingActivity.release();
3201 }
3202 }
3203 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003204 case HANDLE_DISPLAY_ADDED: {
3205 handleDisplayAddedLocked(msg.arg1);
3206 } break;
3207 case HANDLE_DISPLAY_CHANGED: {
3208 handleDisplayChangedLocked(msg.arg1);
3209 } break;
3210 case HANDLE_DISPLAY_REMOVED: {
3211 handleDisplayRemovedLocked(msg.arg1);
3212 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003213 case CONTAINER_CALLBACK_VISIBILITY: {
3214 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003215 final IActivityContainerCallback callback = container.mCallback;
3216 if (callback != null) {
3217 try {
3218 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3219 } catch (RemoteException e) {
3220 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003221 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003222 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003223 case LOCK_TASK_START_MSG: {
3224 // When lock task starts, we disable the status bars.
3225 try {
Jason Monk62515be2014-05-21 16:06:19 -04003226 if (mLockTaskNotify == null) {
3227 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003228 }
Jason Monk62515be2014-05-21 16:06:19 -04003229 mLockTaskNotify.show(true);
3230 if (getStatusBarService() != null) {
3231 int flags =
3232 StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
3233 if (msg.arg2 != 0) {
3234 flags ^= StatusBarManager.DISABLE_HOME
3235 | StatusBarManager.DISABLE_RECENT;
3236 }
3237 getStatusBarService().disable(flags, mToken,
3238 mService.mContext.getPackageName());
3239 }
3240 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003241 if (getDevicePolicyManager() != null) {
3242 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003243 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003244 }
justinzhang5286d3f2014-05-12 17:06:01 -04003245 } catch (RemoteException ex) {
3246 throw new RuntimeException(ex);
3247 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003248 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003249 case LOCK_TASK_END_MSG: {
3250 // When lock task ends, we enable the status bars.
3251 try {
Jason Monk62515be2014-05-21 16:06:19 -04003252 if (getStatusBarService() != null) {
3253 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3254 mService.mContext.getPackageName());
3255 }
3256 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003257 if (getDevicePolicyManager() != null) {
3258 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3259 msg.arg1);
3260 }
Jason Monk62515be2014-05-21 16:06:19 -04003261 if (mLockTaskNotify == null) {
3262 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3263 }
3264 mLockTaskNotify.show(false);
3265 try {
3266 boolean shouldLockKeyguard = Settings.System.getInt(
3267 mService.mContext.getContentResolver(),
3268 Settings.System.LOCK_TO_APP_EXIT_LOCKED) != 0;
3269 if (shouldLockKeyguard) {
3270 mWindowManager.lockNow(null);
3271 }
3272 } catch (SettingNotFoundException e) {
3273 // No setting, don't lock.
3274 }
justinzhang5286d3f2014-05-12 17:06:01 -04003275 } catch (RemoteException ex) {
3276 throw new RuntimeException(ex);
3277 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003278 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003279 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3280 final ActivityContainer container = (ActivityContainer) msg.obj;
3281 final IActivityContainerCallback callback = container.mCallback;
3282 if (callback != null) {
3283 try {
3284 callback.onAllActivitiesComplete(container.asBinder());
3285 } catch (RemoteException e) {
3286 }
3287 }
3288 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003289 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3290 synchronized (mService) {
3291 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3292 msg.obj);
3293 ((ActivityContainer) msg.obj).onTaskListEmptyLocked();
3294 }
3295 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003296 }
3297 }
3298 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003299
Ying Wangb081a592014-04-22 15:20:16 -07003300 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003301 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003302 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003303 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003304 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003305 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003306 ActivityRecord mParentActivity = null;
3307 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003308
Craig Mautnere3a00d72014-04-16 08:31:19 -07003309 boolean mVisible = true;
3310
Craig Mautner4a1cb222013-12-04 16:14:06 -08003311 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003312 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003313
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003314 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3315 final static int CONTAINER_STATE_NO_SURFACE = 1;
3316 final static int CONTAINER_STATE_FINISHING = 2;
3317 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3318
3319 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003320 synchronized (mService) {
3321 mStackId = stackId;
3322 mStack = new ActivityStack(this);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003323 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003324 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003325 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003326 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003327
Craig Mautnere0a38842013-12-16 16:14:02 -08003328 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003329 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3330 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003331 mActivityDisplay = activityDisplay;
3332 mStack.mDisplayId = activityDisplay.mDisplayId;
3333 mStack.mStacks = activityDisplay.mStacks;
3334
3335 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003336 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003337 }
3338
3339 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003340 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003341 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003342 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3343 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003344 return;
3345 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003346 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003347 }
3348 }
3349
3350 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003351 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003352 synchronized (mService) {
3353 if (mActivityDisplay != null) {
3354 return mActivityDisplay.mDisplayId;
3355 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003356 }
3357 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003358 }
3359
Jeff Brownca9bc702014-02-11 14:32:56 -08003360 @Override
3361 public boolean injectEvent(InputEvent event) {
3362 final long origId = Binder.clearCallingIdentity();
3363 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003364 synchronized (mService) {
3365 if (mActivityDisplay != null) {
3366 return mInputManagerInternal.injectInputEvent(event,
3367 mActivityDisplay.mDisplayId,
3368 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3369 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003370 }
3371 return false;
3372 } finally {
3373 Binder.restoreCallingIdentity(origId);
3374 }
3375 }
3376
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003377 @Override
3378 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003379 synchronized (mService) {
3380 if (mContainerState == CONTAINER_STATE_FINISHING) {
3381 return;
3382 }
3383 mContainerState = CONTAINER_STATE_FINISHING;
3384
3385 final Message msg =
3386 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3387 mHandler.sendMessageDelayed(msg, 1000);
3388
3389 long origId = Binder.clearCallingIdentity();
3390 try {
3391 mStack.finishAllActivitiesLocked();
3392 } finally {
3393 Binder.restoreCallingIdentity(origId);
3394 }
3395 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003396 }
3397
Craig Mautner34b73df2014-01-12 21:11:08 -08003398 private void detachLocked() {
3399 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3400 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003401 if (mActivityDisplay != null) {
3402 mActivityDisplay.detachActivitiesLocked(mStack);
3403 mActivityDisplay = null;
3404 mStack.mDisplayId = -1;
3405 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003406 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003407 }
3408 }
3409
3410 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003411 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003412 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003413 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003414 Binder.getCallingUid(), mCurrentUser, false,
3415 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003416 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003417 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003418 String mimeType = intent.getType();
3419 if (mimeType == null && intent.getData() != null
3420 && "content".equals(intent.getData().getScheme())) {
3421 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3422 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003423 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0, 0, null,
Craig Mautnere0a38842013-12-16 16:14:02 -08003424 null, null, null, null, userId, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003425 }
3426
3427 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003428 public final int startActivityIntentSender(IIntentSender intentSender) {
3429 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3430
3431 if (!(intentSender instanceof PendingIntentRecord)) {
3432 throw new IllegalArgumentException("Bad PendingIntent object");
3433 }
3434
3435 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003436 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003437 }
3438
Craig Mautner247ab652014-04-25 10:09:00 -07003439 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3440 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003441 Binder.getCallingUid(), mCurrentUser, false,
3442 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautner247ab652014-04-25 10:09:00 -07003443 if (resolvedType == null) {
3444 resolvedType = intent.getType();
3445 if (resolvedType == null && intent.getData() != null
3446 && "content".equals(intent.getData().getScheme())) {
3447 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3448 }
3449 }
3450 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003451 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003452 throw new SecurityException(
3453 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3454 }
3455 }
3456
3457 /** Throw a SecurityException if allowEmbedded is not true */
3458 @Override
3459 public final void checkEmbeddedAllowed(Intent intent) {
3460 checkEmbeddedAllowedInner(intent, null);
3461 }
3462
3463 /** Throw a SecurityException if allowEmbedded is not true */
3464 @Override
3465 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3466 if (!(intentSender instanceof PendingIntentRecord)) {
3467 throw new IllegalArgumentException("Bad PendingIntent object");
3468 }
3469 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3470 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3471 pendingIntent.key.requestResolvedType);
3472 }
3473
Craig Mautnerdf88d732014-01-27 09:21:32 -08003474 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003475 public IBinder asBinder() {
3476 return this;
3477 }
3478
Craig Mautner4504de52013-12-20 09:06:56 -08003479 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003480 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003481 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003482 }
3483
Craig Mautner4a1cb222013-12-04 16:14:06 -08003484 ActivityStackSupervisor getOuter() {
3485 return ActivityStackSupervisor.this;
3486 }
3487
Craig Mautnerd163e752014-06-13 17:18:47 -07003488 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003489 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003490 }
3491
3492 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003493 synchronized (mService) {
3494 if (mActivityDisplay != null) {
3495 mActivityDisplay.getBounds(outBounds);
3496 } else {
3497 outBounds.set(0, 0);
3498 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003499 }
3500 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003501
Craig Mautner6985bad2014-04-21 15:22:06 -07003502 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003503 void setVisible(boolean visible) {
3504 if (mVisible != visible) {
3505 mVisible = visible;
3506 if (mCallback != null) {
3507 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3508 0 /* unused */, this).sendToTarget();
3509 }
3510 }
3511 }
3512
Craig Mautner6985bad2014-04-21 15:22:06 -07003513 void setDrawn() {
3514 }
3515
Craig Mautner1b4bf852014-05-26 15:06:32 -07003516 // You can always start a new task on a regular ActivityStack.
3517 boolean isEligibleForNewTasks() {
3518 return true;
3519 }
3520
Craig Mautnerd163e752014-06-13 17:18:47 -07003521 void onTaskListEmptyLocked() {
3522 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3523 if (!mStack.isHomeStack()) {
3524 detachLocked();
3525 deleteActivityContainer(this);
3526 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003527 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
3528 }
3529
Craig Mautner34b73df2014-01-12 21:11:08 -08003530 @Override
3531 public String toString() {
3532 return mIdString + (mActivityDisplay == null ? "N" : "A");
3533 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003534 }
3535
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003536 private class VirtualActivityContainer extends ActivityContainer {
3537 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003538 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003539
3540 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3541 super(getNextStackId());
3542 mParentActivity = parent;
3543 mCallback = callback;
3544 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003545 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003546 }
3547
3548 @Override
3549 public void setSurface(Surface surface, int width, int height, int density) {
3550 super.setSurface(surface, width, height, density);
3551
3552 synchronized (mService) {
3553 final long origId = Binder.clearCallingIdentity();
3554 try {
3555 setSurfaceLocked(surface, width, height, density);
3556 } finally {
3557 Binder.restoreCallingIdentity(origId);
3558 }
3559 }
3560 }
3561
3562 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3563 if (mContainerState == CONTAINER_STATE_FINISHING) {
3564 return;
3565 }
3566 VirtualActivityDisplay virtualActivityDisplay =
3567 (VirtualActivityDisplay) mActivityDisplay;
3568 if (virtualActivityDisplay == null) {
3569 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003570 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003571 mActivityDisplay = virtualActivityDisplay;
3572 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3573 attachToDisplayLocked(virtualActivityDisplay);
3574 }
3575
3576 if (mSurface != null) {
3577 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003578 }
3579
Craig Mautner6985bad2014-04-21 15:22:06 -07003580 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003581 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003582 mStack.resumeTopActivityLocked(null);
3583 } else {
3584 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003585 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003586 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
3587 mStack.startPausingLocked(false, true);
3588 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003589 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003590
Craig Mautnerd163e752014-06-13 17:18:47 -07003591 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003592
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003593 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3594 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003595 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003596
Craig Mautner6985bad2014-04-21 15:22:06 -07003597 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003598 boolean isAttachedLocked() {
3599 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003600 }
3601
3602 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003603 void setDrawn() {
3604 synchronized (mService) {
3605 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003606 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003607 }
3608 }
3609
Craig Mautner1b4bf852014-05-26 15:06:32 -07003610 // Never start a new task on an ActivityView if it isn't explicitly specified.
3611 @Override
3612 boolean isEligibleForNewTasks() {
3613 return false;
3614 }
3615
Craig Mautnerd163e752014-06-13 17:18:47 -07003616 private void setSurfaceIfReadyLocked() {
3617 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003618 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3619 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3620 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3621 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3622 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003623 }
3624 }
3625
Craig Mautner4a1cb222013-12-04 16:14:06 -08003626 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3627 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003628 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003629 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003630 int mDisplayId;
3631 Display mDisplay;
3632 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003633
Craig Mautner4a1cb222013-12-04 16:14:06 -08003634 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3635 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08003636 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003637
Craig Mautneree2e45a2014-06-27 12:10:03 -07003638 ActivityRecord mMediaPlayingActivity;
3639
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003640 ActivityDisplay() {
3641 }
Craig Mautner4504de52013-12-20 09:06:56 -08003642
Craig Mautnere0a38842013-12-16 16:14:02 -08003643 ActivityDisplay(int displayId) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003644 init(mDisplayManager.getDisplay(displayId));
Craig Mautner4504de52013-12-20 09:06:56 -08003645 }
3646
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003647 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003648 mDisplay = display;
3649 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003650 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003651 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003652
3653 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003654 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
3655 + mDisplayId);
3656 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003657 }
3658
Craig Mautnere0a38842013-12-16 16:14:02 -08003659 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003660 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003661 + " from displayId=" + mDisplayId);
3662 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003663 }
3664
3665 void getBounds(Point bounds) {
3666 mDisplay.getDisplayInfo(mDisplayInfo);
3667 bounds.x = mDisplayInfo.appWidth;
3668 bounds.y = mDisplayInfo.appHeight;
3669 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003670
Craig Mautneree2e45a2014-06-27 12:10:03 -07003671 void setMediaPlaying(ActivityRecord r) {
3672 mMediaPlayingActivity = r;
3673 }
3674
3675 boolean isMediaPlaying() {
3676 return mMediaPlayingActivity != null;
3677 }
3678
Craig Mautner34b73df2014-01-12 21:11:08 -08003679 @Override
3680 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003681 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3682 }
3683 }
3684
3685 class VirtualActivityDisplay extends ActivityDisplay {
3686 VirtualDisplay mVirtualDisplay;
3687
Craig Mautner6985bad2014-04-21 15:22:06 -07003688 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003689 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
3690 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, VIRTUAL_DISPLAY_BASE_NAME,
Craig Mautner6985bad2014-04-21 15:22:06 -07003691 width, height, density, null, DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003692 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY);
3693
3694 init(mVirtualDisplay.getDisplay());
3695
3696 mWindowManager.handleDisplayAdded(mDisplayId);
3697 }
3698
3699 void setSurface(Surface surface) {
3700 if (mVirtualDisplay != null) {
3701 mVirtualDisplay.setSurface(surface);
3702 }
3703 }
3704
3705 @Override
3706 void detachActivitiesLocked(ActivityStack stack) {
3707 super.detachActivitiesLocked(stack);
3708 if (mVirtualDisplay != null) {
3709 mVirtualDisplay.release();
3710 mVirtualDisplay = null;
3711 }
3712 }
3713
3714 @Override
3715 public String toString() {
3716 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003717 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003718 }
Jose Lima58e66d62014-05-27 20:00:27 -07003719
3720 private boolean isLeanbackOnlyDevice() {
3721 boolean onLeanbackOnly = false;
3722 try {
3723 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
3724 PackageManager.FEATURE_LEANBACK_ONLY);
3725 } catch (RemoteException e) {
3726 // noop
3727 }
3728
3729 return onLeanbackOnly;
3730 }
Craig Mautner27084302013-03-25 08:05:25 -07003731}