blob: 9d1f8c68e9b5e68827b94ad1645ea46ab6fad721 [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 Mautner2420ead2013-04-01 17:13:20 -0700101import com.android.server.am.ActivityStack.ActivityState;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700102import com.android.server.wm.WindowManagerService;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700103
justinzhang5286d3f2014-05-12 17:06:01 -0400104
Craig Mautner8d341ef2013-03-26 09:03:27 -0700105import java.io.FileDescriptor;
106import java.io.IOException;
Craig Mautner27084302013-03-25 08:05:25 -0700107import java.io.PrintWriter;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700108import java.util.ArrayList;
Craig Mautner8d341ef2013-03-26 09:03:27 -0700109import java.util.List;
Craig Mautner27084302013-03-25 08:05:25 -0700110
Craig Mautner4a1cb222013-12-04 16:14:06 -0800111public final class ActivityStackSupervisor implements DisplayListener {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700112 static final boolean DEBUG = ActivityManagerService.DEBUG || false;
113 static final boolean DEBUG_ADD_REMOVE = DEBUG || false;
114 static final boolean DEBUG_APP = DEBUG || false;
Craig Mautner4a9f1292014-06-11 13:44:50 -0700115 static final boolean DEBUG_CONTAINERS = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700116 static final boolean DEBUG_IDLE = DEBUG || false;
Craig Mautneree2e45a2014-06-27 12:10:03 -0700117 static final boolean DEBUG_MEDIA_VISIBILITY = DEBUG || false;
Craig Mautnerd163e752014-06-13 17:18:47 -0700118 static final boolean DEBUG_SAVED_STATE = DEBUG || false;
119 static final boolean DEBUG_SCREENSHOTS = DEBUG || false;
120 static final boolean DEBUG_STATES = DEBUG || false;
Craig Mautner2420ead2013-04-01 17:13:20 -0700121
Craig Mautner2219a1b2013-03-25 09:44:30 -0700122 public static final int HOME_STACK_ID = 0;
Craig Mautner27084302013-03-25 08:05:25 -0700123
Craig Mautnerf3333272013-04-22 10:55:53 -0700124 /** How long we wait until giving up on the last activity telling us it is idle. */
125 static final int IDLE_TIMEOUT = 10*1000;
126
Craig Mautner0eea92c2013-05-16 13:35:39 -0700127 /** How long we can hold the sleep wake lock before giving up. */
128 static final int SLEEP_TIMEOUT = 5*1000;
129
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700130 // How long we can hold the launch wake lock before giving up.
131 static final int LAUNCH_TIMEOUT = 10*1000;
132
Craig Mautner05d29032013-05-03 13:40:13 -0700133 static final int IDLE_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG;
134 static final int IDLE_NOW_MSG = FIRST_SUPERVISOR_STACK_MSG + 1;
135 static final int RESUME_TOP_ACTIVITY_MSG = FIRST_SUPERVISOR_STACK_MSG + 2;
Craig Mautner0eea92c2013-05-16 13:35:39 -0700136 static final int SLEEP_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 3;
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700137 static final int LAUNCH_TIMEOUT_MSG = FIRST_SUPERVISOR_STACK_MSG + 4;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800138 static final int HANDLE_DISPLAY_ADDED = FIRST_SUPERVISOR_STACK_MSG + 5;
139 static final int HANDLE_DISPLAY_CHANGED = FIRST_SUPERVISOR_STACK_MSG + 6;
140 static final int HANDLE_DISPLAY_REMOVED = FIRST_SUPERVISOR_STACK_MSG + 7;
Craig Mautnere3a00d72014-04-16 08:31:19 -0700141 static final int CONTAINER_CALLBACK_VISIBILITY = FIRST_SUPERVISOR_STACK_MSG + 8;
justinzhang5286d3f2014-05-12 17:06:01 -0400142 static final int LOCK_TASK_START_MSG = FIRST_SUPERVISOR_STACK_MSG + 9;
143 static final int LOCK_TASK_END_MSG = FIRST_SUPERVISOR_STACK_MSG + 10;
Craig Mautnerb6011c12014-06-04 20:59:13 -0700144 static final int CONTAINER_CALLBACK_TASK_LIST_EMPTY = FIRST_SUPERVISOR_STACK_MSG + 11;
Craig Mautner4c07d022014-06-11 17:12:59 -0700145 static final int CONTAINER_TASK_LIST_EMPTY_TIMEOUT = FIRST_SUPERVISOR_STACK_MSG + 12;
Craig Mautnerbb742462014-07-07 15:28:55 -0700146 static final int LAUNCH_TASK_BEHIND_COMPLETE = FIRST_SUPERVISOR_STACK_MSG + 13;
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 Mautneree36c772014-07-16 14:56:05 -0700273 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
274 = new ArrayList<PendingActivityLaunch>();
275
276 /**
277 * Description of a request to start a new activity, which has been held
278 * due to app switches being disabled.
279 */
280 static class PendingActivityLaunch {
281 final ActivityRecord r;
282 final ActivityRecord sourceRecord;
283 final int startFlags;
284 final ActivityStack stack;
285
286 PendingActivityLaunch(ActivityRecord _r, ActivityRecord _sourceRecord,
287 int _startFlags, ActivityStack _stack) {
288 r = _r;
289 sourceRecord = _sourceRecord;
290 startFlags = _startFlags;
291 stack = _stack;
292 }
293 }
294
Craig Mautner4a1cb222013-12-04 16:14:06 -0800295 public ActivityStackSupervisor(ActivityManagerService service) {
Craig Mautner27084302013-03-25 08:05:25 -0700296 mService = service;
Jeff Brown2c43c332014-06-12 22:38:59 -0700297 mHandler = new ActivityStackSupervisorHandler(mService.mHandler.getLooper());
298 }
299
300 /**
301 * At the time when the constructor runs, the power manager has not yet been
302 * initialized. So we initialize our wakelocks afterwards.
303 */
304 void initPowerManagement() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800305 PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
Craig Mautner0eea92c2013-05-16 13:35:39 -0700306 mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
Craig Mautner7ea5bd42013-07-05 15:27:08 -0700307 mLaunchingActivity =
308 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
309 mLaunchingActivity.setReferenceCounted(false);
Craig Mautner2219a1b2013-03-25 09:44:30 -0700310 }
311
justinzhang5286d3f2014-05-12 17:06:01 -0400312 // This function returns a IStatusBarService. The value is from ServiceManager.
313 // getService and is cached.
314 private IStatusBarService getStatusBarService() {
315 synchronized (mService) {
316 if (mStatusBarService == null) {
317 mStatusBarService = IStatusBarService.Stub.asInterface(
318 ServiceManager.checkService(Context.STATUS_BAR_SERVICE));
319 if (mStatusBarService == null) {
320 Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE");
321 }
322 }
323 return mStatusBarService;
324 }
325 }
326
Jason Monk35c62a42014-06-17 10:24:47 -0400327 private IDevicePolicyManager getDevicePolicyManager() {
328 synchronized (mService) {
329 if (mDevicePolicyManager == null) {
330 mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
331 ServiceManager.checkService(Context.DEVICE_POLICY_SERVICE));
332 if (mDevicePolicyManager == null) {
333 Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE");
334 }
335 }
336 return mDevicePolicyManager;
337 }
338 }
339
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700340 void setWindowManager(WindowManagerService wm) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800341 synchronized (mService) {
342 mWindowManager = wm;
343
344 mDisplayManager =
345 (DisplayManager)mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
346 mDisplayManager.registerDisplayListener(this, null);
347
348 Display[] displays = mDisplayManager.getDisplays();
349 for (int displayNdx = displays.length - 1; displayNdx >= 0; --displayNdx) {
350 final int displayId = displays[displayNdx].getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -0800351 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
352 mActivityDisplays.put(displayId, activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800353 }
354
Craig Mautnerf4c909b2014-04-17 18:39:38 -0700355 createStackOnDisplay(HOME_STACK_ID, Display.DEFAULT_DISPLAY);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800356 mHomeStack = mFocusedStack = mLastFocusedStack = getStack(HOME_STACK_ID);
Jeff Brownca9bc702014-02-11 14:32:56 -0800357
358 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Jose Lima58e66d62014-05-27 20:00:27 -0700359
360 // Initialize this here, now that we can get a valid reference to PackageManager.
361 mLeanbackOnlyDevice = isLeanbackOnlyDevice();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800362 }
Craig Mautner27084302013-03-25 08:05:25 -0700363 }
364
365 void dismissKeyguard() {
Craig Mautner5314a402013-09-26 12:40:16 -0700366 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen("");
Craig Mautner27084302013-03-25 08:05:25 -0700367 if (mDismissKeyguardOnNextActivity) {
368 mDismissKeyguardOnNextActivity = false;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700369 mWindowManager.dismissKeyguard();
Craig Mautner27084302013-03-25 08:05:25 -0700370 }
371 }
372
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700373 ActivityStack getFocusedStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800374 return mFocusedStack;
Craig Mautner20e72272013-04-01 13:45:53 -0700375 }
376
Craig Mautnerde4ef022013-04-07 19:01:33 -0700377 ActivityStack getLastStack() {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800378 return mLastFocusedStack;
Craig Mautner2219a1b2013-03-25 09:44:30 -0700379 }
380
Craig Mautner4a1cb222013-12-04 16:14:06 -0800381 // TODO: Split into two methods isFrontStack for any visible stack and isFrontmostStack for the
382 // top of all visible stacks.
Craig Mautnerde4ef022013-04-07 19:01:33 -0700383 boolean isFrontStack(ActivityStack stack) {
Craig Mautnerdf88d732014-01-27 09:21:32 -0800384 final ActivityRecord parent = stack.mActivityContainer.mParentActivity;
385 if (parent != null) {
386 stack = parent.task.stack;
387 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800388 ArrayList<ActivityStack> stacks = stack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800389 if (stacks != null && !stacks.isEmpty()) {
390 return stack == stacks.get(stacks.size() - 1);
391 }
392 return false;
Craig Mautner20e72272013-04-01 13:45:53 -0700393 }
394
Craig Mautnerde4ef022013-04-07 19:01:33 -0700395 void moveHomeStack(boolean toFront) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800396 ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800397 int topNdx = stacks.size() - 1;
398 if (topNdx <= 0) {
399 return;
400 }
401 ActivityStack topStack = stacks.get(topNdx);
402 final boolean homeInFront = topStack == mHomeStack;
403 if (homeInFront != toFront) {
404 mLastFocusedStack = topStack;
405 stacks.remove(mHomeStack);
406 stacks.add(toFront ? topNdx : 0, mHomeStack);
407 mFocusedStack = stacks.get(topNdx);
408 if (DEBUG_STACK) Slog.d(TAG, "moveHomeTask: topStack old=" + topStack + " new="
409 + mFocusedStack);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700410 }
411 }
412
Craig Mautner84984fa2014-06-19 11:19:20 -0700413 void moveHomeStackTaskToTop(int homeStackTaskType) {
414 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
415 mWindowManager.showRecentApps();
416 return;
417 }
Craig Mautner69ada552013-04-18 13:51:51 -0700418 moveHomeStack(true);
Craig Mautner84984fa2014-06-19 11:19:20 -0700419 mHomeStack.moveHomeStackTaskToTop(homeStackTaskType);
Craig Mautner8e569572013-10-11 17:36:59 -0700420 }
421
Craig Mautner84984fa2014-06-19 11:19:20 -0700422 boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev) {
423 if (homeStackTaskType == RECENTS_ACTIVITY_TYPE) {
424 mWindowManager.showRecentApps();
425 return false;
Craig Mautnerdf6523f2014-05-20 19:17:54 -0700426 }
Craig Mautner84984fa2014-06-19 11:19:20 -0700427 moveHomeStackTaskToTop(homeStackTaskType);
428 if (prev != null) {
429 prev.task.setTaskToReturnTo(APPLICATION_ACTIVITY_TYPE);
430 }
431
Craig Mautnera8a90e02013-06-28 15:24:50 -0700432 ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
Craig Mautner84984fa2014-06-19 11:19:20 -0700433 // if (r != null && (r.isHomeActivity() || r.isRecentsActivity())) {
Craig Mautner719e6212014-05-29 16:49:41 +0000434 if (r != null && r.isHomeActivity()) {
Craig Mautnera8a90e02013-06-28 15:24:50 -0700435 mService.setFocusedActivityLocked(r);
Craig Mautner05d29032013-05-03 13:40:13 -0700436 return resumeTopActivitiesLocked(mHomeStack, prev, null);
Craig Mautner69ada552013-04-18 13:51:51 -0700437 }
438 return mService.startHomeActivityLocked(mCurrentUser);
439 }
440
Craig Mautner27084302013-03-25 08:05:25 -0700441 void setDismissKeyguard(boolean dismiss) {
Craig Mautner5314a402013-09-26 12:40:16 -0700442 if (ActivityManagerService.DEBUG_LOCKSCREEN) mService.logLockScreen(" dismiss=" + dismiss);
Craig Mautner27084302013-03-25 08:05:25 -0700443 mDismissKeyguardOnNextActivity = dismiss;
444 }
445
Craig Mautner8d341ef2013-03-26 09:03:27 -0700446 TaskRecord anyTaskForIdLocked(int id) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800447 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800448 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800449 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800450 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
451 ActivityStack stack = stacks.get(stackNdx);
452 TaskRecord task = stack.taskForIdLocked(id);
453 if (task != null) {
454 return task;
455 }
Craig Mautner8d341ef2013-03-26 09:03:27 -0700456 }
457 }
458 return null;
459 }
460
Craig Mautner6170f732013-04-02 13:05:23 -0700461 ActivityRecord isInAnyStackLocked(IBinder token) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800462 int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800463 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800464 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800465 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
466 final ActivityRecord r = stacks.get(stackNdx).isInStackLocked(token);
467 if (r != null) {
468 return r;
469 }
Craig Mautner6170f732013-04-02 13:05:23 -0700470 }
471 }
472 return null;
473 }
474
Craig Mautneref73ee12014-04-23 11:45:37 -0700475 void setNextTaskId(int taskId) {
476 if (taskId > mCurTaskId) {
477 mCurTaskId = taskId;
478 }
479 }
480
Craig Mautner8d341ef2013-03-26 09:03:27 -0700481 int getNextTaskId() {
482 do {
483 mCurTaskId++;
484 if (mCurTaskId <= 0) {
485 mCurTaskId = 1;
486 }
487 } while (anyTaskForIdLocked(mCurTaskId) != null);
488 return mCurTaskId;
489 }
490
Craig Mautnerde4ef022013-04-07 19:01:33 -0700491 ActivityRecord resumedAppLocked() {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700492 ActivityStack stack = getFocusedStack();
493 if (stack == null) {
494 return null;
495 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700496 ActivityRecord resumedActivity = stack.mResumedActivity;
497 if (resumedActivity == null || resumedActivity.app == null) {
498 resumedActivity = stack.mPausingActivity;
499 if (resumedActivity == null || resumedActivity.app == null) {
500 resumedActivity = stack.topRunningActivityLocked(null);
501 }
502 }
503 return resumedActivity;
504 }
505
Mike Lockwooded8902d2013-11-15 11:01:47 -0800506 boolean attachApplicationLocked(ProcessRecord app) throws Exception {
Craig Mautner20e72272013-04-01 13:45:53 -0700507 final String processName = app.processName;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800508 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800509 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
510 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800511 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
512 final ActivityStack stack = stacks.get(stackNdx);
513 if (!isFrontStack(stack)) {
514 continue;
515 }
516 ActivityRecord hr = stack.topRunningActivityLocked(null);
517 if (hr != null) {
518 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
519 && processName.equals(hr.processName)) {
520 try {
George Mount2c92c972014-03-20 09:38:23 -0700521 if (realStartActivityLocked(hr, app, true, true)) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800522 didSomething = true;
523 }
524 } catch (Exception e) {
525 Slog.w(TAG, "Exception in new application when starting activity "
526 + hr.intent.getComponent().flattenToShortString(), e);
527 throw e;
Craig Mautner20e72272013-04-01 13:45:53 -0700528 }
Craig Mautner20e72272013-04-01 13:45:53 -0700529 }
Craig Mautner20e72272013-04-01 13:45:53 -0700530 }
531 }
532 }
Craig Mautnerb59dcfd2013-05-06 13:12:58 -0700533 if (!didSomething) {
534 ensureActivitiesVisibleLocked(null, 0);
535 }
Craig Mautner20e72272013-04-01 13:45:53 -0700536 return didSomething;
537 }
538
539 boolean allResumedActivitiesIdle() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800540 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
541 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800542 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
543 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner34b73df2014-01-12 21:11:08 -0800544 if (!isFrontStack(stack) || stack.numActivities() == 0) {
Craig Mautner4a1cb222013-12-04 16:14:06 -0800545 continue;
546 }
547 final ActivityRecord resumedActivity = stack.mResumedActivity;
548 if (resumedActivity == null || !resumedActivity.idle) {
Craig Mautner34b73df2014-01-12 21:11:08 -0800549 if (DEBUG_STATES) Slog.d(TAG, "allResumedActivitiesIdle: stack="
550 + stack.mStackId + " " + resumedActivity + " not idle");
Craig Mautner4a1cb222013-12-04 16:14:06 -0800551 return false;
552 }
Craig Mautner20e72272013-04-01 13:45:53 -0700553 }
554 }
555 return true;
556 }
557
Craig Mautnerde4ef022013-04-07 19:01:33 -0700558 boolean allResumedActivitiesComplete() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800559 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
560 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800561 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
562 final ActivityStack stack = stacks.get(stackNdx);
563 if (isFrontStack(stack)) {
564 final ActivityRecord r = stack.mResumedActivity;
565 if (r != null && r.state != ActivityState.RESUMED) {
566 return false;
567 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700568 }
569 }
570 }
571 // TODO: Not sure if this should check if all Paused are complete too.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800572 if (DEBUG_STACK) Slog.d(TAG,
573 "allResumedActivitiesComplete: mLastFocusedStack changing from=" +
574 mLastFocusedStack + " to=" + mFocusedStack);
575 mLastFocusedStack = mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700576 return true;
577 }
578
579 boolean allResumedActivitiesVisible() {
Craig Mautnere0a38842013-12-16 16:14:02 -0800580 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
581 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800582 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
583 final ActivityStack stack = stacks.get(stackNdx);
584 final ActivityRecord r = stack.mResumedActivity;
585 if (r != null && (!r.nowVisible || r.waitingVisible)) {
586 return false;
587 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700588 }
589 }
590 return true;
591 }
592
Craig Mautner2acc3892013-09-23 10:28:14 -0700593 /**
594 * Pause all activities in either all of the stacks or just the back stacks.
595 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
Craig Mautner2acc3892013-09-23 10:28:14 -0700596 * @return true if any activity was paused as a result of this call.
597 */
Craig Mautner5314a402013-09-26 12:40:16 -0700598 boolean pauseBackStacks(boolean userLeaving) {
Craig Mautnercf910b02013-04-23 11:23:27 -0700599 boolean someActivityPaused = false;
Craig Mautnere0a38842013-12-16 16:14:02 -0800600 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
601 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800602 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
603 final ActivityStack stack = stacks.get(stackNdx);
604 if (!isFrontStack(stack) && stack.mResumedActivity != null) {
605 if (DEBUG_STATES) Slog.d(TAG, "pauseBackStacks: stack=" + stack +
606 " mResumedActivity=" + stack.mResumedActivity);
607 stack.startPausingLocked(userLeaving, false);
608 someActivityPaused = true;
609 }
Craig Mautnercf910b02013-04-23 11:23:27 -0700610 }
611 }
612 return someActivityPaused;
613 }
614
Craig Mautnerde4ef022013-04-07 19:01:33 -0700615 boolean allPausedActivitiesComplete() {
Craig Mautnerac6f8432013-07-17 13:24:59 -0700616 boolean pausing = true;
Craig Mautnere0a38842013-12-16 16:14:02 -0800617 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
618 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800619 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
620 final ActivityStack stack = stacks.get(stackNdx);
621 final ActivityRecord r = stack.mPausingActivity;
622 if (r != null && r.state != ActivityState.PAUSED
623 && r.state != ActivityState.STOPPED
624 && r.state != ActivityState.STOPPING) {
625 if (DEBUG_STATES) {
626 Slog.d(TAG, "allPausedActivitiesComplete: r=" + r + " state=" + r.state);
627 pausing = false;
628 } else {
629 return false;
630 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700631 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700632 }
633 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700634 return pausing;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700635 }
636
Craig Mautnerdf88d732014-01-27 09:21:32 -0800637 void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping) {
John Spurlock8a985d22014-02-25 09:40:05 -0500638 // TODO: Put all stacks in supervisor and iterate through them instead.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800639 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
640 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
641 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
642 final ActivityStack stack = stacks.get(stackNdx);
643 if (stack.mResumedActivity != null &&
644 stack.mActivityContainer.mParentActivity == parent) {
645 stack.startPausingLocked(userLeaving, uiSleeping);
646 }
647 }
648 }
649 }
650
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700651 void reportActivityVisibleLocked(ActivityRecord r) {
Craig Mautner858d8a62013-04-23 17:08:34 -0700652 for (int i = mWaitingActivityVisible.size()-1; i >= 0; i--) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700653 WaitResult w = mWaitingActivityVisible.get(i);
654 w.timeout = false;
655 if (r != null) {
656 w.who = new ComponentName(r.info.packageName, r.info.name);
657 }
658 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
659 w.thisTime = w.totalTime;
660 }
661 mService.notifyAll();
662 dismissKeyguard();
663 }
664
665 void reportActivityLaunchedLocked(boolean timeout, ActivityRecord r,
666 long thisTime, long totalTime) {
667 for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
Craig Mautnerc64f73e2013-04-24 16:44:56 -0700668 WaitResult w = mWaitingActivityLaunched.remove(i);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700669 w.timeout = timeout;
670 if (r != null) {
671 w.who = new ComponentName(r.info.packageName, r.info.name);
672 }
673 w.thisTime = thisTime;
674 w.totalTime = totalTime;
675 }
676 mService.notifyAll();
677 }
678
Craig Mautner29219d92013-04-16 20:19:12 -0700679 ActivityRecord topRunningActivityLocked() {
Craig Mautner1602ec22013-05-12 10:24:27 -0700680 final ActivityStack focusedStack = getFocusedStack();
681 ActivityRecord r = focusedStack.topRunningActivityLocked(null);
682 if (r != null) {
683 return r;
Craig Mautner29219d92013-04-16 20:19:12 -0700684 }
Craig Mautner1602ec22013-05-12 10:24:27 -0700685
Craig Mautner4a1cb222013-12-04 16:14:06 -0800686 // Return to the home stack.
Craig Mautnere0a38842013-12-16 16:14:02 -0800687 final ArrayList<ActivityStack> stacks = mHomeStack.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800688 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
689 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnerac6f8432013-07-17 13:24:59 -0700690 if (stack != focusedStack && isFrontStack(stack)) {
Craig Mautner29219d92013-04-16 20:19:12 -0700691 r = stack.topRunningActivityLocked(null);
692 if (r != null) {
693 return r;
694 }
695 }
696 }
697 return null;
698 }
699
Dianne Hackborn09233282014-04-30 11:33:59 -0700700 void getTasksLocked(int maxNum, List<RunningTaskInfo> list, int callingUid, boolean allowed) {
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700701 // Gather all of the running tasks for each stack into runningTaskLists.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800702 ArrayList<ArrayList<RunningTaskInfo>> runningTaskLists =
703 new ArrayList<ArrayList<RunningTaskInfo>>();
Craig Mautnere0a38842013-12-16 16:14:02 -0800704 final int numDisplays = mActivityDisplays.size();
Craig Mautner4a1cb222013-12-04 16:14:06 -0800705 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -0800706 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800707 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
708 final ActivityStack stack = stacks.get(stackNdx);
709 ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
710 runningTaskLists.add(stackTaskList);
Dianne Hackborn09233282014-04-30 11:33:59 -0700711 stack.getTasksLocked(stackTaskList, callingUid, allowed);
Craig Mautner20e72272013-04-01 13:45:53 -0700712 }
713 }
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700714
715 // The lists are already sorted from most recent to oldest. Just pull the most recent off
716 // each list and add it to list. Stop when all lists are empty or maxNum reached.
717 while (maxNum > 0) {
718 long mostRecentActiveTime = Long.MIN_VALUE;
719 ArrayList<RunningTaskInfo> selectedStackList = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800720 final int numTaskLists = runningTaskLists.size();
721 for (int stackNdx = 0; stackNdx < numTaskLists; ++stackNdx) {
722 ArrayList<RunningTaskInfo> stackTaskList = runningTaskLists.get(stackNdx);
Craig Mautnerc0fd8052013-09-19 11:20:17 -0700723 if (!stackTaskList.isEmpty()) {
724 final long lastActiveTime = stackTaskList.get(0).lastActiveTime;
725 if (lastActiveTime > mostRecentActiveTime) {
726 mostRecentActiveTime = lastActiveTime;
727 selectedStackList = stackTaskList;
728 }
729 }
730 }
731 if (selectedStackList != null) {
732 list.add(selectedStackList.remove(0));
733 --maxNum;
734 } else {
735 break;
736 }
737 }
Craig Mautner20e72272013-04-01 13:45:53 -0700738 }
739
Craig Mautner23ac33b2013-04-01 16:26:35 -0700740 ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
741 String profileFile, ParcelFileDescriptor profileFd, int userId) {
742 // Collect information about the target of the Intent.
743 ActivityInfo aInfo;
744 try {
745 ResolveInfo rInfo =
746 AppGlobals.getPackageManager().resolveIntent(
747 intent, resolvedType,
748 PackageManager.MATCH_DEFAULT_ONLY
749 | ActivityManagerService.STOCK_PM_FLAGS, userId);
750 aInfo = rInfo != null ? rInfo.activityInfo : null;
751 } catch (RemoteException e) {
752 aInfo = null;
753 }
754
755 if (aInfo != null) {
756 // Store the found target back into the intent, because now that
757 // we have it we never want to do this again. For example, if the
758 // user navigates back to this point in the history, we should
759 // always restart the exact same activity.
760 intent.setComponent(new ComponentName(
761 aInfo.applicationInfo.packageName, aInfo.name));
762
763 // Don't debug things in the system process
764 if ((startFlags&ActivityManager.START_FLAG_DEBUG) != 0) {
765 if (!aInfo.processName.equals("system")) {
766 mService.setDebugApp(aInfo.processName, true, false);
767 }
768 }
769
770 if ((startFlags&ActivityManager.START_FLAG_OPENGL_TRACES) != 0) {
771 if (!aInfo.processName.equals("system")) {
772 mService.setOpenGlTraceApp(aInfo.applicationInfo, aInfo.processName);
773 }
774 }
775
776 if (profileFile != null) {
777 if (!aInfo.processName.equals("system")) {
778 mService.setProfileApp(aInfo.applicationInfo, aInfo.processName,
779 profileFile, profileFd,
780 (startFlags&ActivityManager.START_FLAG_AUTO_STOP_PROFILER) != 0);
781 }
782 }
783 }
784 return aInfo;
785 }
786
Craig Mautner2219a1b2013-03-25 09:44:30 -0700787 void startHomeActivity(Intent intent, ActivityInfo aInfo) {
Craig Mautner84984fa2014-06-19 11:19:20 -0700788 moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700789 startActivityLocked(null, intent, null, aInfo, null, null, null, null, 0, 0, 0, null, 0,
Craig Mautnere0a38842013-12-16 16:14:02 -0800790 null, false, null, null);
Craig Mautner8d341ef2013-03-26 09:03:27 -0700791 }
792
Craig Mautner23ac33b2013-04-01 16:26:35 -0700793 final int startActivityMayWait(IApplicationThread caller, int callingUid,
Dianne Hackborn91097de2014-04-04 18:02:06 -0700794 String callingPackage, Intent intent, String resolvedType,
795 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
796 IBinder resultTo, String resultWho, int requestCode, int startFlags, String profileFile,
Craig Mautner23ac33b2013-04-01 16:26:35 -0700797 ParcelFileDescriptor profileFd, WaitResult outResult, Configuration config,
Craig Mautnere0a38842013-12-16 16:14:02 -0800798 Bundle options, int userId, IActivityContainer iContainer) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700799 // Refuse possible leaked file descriptors
800 if (intent != null && intent.hasFileDescriptors()) {
801 throw new IllegalArgumentException("File descriptors passed in Intent");
802 }
803 boolean componentSpecified = intent.getComponent() != null;
804
805 // Don't modify the client's object!
806 intent = new Intent(intent);
807
808 // Collect information about the target of the Intent.
809 ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
810 profileFile, profileFd, userId);
811
Craig Mautnere0a38842013-12-16 16:14:02 -0800812 ActivityContainer container = (ActivityContainer)iContainer;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700813 synchronized (mService) {
814 int callingPid;
815 if (callingUid >= 0) {
816 callingPid = -1;
817 } else if (caller == null) {
818 callingPid = Binder.getCallingPid();
819 callingUid = Binder.getCallingUid();
820 } else {
821 callingPid = callingUid = -1;
822 }
823
Craig Mautnere0a38842013-12-16 16:14:02 -0800824 final ActivityStack stack;
825 if (container == null || container.mStack.isOnHomeDisplay()) {
826 stack = getFocusedStack();
827 } else {
828 stack = container.mStack;
829 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700830 stack.mConfigWillChange = config != null
Craig Mautner23ac33b2013-04-01 16:26:35 -0700831 && mService.mConfiguration.diff(config) != 0;
832 if (DEBUG_CONFIGURATION) Slog.v(TAG,
Craig Mautnerde4ef022013-04-07 19:01:33 -0700833 "Starting activity when config will change = " + stack.mConfigWillChange);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700834
835 final long origId = Binder.clearCallingIdentity();
836
837 if (aInfo != null &&
838 (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
839 // This may be a heavy-weight process! Check to see if we already
840 // have another, different heavy-weight process running.
841 if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
842 if (mService.mHeavyWeightProcess != null &&
843 (mService.mHeavyWeightProcess.info.uid != aInfo.applicationInfo.uid ||
844 !mService.mHeavyWeightProcess.processName.equals(aInfo.processName))) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700845 int realCallingUid = callingUid;
846 if (caller != null) {
847 ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
848 if (callerApp != null) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700849 realCallingUid = callerApp.info.uid;
850 } else {
851 Slog.w(TAG, "Unable to find app for caller " + caller
Craig Mautner76ea2242013-05-15 11:40:05 -0700852 + " (pid=" + callingPid + ") when starting: "
Craig Mautner23ac33b2013-04-01 16:26:35 -0700853 + intent.toString());
854 ActivityOptions.abort(options);
855 return ActivityManager.START_PERMISSION_DENIED;
856 }
857 }
858
859 IIntentSender target = mService.getIntentSenderLocked(
860 ActivityManager.INTENT_SENDER_ACTIVITY, "android",
861 realCallingUid, userId, null, null, 0, new Intent[] { intent },
862 new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
863 | PendingIntent.FLAG_ONE_SHOT, null);
864
865 Intent newIntent = new Intent();
866 if (requestCode >= 0) {
867 // Caller is requesting a result.
868 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
869 }
870 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
871 new IntentSender(target));
872 if (mService.mHeavyWeightProcess.activities.size() > 0) {
873 ActivityRecord hist = mService.mHeavyWeightProcess.activities.get(0);
874 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
875 hist.packageName);
876 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
877 hist.task.taskId);
878 }
879 newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
880 aInfo.packageName);
881 newIntent.setFlags(intent.getFlags());
882 newIntent.setClassName("android",
883 HeavyWeightSwitcherActivity.class.getName());
884 intent = newIntent;
885 resolvedType = null;
886 caller = null;
887 callingUid = Binder.getCallingUid();
888 callingPid = Binder.getCallingPid();
889 componentSpecified = true;
890 try {
891 ResolveInfo rInfo =
892 AppGlobals.getPackageManager().resolveIntent(
893 intent, null,
894 PackageManager.MATCH_DEFAULT_ONLY
895 | ActivityManagerService.STOCK_PM_FLAGS, userId);
896 aInfo = rInfo != null ? rInfo.activityInfo : null;
897 aInfo = mService.getActivityInfoForUser(aInfo, userId);
898 } catch (RemoteException e) {
899 aInfo = null;
900 }
901 }
902 }
903 }
904
Dianne Hackborn91097de2014-04-04 18:02:06 -0700905 int res = startActivityLocked(caller, intent, resolvedType, aInfo,
906 voiceSession, voiceInteractor, resultTo, resultWho,
Craig Mautnere0a38842013-12-16 16:14:02 -0800907 requestCode, callingPid, callingUid, callingPackage, startFlags, options,
908 componentSpecified, null, container);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700909
Craig Mautner85c11a82014-07-17 12:38:18 -0700910 Binder.restoreCallingIdentity(origId);
911
Craig Mautnerde4ef022013-04-07 19:01:33 -0700912 if (stack.mConfigWillChange) {
Craig Mautner23ac33b2013-04-01 16:26:35 -0700913 // If the caller also wants to switch to a new configuration,
914 // do so now. This allows a clean switch, as we are waiting
915 // for the current activity to pause (so we will not destroy
916 // it), and have not yet started the next activity.
917 mService.enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
918 "updateConfiguration()");
Craig Mautnerde4ef022013-04-07 19:01:33 -0700919 stack.mConfigWillChange = false;
Craig Mautner23ac33b2013-04-01 16:26:35 -0700920 if (DEBUG_CONFIGURATION) Slog.v(TAG,
921 "Updating to new configuration after starting activity.");
922 mService.updateConfigurationLocked(config, null, false, false);
923 }
924
Craig Mautner23ac33b2013-04-01 16:26:35 -0700925 if (outResult != null) {
926 outResult.result = res;
927 if (res == ActivityManager.START_SUCCESS) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700928 mWaitingActivityLaunched.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700929 do {
930 try {
931 mService.wait();
932 } catch (InterruptedException e) {
933 }
934 } while (!outResult.timeout && outResult.who == null);
935 } else if (res == ActivityManager.START_TASK_TO_FRONT) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700936 ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700937 if (r.nowVisible) {
938 outResult.timeout = false;
939 outResult.who = new ComponentName(r.info.packageName, r.info.name);
940 outResult.totalTime = 0;
941 outResult.thisTime = 0;
942 } else {
943 outResult.thisTime = SystemClock.uptimeMillis();
Craig Mautnerce5f3cb2013-04-22 08:58:54 -0700944 mWaitingActivityVisible.add(outResult);
Craig Mautner23ac33b2013-04-01 16:26:35 -0700945 do {
946 try {
947 mService.wait();
948 } catch (InterruptedException e) {
949 }
950 } while (!outResult.timeout && outResult.who == null);
951 }
952 }
953 }
954
955 return res;
956 }
957 }
958
959 final int startActivities(IApplicationThread caller, int callingUid, String callingPackage,
960 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
961 Bundle options, int userId) {
962 if (intents == null) {
963 throw new NullPointerException("intents is null");
964 }
965 if (resolvedTypes == null) {
966 throw new NullPointerException("resolvedTypes is null");
967 }
968 if (intents.length != resolvedTypes.length) {
969 throw new IllegalArgumentException("intents are length different than resolvedTypes");
970 }
971
Craig Mautner23ac33b2013-04-01 16:26:35 -0700972
973 int callingPid;
974 if (callingUid >= 0) {
975 callingPid = -1;
976 } else if (caller == null) {
977 callingPid = Binder.getCallingPid();
978 callingUid = Binder.getCallingUid();
979 } else {
980 callingPid = callingUid = -1;
981 }
982 final long origId = Binder.clearCallingIdentity();
983 try {
984 synchronized (mService) {
Craig Mautner76ea2242013-05-15 11:40:05 -0700985 ActivityRecord[] outActivity = new ActivityRecord[1];
Craig Mautner23ac33b2013-04-01 16:26:35 -0700986 for (int i=0; i<intents.length; i++) {
987 Intent intent = intents[i];
988 if (intent == null) {
989 continue;
990 }
991
992 // Refuse possible leaked file descriptors
993 if (intent != null && intent.hasFileDescriptors()) {
994 throw new IllegalArgumentException("File descriptors passed in Intent");
995 }
996
997 boolean componentSpecified = intent.getComponent() != null;
998
999 // Don't modify the client's object!
1000 intent = new Intent(intent);
1001
1002 // Collect information about the target of the Intent.
1003 ActivityInfo aInfo = resolveActivity(intent, resolvedTypes[i],
1004 0, null, null, userId);
1005 // TODO: New, check if this is correct
1006 aInfo = mService.getActivityInfoForUser(aInfo, userId);
1007
1008 if (aInfo != null &&
1009 (aInfo.applicationInfo.flags & ApplicationInfo.FLAG_CANT_SAVE_STATE)
1010 != 0) {
1011 throw new IllegalArgumentException(
1012 "FLAG_CANT_SAVE_STATE not supported here");
1013 }
1014
1015 Bundle theseOptions;
1016 if (options != null && i == intents.length-1) {
1017 theseOptions = options;
1018 } else {
1019 theseOptions = null;
1020 }
Craig Mautner6170f732013-04-02 13:05:23 -07001021 int res = startActivityLocked(caller, intent, resolvedTypes[i],
Dianne Hackborn91097de2014-04-04 18:02:06 -07001022 aInfo, null, null, resultTo, null, -1, callingPid, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001023 0, theseOptions, componentSpecified, outActivity, null);
Craig Mautner23ac33b2013-04-01 16:26:35 -07001024 if (res < 0) {
1025 return res;
1026 }
1027
1028 resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
1029 }
1030 }
1031 } finally {
1032 Binder.restoreCallingIdentity(origId);
1033 }
1034
1035 return ActivityManager.START_SUCCESS;
1036 }
1037
Craig Mautner2420ead2013-04-01 17:13:20 -07001038 final boolean realStartActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001039 ProcessRecord app, boolean andResume, boolean checkConfig)
Craig Mautner2420ead2013-04-01 17:13:20 -07001040 throws RemoteException {
1041
1042 r.startFreezingScreenLocked(app, 0);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07001043 if (false) Slog.d(TAG, "realStartActivity: setting app visibility true");
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001044 mWindowManager.setAppVisibility(r.appToken, true);
Craig Mautner2420ead2013-04-01 17:13:20 -07001045
1046 // schedule launch ticks to collect information about slow apps.
1047 r.startLaunchTickingLocked();
1048
1049 // Have the window manager re-evaluate the orientation of
1050 // the screen based on the new activity order. Note that
1051 // as a result of this, it can call back into the activity
1052 // manager with a new orientation. We don't care about that,
1053 // because the activity is not currently running so we are
1054 // just restarting it anyway.
1055 if (checkConfig) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001056 Configuration config = mWindowManager.updateOrientationFromAppTokens(
Craig Mautner2420ead2013-04-01 17:13:20 -07001057 mService.mConfiguration,
1058 r.mayFreezeScreenLocked(app) ? r.appToken : null);
1059 mService.updateConfigurationLocked(config, r, false, false);
1060 }
1061
1062 r.app = app;
1063 app.waitingToKill = null;
1064 r.launchCount++;
1065 r.lastLaunchTime = SystemClock.uptimeMillis();
1066
1067 if (localLOGV) Slog.v(TAG, "Launching: " + r);
1068
1069 int idx = app.activities.indexOf(r);
1070 if (idx < 0) {
1071 app.activities.add(r);
1072 }
Dianne Hackborndb926082013-10-31 16:32:44 -07001073 mService.updateLruProcessLocked(app, true, null);
1074 mService.updateOomAdjLocked();
Craig Mautner2420ead2013-04-01 17:13:20 -07001075
1076 final ActivityStack stack = r.task.stack;
1077 try {
1078 if (app.thread == null) {
1079 throw new RemoteException();
1080 }
1081 List<ResultInfo> results = null;
1082 List<Intent> newIntents = null;
1083 if (andResume) {
1084 results = r.results;
1085 newIntents = r.newIntents;
1086 }
1087 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
1088 + " icicle=" + r.icicle
1089 + " with results=" + results + " newIntents=" + newIntents
1090 + " andResume=" + andResume);
1091 if (andResume) {
1092 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
1093 r.userId, System.identityHashCode(r),
1094 r.task.taskId, r.shortComponentName);
1095 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001096 if (r.isHomeActivity() && r.isNotResolverActivity()) {
Craig Mautner4ef26932013-09-18 15:15:52 -07001097 // Home process is the root process of the task.
1098 mService.mHomeProcess = r.task.mActivities.get(0).app;
Craig Mautner2420ead2013-04-01 17:13:20 -07001099 }
1100 mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
1101 r.sleeping = false;
1102 r.forceNewConfig = false;
1103 mService.showAskCompatModeDialogLocked(r);
1104 r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);
1105 String profileFile = null;
1106 ParcelFileDescriptor profileFd = null;
1107 boolean profileAutoStop = false;
1108 if (mService.mProfileApp != null && mService.mProfileApp.equals(app.processName)) {
1109 if (mService.mProfileProc == null || mService.mProfileProc == app) {
1110 mService.mProfileProc = app;
1111 profileFile = mService.mProfileFile;
1112 profileFd = mService.mProfileFd;
1113 profileAutoStop = mService.mAutoStopProfiler;
1114 }
1115 }
1116 app.hasShownUi = true;
1117 app.pendingUiClean = true;
1118 if (profileFd != null) {
1119 try {
1120 profileFd = profileFd.dup();
1121 } catch (IOException e) {
1122 if (profileFd != null) {
1123 try {
1124 profileFd.close();
1125 } catch (IOException o) {
1126 }
1127 profileFd = null;
1128 }
1129 }
1130 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08001131
Dianne Hackborna413dc02013-07-12 12:02:55 -07001132 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_TOP);
Craig Mautner2420ead2013-04-01 17:13:20 -07001133 app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
1134 System.identityHashCode(r), r.info,
Craig Mautnera0026042014-04-23 11:45:37 -07001135 new Configuration(mService.mConfiguration), r.compat, r.task.voiceInteractor,
1136 app.repProcState, r.icicle, r.persistentState, results, newIntents, !andResume,
Craig Mautner233ceee2014-05-09 17:05:11 -07001137 mService.isNextTransitionForward(), profileFile, profileFd, profileAutoStop
1138 );
Craig Mautner2420ead2013-04-01 17:13:20 -07001139
1140 if ((app.info.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
1141 // This may be a heavy-weight process! Note that the package
1142 // manager will ensure that only activity can run in the main
1143 // process of the .apk, which is the only thing that will be
1144 // considered heavy-weight.
1145 if (app.processName.equals(app.info.packageName)) {
1146 if (mService.mHeavyWeightProcess != null
1147 && mService.mHeavyWeightProcess != app) {
1148 Slog.w(TAG, "Starting new heavy weight process " + app
1149 + " when already running "
1150 + mService.mHeavyWeightProcess);
1151 }
1152 mService.mHeavyWeightProcess = app;
1153 Message msg = mService.mHandler.obtainMessage(
1154 ActivityManagerService.POST_HEAVY_NOTIFICATION_MSG);
1155 msg.obj = r;
1156 mService.mHandler.sendMessage(msg);
1157 }
1158 }
1159
1160 } catch (RemoteException e) {
1161 if (r.launchFailed) {
1162 // This is the second time we failed -- finish activity
1163 // and give up.
1164 Slog.e(TAG, "Second failure launching "
1165 + r.intent.getComponent().flattenToShortString()
1166 + ", giving up", e);
1167 mService.appDiedLocked(app, app.pid, app.thread);
1168 stack.requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
1169 "2nd-crash", false);
1170 return false;
1171 }
1172
1173 // This is the first time we failed -- restart process and
1174 // retry.
1175 app.activities.remove(r);
1176 throw e;
1177 }
1178
1179 r.launchFailed = false;
1180 if (stack.updateLRUListLocked(r)) {
1181 Slog.w(TAG, "Activity " + r
1182 + " being launched, but already in LRU list");
1183 }
1184
1185 if (andResume) {
1186 // As part of the process of launching, ActivityThread also performs
1187 // a resume.
1188 stack.minimalResumeActivityLocked(r);
1189 } else {
1190 // This activity is not starting in the resumed state... which
1191 // should look like we asked it to pause+stop (but remain visible),
1192 // and it has done so and reported back the current icicle and
1193 // other state.
1194 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPED: " + r
1195 + " (starting in stopped state)");
1196 r.state = ActivityState.STOPPED;
1197 r.stopped = true;
1198 }
1199
1200 // Launch the new version setup screen if needed. We do this -after-
1201 // launching the initial activity (that is, home), so that it can have
1202 // a chance to initialize itself while in the background, making the
1203 // switch back to it faster and look better.
Craig Mautnerde4ef022013-04-07 19:01:33 -07001204 if (isFrontStack(stack)) {
Craig Mautner2420ead2013-04-01 17:13:20 -07001205 mService.startSetupActivityLocked();
1206 }
1207
1208 return true;
1209 }
1210
Craig Mautnere79d42682013-04-01 19:01:53 -07001211 void startSpecificActivityLocked(ActivityRecord r,
George Mount2c92c972014-03-20 09:38:23 -07001212 boolean andResume, boolean checkConfig) {
Craig Mautnere79d42682013-04-01 19:01:53 -07001213 // Is this activity's application already running?
1214 ProcessRecord app = mService.getProcessRecordLocked(r.processName,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001215 r.info.applicationInfo.uid, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001216
1217 r.task.stack.setLaunchTime(r);
1218
1219 if (app != null && app.thread != null) {
1220 try {
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001221 if ((r.info.flags&ActivityInfo.FLAG_MULTIPROCESS) == 0
1222 || !"android".equals(r.info.packageName)) {
1223 // Don't add this if it is a platform component that is marked
1224 // to run in multiple processes, because this is actually
1225 // part of the framework so doesn't make sense to track as a
1226 // separate apk in the process.
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001227 app.addPackage(r.info.packageName, r.info.applicationInfo.versionCode,
1228 mService.mProcessStats);
Dianne Hackborn237cefb2013-10-22 18:45:27 -07001229 }
George Mount2c92c972014-03-20 09:38:23 -07001230 realStartActivityLocked(r, app, andResume, checkConfig);
Craig Mautnere79d42682013-04-01 19:01:53 -07001231 return;
1232 } catch (RemoteException e) {
1233 Slog.w(TAG, "Exception when starting activity "
1234 + r.intent.getComponent().flattenToShortString(), e);
1235 }
1236
1237 // If a dead object exception was thrown -- fall through to
1238 // restart the application.
1239 }
1240
1241 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001242 "activity", r.intent.getComponent(), false, false, true);
Craig Mautnere79d42682013-04-01 19:01:53 -07001243 }
1244
Craig Mautner6170f732013-04-02 13:05:23 -07001245 final int startActivityLocked(IApplicationThread caller,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001246 Intent intent, String resolvedType, ActivityInfo aInfo,
1247 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
1248 IBinder resultTo, String resultWho, int requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001249 int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
Craig Mautnere0a38842013-12-16 16:14:02 -08001250 boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container) {
Craig Mautner6170f732013-04-02 13:05:23 -07001251 int err = ActivityManager.START_SUCCESS;
1252
1253 ProcessRecord callerApp = null;
1254 if (caller != null) {
1255 callerApp = mService.getRecordForAppLocked(caller);
1256 if (callerApp != null) {
1257 callingPid = callerApp.pid;
1258 callingUid = callerApp.info.uid;
1259 } else {
1260 Slog.w(TAG, "Unable to find app for caller " + caller
1261 + " (pid=" + callingPid + ") when starting: "
1262 + intent.toString());
1263 err = ActivityManager.START_PERMISSION_DENIED;
1264 }
1265 }
1266
1267 if (err == ActivityManager.START_SUCCESS) {
1268 final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
1269 Slog.i(TAG, "START u" + userId + " {" + intent.toShortString(true, true, true, false)
Craig Mautner9ef471f2014-02-07 13:11:47 -08001270 + "} from pid " + (callerApp != null ? callerApp.pid : callingPid)
1271 + " on display " + (container == null ? (mFocusedStack == null ?
1272 Display.DEFAULT_DISPLAY : mFocusedStack.mDisplayId) :
1273 (container.mActivityDisplay == null ? Display.DEFAULT_DISPLAY :
1274 container.mActivityDisplay.mDisplayId)));
Craig Mautner6170f732013-04-02 13:05:23 -07001275 }
1276
1277 ActivityRecord sourceRecord = null;
1278 ActivityRecord resultRecord = null;
1279 if (resultTo != null) {
1280 sourceRecord = isInAnyStackLocked(resultTo);
1281 if (DEBUG_RESULTS) Slog.v(
1282 TAG, "Will send result to " + resultTo + " " + sourceRecord);
1283 if (sourceRecord != null) {
1284 if (requestCode >= 0 && !sourceRecord.finishing) {
1285 resultRecord = sourceRecord;
1286 }
1287 }
1288 }
1289 ActivityStack resultStack = resultRecord == null ? null : resultRecord.task.stack;
1290
Dianne Hackborn91097de2014-04-04 18:02:06 -07001291 final int launchFlags = intent.getFlags();
Craig Mautner6170f732013-04-02 13:05:23 -07001292
1293 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
1294 && sourceRecord != null) {
1295 // Transfer the result target from the source activity to the new
1296 // one being started, including any failures.
1297 if (requestCode >= 0) {
1298 ActivityOptions.abort(options);
1299 return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
1300 }
1301 resultRecord = sourceRecord.resultTo;
1302 resultWho = sourceRecord.resultWho;
1303 requestCode = sourceRecord.requestCode;
1304 sourceRecord.resultTo = null;
1305 if (resultRecord != null) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07001306 resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
Craig Mautner6170f732013-04-02 13:05:23 -07001307 }
Dianne Hackbornd4981012014-03-14 16:27:40 +00001308 if (sourceRecord.launchedFromUid == callingUid) {
1309 // The new activity is being launched from the same uid as the previous
1310 // activity in the flow, and asking to forward its result back to the
1311 // previous. In this case the activity is serving as a trampoline between
1312 // the two, so we also want to update its launchedFromPackage to be the
1313 // same as the previous activity. Note that this is safe, since we know
1314 // these two packages come from the same uid; the caller could just as
1315 // well have supplied that same package name itself. This specifially
1316 // deals with the case of an intent picker/chooser being launched in the app
1317 // flow to redirect to an activity picked by the user, where we want the final
1318 // activity to consider it to have been launched by the previous app activity.
1319 callingPackage = sourceRecord.launchedFromPackage;
1320 }
Craig Mautner6170f732013-04-02 13:05:23 -07001321 }
1322
1323 if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
1324 // We couldn't find a class that can handle the given Intent.
1325 // That's the end of that!
1326 err = ActivityManager.START_INTENT_NOT_RESOLVED;
1327 }
1328
1329 if (err == ActivityManager.START_SUCCESS && aInfo == null) {
1330 // We couldn't find the specific class specified in the Intent.
1331 // Also the end of the line.
1332 err = ActivityManager.START_CLASS_NOT_FOUND;
1333 }
1334
Dianne Hackborn91097de2014-04-04 18:02:06 -07001335 if (err == ActivityManager.START_SUCCESS && sourceRecord != null
1336 && sourceRecord.task.voiceSession != null) {
1337 // If this activity is being launched as part of a voice session, we need
1338 // to ensure that it is safe to do so. If the upcoming activity will also
1339 // be part of the voice session, we can only launch it if it has explicitly
1340 // said it supports the VOICE category, or it is a part of the calling app.
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001341 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
Dianne Hackborn91097de2014-04-04 18:02:06 -07001342 && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
1343 try {
1344 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1345 intent, resolvedType)) {
1346 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1347 }
1348 } catch (RemoteException e) {
1349 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1350 }
1351 }
1352 }
1353
1354 if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
1355 // If the caller is starting a new voice session, just make sure the target
1356 // is actually allowing it to run this way.
1357 try {
1358 if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
1359 intent, resolvedType)) {
1360 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1361 }
1362 } catch (RemoteException e) {
1363 err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
1364 }
1365 }
1366
Craig Mautner6170f732013-04-02 13:05:23 -07001367 if (err != ActivityManager.START_SUCCESS) {
1368 if (resultRecord != null) {
1369 resultStack.sendActivityResultLocked(-1,
1370 resultRecord, resultWho, requestCode,
1371 Activity.RESULT_CANCELED, null);
1372 }
1373 setDismissKeyguard(false);
1374 ActivityOptions.abort(options);
1375 return err;
1376 }
1377
1378 final int startAnyPerm = mService.checkPermission(
1379 START_ANY_ACTIVITY, callingPid, callingUid);
1380 final int componentPerm = mService.checkComponentPermission(aInfo.permission, callingPid,
1381 callingUid, aInfo.applicationInfo.uid, aInfo.exported);
1382 if (startAnyPerm != PERMISSION_GRANTED && componentPerm != PERMISSION_GRANTED) {
1383 if (resultRecord != null) {
1384 resultStack.sendActivityResultLocked(-1,
1385 resultRecord, resultWho, requestCode,
1386 Activity.RESULT_CANCELED, null);
1387 }
1388 setDismissKeyguard(false);
1389 String msg;
1390 if (!aInfo.exported) {
1391 msg = "Permission Denial: starting " + intent.toString()
1392 + " from " + callerApp + " (pid=" + callingPid
1393 + ", uid=" + callingUid + ")"
1394 + " not exported from uid " + aInfo.applicationInfo.uid;
1395 } else {
1396 msg = "Permission Denial: starting " + intent.toString()
1397 + " from " + callerApp + " (pid=" + callingPid
1398 + ", uid=" + callingUid + ")"
1399 + " requires " + aInfo.permission;
1400 }
1401 Slog.w(TAG, msg);
1402 throw new SecurityException(msg);
1403 }
1404
Ben Gruverdd72c9e2013-08-06 12:34:17 -07001405 boolean abort = !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
Ben Gruverb6223792013-07-29 16:35:40 -07001406 callingPid, resolvedType, aInfo.applicationInfo);
Ben Gruver5e207332013-04-03 17:41:37 -07001407
Craig Mautner6170f732013-04-02 13:05:23 -07001408 if (mService.mController != null) {
Craig Mautner6170f732013-04-02 13:05:23 -07001409 try {
1410 // The Intent we give to the watcher has the extra data
1411 // stripped off, since it can contain private information.
1412 Intent watchIntent = intent.cloneFilter();
Ben Gruver5e207332013-04-03 17:41:37 -07001413 abort |= !mService.mController.activityStarting(watchIntent,
Craig Mautner6170f732013-04-02 13:05:23 -07001414 aInfo.applicationInfo.packageName);
1415 } catch (RemoteException e) {
1416 mService.mController = null;
1417 }
Ben Gruver5e207332013-04-03 17:41:37 -07001418 }
Craig Mautner6170f732013-04-02 13:05:23 -07001419
Ben Gruver5e207332013-04-03 17:41:37 -07001420 if (abort) {
1421 if (resultRecord != null) {
1422 resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
Craig Mautner6170f732013-04-02 13:05:23 -07001423 Activity.RESULT_CANCELED, null);
Craig Mautner6170f732013-04-02 13:05:23 -07001424 }
Ben Gruver5e207332013-04-03 17:41:37 -07001425 // We pretend to the caller that it was really started, but
1426 // they will just get a cancel result.
1427 setDismissKeyguard(false);
1428 ActivityOptions.abort(options);
1429 return ActivityManager.START_SUCCESS;
Craig Mautner6170f732013-04-02 13:05:23 -07001430 }
1431
1432 ActivityRecord r = new ActivityRecord(mService, callerApp, callingUid, callingPackage,
Craig Mautnere0a38842013-12-16 16:14:02 -08001433 intent, resolvedType, aInfo, mService.mConfiguration, resultRecord, resultWho,
Craig Mautner233ceee2014-05-09 17:05:11 -07001434 requestCode, componentSpecified, this, container, options);
Craig Mautner6170f732013-04-02 13:05:23 -07001435 if (outActivity != null) {
1436 outActivity[0] = r;
1437 }
1438
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001439 final ActivityStack stack = getFocusedStack();
Dianne Hackborn91097de2014-04-04 18:02:06 -07001440 if (voiceSession == null && (stack.mResumedActivity == null
1441 || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
Craig Mautner6170f732013-04-02 13:05:23 -07001442 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
1443 PendingActivityLaunch pal =
Craig Mautnerde4ef022013-04-07 19:01:33 -07001444 new PendingActivityLaunch(r, sourceRecord, startFlags, stack);
Craig Mautneree36c772014-07-16 14:56:05 -07001445 mPendingActivityLaunches.add(pal);
Craig Mautner6170f732013-04-02 13:05:23 -07001446 setDismissKeyguard(false);
1447 ActivityOptions.abort(options);
1448 return ActivityManager.START_SWITCHES_CANCELED;
1449 }
1450 }
1451
1452 if (mService.mDidAppSwitch) {
1453 // This is the second allowed switch since we stopped switches,
1454 // so now just generally allow switches. Use case: user presses
1455 // home (switches disabled, switch to home, mDidAppSwitch now true);
1456 // user taps a home icon (coming from home so allowed, we hit here
1457 // and now allow anyone to switch again).
1458 mService.mAppSwitchesAllowedTime = 0;
1459 } else {
1460 mService.mDidAppSwitch = true;
1461 }
1462
Craig Mautneree36c772014-07-16 14:56:05 -07001463 doPendingActivityLaunchesLocked(false);
Craig Mautner6170f732013-04-02 13:05:23 -07001464
Dianne Hackborn91097de2014-04-04 18:02:06 -07001465 err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor,
1466 startFlags, true, options);
Craig Mautner10385a12013-09-22 21:08:32 -07001467
1468 if (allPausedActivitiesComplete()) {
1469 // If someone asked to have the keyguard dismissed on the next
Craig Mautner6170f732013-04-02 13:05:23 -07001470 // activity start, but we are not actually doing an activity
1471 // switch... just dismiss the keyguard now, because we
1472 // probably want to see whatever is behind it.
1473 dismissKeyguard();
1474 }
1475 return err;
1476 }
1477
Craig Mautner1b4bf852014-05-26 15:06:32 -07001478 ActivityStack adjustStackFocus(ActivityRecord r, boolean newTask) {
Craig Mautner1d001b62013-06-18 16:52:43 -07001479 final TaskRecord task = r.task;
Jose Lima58e66d62014-05-27 20:00:27 -07001480
1481 // On leanback only devices we should keep all activities in the same stack.
1482 if (!mLeanbackOnlyDevice &&
1483 (r.isApplicationActivity() || (task != null && task.isApplicationTask()))) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001484 if (task != null) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001485 final ActivityStack taskStack = task.stack;
Craig Mautnere0a38842013-12-16 16:14:02 -08001486 if (taskStack.isOnHomeDisplay()) {
1487 if (mFocusedStack != taskStack) {
1488 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: Setting " +
1489 "focused stack to r=" + r + " task=" + task);
1490 mFocusedStack = taskStack;
1491 } else {
1492 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1493 "adjustStackFocus: Focused stack already=" + mFocusedStack);
1494 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001495 }
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07001496 return taskStack;
Craig Mautnerac6f8432013-07-17 13:24:59 -07001497 }
1498
Craig Mautnere0a38842013-12-16 16:14:02 -08001499 final ActivityContainer container = r.mInitialActivityContainer;
1500 if (container != null) {
1501 // The first time put it on the desired stack, after this put on task stack.
1502 r.mInitialActivityContainer = null;
1503 return container.mStack;
1504 }
1505
Craig Mautner1b4bf852014-05-26 15:06:32 -07001506 if (mFocusedStack != mHomeStack && (!newTask ||
1507 mFocusedStack.mActivityContainer.isEligibleForNewTasks())) {
Craig Mautnerac6f8432013-07-17 13:24:59 -07001508 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1509 "adjustStackFocus: Have a focused stack=" + mFocusedStack);
1510 return mFocusedStack;
1511 }
1512
Craig Mautnere0a38842013-12-16 16:14:02 -08001513 final ArrayList<ActivityStack> homeDisplayStacks = mHomeStack.mStacks;
1514 for (int stackNdx = homeDisplayStacks.size() - 1; stackNdx >= 0; --stackNdx) {
1515 final ActivityStack stack = homeDisplayStacks.get(stackNdx);
1516 if (!stack.isHomeStack()) {
1517 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG,
1518 "adjustStackFocus: Setting focused stack=" + stack);
1519 mFocusedStack = stack;
1520 return mFocusedStack;
Craig Mautner858d8a62013-04-23 17:08:34 -07001521 }
1522 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07001523
Craig Mautner4a1cb222013-12-04 16:14:06 -08001524 // Need to create an app stack for this user.
Craig Mautnerf4c909b2014-04-17 18:39:38 -07001525 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
Craig Mautnerac6f8432013-07-17 13:24:59 -07001526 if (DEBUG_FOCUS || DEBUG_STACK) Slog.d(TAG, "adjustStackFocus: New stack r=" + r +
1527 " stackId=" + stackId);
1528 mFocusedStack = getStack(stackId);
Craig Mautner29219d92013-04-16 20:19:12 -07001529 return mFocusedStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001530 }
1531 return mHomeStack;
1532 }
1533
Craig Mautner29219d92013-04-16 20:19:12 -07001534 void setFocusedStack(ActivityRecord r) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08001535 if (r != null) {
Craig Mautner12ff7392014-02-21 21:08:00 -08001536 final TaskRecord task = r.task;
1537 boolean isHomeActivity = !r.isApplicationActivity();
1538 if (!isHomeActivity && task != null) {
1539 isHomeActivity = !task.isApplicationTask();
1540 }
1541 if (!isHomeActivity && task != null) {
1542 final ActivityRecord parent = task.stack.mActivityContainer.mParentActivity;
1543 isHomeActivity = parent != null && parent.isHomeActivity();
1544 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08001545 moveHomeStack(isHomeActivity);
Craig Mautner29219d92013-04-16 20:19:12 -07001546 }
1547 }
1548
Craig Mautner8849a5e2013-04-02 16:41:03 -07001549 final int startActivityUncheckedLocked(ActivityRecord r,
Dianne Hackborn91097de2014-04-04 18:02:06 -07001550 ActivityRecord sourceRecord,
1551 IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,
1552 boolean doResume, Bundle options) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001553 final Intent intent = r.intent;
1554 final int callingUid = r.launchedFromUid;
1555
Craig Mautnera228ae92014-07-09 05:44:55 -07001556 final boolean launchSingleInstance = r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE;
1557 final boolean launchSingleTask = r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001558
Craig Mautnera228ae92014-07-09 05:44:55 -07001559 int launchFlags = intent.getFlags();
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001560 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 &&
Craig Mautnera228ae92014-07-09 05:44:55 -07001561 (launchSingleInstance || launchSingleTask)) {
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001562 // We have a conflict between the Intent and the Activity manifest, manifest wins.
1563 Slog.i(TAG, "Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is " +
1564 "\"singleInstance\" or \"singleTask\"");
1565 launchFlags &=
1566 ~(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1567 } else {
1568 switch (r.info.documentLaunchMode) {
1569 case ActivityInfo.DOCUMENT_LAUNCH_NONE:
1570 break;
1571 case ActivityInfo.DOCUMENT_LAUNCH_INTO_EXISTING:
1572 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1573 break;
1574 case ActivityInfo.DOCUMENT_LAUNCH_ALWAYS:
1575 launchFlags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
1576 break;
1577 case ActivityInfo.DOCUMENT_LAUNCH_NEVER:
1578 launchFlags &= ~Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
1579 break;
1580 }
1581 }
Craig Mautnerbb742462014-07-07 15:28:55 -07001582
1583 final boolean launchTaskBehind = r.mLaunchTaskBehind &&
1584 (launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0;
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001585
1586 if (r.resultTo != null && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
1587 // For whatever reason this activity is being launched into a new
1588 // task... yet the caller has requested a result back. Well, that
1589 // is pretty messed up, so instead immediately send back a cancel
1590 // and let the new task continue launched as normal without a
1591 // dependency on its originator.
1592 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
1593 r.resultTo.task.stack.sendActivityResultLocked(-1,
1594 r.resultTo, r.resultWho, r.requestCode,
1595 Activity.RESULT_CANCELED, null);
1596 r.resultTo = null;
1597 }
1598
1599 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) != 0 && r.resultTo == null) {
1600 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1601 }
1602
Craig Mautner8849a5e2013-04-02 16:41:03 -07001603 // We'll invoke onUserLeaving before onPause only if the launching
1604 // activity did not explicitly state that this is an automated launch.
Craig Mautnera254cd72014-05-25 16:47:39 -07001605 mUserLeaving = (launchFlags & Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001606 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() => mUserLeaving=" + mUserLeaving);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001607
1608 // If the caller has asked not to resume at this point, we make note
1609 // of this in the record so that we can skip it when trying to find
1610 // the top running activity.
1611 if (!doResume) {
1612 r.delayedResume = true;
1613 }
1614
Craig Mautnera254cd72014-05-25 16:47:39 -07001615 ActivityRecord notTop =
1616 (launchFlags & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) != 0 ? r : null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001617
1618 // If the onlyIfNeeded flag is set, then we can do this if the activity
1619 // being launched is the same as the one making the call... or, as
1620 // a special case, if we do not know the caller then we count the
1621 // current top activity as the caller.
1622 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1623 ActivityRecord checkedCaller = sourceRecord;
1624 if (checkedCaller == null) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001625 checkedCaller = getFocusedStack().topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001626 }
1627 if (!checkedCaller.realActivity.equals(r.realActivity)) {
1628 // Caller is not the same as launcher, so always needed.
1629 startFlags &= ~ActivityManager.START_FLAG_ONLY_IF_NEEDED;
1630 }
1631 }
1632
1633 if (sourceRecord == null) {
1634 // This activity is not being started from another... in this
1635 // case we -always- start a new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001636 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Craig Mautner29219d92013-04-16 20:19:12 -07001637 Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
1638 "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001639 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1640 }
1641 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
1642 // The original activity who is starting us is running as a single
1643 // instance... this new activity it is starting must go on its
1644 // own task.
1645 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
Craig Mautnera228ae92014-07-09 05:44:55 -07001646 } else if (launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001647 // The activity being started is a single instance... it always
1648 // gets launched into its own task.
1649 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
1650 }
1651
Craig Mautner88629292013-11-10 20:39:05 -08001652 ActivityInfo newTaskInfo = null;
1653 Intent newTaskIntent = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001654 final ActivityStack sourceStack;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001655 if (sourceRecord != null) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001656 if (sourceRecord.finishing) {
1657 // If the source is finishing, we can't further count it as our source. This
1658 // is because the task it is associated with may now be empty and on its way out,
1659 // so we don't want to blindly throw it in to that task. Instead we will take
Craig Mautner88629292013-11-10 20:39:05 -08001660 // the NEW_TASK flow and try to find a task for it. But save the task information
1661 // so it can be used when creating the new task.
Craig Mautnerd00f4742014-03-12 14:17:26 -07001662 if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001663 Slog.w(TAG, "startActivity called from finishing " + sourceRecord
1664 + "; forcing " + "Intent.FLAG_ACTIVITY_NEW_TASK for: " + intent);
1665 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
Craig Mautner88629292013-11-10 20:39:05 -08001666 newTaskInfo = sourceRecord.info;
1667 newTaskIntent = sourceRecord.task.intent;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001668 }
1669 sourceRecord = null;
1670 sourceStack = null;
1671 } else {
1672 sourceStack = sourceRecord.task.stack;
1673 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001674 } else {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001675 sourceStack = null;
1676 }
1677
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001678 intent.setFlags(launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001679
1680 boolean addingToTask = false;
1681 boolean movedHome = false;
1682 TaskRecord reuseTask = null;
Craig Mautnerde4ef022013-04-07 19:01:33 -07001683 ActivityStack targetStack;
Craig Mautnerd00f4742014-03-12 14:17:26 -07001684 if (((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
1685 (launchFlags & Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
Craig Mautnera228ae92014-07-09 05:44:55 -07001686 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001687 // If bring to front is requested, and no result is requested, and
1688 // we can find a task that was started with this same
1689 // component, then instead of launching bring that one to the front.
1690 if (r.resultTo == null) {
1691 // See if there is a task to bring to the front. If this is
1692 // a SINGLE_INSTANCE activity, there can be one and only one
1693 // instance of it in the history, and it is always in its own
1694 // unique task, so we do a special search.
Craig Mautnera228ae92014-07-09 05:44:55 -07001695 ActivityRecord intentActivity = !launchSingleInstance ?
1696 findTaskLocked(r) : findActivityLocked(intent, r.info);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001697 if (intentActivity != null) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001698 if (isLockTaskModeViolation(intentActivity.task)) {
Jason Monka8f569c2014-07-07 12:15:21 -04001699 showLockTaskToast();
Craig Mautneraea74a52014-03-08 14:23:10 -08001700 Slog.e(TAG, "moveTaskToFront: Attempt to violate Lock Task Mode");
1701 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1702 }
Craig Mautner29219d92013-04-16 20:19:12 -07001703 if (r.task == null) {
1704 r.task = intentActivity.task;
1705 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001706 targetStack = intentActivity.task.stack;
Craig Mautner0f922742013-08-06 08:44:42 -07001707 targetStack.mLastPausedActivity = null;
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001708 if (DEBUG_TASKS) Slog.d(TAG, "Bring to front target: " + targetStack
1709 + " from " + intentActivity);
Craig Mautnere0a38842013-12-16 16:14:02 -08001710 targetStack.moveToFront();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001711 if (intentActivity.task.intent == null) {
1712 // This task was started because of movement of
1713 // the activity based on affinity... now that we
1714 // are actually launching it, we can assign the
1715 // base intent.
1716 intentActivity.task.setIntent(intent, r.info);
1717 }
1718 // If the target task is not in the front, then we need
1719 // to bring it to the front... except... well, with
1720 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
1721 // to have the same behavior as if a new instance was
1722 // being started, which means not bringing it to the front
1723 // if the caller is not itself in the front.
Craig Mautner165640b2013-04-20 10:34:33 -07001724 final ActivityStack lastStack = getLastStack();
1725 ActivityRecord curTop = lastStack == null?
1726 null : lastStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner7504d7b2013-09-17 10:50:53 -07001727 if (curTop != null && (curTop.task != intentActivity.task ||
1728 curTop.task != lastStack.topTask())) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001729 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Craig Mautnerd0f964f2013-08-07 11:16:33 -07001730 if (sourceRecord == null || (sourceStack.topActivity() != null &&
1731 sourceStack.topActivity().task == sourceRecord.task)) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001732 // We really do want to push this one into the
1733 // user's face, right now.
Craig Mautnerbb742462014-07-07 15:28:55 -07001734 if (launchTaskBehind && sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001735 intentActivity.setTaskToAffiliateWith(sourceRecord.task);
1736 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001737 movedHome = true;
Craig Mautnerb53d97c2013-10-25 11:54:37 -07001738 targetStack.moveTaskToFrontLocked(intentActivity.task, r, options);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001739 if ((launchFlags &
Craig Mautner29219d92013-04-16 20:19:12 -07001740 (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
1741 == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME)) {
Craig Mautnere12a4a62013-08-29 12:24:56 -07001742 // Caller wants to appear on home activity.
Craig Mautner84984fa2014-06-19 11:19:20 -07001743 intentActivity.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001744 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001745 options = null;
1746 }
1747 }
1748 // If the caller has requested that the target task be
1749 // reset, then do so.
1750 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
1751 intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
1752 }
1753 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1754 // We don't need to start a new activity, and
1755 // the client said not to do anything if that
1756 // is the case, so this is it! And for paranoia, make
1757 // sure we have correctly resumed the top activity.
1758 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001759 resumeTopActivitiesLocked(targetStack, null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001760 } else {
1761 ActivityOptions.abort(options);
1762 }
1763 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1764 }
1765 if ((launchFlags &
1766 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK))
1767 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK)) {
1768 // The caller has requested to completely replace any
1769 // existing task with its new activity. Well that should
1770 // not be too hard...
1771 reuseTask = intentActivity.task;
1772 reuseTask.performClearTaskLocked();
1773 reuseTask.setIntent(r.intent, r.info);
1774 } else if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001775 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001776 // In this situation we want to remove all activities
1777 // from the task up to the one being started. In most
1778 // cases this means we are resetting the task to its
1779 // initial state.
1780 ActivityRecord top =
1781 intentActivity.task.performClearTaskLocked(r, launchFlags);
1782 if (top != null) {
1783 if (top.frontOfTask) {
1784 // Activity aliases may mean we use different
1785 // intents for the top activity, so make sure
1786 // the task now has the identity of the new
1787 // intent.
1788 top.task.setIntent(r.intent, r.info);
1789 }
1790 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT,
1791 r, top.task);
1792 top.deliverNewIntentLocked(callingUid, r.intent);
1793 } else {
1794 // A special case: we need to
1795 // start the activity because it is not currently
1796 // running, and the caller has asked to clear the
1797 // current task to have this activity at the top.
1798 addingToTask = true;
1799 // Now pretend like this activity is being started
1800 // by the top of its task, so it is put in the
1801 // right place.
1802 sourceRecord = intentActivity;
1803 }
1804 } else if (r.realActivity.equals(intentActivity.task.realActivity)) {
1805 // In this case the top activity on the task is the
1806 // same as the one being launched, so we take that
1807 // as a request to bring the task to the foreground.
1808 // If the top activity in the task is the root
1809 // activity, deliver this new intent to it if it
1810 // desires.
1811 if (((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
1812 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP)
1813 && intentActivity.realActivity.equals(r.realActivity)) {
1814 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r,
1815 intentActivity.task);
1816 if (intentActivity.frontOfTask) {
1817 intentActivity.task.setIntent(r.intent, r.info);
1818 }
1819 intentActivity.deliverNewIntentLocked(callingUid, r.intent);
1820 } else if (!r.intent.filterEquals(intentActivity.task.intent)) {
1821 // In this case we are launching the root activity
1822 // of the task, but with a different intent. We
1823 // should start a new instance on top.
1824 addingToTask = true;
1825 sourceRecord = intentActivity;
1826 }
1827 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
1828 // In this case an activity is being launched in to an
1829 // existing task, without resetting that task. This
1830 // is typically the situation of launching an activity
1831 // from a notification or shortcut. We want to place
1832 // the new activity on top of the current task.
1833 addingToTask = true;
1834 sourceRecord = intentActivity;
1835 } else if (!intentActivity.task.rootWasReset) {
1836 // In this case we are launching in to an existing task
1837 // that has not yet been started from its front door.
1838 // The current task has been brought to the front.
1839 // Ideally, we'd probably like to place this new task
1840 // at the bottom of its stack, but that's a little hard
1841 // to do with the current organization of the code so
1842 // for now we'll just drop it.
1843 intentActivity.task.setIntent(r.intent, r.info);
1844 }
1845 if (!addingToTask && reuseTask == null) {
1846 // We didn't do anything... but it was needed (a.k.a., client
1847 // don't use that intent!) And for paranoia, make
1848 // sure we have correctly resumed the top activity.
1849 if (doResume) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001850 targetStack.resumeTopActivityLocked(null, options);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001851 } else {
1852 ActivityOptions.abort(options);
1853 }
1854 return ActivityManager.START_TASK_TO_FRONT;
1855 }
1856 }
1857 }
1858 }
1859
1860 //String uri = r.intent.toURI();
1861 //Intent intent2 = new Intent(uri);
1862 //Slog.i(TAG, "Given intent: " + r.intent);
1863 //Slog.i(TAG, "URI is: " + uri);
1864 //Slog.i(TAG, "To intent: " + intent2);
1865
1866 if (r.packageName != null) {
1867 // If the activity being launched is the same as the one currently
1868 // at the top, then we need to check if it should only be launched
1869 // once.
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07001870 ActivityStack topStack = getFocusedStack();
Craig Mautnerde4ef022013-04-07 19:01:33 -07001871 ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(notTop);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001872 if (top != null && r.resultTo == null) {
1873 if (top.realActivity.equals(r.realActivity) && top.userId == r.userId) {
1874 if (top.app != null && top.app.thread != null) {
Craig Mautnerd00f4742014-03-12 14:17:26 -07001875 if ((launchFlags & Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
Craig Mautnera228ae92014-07-09 05:44:55 -07001876 || launchSingleInstance || launchSingleTask) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001877 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, top,
1878 top.task);
1879 // For paranoia, make sure we have correctly
1880 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001881 topStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001882 if (doResume) {
Craig Mautner05d29032013-05-03 13:40:13 -07001883 resumeTopActivitiesLocked();
Craig Mautner8849a5e2013-04-02 16:41:03 -07001884 }
1885 ActivityOptions.abort(options);
1886 if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
1887 // We don't need to start a new activity, and
1888 // the client said not to do anything if that
1889 // is the case, so this is it!
1890 return ActivityManager.START_RETURN_INTENT_TO_CALLER;
1891 }
1892 top.deliverNewIntentLocked(callingUid, r.intent);
1893 return ActivityManager.START_DELIVERED_TO_TOP;
1894 }
1895 }
1896 }
1897 }
1898
1899 } else {
1900 if (r.resultTo != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001901 r.resultTo.task.stack.sendActivityResultLocked(-1, r.resultTo, r.resultWho,
1902 r.requestCode, Activity.RESULT_CANCELED, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001903 }
1904 ActivityOptions.abort(options);
1905 return ActivityManager.START_CLASS_NOT_FOUND;
1906 }
1907
1908 boolean newTask = false;
1909 boolean keepCurTransition = false;
1910
Craig Mautnerbb742462014-07-07 15:28:55 -07001911 TaskRecord taskToAffiliate = launchTaskBehind && sourceRecord != null ?
Craig Mautnera228ae92014-07-09 05:44:55 -07001912 sourceRecord.task : null;
1913
Craig Mautner8849a5e2013-04-02 16:41:03 -07001914 // Should this be considered a new task?
1915 if (r.resultTo == null && !addingToTask
Craig Mautnerf357c0c2014-06-09 09:23:27 -07001916 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Craig Mautneraea74a52014-03-08 14:23:10 -08001917 if (isLockTaskModeViolation(reuseTask)) {
1918 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1919 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1920 }
Craig Mautner1b4bf852014-05-26 15:06:32 -07001921 newTask = true;
1922 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerbb742462014-07-07 15:28:55 -07001923 if (!launchTaskBehind) {
1924 targetStack.moveToFront();
1925 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001926 if (reuseTask == null) {
Craig Mautner88629292013-11-10 20:39:05 -08001927 r.setTask(targetStack.createTaskRecord(getNextTaskId(),
1928 newTaskInfo != null ? newTaskInfo : r.info,
1929 newTaskIntent != null ? newTaskIntent : intent,
Craig Mautnerbb742462014-07-07 15:28:55 -07001930 voiceSession, voiceInteractor, !launchTaskBehind /* toTop */),
1931 taskToAffiliate);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001932 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r + " in new task " +
1933 r.task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001934 } else {
Craig Mautnera228ae92014-07-09 05:44:55 -07001935 r.setTask(reuseTask, taskToAffiliate);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001936 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001937 if (!movedHome) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001938 if ((launchFlags &
1939 (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME))
1940 == (Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_TASK_ON_HOME)) {
1941 // Caller wants to appear on home activity, so before starting
1942 // their own activity we will bring home to the front.
Craig Mautner84984fa2014-06-19 11:19:20 -07001943 r.task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001944 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001945 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07001946 } else if (sourceRecord != null) {
Craig Mautnera228ae92014-07-09 05:44:55 -07001947 final TaskRecord sourceTask = sourceRecord.task;
Craig Mautneraea74a52014-03-08 14:23:10 -08001948 if (isLockTaskModeViolation(sourceTask)) {
1949 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
1950 return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
1951 }
Craig Mautner525f3d92013-05-07 14:01:50 -07001952 targetStack = sourceTask.stack;
Craig Mautnerdd221b32014-03-19 19:53:45 -07001953 targetStack.moveToFront();
Craig Mautnerabcc59c2014-05-17 15:13:28 -07001954 mWindowManager.moveTaskToTop(targetStack.topTask().taskId);
Craig Mautnera228ae92014-07-09 05:44:55 -07001955 if (!addingToTask && (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Craig Mautner8849a5e2013-04-02 16:41:03 -07001956 // In this case, we are adding the activity to an existing
1957 // task, but the caller has asked to clear that task if the
1958 // activity is already running.
Craig Mautner525f3d92013-05-07 14:01:50 -07001959 ActivityRecord top = sourceTask.performClearTaskLocked(r, launchFlags);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001960 keepCurTransition = true;
1961 if (top != null) {
1962 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
1963 top.deliverNewIntentLocked(callingUid, r.intent);
1964 // For paranoia, make sure we have correctly
1965 // resumed the top activity.
Craig Mautner0f922742013-08-06 08:44:42 -07001966 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001967 if (doResume) {
1968 targetStack.resumeTopActivityLocked(null);
1969 }
1970 ActivityOptions.abort(options);
1971 return ActivityManager.START_DELIVERED_TO_TOP;
1972 }
1973 } else if (!addingToTask &&
1974 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
1975 // In this case, we are launching an activity in our own task
1976 // that may already be running somewhere in the history, and
1977 // we want to shuffle it to the front of the stack if so.
Craig Mautner525f3d92013-05-07 14:01:50 -07001978 final ActivityRecord top = sourceTask.findActivityInHistoryLocked(r);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001979 if (top != null) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001980 final TaskRecord task = top.task;
1981 task.moveActivityToFrontLocked(top);
1982 ActivityStack.logStartActivity(EventLogTags.AM_NEW_INTENT, r, task);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001983 top.updateOptionsLocked(options);
1984 top.deliverNewIntentLocked(callingUid, r.intent);
Craig Mautner0f922742013-08-06 08:44:42 -07001985 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07001986 if (doResume) {
1987 targetStack.resumeTopActivityLocked(null);
1988 }
1989 return ActivityManager.START_DELIVERED_TO_TOP;
1990 }
1991 }
1992 // An existing activity is starting this new activity, so we want
1993 // to keep the new one in the same task as the one that is starting
1994 // it.
Craig Mautnera228ae92014-07-09 05:44:55 -07001995 r.setTask(sourceTask, null);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001996 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
Dianne Hackborn2a272d42013-10-16 13:34:33 -07001997 + " in existing task " + r.task + " from source " + sourceRecord);
Craig Mautner8849a5e2013-04-02 16:41:03 -07001998
1999 } else {
2000 // This not being started from an existing activity, and not part
2001 // of a new task... just put it in the top task, though these days
2002 // this case should never happen.
Craig Mautner1b4bf852014-05-26 15:06:32 -07002003 targetStack = adjustStackFocus(r, newTask);
Craig Mautnerdd221b32014-03-19 19:53:45 -07002004 targetStack.moveToFront();
Craig Mautner1602ec22013-05-12 10:24:27 -07002005 ActivityRecord prev = targetStack.topActivity();
Craig Mautnerc0ffce52014-07-01 12:38:52 -07002006 r.setTask(prev != null ? prev.task : targetStack.createTaskRecord(getNextTaskId(),
Craig Mautnera228ae92014-07-09 05:44:55 -07002007 r.info, intent, null, null, true), null);
Craig Mautner95e9daa2014-03-17 15:57:20 -07002008 mWindowManager.moveTaskToTop(r.task.taskId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002009 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
2010 + " in new guessed " + r.task);
2011 }
2012
2013 mService.grantUriPermissionFromIntentLocked(callingUid, r.packageName,
Nicolas Prevotc6cf95c2014-05-29 11:30:36 +01002014 intent, r.getUriPermissionsLocked(), r.userId);
Craig Mautner8849a5e2013-04-02 16:41:03 -07002015
Craig Mautner76e2a762014-06-24 09:05:43 -07002016 if (sourceRecord != null && sourceRecord.isRecentsActivity()) {
2017 r.task.setTaskToReturnTo(RECENTS_ACTIVITY_TYPE);
2018 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002019 if (newTask) {
2020 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.userId, r.task.taskId);
2021 }
2022 ActivityStack.logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Craig Mautner0f922742013-08-06 08:44:42 -07002023 targetStack.mLastPausedActivity = null;
Craig Mautner8849a5e2013-04-02 16:41:03 -07002024 targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
Craig Mautnerbb742462014-07-07 15:28:55 -07002025 if (!launchTaskBehind) {
2026 // Don't set focus on an activity that's going to the back.
2027 mService.setFocusedActivityLocked(r);
2028 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002029 return ActivityManager.START_SUCCESS;
2030 }
2031
Craig Mautneree36c772014-07-16 14:56:05 -07002032 final void doPendingActivityLaunchesLocked(boolean doResume) {
2033 while (!mPendingActivityLaunches.isEmpty()) {
2034 PendingActivityLaunch pal = mPendingActivityLaunches.remove(0);
Craig Mautneraa9b0f12014-07-17 10:50:18 -07002035 startActivityUncheckedLocked(pal.r, pal.sourceRecord, null, null, pal.startFlags,
Craig Mautneree36c772014-07-16 14:56:05 -07002036 doResume && mPendingActivityLaunches.isEmpty(), null);
2037 }
2038 }
2039
2040 void removePendingActivityLaunchesLocked(ActivityRecord r) {
2041 for (int palNdx = mPendingActivityLaunches.size() - 1; palNdx >= 0; --palNdx) {
2042 PendingActivityLaunch pal = mPendingActivityLaunches.get(palNdx);
2043 if (pal.r == r) {
2044 mPendingActivityLaunches.remove(palNdx);
2045 }
2046 }
2047 }
2048
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002049 void acquireLaunchWakelock() {
2050 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2051 throw new IllegalStateException("Calling must be system uid");
2052 }
2053 mLaunchingActivity.acquire();
2054 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2055 // To be safe, don't allow the wake lock to be held for too long.
2056 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
2057 }
2058 }
2059
Craig Mautnerf3333272013-04-22 10:55:53 -07002060 // Checked.
2061 final ActivityRecord activityIdleInternalLocked(final IBinder token, boolean fromTimeout,
2062 Configuration config) {
2063 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
2064
Craig Mautnerf3333272013-04-22 10:55:53 -07002065 ArrayList<ActivityRecord> stops = null;
2066 ArrayList<ActivityRecord> finishes = null;
2067 ArrayList<UserStartedState> startingUsers = null;
2068 int NS = 0;
2069 int NF = 0;
Craig Mautnerf3333272013-04-22 10:55:53 -07002070 boolean booting = false;
2071 boolean enableScreen = false;
2072 boolean activityRemoved = false;
2073
2074 ActivityRecord r = ActivityRecord.forToken(token);
2075 if (r != null) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07002076 if (DEBUG_IDLE) Slog.d(TAG, "activityIdleInternalLocked: Callers=" +
2077 Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07002078 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
2079 r.finishLaunchTickingLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002080 if (fromTimeout) {
2081 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
Craig Mautnerf3333272013-04-22 10:55:53 -07002082 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002083
2084 // This is a hack to semi-deal with a race condition
2085 // in the client where it can be constructed with a
2086 // newer configuration from when we asked it to launch.
2087 // We'll update with whatever configuration it now says
2088 // it used to launch.
2089 if (config != null) {
2090 r.configuration = config;
2091 }
2092
2093 // We are now idle. If someone is waiting for a thumbnail from
2094 // us, we can now deliver.
2095 r.idle = true;
2096
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002097 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
2098 if (!mService.mBooted && isFrontStack(r.task.stack)) {
2099 mService.mBooted = true;
2100 enableScreen = true;
2101 }
2102 }
2103
2104 if (allResumedActivitiesIdle()) {
2105 if (r != null) {
2106 mService.scheduleAppGcsLocked();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002107 }
2108
2109 if (mLaunchingActivity.isHeld()) {
2110 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
2111 if (VALIDATE_WAKE_LOCK_CALLER &&
2112 Binder.getCallingUid() != Process.myUid()) {
2113 throw new IllegalStateException("Calling must be system uid");
2114 }
2115 mLaunchingActivity.release();
2116 }
2117 ensureActivitiesVisibleLocked(null, 0);
Craig Mautnerf3333272013-04-22 10:55:53 -07002118 }
2119
2120 // Atomically retrieve all of the other things to do.
2121 stops = processStoppingActivitiesLocked(true);
2122 NS = stops != null ? stops.size() : 0;
2123 if ((NF=mFinishingActivities.size()) > 0) {
2124 finishes = new ArrayList<ActivityRecord>(mFinishingActivities);
2125 mFinishingActivities.clear();
2126 }
2127
Craig Mautnerf3333272013-04-22 10:55:53 -07002128 if (isFrontStack(mHomeStack)) {
2129 booting = mService.mBooting;
2130 mService.mBooting = false;
2131 }
2132
2133 if (mStartingUsers.size() > 0) {
2134 startingUsers = new ArrayList<UserStartedState>(mStartingUsers);
2135 mStartingUsers.clear();
2136 }
2137
Craig Mautnerf3333272013-04-22 10:55:53 -07002138 // Stop any activities that are scheduled to do so but have been
2139 // waiting for the next one to start.
2140 for (int i = 0; i < NS; i++) {
2141 r = stops.get(i);
2142 final ActivityStack stack = r.task.stack;
2143 if (r.finishing) {
2144 stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false);
2145 } else {
2146 stack.stopActivityLocked(r);
2147 }
2148 }
2149
2150 // Finish any activities that are scheduled to do so but have been
2151 // waiting for the next one to start.
2152 for (int i = 0; i < NF; i++) {
2153 r = finishes.get(i);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002154 activityRemoved |= r.task.stack.destroyActivityLocked(r, true, "finish-idle");
Craig Mautnerf3333272013-04-22 10:55:53 -07002155 }
2156
2157 if (booting) {
2158 mService.finishBooting();
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002159 } else {
2160 // Complete user switch
2161 if (startingUsers != null) {
2162 for (int i = 0; i < startingUsers.size(); i++) {
2163 mService.finishUserSwitch(startingUsers.get(i));
2164 }
2165 }
2166 // Complete starting up of background users
2167 if (mStartingBackgroundUsers.size() > 0) {
2168 startingUsers = new ArrayList<UserStartedState>(mStartingBackgroundUsers);
2169 mStartingBackgroundUsers.clear();
2170 for (int i = 0; i < startingUsers.size(); i++) {
2171 mService.finishUserBoot(startingUsers.get(i));
2172 }
Craig Mautnerf3333272013-04-22 10:55:53 -07002173 }
2174 }
2175
2176 mService.trimApplications();
2177 //dump();
2178 //mWindowManager.dump();
2179
2180 if (enableScreen) {
2181 mService.enableScreenAfterBoot();
2182 }
2183
2184 if (activityRemoved) {
Craig Mautner05d29032013-05-03 13:40:13 -07002185 resumeTopActivitiesLocked();
Craig Mautnerf3333272013-04-22 10:55:53 -07002186 }
2187
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002188 return r;
Craig Mautnerf3333272013-04-22 10:55:53 -07002189 }
2190
Craig Mautner8e569572013-10-11 17:36:59 -07002191 boolean handleAppDiedLocked(ProcessRecord app) {
Craig Mautner19091252013-10-05 00:03:53 -07002192 boolean hasVisibleActivities = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002193 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2194 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002195 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2196 hasVisibleActivities |= stacks.get(stackNdx).handleAppDiedLocked(app);
2197 }
Craig Mautner6b74cb52013-09-27 17:02:21 -07002198 }
Craig Mautner19091252013-10-05 00:03:53 -07002199 return hasVisibleActivities;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002200 }
2201
2202 void closeSystemDialogsLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08002203 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2204 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002205 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2206 stacks.get(stackNdx).closeSystemDialogsLocked();
2207 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002208 }
2209 }
2210
Craig Mautner93529a42013-10-04 15:03:13 -07002211 void removeUserLocked(int userId) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002212 mUserStackInFront.delete(userId);
Craig Mautner93529a42013-10-04 15:03:13 -07002213 }
2214
Craig Mautner8d341ef2013-03-26 09:03:27 -07002215 /**
2216 * @return true if some activity was finished (or would have finished if doit were true).
2217 */
2218 boolean forceStopPackageLocked(String name, boolean doit, boolean evenPersistent, int userId) {
2219 boolean didSomething = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002220 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2221 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002222 final int numStacks = stacks.size();
2223 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2224 final ActivityStack stack = stacks.get(stackNdx);
2225 if (stack.forceStopPackageLocked(name, doit, evenPersistent, userId)) {
2226 didSomething = true;
2227 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002228 }
2229 }
2230 return didSomething;
2231 }
2232
Dianne Hackborna413dc02013-07-12 12:02:55 -07002233 void updatePreviousProcessLocked(ActivityRecord r) {
2234 // Now that this process has stopped, we may want to consider
2235 // it to be the previous app to try to keep around in case
2236 // the user wants to return to it.
2237
2238 // First, found out what is currently the foreground app, so that
2239 // we don't blow away the previous app if this activity is being
2240 // hosted by the process that is actually still the foreground.
2241 ProcessRecord fgApp = null;
Craig Mautnere0a38842013-12-16 16:14:02 -08002242 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2243 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002244 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2245 final ActivityStack stack = stacks.get(stackNdx);
2246 if (isFrontStack(stack)) {
2247 if (stack.mResumedActivity != null) {
2248 fgApp = stack.mResumedActivity.app;
2249 } else if (stack.mPausingActivity != null) {
2250 fgApp = stack.mPausingActivity.app;
2251 }
2252 break;
Dianne Hackborna413dc02013-07-12 12:02:55 -07002253 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07002254 }
2255 }
2256
2257 // Now set this one as the previous process, only if that really
2258 // makes sense to.
2259 if (r.app != null && fgApp != null && r.app != fgApp
2260 && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
Craig Mautner4ef26932013-09-18 15:15:52 -07002261 && r.app != mService.mHomeProcess) {
Dianne Hackborna413dc02013-07-12 12:02:55 -07002262 mService.mPreviousProcess = r.app;
2263 mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
2264 }
2265 }
2266
Craig Mautner05d29032013-05-03 13:40:13 -07002267 boolean resumeTopActivitiesLocked() {
2268 return resumeTopActivitiesLocked(null, null, null);
2269 }
2270
2271 boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
2272 Bundle targetOptions) {
2273 if (targetStack == null) {
2274 targetStack = getFocusedStack();
2275 }
Craig Mautner12ff7392014-02-21 21:08:00 -08002276 // Do targetStack first.
Craig Mautner05d29032013-05-03 13:40:13 -07002277 boolean result = false;
Craig Mautner12ff7392014-02-21 21:08:00 -08002278 if (isFrontStack(targetStack)) {
2279 result = targetStack.resumeTopActivityLocked(target, targetOptions);
2280 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002281 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2282 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002283 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2284 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautner12ff7392014-02-21 21:08:00 -08002285 if (stack == targetStack) {
2286 // Already started above.
2287 continue;
2288 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002289 if (isFrontStack(stack)) {
Craig Mautner12ff7392014-02-21 21:08:00 -08002290 stack.resumeTopActivityLocked(null);
Craig Mautner05d29032013-05-03 13:40:13 -07002291 }
Craig Mautnerf88c50f2013-04-18 19:25:12 -07002292 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002293 }
Craig Mautner05d29032013-05-03 13:40:13 -07002294 return result;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002295 }
2296
2297 void finishTopRunningActivityLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002298 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2299 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002300 final int numStacks = stacks.size();
2301 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2302 final ActivityStack stack = stacks.get(stackNdx);
2303 stack.finishTopRunningActivityLocked(app);
2304 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002305 }
2306 }
2307
Dianne Hackborn6ea0d0a2014-07-02 16:23:21 -07002308 void finishVoiceTask(IVoiceInteractionSession session) {
2309 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2310 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
2311 final int numStacks = stacks.size();
2312 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2313 final ActivityStack stack = stacks.get(stackNdx);
2314 stack.finishVoiceTask(session);
2315 }
2316 }
2317 }
2318
Craig Mautneraea74a52014-03-08 14:23:10 -08002319 void findTaskToMoveToFrontLocked(TaskRecord task, int flags, Bundle options) {
2320 if ((flags & ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
2321 mUserLeaving = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002322 }
Craig Mautneraea74a52014-03-08 14:23:10 -08002323 if ((flags & ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
2324 // Caller wants the home activity moved with it. To accomplish this,
2325 // we'll just indicate that this task returns to the home task.
Craig Mautner84984fa2014-06-19 11:19:20 -07002326 task.setTaskToReturnTo(HOME_ACTIVITY_TYPE);
Craig Mautneraea74a52014-03-08 14:23:10 -08002327 }
2328 task.stack.moveTaskToFrontLocked(task, null, options);
2329 if (DEBUG_STACK) Slog.d(TAG, "findTaskToMoveToFront: moved to front of stack="
2330 + task.stack);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002331 }
2332
Craig Mautner967212c2013-04-13 21:10:58 -07002333 ActivityStack getStack(int stackId) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002334 ActivityContainer activityContainer = mActivityContainers.get(stackId);
2335 if (activityContainer != null) {
2336 return activityContainer.mStack;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002337 }
2338 return null;
2339 }
2340
Craig Mautner967212c2013-04-13 21:10:58 -07002341 ArrayList<ActivityStack> getStacks() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002342 ArrayList<ActivityStack> allStacks = new ArrayList<ActivityStack>();
Craig Mautnere0a38842013-12-16 16:14:02 -08002343 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2344 allStacks.addAll(mActivityDisplays.valueAt(displayNdx).mStacks);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002345 }
2346 return allStacks;
Craig Mautner967212c2013-04-13 21:10:58 -07002347 }
2348
Craig Mautner4a1cb222013-12-04 16:14:06 -08002349 IBinder getHomeActivityToken() {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002350 ActivityRecord homeActivity = getHomeActivity();
2351 if (homeActivity != null) {
2352 return homeActivity.appToken;
2353 }
2354 return null;
2355 }
2356
2357 ActivityRecord getHomeActivity() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002358 final ArrayList<TaskRecord> tasks = mHomeStack.getAllTasks();
2359 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2360 final TaskRecord task = tasks.get(taskNdx);
2361 if (task.isHomeTask()) {
2362 final ArrayList<ActivityRecord> activities = task.mActivities;
2363 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2364 final ActivityRecord r = activities.get(activityNdx);
2365 if (r.isHomeActivity()) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002366 return r;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002367 }
2368 }
2369 }
2370 }
2371 return null;
2372 }
2373
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002374 ActivityContainer createActivityContainer(ActivityRecord parentActivity,
Craig Mautner4a1cb222013-12-04 16:14:06 -08002375 IActivityContainerCallback callback) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002376 ActivityContainer activityContainer =
2377 new VirtualActivityContainer(parentActivity, callback);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002378 mActivityContainers.put(activityContainer.mStackId, activityContainer);
Craig Mautnerd163e752014-06-13 17:18:47 -07002379 if (DEBUG_CONTAINERS) Slog.d(TAG, "createActivityContainer: " + activityContainer);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002380 parentActivity.mChildContainers.add(activityContainer);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002381 return activityContainer;
2382 }
2383
Craig Mautner34b73df2014-01-12 21:11:08 -08002384 void removeChildActivityContainers(ActivityRecord parentActivity) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002385 final ArrayList<ActivityContainer> childStacks = parentActivity.mChildContainers;
2386 for (int containerNdx = childStacks.size() - 1; containerNdx >= 0; --containerNdx) {
2387 ActivityContainer container = childStacks.remove(containerNdx);
Craig Mautnerd163e752014-06-13 17:18:47 -07002388 if (DEBUG_CONTAINERS) Slog.d(TAG, "removeChildActivityContainers: removing " +
2389 container);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002390 container.release();
Craig Mautner34b73df2014-01-12 21:11:08 -08002391 }
2392 }
2393
Craig Mautner95da1082014-02-24 17:54:35 -08002394 void deleteActivityContainer(IActivityContainer container) {
2395 ActivityContainer activityContainer = (ActivityContainer)container;
2396 if (activityContainer != null) {
Craig Mautnerd163e752014-06-13 17:18:47 -07002397 if (DEBUG_CONTAINERS) Slog.d(TAG, "deleteActivityContainer: ",
2398 new RuntimeException("here").fillInStackTrace());
Craig Mautner95da1082014-02-24 17:54:35 -08002399 final int stackId = activityContainer.mStackId;
2400 mActivityContainers.remove(stackId);
2401 mWindowManager.removeStack(stackId);
2402 }
2403 }
2404
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002405 private int createStackOnDisplay(int stackId, int displayId) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002406 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
2407 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002408 return -1;
2409 }
2410
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002411 ActivityContainer activityContainer = new ActivityContainer(stackId);
2412 mActivityContainers.put(stackId, activityContainer);
Craig Mautnere0a38842013-12-16 16:14:02 -08002413 activityContainer.attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002414 return stackId;
2415 }
2416
2417 int getNextStackId() {
Craig Mautner858d8a62013-04-23 17:08:34 -07002418 while (true) {
2419 if (++mLastStackId <= HOME_STACK_ID) {
2420 mLastStackId = HOME_STACK_ID + 1;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002421 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002422 if (getStack(mLastStackId) == null) {
2423 break;
2424 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002425 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002426 return mLastStackId;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002427 }
2428
Craig Mautneref73ee12014-04-23 11:45:37 -07002429 void createStackForRestoredTaskHistory(ArrayList<TaskRecord> tasks) {
2430 int stackId = createStackOnDisplay(getNextStackId(), Display.DEFAULT_DISPLAY);
2431 final ActivityStack stack = getStack(stackId);
2432 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
2433 final TaskRecord task = tasks.get(taskNdx);
2434 stack.addTask(task, false, false);
2435 final int taskId = task.taskId;
2436 final ArrayList<ActivityRecord> activities = task.mActivities;
2437 for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
2438 final ActivityRecord r = activities.get(activityNdx);
2439 mWindowManager.addAppToken(0, r.appToken, taskId, stackId,
2440 r.info.screenOrientation, r.fullscreen,
2441 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0,
Craig Mautnerbb742462014-07-07 15:28:55 -07002442 r.userId, r.info.configChanges, task.voiceSession != null,
2443 r.mLaunchTaskBehind);
Craig Mautneref73ee12014-04-23 11:45:37 -07002444 }
2445 mWindowManager.addTask(taskId, stackId, false);
2446 }
Craig Mautner84984fa2014-06-19 11:19:20 -07002447 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
Craig Mautneref73ee12014-04-23 11:45:37 -07002448 }
2449
Craig Mautner8d341ef2013-03-26 09:03:27 -07002450 void moveTaskToStack(int taskId, int stackId, boolean toTop) {
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002451 final TaskRecord task = anyTaskForIdLocked(taskId);
2452 if (task == null) {
2453 return;
2454 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002455 final ActivityStack stack = getStack(stackId);
2456 if (stack == null) {
2457 Slog.w(TAG, "moveTaskToStack: no stack for id=" + stackId);
2458 return;
2459 }
Craig Mautner41db4a72014-05-07 17:20:56 -07002460 task.stack.removeTask(task);
Dianne Hackbornc03c9162014-05-02 10:45:59 -07002461 stack.addTask(task, toTop, true);
Craig Mautnerb3b36ba2013-05-20 13:21:10 -07002462 mWindowManager.addTask(taskId, stackId, toTop);
Craig Mautner05d29032013-05-03 13:40:13 -07002463 resumeTopActivitiesLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002464 }
2465
Craig Mautnerac6f8432013-07-17 13:24:59 -07002466 ActivityRecord findTaskLocked(ActivityRecord r) {
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002467 if (DEBUG_TASKS) Slog.d(TAG, "Looking for task of " + r);
Craig Mautnere0a38842013-12-16 16:14:02 -08002468 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2469 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002470 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2471 final ActivityStack stack = stacks.get(stackNdx);
2472 if (!r.isApplicationActivity() && !stack.isHomeStack()) {
Craig Mautner1b4bf852014-05-26 15:06:32 -07002473 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (home activity) " + stack);
2474 continue;
2475 }
2476 if (!stack.mActivityContainer.isEligibleForNewTasks()) {
2477 if (DEBUG_TASKS) Slog.d(TAG, "Skipping stack: (new task not allowed) " +
2478 stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002479 continue;
2480 }
2481 final ActivityRecord ar = stack.findTaskLocked(r);
2482 if (ar != null) {
2483 return ar;
2484 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002485 }
2486 }
Dianne Hackborn2a272d42013-10-16 13:34:33 -07002487 if (DEBUG_TASKS) Slog.d(TAG, "No task found");
Craig Mautner8849a5e2013-04-02 16:41:03 -07002488 return null;
2489 }
2490
2491 ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002492 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2493 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002494 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2495 final ActivityRecord ar = stacks.get(stackNdx).findActivityLocked(intent, info);
2496 if (ar != null) {
2497 return ar;
2498 }
Craig Mautner8849a5e2013-04-02 16:41:03 -07002499 }
2500 }
2501 return null;
2502 }
2503
Craig Mautner8d341ef2013-03-26 09:03:27 -07002504 void goingToSleepLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002505 scheduleSleepTimeout();
2506 if (!mGoingToSleep.isHeld()) {
2507 mGoingToSleep.acquire();
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002508 if (mLaunchingActivity.isHeld()) {
2509 if (VALIDATE_WAKE_LOCK_CALLER && Binder.getCallingUid() != Process.myUid()) {
2510 throw new IllegalStateException("Calling must be system uid");
Craig Mautner0eea92c2013-05-16 13:35:39 -07002511 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07002512 mLaunchingActivity.release();
2513 mService.mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
Craig Mautner0eea92c2013-05-16 13:35:39 -07002514 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002515 }
Amith Yamasanice15e152013-09-19 12:30:32 -07002516 checkReadyForSleepLocked();
Craig Mautner8d341ef2013-03-26 09:03:27 -07002517 }
2518
2519 boolean shutdownLocked(int timeout) {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002520 goingToSleepLocked();
Craig Mautner0eea92c2013-05-16 13:35:39 -07002521
Craig Mautnerf4c909b2014-04-17 18:39:38 -07002522 boolean timedout = false;
Craig Mautner0eea92c2013-05-16 13:35:39 -07002523 final long endTime = System.currentTimeMillis() + timeout;
2524 while (true) {
2525 boolean cantShutdown = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002526 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2527 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002528 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2529 cantShutdown |= stacks.get(stackNdx).checkReadyForSleepLocked();
2530 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002531 }
2532 if (cantShutdown) {
2533 long timeRemaining = endTime - System.currentTimeMillis();
2534 if (timeRemaining > 0) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002535 try {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002536 mService.wait(timeRemaining);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002537 } catch (InterruptedException e) {
2538 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002539 } else {
2540 Slog.w(TAG, "Activity manager shutdown timed out");
2541 timedout = true;
2542 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002543 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002544 } else {
2545 break;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002546 }
2547 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002548
2549 // Force checkReadyForSleep to complete.
2550 mSleepTimeout = true;
2551 checkReadyForSleepLocked();
2552
Craig Mautner8d341ef2013-03-26 09:03:27 -07002553 return timedout;
2554 }
2555
2556 void comeOutOfSleepIfNeededLocked() {
Craig Mautner0eea92c2013-05-16 13:35:39 -07002557 removeSleepTimeouts();
2558 if (mGoingToSleep.isHeld()) {
2559 mGoingToSleep.release();
2560 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002561 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2562 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002563 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2564 final ActivityStack stack = stacks.get(stackNdx);
2565 stack.awakeFromSleepingLocked();
2566 if (isFrontStack(stack)) {
2567 resumeTopActivitiesLocked();
2568 }
Craig Mautner5314a402013-09-26 12:40:16 -07002569 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002570 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002571 mGoingToSleepActivities.clear();
2572 }
2573
2574 void activitySleptLocked(ActivityRecord r) {
2575 mGoingToSleepActivities.remove(r);
2576 checkReadyForSleepLocked();
2577 }
2578
2579 void checkReadyForSleepLocked() {
2580 if (!mService.isSleepingOrShuttingDown()) {
2581 // Do not care.
2582 return;
2583 }
2584
2585 if (!mSleepTimeout) {
2586 boolean dontSleep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002587 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2588 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002589 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2590 dontSleep |= stacks.get(stackNdx).checkReadyForSleepLocked();
2591 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002592 }
2593
2594 if (mStoppingActivities.size() > 0) {
2595 // Still need to tell some activities to stop; can't sleep yet.
2596 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
2597 + mStoppingActivities.size() + " activities");
2598 scheduleIdleLocked();
2599 dontSleep = true;
2600 }
2601
2602 if (mGoingToSleepActivities.size() > 0) {
2603 // Still need to tell some activities to sleep; can't sleep yet.
2604 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to sleep "
2605 + mGoingToSleepActivities.size() + " activities");
2606 dontSleep = true;
2607 }
2608
2609 if (dontSleep) {
2610 return;
2611 }
2612 }
2613
Craig Mautnere0a38842013-12-16 16:14:02 -08002614 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2615 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002616 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2617 stacks.get(stackNdx).goToSleep();
2618 }
Craig Mautner0eea92c2013-05-16 13:35:39 -07002619 }
2620
2621 removeSleepTimeouts();
2622
2623 if (mGoingToSleep.isHeld()) {
2624 mGoingToSleep.release();
2625 }
2626 if (mService.mShuttingDown) {
2627 mService.notifyAll();
2628 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002629 }
2630
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002631 boolean reportResumedActivityLocked(ActivityRecord r) {
2632 final ActivityStack stack = r.task.stack;
2633 if (isFrontStack(stack)) {
Jeff Sharkey5782da72013-04-25 14:32:30 -07002634 mService.updateUsageStats(r, true);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002635 }
2636 if (allResumedActivitiesComplete()) {
2637 ensureActivitiesVisibleLocked(null, 0);
2638 mWindowManager.executeAppTransition();
2639 return true;
2640 }
2641 return false;
2642 }
2643
Craig Mautner8d341ef2013-03-26 09:03:27 -07002644 void handleAppCrashLocked(ProcessRecord app) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002645 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2646 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002647 final int numStacks = stacks.size();
2648 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2649 final ActivityStack stack = stacks.get(stackNdx);
2650 stack.handleAppCrashLocked(app);
2651 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002652 }
2653 }
2654
Craig Mautneree2e45a2014-06-27 12:10:03 -07002655 boolean setMediaPlayingLocked(ActivityRecord r, boolean playing) {
2656 final ActivityStack stack = r.task.stack;
2657 if (stack == null) {
2658 if (DEBUG_MEDIA_VISIBILITY) Slog.d(TAG, "setMediaPlaying: r=" + r + " playing=" +
2659 playing + " stack is null");
2660 return false;
2661 }
2662 final boolean isPlaying = stack.isMediaPlaying();
2663 if (DEBUG_MEDIA_VISIBILITY) Slog.d(TAG, "setMediaPlayer: r=" + r + " playing=" +
2664 playing + " isPlaying=" + isPlaying);
2665
2666 final ActivityRecord top = topRunningActivityLocked();
2667 if (top == null || top == r || (playing == isPlaying)) {
2668 if (DEBUG_MEDIA_VISIBILITY) Slog.d(TAG, "setMediaPlaying: quick return");
2669 stack.setMediaPlayer(playing ? r : null);
2670 return true;
2671 }
2672
2673 // A non-top activity is reporting a visibility change.
2674 if (top.fullscreen || top.state != ActivityState.RESUMED || top.app == null ||
2675 top.app.thread == null) {
2676 // Can't carry out this request.
2677 if (DEBUG_MEDIA_VISIBILITY) Slog.d(TAG, "setMediaPlaying: returning top.fullscreen=" +
2678 top.fullscreen+ " top.state=" + top.state + " top.app=" + top.app +
2679 " top.app.thread=" + top.app.thread);
2680 return false;
2681 }
2682
2683 stack.setMediaPlayer(playing ? r : null);
2684 try {
2685 top.app.thread.scheduleBackgroundMediaPlayingChanged(top.appToken, playing);
2686 } catch (RemoteException e) {
2687 }
2688 return true;
2689 }
2690
Craig Mautnerbb742462014-07-07 15:28:55 -07002691 // Called when WindowManager has finished animating the launchingBehind activity to the back.
2692 void handleLaunchTaskBehindCompleteLocked(ActivityRecord r) {
2693 r.mLaunchTaskBehind = false;
2694 final TaskRecord task = r.task;
2695 task.setLastThumbnail(task.stack.screenshotActivities(r));
2696 mService.addRecentTaskLocked(task);
2697 mWindowManager.setAppVisibility(r.appToken, false);
2698 }
2699
2700 void scheduleLaunchTaskBehindComplete(IBinder token) {
2701 mHandler.obtainMessage(LAUNCH_TASK_BEHIND_COMPLETE, token).sendToTarget();
2702 }
2703
Craig Mautnerde4ef022013-04-07 19:01:33 -07002704 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges) {
Craig Mautner580ea812013-04-25 12:58:38 -07002705 // First the front stacks. In case any are not fullscreen and are in front of home.
Craig Mautnere0a38842013-12-16 16:14:02 -08002706 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2707 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002708 final int topStackNdx = stacks.size() - 1;
2709 for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) {
2710 final ActivityStack stack = stacks.get(stackNdx);
Jose Lima729cb232014-05-05 15:59:40 -07002711 stack.ensureActivitiesVisibleLocked(starting, configChanges);
Craig Mautner580ea812013-04-25 12:58:38 -07002712 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002713 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002714 }
2715
2716 void scheduleDestroyAllActivities(ProcessRecord app, String reason) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002717 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2718 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002719 final int numStacks = stacks.size();
2720 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2721 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002722 stack.scheduleDestroyActivities(app, reason);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002723 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002724 }
2725 }
2726
2727 boolean switchUserLocked(int userId, UserStartedState uss) {
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002728 mUserStackInFront.put(mCurrentUser, getFocusedStack().getStackId());
2729 final int restoreStackId = mUserStackInFront.get(userId, HOME_STACK_ID);
Craig Mautner2420ead2013-04-01 17:13:20 -07002730 mCurrentUser = userId;
Craig Mautnerac6f8432013-07-17 13:24:59 -07002731
Craig Mautner858d8a62013-04-23 17:08:34 -07002732 mStartingUsers.add(uss);
Craig Mautnere0a38842013-12-16 16:14:02 -08002733 for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
2734 final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002735 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautnere0a38842013-12-16 16:14:02 -08002736 final ActivityStack stack = stacks.get(stackNdx);
2737 stack.switchUserLocked(userId);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002738 TaskRecord task = stack.topTask();
2739 if (task != null) {
2740 mWindowManager.moveTaskToTop(task.taskId);
2741 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002742 }
Craig Mautnerac6f8432013-07-17 13:24:59 -07002743 }
Craig Mautner858d8a62013-04-23 17:08:34 -07002744
Craig Mautner4f1df4f2013-10-15 15:44:14 -07002745 ActivityStack stack = getStack(restoreStackId);
2746 if (stack == null) {
2747 stack = mHomeStack;
2748 }
2749 final boolean homeInFront = stack.isHomeStack();
Craig Mautnere0a38842013-12-16 16:14:02 -08002750 if (stack.isOnHomeDisplay()) {
2751 moveHomeStack(homeInFront);
Alexandra Gherghinadae57a12014-03-12 19:15:26 +00002752 TaskRecord task = stack.topTask();
2753 if (task != null) {
2754 mWindowManager.moveTaskToTop(task.taskId);
2755 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002756 } else {
2757 // Stack was moved to another display while user was swapped out.
Craig Mautner84984fa2014-06-19 11:19:20 -07002758 resumeHomeStackTask(HOME_ACTIVITY_TYPE, null);
Craig Mautnere0a38842013-12-16 16:14:02 -08002759 }
Craig Mautner93529a42013-10-04 15:03:13 -07002760 return homeInFront;
Craig Mautner2219a1b2013-03-25 09:44:30 -07002761 }
2762
Amith Yamasani1a7eaaa52014-05-07 10:22:15 -07002763 /**
2764 * Add background users to send boot completed events to.
2765 * @param userId The user being started in the background
2766 * @param uss The state object for the user.
2767 */
2768 public void startBackgroundUserLocked(int userId, UserStartedState uss) {
2769 mStartingBackgroundUsers.add(uss);
2770 }
2771
Craig Mautnerde4ef022013-04-07 19:01:33 -07002772 final ArrayList<ActivityRecord> processStoppingActivitiesLocked(boolean remove) {
2773 int N = mStoppingActivities.size();
2774 if (N <= 0) return null;
2775
2776 ArrayList<ActivityRecord> stops = null;
2777
2778 final boolean nowVisible = allResumedActivitiesVisible();
2779 for (int i=0; i<N; i++) {
2780 ActivityRecord s = mStoppingActivities.get(i);
Craig Mautnera7f2bd42013-10-15 16:13:50 -07002781 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
Craig Mautnerde4ef022013-04-07 19:01:33 -07002782 + nowVisible + " waitingVisible=" + s.waitingVisible
2783 + " finishing=" + s.finishing);
2784 if (s.waitingVisible && nowVisible) {
2785 mWaitingVisibleActivities.remove(s);
2786 s.waitingVisible = false;
2787 if (s.finishing) {
2788 // If this activity is finishing, it is sitting on top of
2789 // everyone else but we now know it is no longer needed...
2790 // so get rid of it. Otherwise, we need to go through the
2791 // normal flow and hide it once we determine that it is
2792 // hidden by the activities in front of it.
2793 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002794 mWindowManager.setAppVisibility(s.appToken, false);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002795 }
2796 }
2797 if ((!s.waitingVisible || mService.isSleepingOrShuttingDown()) && remove) {
2798 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
2799 if (stops == null) {
2800 stops = new ArrayList<ActivityRecord>();
2801 }
2802 stops.add(s);
2803 mStoppingActivities.remove(i);
2804 N--;
2805 i--;
2806 }
2807 }
2808
2809 return stops;
2810 }
2811
Craig Mautnercf910b02013-04-23 11:23:27 -07002812 void validateTopActivitiesLocked() {
Craig Mautner4a1cb222013-12-04 16:14:06 -08002813 // FIXME
2814/* for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
2815 final ActivityStack stack = stacks.get(stackNdx);
Craig Mautnercf910b02013-04-23 11:23:27 -07002816 final ActivityRecord r = stack.topRunningActivityLocked(null);
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002817 final ActivityState state = r == null ? ActivityState.DESTROYED : r.state;
Craig Mautnercf910b02013-04-23 11:23:27 -07002818 if (isFrontStack(stack)) {
2819 if (r == null) {
2820 Slog.e(TAG, "validateTop...: null top activity, stack=" + stack);
2821 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002822 final ActivityRecord pausing = stack.mPausingActivity;
2823 if (pausing != null && pausing == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002824 Slog.e(TAG, "validateTop...: top stack has pausing activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002825 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002826 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002827 if (state != ActivityState.INITIALIZING && state != ActivityState.RESUMED) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002828 Slog.e(TAG, "validateTop...: activity in front not resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002829 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002830 }
2831 }
2832 } else {
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002833 final ActivityRecord resumed = stack.mResumedActivity;
2834 if (resumed != null && resumed == r) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002835 Slog.e(TAG, "validateTop...: back stack has resumed activity r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002836 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002837 }
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002838 if (r != null && (state == ActivityState.INITIALIZING
2839 || state == ActivityState.RESUMED)) {
Craig Mautnercf910b02013-04-23 11:23:27 -07002840 Slog.e(TAG, "validateTop...: activity in back resumed r=" + r +
Craig Mautnerf0ac5c82013-06-24 11:21:57 -07002841 " state=" + state);
Craig Mautnercf910b02013-04-23 11:23:27 -07002842 }
2843 }
2844 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002845*/
Craig Mautner76ea2242013-05-15 11:40:05 -07002846 }
2847
Craig Mautner27084302013-03-25 08:05:25 -07002848 public void dump(PrintWriter pw, String prefix) {
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002849 pw.print(prefix); pw.print("mDismissKeyguardOnNextActivity=");
Craig Mautner27084302013-03-25 08:05:25 -07002850 pw.println(mDismissKeyguardOnNextActivity);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002851 pw.print(prefix); pw.print("mFocusedStack=" + mFocusedStack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08002852 pw.print(" mLastFocusedStack="); pw.println(mLastFocusedStack);
Craig Mautnerd1bbdb4622013-10-22 09:53:20 -07002853 pw.print(prefix); pw.println("mSleepTimeout=" + mSleepTimeout);
2854 pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
2855 pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
Craig Mautner95da1082014-02-24 17:54:35 -08002856 pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
Craig Mautner27084302013-03-25 08:05:25 -07002857 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002858
Craig Mautner20e72272013-04-01 13:45:53 -07002859 ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07002860 return getFocusedStack().getDumpActivitiesLocked(name);
Craig Mautner20e72272013-04-01 13:45:53 -07002861 }
2862
Dianne Hackborn390517b2013-05-30 15:03:32 -07002863 static boolean printThisActivity(PrintWriter pw, ActivityRecord activity, String dumpPackage,
2864 boolean needSep, String prefix) {
2865 if (activity != null) {
2866 if (dumpPackage == null || dumpPackage.equals(activity.packageName)) {
2867 if (needSep) {
2868 pw.println();
Dianne Hackborn390517b2013-05-30 15:03:32 -07002869 }
2870 pw.print(prefix);
2871 pw.println(activity);
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002872 return true;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002873 }
2874 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002875 return false;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002876 }
2877
Craig Mautner8d341ef2013-03-26 09:03:27 -07002878 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
2879 boolean dumpClient, String dumpPackage) {
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002880 boolean printed = false;
2881 boolean needSep = false;
Craig Mautnere0a38842013-12-16 16:14:02 -08002882 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
2883 ActivityDisplay activityDisplay = mActivityDisplays.valueAt(displayNdx);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002884 pw.print("Display #"); pw.print(activityDisplay.mDisplayId);
2885 pw.println(" (activities from bottom to top):");
Craig Mautnere0a38842013-12-16 16:14:02 -08002886 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002887 final int numStacks = stacks.size();
2888 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
2889 final ActivityStack stack = stacks.get(stackNdx);
2890 StringBuilder stackHeader = new StringBuilder(128);
2891 stackHeader.append(" Stack #");
2892 stackHeader.append(stack.mStackId);
2893 stackHeader.append(":");
2894 printed |= stack.dumpActivitiesLocked(fd, pw, dumpAll, dumpClient, dumpPackage,
2895 needSep, stackHeader.toString());
2896 printed |= dumpHistoryList(fd, pw, stack.mLRUActivities, " ", "Run", false,
2897 !dumpAll, false, dumpPackage, true,
2898 " Running activities (most recent first):", null);
Craig Mautner8d341ef2013-03-26 09:03:27 -07002899
Craig Mautner4a1cb222013-12-04 16:14:06 -08002900 needSep = printed;
2901 boolean pr = printThisActivity(pw, stack.mPausingActivity, dumpPackage, needSep,
2902 " mPausingActivity: ");
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002903 if (pr) {
2904 printed = true;
Craig Mautner4a1cb222013-12-04 16:14:06 -08002905 needSep = false;
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002906 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08002907 pr = printThisActivity(pw, stack.mResumedActivity, dumpPackage, needSep,
2908 " mResumedActivity: ");
2909 if (pr) {
2910 printed = true;
2911 needSep = false;
2912 }
2913 if (dumpAll) {
2914 pr = printThisActivity(pw, stack.mLastPausedActivity, dumpPackage, needSep,
2915 " mLastPausedActivity: ");
2916 if (pr) {
2917 printed = true;
2918 needSep = true;
2919 }
2920 printed |= printThisActivity(pw, stack.mLastNoHistoryActivity, dumpPackage,
2921 needSep, " mLastNoHistoryActivity: ");
2922 }
2923 needSep = printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002924 }
2925 }
2926
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002927 printed |= dumpHistoryList(fd, pw, mFinishingActivities, " ", "Fin", false, !dumpAll,
2928 false, dumpPackage, true, " Activities waiting to finish:", null);
2929 printed |= dumpHistoryList(fd, pw, mStoppingActivities, " ", "Stop", false, !dumpAll,
2930 false, dumpPackage, true, " Activities waiting to stop:", null);
2931 printed |= dumpHistoryList(fd, pw, mWaitingVisibleActivities, " ", "Wait", false, !dumpAll,
2932 false, dumpPackage, true, " Activities waiting for another to become visible:",
2933 null);
2934 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2935 false, dumpPackage, true, " Activities waiting to sleep:", null);
2936 printed |= dumpHistoryList(fd, pw, mGoingToSleepActivities, " ", "Sleep", false, !dumpAll,
2937 false, dumpPackage, true, " Activities waiting to sleep:", null);
Craig Mautnerf3333272013-04-22 10:55:53 -07002938
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002939 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002940 }
2941
Dianne Hackborn390517b2013-05-30 15:03:32 -07002942 static boolean dumpHistoryList(FileDescriptor fd, PrintWriter pw, List<ActivityRecord> list,
Craig Mautner8d341ef2013-03-26 09:03:27 -07002943 String prefix, String label, boolean complete, boolean brief, boolean client,
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002944 String dumpPackage, boolean needNL, String header1, String header2) {
Craig Mautner8d341ef2013-03-26 09:03:27 -07002945 TaskRecord lastTask = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002946 String innerPrefix = null;
2947 String[] args = null;
2948 boolean printed = false;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002949 for (int i=list.size()-1; i>=0; i--) {
2950 final ActivityRecord r = list.get(i);
2951 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
2952 continue;
2953 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07002954 if (innerPrefix == null) {
2955 innerPrefix = prefix + " ";
2956 args = new String[0];
2957 }
2958 printed = true;
Craig Mautner8d341ef2013-03-26 09:03:27 -07002959 final boolean full = !brief && (complete || !r.isInHistory());
2960 if (needNL) {
Dianne Hackborn390517b2013-05-30 15:03:32 -07002961 pw.println("");
Craig Mautner8d341ef2013-03-26 09:03:27 -07002962 needNL = false;
2963 }
Dianne Hackborn7ad34e52013-06-05 18:41:45 -07002964 if (header1 != null) {
2965 pw.println(header1);
2966 header1 = null;
2967 }
2968 if (header2 != null) {
2969 pw.println(header2);
2970 header2 = null;
Dianne Hackborn390517b2013-05-30 15:03:32 -07002971 }
Craig Mautner8d341ef2013-03-26 09:03:27 -07002972 if (lastTask != r.task) {
2973 lastTask = r.task;
2974 pw.print(prefix);
2975 pw.print(full ? "* " : " ");
2976 pw.println(lastTask);
2977 if (full) {
2978 lastTask.dump(pw, prefix + " ");
2979 } else if (complete) {
2980 // Complete + brief == give a summary. Isn't that obvious?!?
2981 if (lastTask.intent != null) {
2982 pw.print(prefix); pw.print(" ");
2983 pw.println(lastTask.intent.toInsecureStringWithClip());
2984 }
2985 }
2986 }
2987 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
2988 pw.print(" #"); pw.print(i); pw.print(": ");
2989 pw.println(r);
2990 if (full) {
2991 r.dump(pw, innerPrefix);
2992 } else if (complete) {
2993 // Complete + brief == give a summary. Isn't that obvious?!?
2994 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
2995 if (r.app != null) {
2996 pw.print(innerPrefix); pw.println(r.app);
2997 }
2998 }
2999 if (client && r.app != null && r.app.thread != null) {
3000 // flush anything that is already in the PrintWriter since the thread is going
3001 // to write to the file descriptor directly
3002 pw.flush();
3003 try {
3004 TransferPipe tp = new TransferPipe();
3005 try {
3006 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
3007 r.appToken, innerPrefix, args);
3008 // Short timeout, since blocking here can
3009 // deadlock with the application.
3010 tp.go(fd, 2000);
3011 } finally {
3012 tp.kill();
3013 }
3014 } catch (IOException e) {
3015 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
3016 } catch (RemoteException e) {
3017 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
3018 }
3019 needNL = true;
3020 }
3021 }
Dianne Hackborn390517b2013-05-30 15:03:32 -07003022 return printed;
Craig Mautner8d341ef2013-03-26 09:03:27 -07003023 }
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003024
Craig Mautnerf3333272013-04-22 10:55:53 -07003025 void scheduleIdleTimeoutLocked(ActivityRecord next) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003026 if (DEBUG_IDLE) Slog.d(TAG, "scheduleIdleTimeoutLocked: Callers=" + Debug.getCallers(4));
Craig Mautnerc64f73e2013-04-24 16:44:56 -07003027 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next);
3028 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
Craig Mautnerf3333272013-04-22 10:55:53 -07003029 }
3030
3031 final void scheduleIdleLocked() {
Craig Mautner05d29032013-05-03 13:40:13 -07003032 mHandler.sendEmptyMessage(IDLE_NOW_MSG);
Craig Mautnerf3333272013-04-22 10:55:53 -07003033 }
3034
3035 void removeTimeoutsForActivityLocked(ActivityRecord r) {
Craig Mautnerb59dcfd2013-05-06 13:12:58 -07003036 if (DEBUG_IDLE) Slog.d(TAG, "removeTimeoutsForActivity: Callers=" + Debug.getCallers(4));
Craig Mautnerf3333272013-04-22 10:55:53 -07003037 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
3038 }
3039
Craig Mautner05d29032013-05-03 13:40:13 -07003040 final void scheduleResumeTopActivities() {
Craig Mautner34b73df2014-01-12 21:11:08 -08003041 if (!mHandler.hasMessages(RESUME_TOP_ACTIVITY_MSG)) {
3042 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
3043 }
Craig Mautner05d29032013-05-03 13:40:13 -07003044 }
3045
Craig Mautner0eea92c2013-05-16 13:35:39 -07003046 void removeSleepTimeouts() {
3047 mSleepTimeout = false;
3048 mHandler.removeMessages(SLEEP_TIMEOUT_MSG);
3049 }
3050
3051 final void scheduleSleepTimeout() {
3052 removeSleepTimeouts();
3053 mHandler.sendEmptyMessageDelayed(SLEEP_TIMEOUT_MSG, SLEEP_TIMEOUT);
3054 }
3055
Craig Mautner4a1cb222013-12-04 16:14:06 -08003056 @Override
3057 public void onDisplayAdded(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003058 Slog.v(TAG, "Display added displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003059 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_ADDED, displayId, 0));
3060 }
3061
3062 @Override
3063 public void onDisplayRemoved(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003064 Slog.v(TAG, "Display removed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003065 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_REMOVED, displayId, 0));
3066 }
3067
3068 @Override
3069 public void onDisplayChanged(int displayId) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003070 Slog.v(TAG, "Display changed displayId=" + displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003071 mHandler.sendMessage(mHandler.obtainMessage(HANDLE_DISPLAY_CHANGED, displayId, 0));
3072 }
3073
3074 public void handleDisplayAddedLocked(int displayId) {
Craig Mautner4504de52013-12-20 09:06:56 -08003075 boolean newDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003076 synchronized (mService) {
Craig Mautner4504de52013-12-20 09:06:56 -08003077 newDisplay = mActivityDisplays.get(displayId) == null;
3078 if (newDisplay) {
3079 ActivityDisplay activityDisplay = new ActivityDisplay(displayId);
3080 mActivityDisplays.put(displayId, activityDisplay);
3081 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003082 }
Craig Mautner4504de52013-12-20 09:06:56 -08003083 if (newDisplay) {
3084 mWindowManager.onDisplayAdded(displayId);
3085 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003086 }
3087
3088 public void handleDisplayRemovedLocked(int displayId) {
3089 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003090 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3091 if (activityDisplay != null) {
3092 ArrayList<ActivityStack> stacks = activityDisplay.mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003093 for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003094 stacks.get(stackNdx).mActivityContainer.detachLocked();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003095 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003096 mActivityDisplays.remove(displayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003097 }
3098 }
3099 mWindowManager.onDisplayRemoved(displayId);
3100 }
3101
3102 public void handleDisplayChangedLocked(int displayId) {
3103 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003104 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3105 if (activityDisplay != null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003106 // TODO: Update the bounds.
3107 }
3108 }
3109 mWindowManager.onDisplayChanged(displayId);
3110 }
3111
3112 StackInfo getStackInfo(ActivityStack stack) {
3113 StackInfo info = new StackInfo();
3114 mWindowManager.getStackBounds(stack.mStackId, info.bounds);
3115 info.displayId = Display.DEFAULT_DISPLAY;
3116 info.stackId = stack.mStackId;
3117
3118 ArrayList<TaskRecord> tasks = stack.getAllTasks();
3119 final int numTasks = tasks.size();
3120 int[] taskIds = new int[numTasks];
3121 String[] taskNames = new String[numTasks];
3122 for (int i = 0; i < numTasks; ++i) {
3123 final TaskRecord task = tasks.get(i);
3124 taskIds[i] = task.taskId;
3125 taskNames[i] = task.origActivity != null ? task.origActivity.flattenToString()
3126 : task.realActivity != null ? task.realActivity.flattenToString()
3127 : task.getTopActivity() != null ? task.getTopActivity().packageName
3128 : "unknown";
3129 }
3130 info.taskIds = taskIds;
3131 info.taskNames = taskNames;
3132 return info;
3133 }
3134
3135 StackInfo getStackInfoLocked(int stackId) {
3136 ActivityStack stack = getStack(stackId);
3137 if (stack != null) {
3138 return getStackInfo(stack);
3139 }
3140 return null;
3141 }
3142
3143 ArrayList<StackInfo> getAllStackInfosLocked() {
3144 ArrayList<StackInfo> list = new ArrayList<StackInfo>();
Craig Mautnere0a38842013-12-16 16:14:02 -08003145 for (int displayNdx = 0; displayNdx < mActivityDisplays.size(); ++displayNdx) {
3146 ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003147 for (int ndx = stacks.size() - 1; ndx >= 0; --ndx) {
3148 list.add(getStackInfo(stacks.get(ndx)));
3149 }
3150 }
3151 return list;
3152 }
3153
Jason Monka8f569c2014-07-07 12:15:21 -04003154 void showLockTaskToast() {
3155 mLockTaskNotify.showToast(mLockTaskIsLocked);
3156 }
3157
3158 void setLockTaskModeLocked(TaskRecord task, boolean isLocked) {
Craig Mautneraea74a52014-03-08 14:23:10 -08003159 if (task == null) {
Christopher Tate3bd90612014-06-18 16:37:52 -07003160 // Take out of lock task mode if necessary
3161 if (mLockTaskModeTask != null) {
3162 final Message lockTaskMsg = Message.obtain();
3163 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
3164 lockTaskMsg.what = LOCK_TASK_END_MSG;
3165 mLockTaskModeTask = null;
3166 mHandler.sendMessage(lockTaskMsg);
3167 }
Craig Mautneraea74a52014-03-08 14:23:10 -08003168 return;
3169 }
3170 if (isLockTaskModeViolation(task)) {
3171 Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
3172 return;
3173 }
3174 mLockTaskModeTask = task;
3175 findTaskToMoveToFrontLocked(task, 0, null);
3176 resumeTopActivitiesLocked();
Christopher Tate3bd90612014-06-18 16:37:52 -07003177
3178 final Message lockTaskMsg = Message.obtain();
Jason Monk35c62a42014-06-17 10:24:47 -04003179 lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
3180 lockTaskMsg.arg1 = mLockTaskModeTask.userId;
justinzhang5286d3f2014-05-12 17:06:01 -04003181 lockTaskMsg.what = LOCK_TASK_START_MSG;
Jason Monka8f569c2014-07-07 12:15:21 -04003182 lockTaskMsg.arg2 = !isLocked ? 1 : 0;
justinzhang5286d3f2014-05-12 17:06:01 -04003183 mHandler.sendMessage(lockTaskMsg);
Craig Mautneraea74a52014-03-08 14:23:10 -08003184 }
3185
3186 boolean isLockTaskModeViolation(TaskRecord task) {
3187 return mLockTaskModeTask != null && mLockTaskModeTask != task;
3188 }
3189
3190 void endLockTaskModeIfTaskEnding(TaskRecord task) {
3191 if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
3192 mLockTaskModeTask = null;
3193 }
3194 }
3195
3196 boolean isInLockTaskMode() {
3197 return mLockTaskModeTask != null;
3198 }
3199
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003200 private final class ActivityStackSupervisorHandler extends Handler {
Craig Mautnerf3333272013-04-22 10:55:53 -07003201
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003202 public ActivityStackSupervisorHandler(Looper looper) {
3203 super(looper);
3204 }
3205
Craig Mautnerf3333272013-04-22 10:55:53 -07003206 void activityIdleInternal(ActivityRecord r) {
3207 synchronized (mService) {
3208 activityIdleInternalLocked(r != null ? r.appToken : null, true, null);
3209 }
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003210 }
3211
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003212 @Override
3213 public void handleMessage(Message msg) {
3214 switch (msg.what) {
Craig Mautnerf3333272013-04-22 10:55:53 -07003215 case IDLE_TIMEOUT_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003216 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_TIMEOUT_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003217 if (mService.mDidDexOpt) {
3218 mService.mDidDexOpt = false;
3219 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
3220 nmsg.obj = msg.obj;
3221 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
3222 return;
3223 }
3224 // We don't at this point know if the activity is fullscreen,
3225 // so we need to be conservative and assume it isn't.
3226 activityIdleInternal((ActivityRecord)msg.obj);
3227 } break;
3228 case IDLE_NOW_MSG: {
Craig Mautner5eda9b32013-07-02 11:58:16 -07003229 if (DEBUG_IDLE) Slog.d(TAG, "handleMessage: IDLE_NOW_MSG: r=" + msg.obj);
Craig Mautnerf3333272013-04-22 10:55:53 -07003230 activityIdleInternal((ActivityRecord)msg.obj);
3231 } break;
Craig Mautner05d29032013-05-03 13:40:13 -07003232 case RESUME_TOP_ACTIVITY_MSG: {
3233 synchronized (mService) {
3234 resumeTopActivitiesLocked();
3235 }
3236 } break;
Craig Mautner0eea92c2013-05-16 13:35:39 -07003237 case SLEEP_TIMEOUT_MSG: {
3238 synchronized (mService) {
3239 if (mService.isSleepingOrShuttingDown()) {
3240 Slog.w(TAG, "Sleep timeout! Sleeping now.");
3241 mSleepTimeout = true;
3242 checkReadyForSleepLocked();
3243 }
3244 }
3245 } break;
Craig Mautner7ea5bd42013-07-05 15:27:08 -07003246 case LAUNCH_TIMEOUT_MSG: {
3247 if (mService.mDidDexOpt) {
3248 mService.mDidDexOpt = false;
3249 mHandler.sendEmptyMessageDelayed(LAUNCH_TIMEOUT_MSG, LAUNCH_TIMEOUT);
3250 return;
3251 }
3252 synchronized (mService) {
3253 if (mLaunchingActivity.isHeld()) {
3254 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
3255 if (VALIDATE_WAKE_LOCK_CALLER
3256 && Binder.getCallingUid() != Process.myUid()) {
3257 throw new IllegalStateException("Calling must be system uid");
3258 }
3259 mLaunchingActivity.release();
3260 }
3261 }
3262 } break;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003263 case HANDLE_DISPLAY_ADDED: {
3264 handleDisplayAddedLocked(msg.arg1);
3265 } break;
3266 case HANDLE_DISPLAY_CHANGED: {
3267 handleDisplayChangedLocked(msg.arg1);
3268 } break;
3269 case HANDLE_DISPLAY_REMOVED: {
3270 handleDisplayRemovedLocked(msg.arg1);
3271 } break;
Craig Mautnere3a00d72014-04-16 08:31:19 -07003272 case CONTAINER_CALLBACK_VISIBILITY: {
3273 final ActivityContainer container = (ActivityContainer) msg.obj;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003274 final IActivityContainerCallback callback = container.mCallback;
3275 if (callback != null) {
3276 try {
3277 callback.setVisible(container.asBinder(), msg.arg1 == 1);
3278 } catch (RemoteException e) {
3279 }
Craig Mautnere3a00d72014-04-16 08:31:19 -07003280 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003281 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003282 case LOCK_TASK_START_MSG: {
3283 // When lock task starts, we disable the status bars.
3284 try {
Jason Monk62515be2014-05-21 16:06:19 -04003285 if (mLockTaskNotify == null) {
3286 mLockTaskNotify = new LockTaskNotify(mService.mContext);
justinzhang5286d3f2014-05-12 17:06:01 -04003287 }
Jason Monk62515be2014-05-21 16:06:19 -04003288 mLockTaskNotify.show(true);
3289 if (getStatusBarService() != null) {
3290 int flags =
3291 StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
3292 if (msg.arg2 != 0) {
3293 flags ^= StatusBarManager.DISABLE_HOME
3294 | StatusBarManager.DISABLE_RECENT;
3295 }
3296 getStatusBarService().disable(flags, mToken,
3297 mService.mContext.getPackageName());
3298 }
3299 mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG);
Jason Monk35c62a42014-06-17 10:24:47 -04003300 if (getDevicePolicyManager() != null) {
3301 getDevicePolicyManager().notifyLockTaskModeChanged(true,
Jason Monk62515be2014-05-21 16:06:19 -04003302 (String)msg.obj, msg.arg1);
Jason Monk35c62a42014-06-17 10:24:47 -04003303 }
justinzhang5286d3f2014-05-12 17:06:01 -04003304 } catch (RemoteException ex) {
3305 throw new RuntimeException(ex);
3306 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003307 } break;
justinzhang5286d3f2014-05-12 17:06:01 -04003308 case LOCK_TASK_END_MSG: {
3309 // When lock task ends, we enable the status bars.
3310 try {
Jason Monk62515be2014-05-21 16:06:19 -04003311 if (getStatusBarService() != null) {
3312 getStatusBarService().disable(StatusBarManager.DISABLE_NONE, mToken,
3313 mService.mContext.getPackageName());
3314 }
3315 mWindowManager.reenableKeyguard(mToken);
Jason Monk35c62a42014-06-17 10:24:47 -04003316 if (getDevicePolicyManager() != null) {
3317 getDevicePolicyManager().notifyLockTaskModeChanged(false, null,
3318 msg.arg1);
3319 }
Jason Monk62515be2014-05-21 16:06:19 -04003320 if (mLockTaskNotify == null) {
3321 mLockTaskNotify = new LockTaskNotify(mService.mContext);
3322 }
3323 mLockTaskNotify.show(false);
3324 try {
3325 boolean shouldLockKeyguard = Settings.System.getInt(
3326 mService.mContext.getContentResolver(),
3327 Settings.System.LOCK_TO_APP_EXIT_LOCKED) != 0;
3328 if (shouldLockKeyguard) {
3329 mWindowManager.lockNow(null);
Jason Monk7779bf12014-07-14 10:20:21 -04003330 mWindowManager.dismissKeyguard();
Jason Monk62515be2014-05-21 16:06:19 -04003331 }
3332 } catch (SettingNotFoundException e) {
3333 // No setting, don't lock.
3334 }
justinzhang5286d3f2014-05-12 17:06:01 -04003335 } catch (RemoteException ex) {
3336 throw new RuntimeException(ex);
3337 }
Craig Mautnerb6011c12014-06-04 20:59:13 -07003338 } break;
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003339 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
3340 final ActivityContainer container = (ActivityContainer) msg.obj;
3341 final IActivityContainerCallback callback = container.mCallback;
3342 if (callback != null) {
3343 try {
3344 callback.onAllActivitiesComplete(container.asBinder());
3345 } catch (RemoteException e) {
3346 }
3347 }
3348 } break;
Craig Mautnerd163e752014-06-13 17:18:47 -07003349 case CONTAINER_TASK_LIST_EMPTY_TIMEOUT: {
3350 synchronized (mService) {
3351 Slog.w(TAG, "Timeout waiting for all activities in task to finish. " +
3352 msg.obj);
Craig Mautneree36c772014-07-16 14:56:05 -07003353 final ActivityContainer container = (ActivityContainer) msg.obj;
3354 container.mStack.finishAllActivitiesLocked(true);
3355 container.onTaskListEmptyLocked();
Craig Mautnerd163e752014-06-13 17:18:47 -07003356 }
3357 } break;
Craig Mautnerbb742462014-07-07 15:28:55 -07003358 case LAUNCH_TASK_BEHIND_COMPLETE: {
3359 synchronized (mService) {
3360 ActivityRecord r = ActivityRecord.forToken((IBinder) msg.obj);
3361 if (r != null) {
3362 handleLaunchTaskBehindCompleteLocked(r);
3363 }
3364 }
3365 } break;
Craig Mautnerce5f3cb2013-04-22 08:58:54 -07003366 }
3367 }
3368 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003369
Ying Wangb081a592014-04-22 15:20:16 -07003370 class ActivityContainer extends android.app.IActivityContainer.Stub {
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003371 final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Craig Mautnere6d80f42014-06-10 13:31:02 -07003372 Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003373 final int mStackId;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003374 IActivityContainerCallback mCallback = null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003375 final ActivityStack mStack;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003376 ActivityRecord mParentActivity = null;
3377 String mIdString;
Craig Mautnered6649f2013-12-02 14:08:25 -08003378
Craig Mautnere3a00d72014-04-16 08:31:19 -07003379 boolean mVisible = true;
3380
Craig Mautner4a1cb222013-12-04 16:14:06 -08003381 /** Display this ActivityStack is currently on. Null if not attached to a Display. */
Craig Mautnere0a38842013-12-16 16:14:02 -08003382 ActivityDisplay mActivityDisplay;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003383
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003384 final static int CONTAINER_STATE_HAS_SURFACE = 0;
3385 final static int CONTAINER_STATE_NO_SURFACE = 1;
3386 final static int CONTAINER_STATE_FINISHING = 2;
3387 int mContainerState = CONTAINER_STATE_HAS_SURFACE;
3388
3389 ActivityContainer(int stackId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003390 synchronized (mService) {
3391 mStackId = stackId;
3392 mStack = new ActivityStack(this);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003393 mIdString = "ActivtyContainer{" + mStackId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003394 if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003395 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003396 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003397
Craig Mautnere0a38842013-12-16 16:14:02 -08003398 void attachToDisplayLocked(ActivityDisplay activityDisplay) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003399 if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
3400 + " to display=" + activityDisplay);
Craig Mautnere0a38842013-12-16 16:14:02 -08003401 mActivityDisplay = activityDisplay;
3402 mStack.mDisplayId = activityDisplay.mDisplayId;
3403 mStack.mStacks = activityDisplay.mStacks;
3404
3405 activityDisplay.attachActivities(mStack);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003406 mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003407 }
3408
3409 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003410 public void attachToDisplay(int displayId) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003411 synchronized (mService) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003412 ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
3413 if (activityDisplay == null) {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003414 return;
3415 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003416 attachToDisplayLocked(activityDisplay);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003417 }
3418 }
3419
3420 @Override
Jeff Brownca9bc702014-02-11 14:32:56 -08003421 public int getDisplayId() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003422 synchronized (mService) {
3423 if (mActivityDisplay != null) {
3424 return mActivityDisplay.mDisplayId;
3425 }
Craig Mautnere0a38842013-12-16 16:14:02 -08003426 }
3427 return -1;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003428 }
3429
Jeff Brownca9bc702014-02-11 14:32:56 -08003430 @Override
3431 public boolean injectEvent(InputEvent event) {
3432 final long origId = Binder.clearCallingIdentity();
3433 try {
Craig Mautnerd163e752014-06-13 17:18:47 -07003434 synchronized (mService) {
3435 if (mActivityDisplay != null) {
3436 return mInputManagerInternal.injectInputEvent(event,
3437 mActivityDisplay.mDisplayId,
3438 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3439 }
Jeff Brownca9bc702014-02-11 14:32:56 -08003440 }
3441 return false;
3442 } finally {
3443 Binder.restoreCallingIdentity(origId);
3444 }
3445 }
3446
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003447 @Override
3448 public void release() {
Craig Mautnerd163e752014-06-13 17:18:47 -07003449 synchronized (mService) {
3450 if (mContainerState == CONTAINER_STATE_FINISHING) {
3451 return;
3452 }
3453 mContainerState = CONTAINER_STATE_FINISHING;
3454
3455 final Message msg =
3456 mHandler.obtainMessage(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
Craig Mautneree36c772014-07-16 14:56:05 -07003457 mHandler.sendMessageDelayed(msg, 2000);
Craig Mautnerd163e752014-06-13 17:18:47 -07003458
3459 long origId = Binder.clearCallingIdentity();
3460 try {
Craig Mautneree36c772014-07-16 14:56:05 -07003461 mStack.finishAllActivitiesLocked(false);
Craig Mautnerd163e752014-06-13 17:18:47 -07003462 } finally {
3463 Binder.restoreCallingIdentity(origId);
3464 }
3465 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003466 }
3467
Craig Mautner34b73df2014-01-12 21:11:08 -08003468 private void detachLocked() {
3469 if (DEBUG_STACK) Slog.d(TAG, "detachLocked: " + this + " from display="
3470 + mActivityDisplay + " Callers=" + Debug.getCallers(2));
Craig Mautnere0a38842013-12-16 16:14:02 -08003471 if (mActivityDisplay != null) {
3472 mActivityDisplay.detachActivitiesLocked(mStack);
3473 mActivityDisplay = null;
3474 mStack.mDisplayId = -1;
3475 mStack.mStacks = null;
Craig Mautnerdf88d732014-01-27 09:21:32 -08003476 mWindowManager.detachStack(mStackId);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003477 }
3478 }
3479
3480 @Override
Craig Mautnere0a38842013-12-16 16:14:02 -08003481 public final int startActivity(Intent intent) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003482 mService.enforceNotIsolatedCaller("ActivityContainer.startActivity");
Craig Mautnere0a38842013-12-16 16:14:02 -08003483 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003484 Binder.getCallingUid(), mCurrentUser, false,
3485 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautnere0a38842013-12-16 16:14:02 -08003486 // TODO: Switch to user app stacks here.
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003487 intent.addFlags(FORCE_NEW_TASK_FLAGS);
Craig Mautnere0a38842013-12-16 16:14:02 -08003488 String mimeType = intent.getType();
3489 if (mimeType == null && intent.getData() != null
3490 && "content".equals(intent.getData().getScheme())) {
3491 mimeType = mService.getProviderMimeType(intent.getData(), userId);
3492 }
Dianne Hackborn91097de2014-04-04 18:02:06 -07003493 return startActivityMayWait(null, -1, null, intent, mimeType, null, null, null, null, 0, 0, null,
Craig Mautnere0a38842013-12-16 16:14:02 -08003494 null, null, null, null, userId, this);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003495 }
3496
3497 @Override
Craig Mautnerdf88d732014-01-27 09:21:32 -08003498 public final int startActivityIntentSender(IIntentSender intentSender) {
3499 mService.enforceNotIsolatedCaller("ActivityContainer.startActivityIntentSender");
3500
3501 if (!(intentSender instanceof PendingIntentRecord)) {
3502 throw new IllegalArgumentException("Bad PendingIntent object");
3503 }
3504
3505 return ((PendingIntentRecord)intentSender).sendInner(0, null, null, null, null, null,
Craig Mautner7f7bdb22014-04-22 19:57:19 -07003506 null, 0, FORCE_NEW_TASK_FLAGS, FORCE_NEW_TASK_FLAGS, null, this);
Craig Mautnerdf88d732014-01-27 09:21:32 -08003507 }
3508
Craig Mautner247ab652014-04-25 10:09:00 -07003509 private void checkEmbeddedAllowedInner(Intent intent, String resolvedType) {
3510 int userId = mService.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn409297d2014-07-10 17:39:20 -07003511 Binder.getCallingUid(), mCurrentUser, false,
3512 ActivityManagerService.ALLOW_FULL_ONLY, "ActivityContainer", null);
Craig Mautner247ab652014-04-25 10:09:00 -07003513 if (resolvedType == null) {
3514 resolvedType = intent.getType();
3515 if (resolvedType == null && intent.getData() != null
3516 && "content".equals(intent.getData().getScheme())) {
3517 resolvedType = mService.getProviderMimeType(intent.getData(), userId);
3518 }
3519 }
3520 ActivityInfo aInfo = resolveActivity(intent, resolvedType, 0, null, null, userId);
Craig Mautner05678d52014-05-05 12:32:40 -07003521 if (aInfo != null && (aInfo.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) {
Craig Mautner247ab652014-04-25 10:09:00 -07003522 throw new SecurityException(
3523 "Attempt to embed activity that has not set allowEmbedded=\"true\"");
3524 }
3525 }
3526
3527 /** Throw a SecurityException if allowEmbedded is not true */
3528 @Override
3529 public final void checkEmbeddedAllowed(Intent intent) {
3530 checkEmbeddedAllowedInner(intent, null);
3531 }
3532
3533 /** Throw a SecurityException if allowEmbedded is not true */
3534 @Override
3535 public final void checkEmbeddedAllowedIntentSender(IIntentSender intentSender) {
3536 if (!(intentSender instanceof PendingIntentRecord)) {
3537 throw new IllegalArgumentException("Bad PendingIntent object");
3538 }
3539 PendingIntentRecord pendingIntent = (PendingIntentRecord) intentSender;
3540 checkEmbeddedAllowedInner(pendingIntent.key.requestIntent,
3541 pendingIntent.key.requestResolvedType);
3542 }
3543
Craig Mautnerdf88d732014-01-27 09:21:32 -08003544 @Override
Craig Mautner4a1cb222013-12-04 16:14:06 -08003545 public IBinder asBinder() {
3546 return this;
3547 }
3548
Craig Mautner4504de52013-12-20 09:06:56 -08003549 @Override
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003550 public void setSurface(Surface surface, int width, int height, int density) {
Craig Mautnerdf88d732014-01-27 09:21:32 -08003551 mService.enforceNotIsolatedCaller("ActivityContainer.attachToSurface");
Craig Mautner4504de52013-12-20 09:06:56 -08003552 }
3553
Craig Mautner4a1cb222013-12-04 16:14:06 -08003554 ActivityStackSupervisor getOuter() {
3555 return ActivityStackSupervisor.this;
3556 }
3557
Craig Mautnerd163e752014-06-13 17:18:47 -07003558 boolean isAttachedLocked() {
Craig Mautnere0a38842013-12-16 16:14:02 -08003559 return mActivityDisplay != null;
Craig Mautner4a1cb222013-12-04 16:14:06 -08003560 }
3561
3562 void getBounds(Point outBounds) {
Craig Mautnerd163e752014-06-13 17:18:47 -07003563 synchronized (mService) {
3564 if (mActivityDisplay != null) {
3565 mActivityDisplay.getBounds(outBounds);
3566 } else {
3567 outBounds.set(0, 0);
3568 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003569 }
3570 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003571
Craig Mautner6985bad2014-04-21 15:22:06 -07003572 // TODO: Make sure every change to ActivityRecord.visible results in a call to this.
Craig Mautnere3a00d72014-04-16 08:31:19 -07003573 void setVisible(boolean visible) {
3574 if (mVisible != visible) {
3575 mVisible = visible;
3576 if (mCallback != null) {
3577 mHandler.obtainMessage(CONTAINER_CALLBACK_VISIBILITY, visible ? 1 : 0,
3578 0 /* unused */, this).sendToTarget();
3579 }
3580 }
3581 }
3582
Craig Mautner6985bad2014-04-21 15:22:06 -07003583 void setDrawn() {
3584 }
3585
Craig Mautner1b4bf852014-05-26 15:06:32 -07003586 // You can always start a new task on a regular ActivityStack.
3587 boolean isEligibleForNewTasks() {
3588 return true;
3589 }
3590
Craig Mautnerd163e752014-06-13 17:18:47 -07003591 void onTaskListEmptyLocked() {
3592 mHandler.removeMessages(CONTAINER_TASK_LIST_EMPTY_TIMEOUT, this);
3593 if (!mStack.isHomeStack()) {
3594 detachLocked();
3595 deleteActivityContainer(this);
3596 }
Craig Mautnerd94b47f2014-06-02 15:06:40 -07003597 mHandler.obtainMessage(CONTAINER_CALLBACK_TASK_LIST_EMPTY, this).sendToTarget();
3598 }
3599
Craig Mautner34b73df2014-01-12 21:11:08 -08003600 @Override
3601 public String toString() {
3602 return mIdString + (mActivityDisplay == null ? "N" : "A");
3603 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003604 }
3605
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003606 private class VirtualActivityContainer extends ActivityContainer {
3607 Surface mSurface;
Craig Mautner6985bad2014-04-21 15:22:06 -07003608 boolean mDrawn = false;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003609
3610 VirtualActivityContainer(ActivityRecord parent, IActivityContainerCallback callback) {
3611 super(getNextStackId());
3612 mParentActivity = parent;
3613 mCallback = callback;
3614 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautnerd163e752014-06-13 17:18:47 -07003615 mIdString = "VirtualActivityContainer{" + mStackId + ", parent=" + mParentActivity + "}";
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003616 }
3617
3618 @Override
3619 public void setSurface(Surface surface, int width, int height, int density) {
3620 super.setSurface(surface, width, height, density);
3621
3622 synchronized (mService) {
3623 final long origId = Binder.clearCallingIdentity();
3624 try {
3625 setSurfaceLocked(surface, width, height, density);
3626 } finally {
3627 Binder.restoreCallingIdentity(origId);
3628 }
3629 }
3630 }
3631
3632 private void setSurfaceLocked(Surface surface, int width, int height, int density) {
3633 if (mContainerState == CONTAINER_STATE_FINISHING) {
3634 return;
3635 }
3636 VirtualActivityDisplay virtualActivityDisplay =
3637 (VirtualActivityDisplay) mActivityDisplay;
3638 if (virtualActivityDisplay == null) {
3639 virtualActivityDisplay =
Craig Mautner6985bad2014-04-21 15:22:06 -07003640 new VirtualActivityDisplay(width, height, density);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003641 mActivityDisplay = virtualActivityDisplay;
3642 mActivityDisplays.put(virtualActivityDisplay.mDisplayId, virtualActivityDisplay);
3643 attachToDisplayLocked(virtualActivityDisplay);
3644 }
3645
3646 if (mSurface != null) {
3647 mSurface.release();
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003648 }
3649
Craig Mautner6985bad2014-04-21 15:22:06 -07003650 mSurface = surface;
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003651 if (surface != null) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003652 mStack.resumeTopActivityLocked(null);
3653 } else {
3654 mContainerState = CONTAINER_STATE_NO_SURFACE;
Craig Mautner6985bad2014-04-21 15:22:06 -07003655 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
Craig Mautnerd13a5582014-05-05 12:07:40 -07003656 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
3657 mStack.startPausingLocked(false, true);
3658 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003659 }
Craig Mautner6985bad2014-04-21 15:22:06 -07003660
Craig Mautnerd163e752014-06-13 17:18:47 -07003661 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003662
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003663 if (DEBUG_STACK) Slog.d(TAG, "setSurface: " + this + " to display="
3664 + virtualActivityDisplay);
Craig Mautner6985bad2014-04-21 15:22:06 -07003665 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003666
Craig Mautner6985bad2014-04-21 15:22:06 -07003667 @Override
Craig Mautnerd163e752014-06-13 17:18:47 -07003668 boolean isAttachedLocked() {
3669 return mSurface != null && super.isAttachedLocked();
Craig Mautnerd13a5582014-05-05 12:07:40 -07003670 }
3671
3672 @Override
Craig Mautner6985bad2014-04-21 15:22:06 -07003673 void setDrawn() {
3674 synchronized (mService) {
3675 mDrawn = true;
Craig Mautnerd163e752014-06-13 17:18:47 -07003676 setSurfaceIfReadyLocked();
Craig Mautner6985bad2014-04-21 15:22:06 -07003677 }
3678 }
3679
Craig Mautner1b4bf852014-05-26 15:06:32 -07003680 // Never start a new task on an ActivityView if it isn't explicitly specified.
3681 @Override
3682 boolean isEligibleForNewTasks() {
3683 return false;
3684 }
3685
Craig Mautnerd163e752014-06-13 17:18:47 -07003686 private void setSurfaceIfReadyLocked() {
3687 if (DEBUG_STACK) Slog.v(TAG, "setSurfaceIfReadyLocked: mDrawn=" + mDrawn +
Craig Mautner6985bad2014-04-21 15:22:06 -07003688 " mContainerState=" + mContainerState + " mSurface=" + mSurface);
3689 if (mDrawn && mSurface != null && mContainerState == CONTAINER_STATE_NO_SURFACE) {
3690 ((VirtualActivityDisplay) mActivityDisplay).setSurface(mSurface);
3691 mContainerState = CONTAINER_STATE_HAS_SURFACE;
3692 }
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003693 }
3694 }
3695
Craig Mautner4a1cb222013-12-04 16:14:06 -08003696 /** Exactly one of these classes per Display in the system. Capable of holding zero or more
3697 * attached {@link ActivityStack}s */
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003698 class ActivityDisplay {
Craig Mautner4a1cb222013-12-04 16:14:06 -08003699 /** Actual Display this object tracks. */
Craig Mautner34b73df2014-01-12 21:11:08 -08003700 int mDisplayId;
3701 Display mDisplay;
3702 DisplayInfo mDisplayInfo = new DisplayInfo();
Craig Mautnered6649f2013-12-02 14:08:25 -08003703
Craig Mautner4a1cb222013-12-04 16:14:06 -08003704 /** All of the stacks on this display. Order matters, topmost stack is in front of all other
3705 * stacks, bottommost behind. Accessed directly by ActivityManager package classes */
Craig Mautnere0a38842013-12-16 16:14:02 -08003706 final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003707
Craig Mautneree2e45a2014-06-27 12:10:03 -07003708 ActivityRecord mMediaPlayingActivity;
3709
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003710 ActivityDisplay() {
3711 }
Craig Mautner4504de52013-12-20 09:06:56 -08003712
Craig Mautnere0a38842013-12-16 16:14:02 -08003713 ActivityDisplay(int displayId) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003714 init(mDisplayManager.getDisplay(displayId));
Craig Mautner4504de52013-12-20 09:06:56 -08003715 }
3716
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003717 void init(Display display) {
Craig Mautner4504de52013-12-20 09:06:56 -08003718 mDisplay = display;
3719 mDisplayId = display.getDisplayId();
Craig Mautner4a1cb222013-12-04 16:14:06 -08003720 mDisplay.getDisplayInfo(mDisplayInfo);
Craig Mautnered6649f2013-12-02 14:08:25 -08003721 }
Craig Mautner4a1cb222013-12-04 16:14:06 -08003722
3723 void attachActivities(ActivityStack stack) {
Craig Mautnere0a38842013-12-16 16:14:02 -08003724 if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
3725 + mDisplayId);
3726 mStacks.add(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003727 }
3728
Craig Mautnere0a38842013-12-16 16:14:02 -08003729 void detachActivitiesLocked(ActivityStack stack) {
Craig Mautner34b73df2014-01-12 21:11:08 -08003730 if (DEBUG_STACK) Slog.v(TAG, "detachActivitiesLocked: detaching " + stack
Craig Mautnere0a38842013-12-16 16:14:02 -08003731 + " from displayId=" + mDisplayId);
3732 mStacks.remove(stack);
Craig Mautner4a1cb222013-12-04 16:14:06 -08003733 }
3734
3735 void getBounds(Point bounds) {
3736 mDisplay.getDisplayInfo(mDisplayInfo);
3737 bounds.x = mDisplayInfo.appWidth;
3738 bounds.y = mDisplayInfo.appHeight;
3739 }
Craig Mautner34b73df2014-01-12 21:11:08 -08003740
Craig Mautneree2e45a2014-06-27 12:10:03 -07003741 void setMediaPlaying(ActivityRecord r) {
3742 mMediaPlayingActivity = r;
3743 }
3744
3745 boolean isMediaPlaying() {
3746 return mMediaPlayingActivity != null;
3747 }
3748
Craig Mautner34b73df2014-01-12 21:11:08 -08003749 @Override
3750 public String toString() {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003751 return "ActivityDisplay={" + mDisplayId + " numStacks=" + mStacks.size() + "}";
3752 }
3753 }
3754
3755 class VirtualActivityDisplay extends ActivityDisplay {
3756 VirtualDisplay mVirtualDisplay;
3757
Craig Mautner6985bad2014-04-21 15:22:06 -07003758 VirtualActivityDisplay(int width, int height, int density) {
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003759 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Michael Wrightc39d47a2014-07-08 18:07:36 -07003760 mVirtualDisplay = dm.createVirtualDisplay(mService.mContext, null,
3761 VIRTUAL_DISPLAY_BASE_NAME, width, height, density, null,
3762 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
3763 DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY, null, null);
Craig Mautnerf4c909b2014-04-17 18:39:38 -07003764
3765 init(mVirtualDisplay.getDisplay());
3766
3767 mWindowManager.handleDisplayAdded(mDisplayId);
3768 }
3769
3770 void setSurface(Surface surface) {
3771 if (mVirtualDisplay != null) {
3772 mVirtualDisplay.setSurface(surface);
3773 }
3774 }
3775
3776 @Override
3777 void detachActivitiesLocked(ActivityStack stack) {
3778 super.detachActivitiesLocked(stack);
3779 if (mVirtualDisplay != null) {
3780 mVirtualDisplay.release();
3781 mVirtualDisplay = null;
3782 }
3783 }
3784
3785 @Override
3786 public String toString() {
3787 return "VirtualActivityDisplay={" + mDisplayId + "}";
Craig Mautner34b73df2014-01-12 21:11:08 -08003788 }
Craig Mautnered6649f2013-12-02 14:08:25 -08003789 }
Jose Lima58e66d62014-05-27 20:00:27 -07003790
3791 private boolean isLeanbackOnlyDevice() {
3792 boolean onLeanbackOnly = false;
3793 try {
3794 onLeanbackOnly = AppGlobals.getPackageManager().hasSystemFeature(
3795 PackageManager.FEATURE_LEANBACK_ONLY);
3796 } catch (RemoteException e) {
3797 // noop
3798 }
3799
3800 return onLeanbackOnly;
3801 }
Craig Mautner27084302013-03-25 08:05:25 -07003802}